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 /**************************************************************************/
14 /**                                                                       */
15 /** NetX Component                                                        */
16 /**                                                                       */
17 /**   Internet Group Management Protocol (IGMP)                           */
18 /**                                                                       */
19 /**************************************************************************/
20 /**************************************************************************/
21 
22 #define NX_SOURCE_CODE
23 
24 
25 /* Include necessary system files.  */
26 
27 #include "nx_api.h"
28 #include "nx_packet.h"
29 #include "nx_igmp.h"
30 #include "nx_ip.h"
31 #include "tx_thread.h"
32 
33 
34 #ifndef NX_DISABLE_IPV4
35 /**************************************************************************/
36 /*                                                                        */
37 /*  FUNCTION                                               RELEASE        */
38 /*                                                                        */
39 /*    _nx_igmp_packet_receive                             PORTABLE C      */
40 /*                                                           6.1          */
41 /*  AUTHOR                                                                */
42 /*                                                                        */
43 /*    Yuxin Zhou, Microsoft Corporation                                   */
44 /*                                                                        */
45 /*  DESCRIPTION                                                           */
46 /*                                                                        */
47 /*    This function handles reception of IGMP packets on the "all hosts"  */
48 /*    multicast address.  If this routine is called from an ISR, the      */
49 /*    IGMP packet is queued.  Otherwise, if this routine is called from   */
50 /*    the IP helper thread, the processing of the IGMP packet is called   */
51 /*    directly.                                                           */
52 /*                                                                        */
53 /*  INPUT                                                                 */
54 /*                                                                        */
55 /*    ip_ptr                                IP instance pointer           */
56 /*    packet_ptr                            IGMP packet pointer           */
57 /*                                                                        */
58 /*  OUTPUT                                                                */
59 /*                                                                        */
60 /*    None                                                                */
61 /*                                                                        */
62 /*  CALLS                                                                 */
63 /*                                                                        */
64 /*    _nx_igmp_packet_process               Process the IGMP packet       */
65 /*    tx_event_flags_set                    Set event flags for IP helper */
66 /*                                            thread                      */
67 /*                                                                        */
68 /*  CALLED BY                                                             */
69 /*                                                                        */
70 /*    _nx_ip_packet_receive                 Raw IP packet receive         */
71 /*                                                                        */
72 /*  RELEASE HISTORY                                                       */
73 /*                                                                        */
74 /*    DATE              NAME                      DESCRIPTION             */
75 /*                                                                        */
76 /*  05-19-2020     Yuxin Zhou               Initial Version 6.0           */
77 /*  09-30-2020     Yuxin Zhou               Modified comment(s),          */
78 /*                                            resulting in version 6.1    */
79 /*                                                                        */
80 /**************************************************************************/
_nx_igmp_packet_receive(NX_IP * ip_ptr,NX_PACKET * packet_ptr)81 VOID  _nx_igmp_packet_receive(NX_IP *ip_ptr, NX_PACKET *packet_ptr)
82 {
83 
84 TX_INTERRUPT_SAVE_AREA
85 
86 
87     /* Add debug information. */
88     NX_PACKET_DEBUG(__FILE__, __LINE__, packet_ptr);
89 
90 #ifndef NX_DISABLE_RX_SIZE_CHECKING
91 
92     /* Check for valid packet length.  */
93     if (packet_ptr -> nx_packet_length < sizeof(NX_IGMP_HEADER))
94     {
95 
96 #ifndef NX_DISABLE_IGMP_INFO
97         /* Increment the IGMP invalid packet error.  */
98         ip_ptr -> nx_ip_igmp_invalid_packets++;
99 #endif
100 
101         /* Invalid packet length, just release it.  */
102         _nx_packet_release(packet_ptr);
103 
104         /* The function is complete, just return!  */
105         return;
106     }
107 #endif
108 
109     /* Determine if this routine is being called from an ISR.  */
110     if ((TX_THREAD_GET_SYSTEM_STATE()) || (&(ip_ptr -> nx_ip_thread) != _tx_thread_current_ptr))
111     {
112 
113         /* If system state is non-zero, we are in an ISR. If the current thread is not the IP thread,
114            we need to prevent unnecessary recursion in loopback.  Just place the message at the
115            end of the IGMP message queue and wakeup the IP helper thread.  */
116 
117         /* Disable interrupts.  */
118         TX_DISABLE
119 
120         /* Add the packet to the IGMP message queue.  */
121         if (ip_ptr -> nx_ip_igmp_queue_head)
122         {
123 
124             /* Link the current packet to the list head.  */
125             packet_ptr -> nx_packet_queue_next =  ip_ptr -> nx_ip_igmp_queue_head;
126         }
127         else
128         {
129 
130             /* Empty queue, add to the head of the IGMP message queue.  */
131             packet_ptr -> nx_packet_queue_next =  NX_NULL;
132         }
133 
134         /* Add debug information. */
135         NX_PACKET_DEBUG(__FILE__, __LINE__, packet_ptr);
136 
137         /* Update the queue head pointer.  */
138         ip_ptr -> nx_ip_igmp_queue_head =  packet_ptr;
139 
140         /* Restore interrupts.  */
141         TX_RESTORE
142 
143         /* Wakeup IP thread for processing one or more messages in the IGMP queue.  */
144         tx_event_flags_set(&(ip_ptr -> nx_ip_events), NX_IP_IGMP_EVENT, TX_OR);
145     }
146     else
147     {
148 
149         /* The IP message was deferred, so this routine is called from the IP helper
150            thread and thus may call the IGMP processing directly.  */
151         _nx_igmp_packet_process(ip_ptr, packet_ptr);
152     }
153 }
154 #endif /* !NX_DISABLE_IPV4  */
155 
156