1 /***************************************************************************
2  * Copyright (c) 2024 Microsoft Corporation
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the MIT License which is available at
6  * https://opensource.org/licenses/MIT.
7  *
8  * SPDX-License-Identifier: MIT
9  **************************************************************************/
10 /**************************************************************************/
11 /**************************************************************************/
12 /**                                                                       */
13 /** NetX Component                                                        */
14 /**                                                                       */
15 /**   Internet Protocol version 6 Default Router Table (IPv6 router)      */
16 /**                                                                       */
17 /**************************************************************************/
18 /**************************************************************************/
19 #define NX_SOURCE_CODE
20 
21 
22 /* Include necessary system files.  */
23 
24 #include "nx_api.h"
25 #include "nx_ipv6.h"
26 #include "nx_nd_cache.h"
27 
28 
29 #ifdef FEATURE_NX_IPV6
30 /**************************************************************************/
31 /*                                                                        */
32 /*  FUNCTION                                               RELEASE        */
33 /*                                                                        */
34 /*    _nxd_ipv6_search_onlink                              PORTABLE C     */
35 /*                                                           6.1          */
36 /*  AUTHOR                                                                */
37 /*                                                                        */
38 /*    Yuxin Zhou, Microsoft Corporation                                   */
39 /*                                                                        */
40 /*  DESCRIPTION                                                           */
41 /*                                                                        */
42 /*    This function checks whether a given IP address is on link.         */
43 /*                                                                        */
44 /*  INPUT                                                                 */
45 /*                                                                        */
46 /*    ip_ptr                                IP instance                   */
47 /*    dest_addr                             The destination address to    */
48 /*                                             be checked.                */
49 /*                                                                        */
50 /*  OUTPUT                                                                */
51 /*                                                                        */
52 /*    status                                0: Address is not onlink      */
53 /*                                          1: Address is onlink          */
54 /*                                                                        */
55 /*  CALLS                                                                 */
56 /*                                                                        */
57 /*    NONE                                                                */
58 /*                                                                        */
59 /*  CALLED BY                                                             */
60 /*                                                                        */
61 /*    nx_ipv6_packet_send                  Sends IPv6 packets from upper  */
62 /*                                           layer to remote host         */
63 /*                                                                        */
64 /*  RELEASE HISTORY                                                       */
65 /*                                                                        */
66 /*    DATE              NAME                      DESCRIPTION             */
67 /*                                                                        */
68 /*  05-19-2020     Yuxin Zhou               Initial Version 6.0           */
69 /*  09-30-2020     Yuxin Zhou               Modified comment(s),          */
70 /*                                            resulting in version 6.1    */
71 /*                                                                        */
72 /**************************************************************************/
_nxd_ipv6_search_onlink(NX_IP * ip_ptr,ULONG * dest_addr)73 INT _nxd_ipv6_search_onlink(NX_IP *ip_ptr, ULONG *dest_addr)
74 {
75 
76 UINT                  addr_index;
77 NX_IPV6_PREFIX_ENTRY *prefix_entry;
78 NXD_IPV6_ADDRESS     *ipv6_address;
79 
80 
81     /* First special case is the link local address. All these
82        addresses are onlink.  */
83     if (IPv6_Address_Type(dest_addr) & IPV6_ADDRESS_LINKLOCAL)
84     {
85         return(1);
86     }
87 
88     /* Set a local pointer for convenience. */
89     prefix_entry = ip_ptr -> nx_ipv6_prefix_list_ptr;
90 
91     /* Loop through the prefix table. Prefixes are the IPv6 equivalent of
92        network domains in IPv4.  */
93     while (prefix_entry)
94     {
95 
96         /* Check whether or not the destination address is matched. */
97         if (CHECK_IP_ADDRESSES_BY_PREFIX(dest_addr,
98                                          prefix_entry -> nx_ipv6_prefix_entry_network_address,
99                                          prefix_entry -> nx_ipv6_prefix_entry_prefix_length))
100         {
101             return(1);
102         }
103 
104         /* No match. Try the next prefix. */
105         prefix_entry = prefix_entry -> nx_ipv6_prefix_entry_next;
106     }
107 
108     /* If no matches found in the prefix list, search the manually configured IPv6 interface addresses. */
109     for (addr_index = 0; addr_index < NX_MAX_IPV6_ADDRESSES; addr_index++)
110     {
111 
112         ipv6_address = &ip_ptr -> nx_ipv6_address[addr_index];
113         /* Skip invalid entries. */
114         if (!(ipv6_address -> nxd_ipv6_address_valid))
115         {
116             continue;
117         }
118 
119         /* Skip non-manually configured entires. */
120         if (ipv6_address -> nxd_ipv6_address_ConfigurationMethod != NX_IPV6_ADDRESS_MANUAL_CONFIG)
121         {
122             continue;
123         }
124 
125         /* Check whether or not the destination address is matched. */
126         if (CHECK_IP_ADDRESSES_BY_PREFIX(dest_addr,
127                                          ipv6_address -> nxd_ipv6_address,
128                                          ipv6_address -> nxd_ipv6_address_prefix_length))
129         {
130             return(1);
131         }
132     }
133 
134 
135     /* No matches found. Not an onlink address. */
136     return(0);
137 }
138 
139 #endif /* FEATURE_NX_IPV6 */
140 
141