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 #include "nx_icmpv6.h"
28 
29 
30 #ifdef FEATURE_NX_IPV6
31 
32 #ifndef NX_DISABLE_ICMPV6_ROUTER_SOLICITATION
33 /**************************************************************************/
34 /*                                                                        */
35 /*  FUNCTION                                               RELEASE        */
36 /*                                                                        */
37 /*    _nxd_ipv6_router_solicitation_check                 PORTABLE C      */
38 /*                                                           6.1          */
39 /*  AUTHOR                                                                */
40 /*                                                                        */
41 /*    Yuxin Zhou, Microsoft Corporation                                   */
42 /*                                                                        */
43 /*  DESCRIPTION                                                           */
44 /*                                                                        */
45 /*    At every time tick, this function decrement the router solicitation */
46 /*    counter.   When the counter reaches zero, the stack sends out       */
47 /*    router solicitation.                                                */
48 /*                                                                        */
49 /*  INPUT                                                                 */
50 /*                                                                        */
51 /*    ip_ptr                                IP instance pointer           */
52 /*    router_address                        The specific gateway address  */
53 /*                                            to search for.              */
54 /*                                                                        */
55 /*  OUTPUT                                                                */
56 /*                                                                        */
57 /*    None                                                                */
58 /*                                                                        */
59 /*  CALLS                                                                 */
60 /*                                                                        */
61 /*    _nx_icmpv6_send_rs                   Send router solicitation packet*/
62 /*                                                                        */
63 /*  CALLED BY                                                             */
64 /*                                                                        */
65 /*    nx_ip_thread_entry                    Handle IP thread task events  */
66 /*                                                                        */
67 /*  NOTE                                                                  */
68 /*                                                                        */
69 /*    Caller must obtain nx_ip_protection mutex before calling this       */
70 /*    function.                                                           */
71 /*                                                                        */
72 /*    This function cannot be called from ISR.                            */
73 /*                                                                        */
74 /*  RELEASE HISTORY                                                       */
75 /*                                                                        */
76 /*    DATE              NAME                      DESCRIPTION             */
77 /*                                                                        */
78 /*  05-19-2020     Yuxin Zhou               Initial Version 6.0           */
79 /*  09-30-2020     Yuxin Zhou               Modified comment(s),          */
80 /*                                            resulting in version 6.1    */
81 /*                                                                        */
82 /**************************************************************************/
_nxd_ipv6_router_solicitation_check(NX_IP * ip_ptr)83 void _nxd_ipv6_router_solicitation_check(NX_IP *ip_ptr)
84 {
85 UINT i;
86 
87     for (i = 0; i < NX_MAX_PHYSICAL_INTERFACES; i++)
88     {
89         if (ip_ptr -> nx_ip_interface[i].nx_interface_valid == NX_TRUE)
90         {
91 
92             /* Check if max number of router solicitation messages have been sent. */
93             if (ip_ptr -> nx_ip_interface[i].nx_ipv6_rtr_solicitation_count != 0)
94             {
95 
96                 /* Check on count down timer for sending out next router solicitation message. */
97                 ip_ptr -> nx_ip_interface[i].nx_ipv6_rtr_solicitation_timer--;
98                 if (ip_ptr -> nx_ip_interface[i].nx_ipv6_rtr_solicitation_timer == 0)
99                 {
100                     if (_nx_icmpv6_send_rs(ip_ptr, i) &&
101                         (ip_ptr -> nx_ip_interface[i].nx_ipv6_rtr_solicitation_count ==
102                          ip_ptr -> nx_ip_interface[i].nx_ipv6_rtr_solicitation_max))
103                     {
104 
105                         /* Initial RS is not sent successfully. */
106                         /* Try it next round. */
107                         ip_ptr -> nx_ip_interface[i].nx_ipv6_rtr_solicitation_timer = 1;
108                     }
109                     else
110                     {
111                         ip_ptr -> nx_ip_interface[i].nx_ipv6_rtr_solicitation_count--;
112                         ip_ptr -> nx_ip_interface[i].nx_ipv6_rtr_solicitation_timer = ip_ptr -> nx_ip_interface[i].nx_ipv6_rtr_solicitation_interval;
113                     }
114                 }
115             }
116         }
117     }
118 }
119 #endif /* NX_DISABLE_ICMPV6_ROUTER_SOLICITATION */
120 #endif /* FEATURE_NX_IPV6 */
121 
122