summaryrefslogtreecommitdiff
path: root/common/src/nx_icmpv6_validate_neighbor_message.c
blob: a3a14e860625d1b9ae64a6dc04c3eb9760ffbc31 (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
/***************************************************************************
 * 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 Control Message Protocol (ICMP)                            */
/**                                                                       */
/**************************************************************************/
/**************************************************************************/

#define NX_SOURCE_CODE


/* Include necessary system files.  */

#include "nx_api.h"
#include "nx_ipv6.h"
#include "nx_icmpv6.h"

#ifdef FEATURE_NX_IPV6


/**************************************************************************/
/*                                                                        */
/*  FUNCTION                                               RELEASE        */
/*                                                                        */
/*    _nx_icmpv6_validate_neighbor_message                PORTABLE C      */
/*                                                           6.4.3        */
/*  AUTHOR                                                                */
/*                                                                        */
/*    Yuxin Zhou, Microsoft Corporation                                   */
/*                                                                        */
/*  DESCRIPTION                                                           */
/*                                                                        */
/*    This internal function validates incoming neighbor advertisement    */
/*    message and neighbor solicitation message.                          */
/*                                                                        */
/*  INPUT                                                                 */
/*                                                                        */
/*    packet_ptr                   ICMPv6 packet to validate              */
/*                                                                        */
/*  OUTPUT                                                                */
/*                                                                        */
/*    NX_SUCCESS                   ICMPv6 options are valid               */
/*    NX_NOT_SUCCESS               ICMPv6 options are invalid             */
/*                                                                        */
/*  CALLS                                                                 */
/*                                                                        */
/*    IPv6_Address_Type             Determine address type (e.g. unicast) */
/*    _nx_icmpv6_validate_options   Validate option fields in ICMP header */
/*                                                                        */
/*  CALLED BY                                                             */
/*                                                                        */
/*    _nx_icmpv6_process_ns         Process received NS packet            */
/*    _nx_icmpv6_process_na         Process received NA packet            */
/*                                                                        */
/**************************************************************************/
UINT _nx_icmpv6_validate_neighbor_message(NX_PACKET *packet_ptr)
{

NX_ICMPV6_ND     *nd_header_ptr;
NX_IPV6_HEADER   *ipv6_header;
NX_ICMPV6_OPTION *option_ptr;
UINT              option_length;
UINT              option_check;
ULONG             source_address_type;
ULONG             dest_address_type;


    /* Points to the IPv6 header. */
    /*lint -e{927} -e{826} suppress cast of pointer to pointer, since it is necessary  */
    ipv6_header = (NX_IPV6_HEADER *)packet_ptr -> nx_packet_ip_header;

    /*
     * Validate the IP header information.
     * The following validation procedure is defined in RFC2461
     * 7.1.1: Validation of Neighbor Solicitations
     * 7.1.2: Validation of Neighbor Advertisements
     */

    /* Hop limit must be 255, I.e., the packet could not possibly have been forwarded by a router. */
    if ((ipv6_header -> nx_ip_header_word_1 & 0xFF) != 0xFF)
    {
        return(NX_NOT_SUCCESSFUL);
    }

    /* Points to the ICMP message header.  */
    /*lint -e{927} -e{826} suppress cast of pointer to pointer, since it is necessary  */
    nd_header_ptr = (NX_ICMPV6_ND *)packet_ptr -> nx_packet_prepend_ptr;

    /* ICMP code must be 0 */
    if (nd_header_ptr -> nx_icmpv6_nd_header.nx_icmpv6_header_code != 0)
    {
        return(NX_NOT_SUCCESSFUL);
    }

    /* ICMP length (derived from the IP Length) is 24 or more. */
    if (packet_ptr -> nx_packet_length < 24)
    {
        return(NX_NOT_SUCCESSFUL);
    }

    /* Target Address must not be a multicast address. */
    if ((nd_header_ptr -> nx_icmpv6_nd_targetAddress[0] & (ULONG)0xFF000000) == (ULONG)0xFF000000)
    {
        return(NX_NOT_SUCCESSFUL);
    }

    /* Find out the destination IP address type.  */
    dest_address_type = IPv6_Address_Type(ipv6_header -> nx_ip_header_destination_ip);

    option_check = 0;

    if (nd_header_ptr -> nx_icmpv6_nd_header.nx_icmpv6_header_type == NX_ICMPV6_NEIGHBOR_SOLICITATION_TYPE)
    {

        /* Find out the source IP address type.  */
        source_address_type = IPv6_Address_Type(ipv6_header -> nx_ip_header_source_ip);

        /* If the IP source address is the unspecified address, the IP destination address
           is a solicated-node multicast address. */
        if (source_address_type == IPV6_ADDRESS_UNSPECIFIED)
        {

            if ((dest_address_type & IPV6_SOLICITED_NODE_MCAST) != IPV6_SOLICITED_NODE_MCAST)
            {
                return(NX_NOT_SUCCESSFUL);
            }

            /* if the IP source address is the unspecified address, it must not contain
               source link-layer address option in the message. */
            option_check = NX_NO_SLLA;
        }
    }
    else
    {

        /* If the IP Destination Address is a multicast address, the
           Solicted flag must be zero. */
        if (((ipv6_header -> nx_ip_header_destination_ip[0] & (ULONG)0xFF000000) == (ULONG)0xFF000000) &&
            nd_header_ptr -> nx_icmpv6_nd_flag & 0x40000000)
        {

            return(NX_NOT_SUCCESSFUL);
        }
    }

    /* Locate the option field. */
    /*lint -e{923} suppress cast between pointer and ULONG, since it is necessary  */
    option_ptr    = (NX_ICMPV6_OPTION *)NX_UCHAR_POINTER_ADD(nd_header_ptr, sizeof(NX_ICMPV6_ND));
    option_length = (UINT)(packet_ptr -> nx_packet_length - sizeof(NX_ICMPV6_ND));

    /* Validate option fields if there are any. */
    if (option_length)
    {
        return(_nx_icmpv6_validate_options(option_ptr, (INT)option_length, (INT)option_check));
    }

    return(NX_SUCCESS);
}


#endif /* FEATURE_NX_IPV6 */