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 Protocol (IP)                                              */
18 /**                                                                       */
19 /**************************************************************************/
20 /**************************************************************************/
21 
22 #define NX_SOURCE_CODE
23 
24 
25 
26 /* Include necessary system files.  */
27 
28 #include "nx_api.h"
29 #include "nx_ipv6.h"
30 #include "nx_icmpv6.h"
31 
32 #ifdef FEATURE_NX_IPV6
33 
34 /**************************************************************************/
35 /*                                                                        */
36 /*  FUNCTION                                               RELEASE        */
37 /*                                                                        */
38 /*    _nx_ipv6_option_error                               PORTABLE C      */
39 /*                                                           6.1          */
40 /*  AUTHOR                                                                */
41 /*                                                                        */
42 /*    Yuxin Zhou, Microsoft Corporation                                   */
43 /*                                                                        */
44 /*  DESCRIPTION                                                           */
45 /*                                                                        */
46 /*    This function handles an invalid Options header packet by examining */
47 /*    the option header option type's most significant bits and           */
48 /*    determining if an error message is sent and if the rest of the      */
49 /*    packet should be processed or discarded.                            */
50 /*                                                                        */
51 /*  INPUT                                                                 */
52 /*                                                                        */
53 /*    ip_ptr                                Pointer to IP control block   */
54 /*    packet_ptr                            Pointer to packet to send     */
55 /*    option_type                           The type of option            */
56 /*    offset                                Where the error occurs        */
57 /*                                                                        */
58 /*                                                                        */
59 /*  OUTPUT                                                                */
60 /*                                                                        */
61 /*    NX_SUCCESS                             Skip this option; no errors  */
62 /*    NX_OPTION_HEADER_ERROR                 Error; Drop the entire packet*/
63 /*                                                                        */
64 /*  CALLS                                                                 */
65 /*                                                                        */
66 /*    None                                                                */
67 /*                                                                        */
68 /*  CALLED BY                                                             */
69 /*                                                                        */
70 /*    _nx_ipv6_process_hop_by_hop_option                                  */
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_ipv6_option_error(NX_IP * ip_ptr,NX_PACKET * packet_ptr,UCHAR option_type,UINT offset)81 UINT _nx_ipv6_option_error(NX_IP *ip_ptr, NX_PACKET *packet_ptr, UCHAR option_type, UINT offset)
82 {
83 
84 UINT rv = NX_SUCCESS;
85 
86 /*lint -e{927} -e{826} suppress cast of pointer to pointer, since it is necessary  */
87 NX_IPV6_HEADER *ip_header_ptr = (NX_IPV6_HEADER *)packet_ptr -> nx_packet_ip_header;
88 
89     /* Top 2 bits of the option type indicate how we shall process this option
90        in case of an error. */
91     switch (option_type >> 6)
92     {
93 
94     case 3: /* Discard the packet and send ICMP Parameter Problem to unicast address */
95         if ((ip_header_ptr -> nx_ip_header_destination_ip[0] & (ULONG)0xFF000000) == (ULONG)0xFF000000)
96         {
97 
98             /* If the destination address is a multicast address, we discard the packet. */
99             rv = NX_OPTION_HEADER_ERROR;
100             break;
101         }
102     /*
103        No need to break here:  execute the next two cases:
104        (1) transmit ICMP error message
105        (2) release the packet.
106      */
107 
108     /*lint -e{825} suppress fallthrough, since it is necessary.  */ /* fallthrough */
109     case 2: /* Discard the packet and send ICMP Parameter Problem */
110 
111 #ifndef NX_DISABLE_ICMPV6_ERROR_MESSAGE
112 
113         NX_ICMPV6_SEND_PARAMETER_PROBLEM(ip_ptr, packet_ptr, 2, (ULONG)(offset + sizeof(NX_IPV6_HEADER)));
114 #else
115         NX_PARAMETER_NOT_USED(ip_ptr);
116         NX_PARAMETER_NOT_USED(offset);
117 #endif
118 
119     /* No break here.  Execute the next statement to release the packet. */
120 
121     /*lint -e{825} suppress fallthrough, since it is necessary.  */ /* fallthrough */
122     case 1: /* Discard the packet */
123 
124         /* Error status - Drop the packet */
125         rv = NX_OPTION_HEADER_ERROR;
126         break;
127 
128     case 0: /* Skip over this option and continue processing the rest of the packet. */
129     default:
130         break;
131     }
132 
133     return(rv);
134 }
135 
136 
137 #endif /*  FEATURE_NX_IPV6 */
138 
139