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 /**************************************************************************/
15 /**                                                                       */
16 /** NetX Component                                                        */
17 /**                                                                       */
18 /**   Internet Group Management Protocol (IGMP)                           */
19 /**                                                                       */
20 /**************************************************************************/
21 /**************************************************************************/
22 
23 #define NX_SOURCE_CODE
24 
25 
26 /* Include necessary system files.  */
27 
28 #include "nx_api.h"
29 #include "nx_igmp.h"
30 
31 #ifndef NX_DISABLE_IPV4
32 /**************************************************************************/
33 /*                                                                        */
34 /*  FUNCTION                                               RELEASE        */
35 /*                                                                        */
36 /*    _nx_igmp_multicast_interface_leave_internal         PORTABLE C      */
37 /*                                                           6.1          */
38 /*  AUTHOR                                                                */
39 /*                                                                        */
40 /*    Yuxin Zhou, Microsoft Corporation                                   */
41 /*                                                                        */
42 /*  DESCRIPTION                                                           */
43 /*                                                                        */
44 /*    This function handles the request to leave the specified multicast  */
45 /*    group.                                                              */
46 /*                                                                        */
47 /*  INPUT                                                                 */
48 /*                                                                        */
49 /*    ip_ptr                                IP instance pointer           */
50 /*    group_address                         Multicast group to join       */
51 /*    interface_index                       Interface index               */
52 /*                                                                        */
53 /*  OUTPUT                                                                */
54 /*                                                                        */
55 /*    NX_SUCCESS                            Successful completion status  */
56 /*    NX_ENTRY_NOT_FOUND                    Group address not found in the*/
57 /*                                                IGMP join list          */
58 /*                                                                        */
59 /*  CALLS                                                                 */
60 /*                                                                        */
61 /*    tx_mutex_get                          Obtain protection mutex       */
62 /*    tx_mutex_put                          Release protection mutex      */
63 /*    (ip_link_driver)                      Associated IP link driver     */
64 /*    nx_igmp_interface_report_send         Send IGMP group reports       */
65 /*                                                                        */
66 /*  CALLED BY                                                             */
67 /*                                                                        */
68 /*    Application Code                                                    */
69 /*                                                                        */
70 /*  RELEASE HISTORY                                                       */
71 /*                                                                        */
72 /*    DATE              NAME                      DESCRIPTION             */
73 /*                                                                        */
74 /*  05-19-2020     Yuxin Zhou               Initial Version 6.0           */
75 /*  09-30-2020     Yuxin Zhou               Modified comment(s),          */
76 /*                                            resulting in version 6.1    */
77 /*                                                                        */
78 /**************************************************************************/
_nx_igmp_multicast_interface_leave_internal(NX_IP * ip_ptr,ULONG group_address,UINT interface_index)79 UINT  _nx_igmp_multicast_interface_leave_internal(NX_IP *ip_ptr, ULONG group_address, UINT interface_index)
80 {
81 
82 UINT          i;
83 NX_IP_DRIVER  driver_request;
84 NX_INTERFACE *nx_interface;
85 
86 
87     /* If trace is enabled, insert this event into the trace buffer.  */
88     NX_TRACE_IN_LINE_INSERT(NX_TRACE_IGMP_MULTICAST_LEAVE, ip_ptr, group_address, 0, 0, NX_TRACE_IGMP_EVENTS, 0, 0);
89 
90     /* Obtain the IP mutex so we can search the multicast join list.  */
91     tx_mutex_get(&(ip_ptr -> nx_ip_protection), TX_WAIT_FOREVER);
92 
93     nx_interface = &ip_ptr -> nx_ip_interface[interface_index];
94 
95     /* Search the multicast join list for the group request.  */
96 
97     for (i = 0; i < NX_MAX_MULTICAST_GROUPS; i++)
98     {
99 
100         /* Determine if the specified entry is present.  */
101         if ((ip_ptr -> nx_ipv4_multicast_entry[i].nx_ipv4_multicast_join_list == group_address) &&
102             (ip_ptr -> nx_ipv4_multicast_entry[i].nx_ipv4_multicast_join_interface_list == nx_interface))
103         {
104 
105             /* Yes, we have found the same entry.  */
106 
107             /* Decrease the join count.  */
108             ip_ptr -> nx_ipv4_multicast_entry[i].nx_ipv4_multicast_join_count--;
109 
110             /* Determine if there are no other join requests for this group address.  */
111             if (ip_ptr -> nx_ipv4_multicast_entry[i].nx_ipv4_multicast_join_count == 0)
112             {
113 
114                 /* Clear the group join value.  */
115                 ip_ptr -> nx_ipv4_multicast_entry[i].nx_ipv4_multicast_join_list = 0;
116 
117                 /* Un-register the new multicast group with the underlying driver.  */
118                 driver_request.nx_ip_driver_ptr =                    ip_ptr;
119                 driver_request.nx_ip_driver_command =                NX_LINK_MULTICAST_LEAVE;
120                 driver_request.nx_ip_driver_physical_address_msw =   NX_IP_MULTICAST_UPPER;
121                 driver_request.nx_ip_driver_physical_address_lsw =   NX_IP_MULTICAST_LOWER | (group_address & NX_IP_MULTICAST_MASK);
122                 driver_request.nx_ip_driver_interface =              ip_ptr -> nx_ipv4_multicast_entry[i].nx_ipv4_multicast_join_interface_list;
123 
124                 /* If trace is enabled, insert this event into the trace buffer.  */
125                 NX_TRACE_IN_LINE_INSERT(NX_TRACE_INTERNAL_IO_DRIVER_MULTICAST_LEAVE, ip_ptr, 0, 0, 0, NX_TRACE_INTERNAL_EVENTS, 0, 0);
126 
127                 (ip_ptr -> nx_ipv4_multicast_entry[i].nx_ipv4_multicast_join_interface_list -> nx_interface_link_driver_entry)(&driver_request);
128 
129                 /* Clear the interface entry for version IGMPv1. Don't need it anymore. */
130                 ip_ptr -> nx_ipv4_multicast_entry[i].nx_ipv4_multicast_join_interface_list = NX_NULL;
131 
132 #ifndef NX_DISABLE_IGMP_INFO
133                 /* Decrement the IGMP groups joined count.  */
134                 ip_ptr -> nx_ip_igmp_groups_joined--;
135 #endif
136 
137 #ifndef NX_DISABLE_IGMPV2
138                 /* Check if the entry is a local multicast join. */
139                 if (ip_ptr -> nx_ipv4_multicast_entry[i].nx_ipv4_multicast_update_time == NX_WAIT_FOREVER)
140                 {
141 
142                     /* It is. Release the IP protection.  */
143                     tx_mutex_put(&(ip_ptr -> nx_ip_protection));
144 
145                     /* Return success.  */
146                     return(NX_SUCCESS);
147                 }
148 
149                 /* IGMPv1 hosts do not send a leave group message. */
150                 if (ip_ptr -> nx_ip_igmp_router_version == NX_IGMP_HOST_VERSION_1)
151                 {
152 
153                     /* Release the IP protection.  */
154                     tx_mutex_put(&(ip_ptr -> nx_ip_protection));
155 
156                     /* Return success!  */
157                     return(NX_SUCCESS);
158                 }
159 
160                 /* Build and send the leave report packet. */
161                 _nx_igmp_interface_report_send(ip_ptr, group_address, interface_index, NX_FALSE);
162 
163 #endif /* NX_DISABLE_IGMPV2 */
164             }
165 
166             /* Release the IP protection.  */
167             tx_mutex_put(&(ip_ptr -> nx_ip_protection));
168 
169             /* Return success.  */
170             return(NX_SUCCESS);
171         }
172     }
173 
174     /* At this point we know that the supplied entry was not found.  */
175 
176     /* Release the protection of the IP instance.  */
177     tx_mutex_put(&(ip_ptr -> nx_ip_protection));
178 
179     /* Return an error code.  */
180     return(NX_ENTRY_NOT_FOUND);
181 }
182 #endif /* !NX_DISABLE_IPV4  */
183 
184