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 Control Message Protocol (ICMP)                            */
19 /**                                                                       */
20 /**************************************************************************/
21 /**************************************************************************/
22 
23 
24 /**************************************************************************/
25 /*                                                                        */
26 /*  COMPONENT DEFINITION                                   RELEASE        */
27 /*                                                                        */
28 /*    nx_icmpv4.h                                         PORTABLE C      */
29 /*                                                           6.1.9        */
30 /*  AUTHOR                                                                */
31 /*                                                                        */
32 /*    Yuxin Zhou, Microsoft Corporation                                   */
33 /*                                                                        */
34 /*  DESCRIPTION                                                           */
35 /*                                                                        */
36 /*    This file defines the NetX Internet Control Message Protocol (ICMP) */
37 /*    component, including all data types and external references.  It is */
38 /*    assumed that nx_api.h and nx_port.h have already been included.     */
39 /*                                                                        */
40 /*  RELEASE HISTORY                                                       */
41 /*                                                                        */
42 /*    DATE              NAME                      DESCRIPTION             */
43 /*                                                                        */
44 /*  05-19-2020     Yuxin Zhou               Initial Version 6.0           */
45 /*  09-30-2020     Yuxin Zhou               Modified comment(s),          */
46 /*                                            resulting in version 6.1    */
47 /*  10-15-2021     Yuxin Zhou               Modified comment(s), included */
48 /*                                            necessary header file,      */
49 /*                                            resulting in version 6.1.9  */
50 /*                                                                        */
51 /**************************************************************************/
52 
53 #ifndef NX_ICMPV4_H
54 #define NX_ICMPV4_H
55 
56 #include "nx_api.h"
57 
58 #ifndef NX_DISABLE_IPV4
59 /* Define ICMP types and codes.  According to RFC792.  */
60 
61 /* Define ICMP types.  */
62 #define NX_ICMP_ECHO_REPLY_TYPE       0
63 #define NX_ICMP_DEST_UNREACHABLE_TYPE 3
64 /*
65 #define NX_ICMP_SOURCE_QUENCH_TYPE    4
66 #define NX_ICMP_REDIRECT_TYPE         5
67  */
68 #define NX_ICMP_ECHO_REQUEST_TYPE     8
69 #define NX_ICMP_TIME_EXCEEDED_TYPE    11
70 #define NX_ICMP_PARAMETER_PROB_TYPE   12
71 #define NX_ICMP_TIMESTAMP_REQ_TYPE    13
72 /*
73 #define NX_ICMP_TIMESTAMP_REP_TYPE    14
74 #define NX_ICMP_INFORMATION_REQ_TYPE  15
75 #define NX_ICMP_INFORMATION_REP_TYPE  16
76 #define NX_ICMP_ADDRESS_MASK_REQ_TYPE 17
77 #define NX_ICMP_ADDRESS_MASK_REP_TYPE 18
78  */
79 
80 /* Define the ICMP Destination Unreachable Message codes.  */
81 /*
82 #define NX_ICMP_NETWORK_UNREACH_CODE  0
83 #define NX_ICMP_HOST_UNREACH_CODE     1
84  */
85 #define NX_ICMP_PROTOCOL_UNREACH_CODE 2
86 #define NX_ICMP_PORT_UNREACH_CODE     3
87 /*
88 #define NX_ICMP_FRAMENT_NEEDED_CODE   4
89 #define NX_ICMP_SOURCE_ROUTE_CODE     5
90  */
91 
92 /* Define the TIME Exceeded codes.  */
93 /*
94 #define NX_ICMP_TTL_EXCEEDED_CODE     0
95  */
96 #define NX_ICMP_FRT_EXCEEDED_CODE     1
97 
98 /* Define the Zero Code for Parameter Problem Message, Source Quench Message, Redirect Message,
99    Echo Request/Reply, Timestamp Request/Reply and Information Request/Reply Message .  */
100 #define NX_ICMP_ZERO_CODE             0
101 
102 /* Define Basic ICMP packet header data type.  This will be used to
103    build new ICMP packets and to examine incoming packets into NetX.  */
104 
105 typedef  struct NX_ICMP_HEADER_STRUCT
106 {
107     /* Define the first 32-bit word of the ICMP header.  This word contains
108        the following information:
109 
110             bits 31-24  ICMP 8-bit type defined as follows:
111 
112                                         Type Field      ICMP Message Type
113 
114                                             0           Echo Reply
115                                             3           Destination Unreachable
116                                             4           Source Quench
117                                             5           Redirect (change a route)
118                                             8           Echo Request
119                                             11          Time exceeded for Datagram
120                                             12          Parameter Problem on a Datagram
121                                             13          Timestamp Request
122                                             14          Timestamp Reply
123                                             17          Address Mask Request
124                                             18          Address Mask Reply
125 
126             bits 23-16  ICMP 8-bit code defined as follows:
127 
128                                         Code Field      ICMP Code Meaning
129 
130                                             0           Network unreachable
131                                             1           Host unreachable
132                                             2           Protocol unreachable
133                                             3           Port unreachable
134                                             4           Fragmentation needed and DF is set
135                                             5           Source route failed
136                                             6           Destination network unknown
137                                             7           Destination host unknown
138                                             8           Source host isolated
139                                             9           Communication with destination network
140                                                             administratively prohibited
141                                             10          Communication with destination host
142                                                             administratively prohibited
143                                             11          Network unreachable for type of service
144                                             12          Host unreachable for type of service
145 
146             bits 15-0   ICMP 16-bit checksum
147 
148      */
149 
150     ULONG nx_icmp_header_word_0;
151 
152     /* Define the second and final word of the ICMP header.  This word contains
153        the following information:
154 
155             bits 31-16  ICMP 16-bit Identification
156             bits 15-0   ICMP 16-bit Sequence Number
157      */
158     ULONG nx_icmp_header_word_1;
159 } NX_ICMP_HEADER;
160 
161 /* Define the ICMP echo request header message size.  */
162 
163 #define NX_ICMP_HEADER_SIZE sizeof(NX_ICMP_HEADER)
164 
165 
166 /* Define basic ICMPv4 packet header data type. */
167 
168 typedef  struct NX_ICMPV4_HEADER_STRUCT
169 {
170 
171     /*  Header field for ICMPv4 message type */
172     UCHAR   nx_icmpv4_header_type;
173 
174     /*  Header field for ICMPv4 message code (type context specific) */
175     UCHAR   nx_icmpv4_header_code;
176 
177     /*  Header field for ICMPv4 header checksum */
178     USHORT  nx_icmpv4_header_checksum;
179 } NX_ICMPV4_HEADER;
180 
181 /* Define ICMPv4 error message type. */
182 
183 typedef struct NX_ICMPV4_ERROR_STRUCT
184 {
185     /* General ICMP header. */
186     NX_ICMPV4_HEADER nx_icmpv4_error_header;
187 
188     /* Pointer to the original IPv4 packet where error is detected. */
189     ULONG            nx_icmpv4_error_pointer;
190 } NX_ICMPV4_ERROR;
191 
192 /* ICMP echo request message type. */
193 
194 typedef struct NX_ICMPV4_ECHO_STRUCT
195 {
196 
197     /* General ICMP header. */
198     /*lint -esym(768,NX_ICMPV4_ECHO_STRUCT::nx_icmpv4_echo_header) suppress member not referenced. It is used before casting from NX_ICMPV4_HEADER. */
199     NX_ICMPV4_HEADER nx_icmpv4_echo_header;
200 
201     /* Echo request message ID */
202     /*lint -esym(768,NX_ICMPV4_ECHO_STRUCT::nx_icmpv4_echo_identifier) suppress member not referenced. It is not used as a host. */
203     USHORT           nx_icmpv4_echo_identifier;
204 
205     /* Echo request message sequence number */
206     USHORT           nx_icmpv4_echo_sequence_num;
207 } NX_ICMPV4_ECHO;
208 
209 
210 #ifndef NX_DISABLE_ICMPV4_ERROR_MESSAGE
211 /* Define macros for sending out ICMPv4 error messages. */
212 #define NX_ICMPV4_SEND_DEST_UNREACHABLE(ip_ptr, packet, code) \
213     _nx_icmpv4_send_error_message((ip_ptr), (packet), (ULONG)((NX_ICMP_DEST_UNREACHABLE_TYPE << 24) | ((code) << 16)), 0)
214 
215 #define NX_ICMPV4_SEND_TIME_EXCEED(ip_ptr, packet, code) \
216     _nx_icmpv4_send_error_message((ip_ptr), (packet), (ULONG)((NX_ICMP_TIME_EXCEEDED_TYPE << 24) | ((code) << 16)), 0)
217 
218 #define NX_ICMPV4_SEND_PARAMETER_PROBLEM(ip_ptr, packet, code, offset) \
219     _nx_icmpv4_send_error_message((ip_ptr), (packet), (ULONG)((NX_ICMP_PARAMETER_PROB_TYPE << 24) | ((code) << 16)), (offset))
220 
221 #endif /* NX_DISABLE_ICMPV4_ERROR_MESSAGE */
222 
223 
224 /* Define service for sending ICMPv4 error messages. */
225 
226 #ifndef NX_DISABLE_ICMPV4_ERROR_MESSAGE
227 VOID _nx_icmpv4_send_error_message(NX_IP *ip_ptr, NX_PACKET *packet, ULONG word1, ULONG pointer);
228 #endif /* NX_DISABLE_ICMPV4_ERROR_MESSAGE */
229 
230 /* Define internal ICMPv4 handling functions. */
231 
232 VOID _nx_icmp_packet_process(NX_IP *ip_ptr, NX_PACKET *packet_ptr);
233 VOID _nx_icmp_queue_process(NX_IP *ip_ptr);
234 VOID _nx_icmpv4_packet_process(NX_IP *ip_ptr, NX_PACKET *packet_ptr);
235 VOID _nx_icmpv4_process_echo_reply(NX_IP *ip_ptr, NX_PACKET *packet_ptr);
236 VOID _nx_icmpv4_process_echo_request(NX_IP *ip_ptr, NX_PACKET *packet_ptr);
237 UINT _nx_icmp_interface_ping(NX_IP *ip_ptr, ULONG ip_address,
238                              NX_INTERFACE *interface_ptr, ULONG next_hop_address,
239                              CHAR *data_ptr, ULONG data_size,
240                              NX_PACKET **response_ptr, ULONG wait_option);
241 #endif /* NX_DISABLE_IPV4 */
242 
243 
244 /* Define ICMPv4 API function prototypes. */
245 
246 UINT _nx_icmp_enable(NX_IP *ip_ptr);
247 UINT _nx_icmp_info_get(NX_IP *ip_ptr, ULONG *pings_sent, ULONG *ping_timeouts,
248                        ULONG *ping_threads_suspended, ULONG *ping_responses_received,
249                        ULONG *icmp_checksum_errors, ULONG *icmp_unhandled_messages);
250 UINT _nx_icmp_ping(NX_IP *ip_ptr, ULONG ip_address, CHAR *data, ULONG data_size,
251                    NX_PACKET **response_ptr, ULONG wait_option);
252 
253 /* Define error checking shells for API services.  These are only referenced by the
254    application.  */
255 
256 UINT _nxe_icmp_enable(NX_IP *ip_ptr);
257 UINT _nxe_icmp_info_get(NX_IP *ip_ptr, ULONG *pings_sent, ULONG *ping_timeouts,
258                         ULONG *ping_threads_suspended, ULONG *ping_responses_received,
259                         ULONG *icmp_checksum_errors, ULONG *icmp_unhandled_messages);
260 UINT _nxe_icmp_ping(NX_IP *ip_ptr, ULONG ip_address, CHAR *data, ULONG data_size,
261                     NX_PACKET **response_ptr, ULONG wait_option);
262 
263 
264 #endif
265 
266