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