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 "nx_api.h"
24 #include "nx_ipv6.h"
25 
26 #ifdef FEATURE_NX_IPV6
27 
28 /**************************************************************************/
29 /*                                                                        */
30 /*  FUNCTION                                               RELEASE        */
31 /*                                                                        */
32 /*    _nxd_ipv6_find_max_prefix_length                    PORTABLE C      */
33 /*                                                           6.1          */
34 /*  AUTHOR                                                                */
35 /*                                                                        */
36 /*    Yuxin Zhou, Microsoft Corporation                                   */
37 /*                                                                        */
38 /*  DESCRIPTION                                                           */
39 /*                                                                        */
40 /*    This function finds the longest matching prefix between two IPv6    */
41 /*      addresses.                                                        */
42 /*                                                                        */
43 /*  INPUT                                                                 */
44 /*                                                                        */
45 /*    addr1                                 IPv6 address 1                */
46 /*    addr2                                 IPv6 address 2                */
47 /*    max_length                            Maximum length to match       */
48 /*                                                                        */
49 /*  OUTPUT                                                                */
50 /*                                                                        */
51 /*    Number of matching bits                                             */
52 /*                                                                        */
53 /*  CALLS                                                                 */
54 /*                                                                        */
55 /*    None                                                                */
56 /*                                                                        */
57 /*  CALLED BY                                                             */
58 /*                                                                        */
59 /*    _nx_ipv6_source_selection                                           */
60 /*                                                                        */
61 /*  NOTE                                                                  */
62 /*                                                                        */
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_find_max_prefix_length(ULONG * addr1,ULONG * addr2,UINT max_length)73 UINT _nxd_ipv6_find_max_prefix_length(ULONG *addr1, ULONG *addr2, UINT max_length)
74 {
75 UINT length = 0;
76 UINT i, j, bit, time;
77 
78     for (i = 0; i < 4; i++)
79     {
80         if (addr1[i] == addr2[i])
81         {
82             length += 32;
83         }
84         /* Length shall not exceed max_length. Stop compare. */
85         else if (length + 31 < max_length)
86         {
87             break;
88         }
89         else
90         {
91             bit = 16;
92             time = 16;
93             for (j = 0; j < 5; j++)
94             {
95                 time = time / 2;
96                 if (addr1[i] >> bit == addr2[i] >> bit)
97                 {
98                     bit -= time;
99                     if (time == 0)
100                     {
101                         length += (32 - bit);
102                     }
103                 }
104                 else if (j == 4)
105                 {
106                     length += (31 - bit);
107                     break;
108                 }
109                 else
110                 {
111                     bit += time;
112                 }
113             }
114             break;
115         }
116     }
117 
118 
119     return(length);
120 }
121 
122 #endif /* FEATURE_NX_IPV6 */
123 
124