summaryrefslogtreecommitdiff
path: root/common/src/nx_ip_packet_receive.c
blob: 404e09a67e8723640f2e319d5e405e46bf8eb7ee (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
/***************************************************************************
 * Copyright (c) 2024 Microsoft Corporation
 * Copyright (c) 2025-present Eclipse ThreadX Contributors
 *
 * This program and the accompanying materials are made available under the
 * terms of the MIT License which is available at
 * https://opensource.org/licenses/MIT.
 *
 * SPDX-License-Identifier: MIT
 **************************************************************************/


/**************************************************************************/
/**************************************************************************/
/**                                                                       */
/** NetX Component                                                        */
/**                                                                       */
/**   Internet Protocol (IP)                                              */
/**                                                                       */
/**************************************************************************/
/**************************************************************************/

#define NX_SOURCE_CODE


/* Include necessary system files.  */

#include "nx_api.h"
#include "nx_ip.h"
#include "nx_packet.h"


/**************************************************************************/
/*                                                                        */
/*  FUNCTION                                               RELEASE        */
/*                                                                        */
/*    _nx_ip_packet_receive                               PORTABLE C      */
/*                                                           6.4.3        */
/*  AUTHOR                                                                */
/*                                                                        */
/*    Yuxin Zhou, Microsoft Corporation                                   */
/*                                                                        */
/*  DESCRIPTION                                                           */
/*                                                                        */
/*    This function receives a packet from the link driver (usually the   */
/*    link driver's input ISR) and either processes it or places it in a  */
/*    deferred processing queue, depending on the complexity of the       */
/*    packet.                                                             */
/*                                                                        */
/*  INPUT                                                                 */
/*                                                                        */
/*    ip_ptr                                Pointer to IP control block   */
/*    packet_ptr                            Pointer to received packet    */
/*                                                                        */
/*  OUTPUT                                                                */
/*                                                                        */
/*    None                                                                */
/*                                                                        */
/*  CALLS                                                                 */
/*                                                                        */
/*    (ipv4_packet_receive)                 Receive an IPv4 packet        */
/*    (ipv6_packet_receive)                 Receive an IPv6 packet        */
/*    _nx_packet_release                    Packet release                */
/*                                                                        */
/*  CALLED BY                                                             */
/*                                                                        */
/*    Application I/O Driver                                              */
/*    _nx_ip_packet_send                    IP loopback packet send       */
/*                                                                        */
/**************************************************************************/
VOID  _nx_ip_packet_receive(NX_IP *ip_ptr, NX_PACKET *packet_ptr)
{

UCHAR ip_version;
UCHAR version_byte;


#ifndef NX_DISABLE_IP_INFO
    /* Increment the IP packet count.  */
    ip_ptr -> nx_ip_total_packets_received++;
#endif

    /* Add debug information. */
    NX_PACKET_DEBUG(__FILE__, __LINE__, packet_ptr);

    /* If packet_ptr -> nx_packet_interface_ptr is not set, stamp the packet with interface[0].
       Legacy Ethernet drivers do not stamp incoming packets. */
    if (packet_ptr -> nx_packet_address.nx_packet_interface_ptr == NX_NULL)
    {
        packet_ptr -> nx_packet_address.nx_packet_interface_ptr = &(ip_ptr -> nx_ip_interface[0]);
    }

#ifndef NX_DISABLE_IPV4
    /* GHSA-pf5q-r6q5-6j2f:
       This is an IPv4 packet. Therefore the header length must be at least 20 bytes.
       Validate the payload size before accessing the IP header. */
    if(packet_ptr -> nx_packet_length < sizeof(NX_IPV4_HEADER))
    {
        /* Invalid payload length */

        /* Drop the packet. */
        _nx_packet_release(packet_ptr);

        return;        
    }
#endif

    /* It's assumed that the IP link driver has positioned the top pointer in the
       packet to the start of the IP address... so that's where we will start.  */
    version_byte =  *(packet_ptr -> nx_packet_prepend_ptr);

    /* Check the version number */
    ip_version = (version_byte >> 4);

    packet_ptr -> nx_packet_ip_version = ip_version;

    packet_ptr -> nx_packet_ip_header = packet_ptr -> nx_packet_prepend_ptr;

#ifdef NX_ENABLE_IP_PACKET_FILTER
    /* Check if the IP packet filter is set. */
    if (ip_ptr -> nx_ip_packet_filter)
    {

        /* Yes, call the IP packet filter routine. */
        if (ip_ptr -> nx_ip_packet_filter((VOID *)(packet_ptr -> nx_packet_prepend_ptr),
                                          NX_IP_PACKET_IN) != NX_SUCCESS)
        {

            /* Drop the packet. */
            _nx_packet_release(packet_ptr);
            return;
        }
    }

    /* Check if the IP packet filter extended is set. */
    if (ip_ptr -> nx_ip_packet_filter_extended)
    {

        /* Yes, call the IP packet filter extended routine. */
        if (ip_ptr -> nx_ip_packet_filter_extended(ip_ptr, packet_ptr, NX_IP_PACKET_IN) != NX_SUCCESS)
        {

            /* Drop the packet. */
            _nx_packet_release(packet_ptr);
            return;
        }
    }
#endif /* NX_ENABLE_IP_PACKET_FILTER */

#ifndef NX_DISABLE_IPV4

    /* Process the packet according to IP version. */
    if (ip_version == NX_IP_VERSION_V4 && ip_ptr -> nx_ipv4_packet_receive)
    {

        /* Call the IPv4 packet handler. */
        (ip_ptr -> nx_ipv4_packet_receive)(ip_ptr, packet_ptr);
        return;
    }
#endif /* !NX_DISABLE_IPV4  */

#ifdef FEATURE_NX_IPV6
    if (ip_version == NX_IP_VERSION_V6 && ip_ptr -> nx_ipv6_packet_receive)
    {

        /* Call the IPv6 packet handler. */
        (ip_ptr -> nx_ipv6_packet_receive)(ip_ptr, packet_ptr);
        return;
    }
#endif /* FEATURE_NX_IPV6 */

    /* Either the ip_version number is unkonwn, or the ip_packet_receive function is
        not defined.  In this case, the packet is reclaimed. */

#ifndef NX_DISABLE_IP_INFO

    /* Increment the IP invalid packet error.  */
    ip_ptr -> nx_ip_invalid_packets++;

    /* Increment the IP receive packets dropped count.  */
    ip_ptr -> nx_ip_receive_packets_dropped++;
#endif

    _nx_packet_release(packet_ptr);

    return;
}