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 Protocol (IP)                                              */
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_ip.h"
30 #include "nx_packet.h"
31 
32 
33 /**************************************************************************/
34 /*                                                                        */
35 /*  FUNCTION                                               RELEASE        */
36 /*                                                                        */
37 /*    _nx_ip_packet_receive                               PORTABLE C      */
38 /*                                                           6.1.8        */
39 /*  AUTHOR                                                                */
40 /*                                                                        */
41 /*    Yuxin Zhou, Microsoft Corporation                                   */
42 /*                                                                        */
43 /*  DESCRIPTION                                                           */
44 /*                                                                        */
45 /*    This function receives a packet from the link driver (usually the   */
46 /*    link driver's input ISR) and either processes it or places it in a  */
47 /*    deferred processing queue, depending on the complexity of the       */
48 /*    packet.                                                             */
49 /*                                                                        */
50 /*  INPUT                                                                 */
51 /*                                                                        */
52 /*    ip_ptr                                Pointer to IP control block   */
53 /*    packet_ptr                            Pointer to received packet    */
54 /*                                                                        */
55 /*  OUTPUT                                                                */
56 /*                                                                        */
57 /*    None                                                                */
58 /*                                                                        */
59 /*  CALLS                                                                 */
60 /*                                                                        */
61 /*    (ipv4_packet_receive)                 Receive an IPv4 packet        */
62 /*    (ipv6_packet_receive)                 Receive an IPv6 packet        */
63 /*    _nx_packet_release                    Packet release                */
64 /*                                                                        */
65 /*  CALLED BY                                                             */
66 /*                                                                        */
67 /*    Application I/O Driver                                              */
68 /*    _nx_ip_packet_send                    IP loopback packet send       */
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 /*  08-02-2021     Yuxin Zhou               Modified comment(s), and      */
78 /*                                            added new ip filter,        */
79 /*                                            resulting in version 6.1.8  */
80 /*                                                                        */
81 /**************************************************************************/
_nx_ip_packet_receive(NX_IP * ip_ptr,NX_PACKET * packet_ptr)82 VOID  _nx_ip_packet_receive(NX_IP *ip_ptr, NX_PACKET *packet_ptr)
83 {
84 
85 UCHAR ip_version;
86 UCHAR version_byte;
87 
88 
89 #ifndef NX_DISABLE_IP_INFO
90     /* Increment the IP packet count.  */
91     ip_ptr -> nx_ip_total_packets_received++;
92 #endif
93 
94     /* Add debug information. */
95     NX_PACKET_DEBUG(__FILE__, __LINE__, packet_ptr);
96 
97     /* If packet_ptr -> nx_packet_interface_ptr is not set, stamp the packet with interface[0].
98        Legacy Ethernet drivers do not stamp incoming packets. */
99     if (packet_ptr -> nx_packet_address.nx_packet_interface_ptr == NX_NULL)
100     {
101         packet_ptr -> nx_packet_address.nx_packet_interface_ptr = &(ip_ptr -> nx_ip_interface[0]);
102     }
103 
104     /* It's assumed that the IP link driver has positioned the top pointer in the
105        packet to the start of the IP address... so that's where we will start.  */
106     version_byte =  *(packet_ptr -> nx_packet_prepend_ptr);
107 
108     /* Check the version number */
109     ip_version = (version_byte >> 4);
110 
111     packet_ptr -> nx_packet_ip_version = ip_version;
112 
113     packet_ptr -> nx_packet_ip_header = packet_ptr -> nx_packet_prepend_ptr;
114 
115 #ifdef NX_ENABLE_IP_PACKET_FILTER
116     /* Check if the IP packet filter is set. */
117     if (ip_ptr -> nx_ip_packet_filter)
118     {
119 
120         /* Yes, call the IP packet filter routine. */
121         if (ip_ptr -> nx_ip_packet_filter((VOID *)(packet_ptr -> nx_packet_prepend_ptr),
122                                           NX_IP_PACKET_IN) != NX_SUCCESS)
123         {
124 
125             /* Drop the packet. */
126             _nx_packet_release(packet_ptr);
127             return;
128         }
129     }
130 
131     /* Check if the IP packet filter extended is set. */
132     if (ip_ptr -> nx_ip_packet_filter_extended)
133     {
134 
135         /* Yes, call the IP packet filter extended routine. */
136         if (ip_ptr -> nx_ip_packet_filter_extended(ip_ptr, packet_ptr, NX_IP_PACKET_IN) != NX_SUCCESS)
137         {
138 
139             /* Drop the packet. */
140             _nx_packet_release(packet_ptr);
141             return;
142         }
143     }
144 #endif /* NX_ENABLE_IP_PACKET_FILTER */
145 
146 #ifndef NX_DISABLE_IPV4
147 
148     /* Process the packet according to IP version. */
149     if (ip_version == NX_IP_VERSION_V4 && ip_ptr -> nx_ipv4_packet_receive)
150     {
151 
152         /* Call the IPv4 packet handler. */
153         (ip_ptr -> nx_ipv4_packet_receive)(ip_ptr, packet_ptr);
154         return;
155     }
156 #endif /* !NX_DISABLE_IPV4  */
157 
158 #ifdef FEATURE_NX_IPV6
159     if (ip_version == NX_IP_VERSION_V6 && ip_ptr -> nx_ipv6_packet_receive)
160     {
161 
162         /* Call the IPv6 packet handler. */
163         (ip_ptr -> nx_ipv6_packet_receive)(ip_ptr, packet_ptr);
164         return;
165     }
166 #endif /* FEATURE_NX_IPV6 */
167 
168     /* Either the ip_version number is unkonwn, or the ip_packet_receive function is
169         not defined.  In this case, the packet is reclaimed. */
170 
171 #ifndef NX_DISABLE_IP_INFO
172 
173     /* Increment the IP invalid packet error.  */
174     ip_ptr -> nx_ip_invalid_packets++;
175 
176     /* Increment the IP receive packets dropped count.  */
177     ip_ptr -> nx_ip_receive_packets_dropped++;
178 #endif
179 
180     _nx_packet_release(packet_ptr);
181 
182     return;
183 }
184 
185