icmp.h
Go to the documentation of this file.
1 /**
2  * @file icmp.h
3  * @brief ICMP (Internet Control Message Protocol)
4  *
5  * @section License
6  *
7  * SPDX-License-Identifier: GPL-2.0-or-later
8  *
9  * Copyright (C) 2010-2026 Oryx Embedded SARL. All rights reserved.
10  *
11  * This file is part of CycloneTCP Open.
12  *
13  * This program is free software; you can redistribute it and/or
14  * modify it under the terms of the GNU General Public License
15  * as published by the Free Software Foundation; either version 2
16  * of the License, or (at your option) any later version.
17  *
18  * This program is distributed in the hope that it will be useful,
19  * but WITHOUT ANY WARRANTY; without even the implied warranty of
20  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21  * GNU General Public License for more details.
22  *
23  * You should have received a copy of the GNU General Public License
24  * along with this program; if not, write to the Free Software Foundation,
25  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
26  *
27  * @author Oryx Embedded SARL (www.oryx-embedded.com)
28  * @version 2.6.0
29  **/
30 
31 #ifndef _ICMP_H
32 #define _ICMP_H
33 
34 //Dependencies
35 #include "core/net.h"
36 
37 //ICMP statistics support
38 #ifndef ICMP_STATS_SUPPORT
39 #if (MIB2_SUPPORT == ENABLED || IP_MIB_SUPPORT == ENABLED)
40  #define ICMP_STATS_SUPPORT ENABLED
41 #else
42  #define ICMP_STATS_SUPPORT DISABLED
43 #endif
44 #elif (ICMP_STATS_SUPPORT != ENABLED && ICMP_STATS_SUPPORT != DISABLED)
45  #error ICMP_STATS_SUPPORT parameter is not valid
46 #endif
47 
48 //ICMP query identifier range (lower limit)
49 #ifndef ICMP_QUERY_ID_MIN
50  #define ICMP_QUERY_ID_MIN 0
51 #elif (ICMP_QUERY_ID_MIN < 0)
52  #error ICMP_QUERY_ID_MIN parameter is not valid
53 #endif
54 
55 //ICMP query identifier range (upper limit)
56 #ifndef ICMP_QUERY_ID_MAX
57  #define ICMP_QUERY_ID_MAX 32767
58 #elif (ICMP_QUERY_ID_MAX <= ICMP_QUERY_ID_MIN || ICMP_QUERY_ID_MAX > 65535)
59  #error ICMP_QUERY_ID_MAX parameter is not valid
60 #endif
61 
62 //ICMP statistics
63 #if (ICMP_STATS_SUPPORT == ENABLED)
64  #define ICMP_STATS_INC_COUNTER32(name, value) interface->netContext->icmpStats.name += value
65 #else
66  #define ICMP_STATS_INC_COUNTER32(name, value)
67 #endif
68 
69 //C++ guard
70 #ifdef __cplusplus
71 extern "C" {
72 #endif
73 
74 
75 /**
76  * @brief ICMP message type
77  *
78  * The type field indicates the type of the message. Its
79  * value determines the format of the remaining data
80  *
81  **/
82 
83 typedef enum
84 {
103 
104 
105 /**
106  * @brief Destination Unreachable message codes
107  **/
108 
109 typedef enum
110 {
118 
119 
120 /**
121  * @brief Time Exceeded message codes
122  **/
123 
124 typedef enum
125 {
129 
130 
131 //CC-RX, CodeWarrior or Win32 compiler?
132 #if defined(__CCRX__)
133  #pragma pack
134 #elif defined(__CWCC__) || defined(_WIN32)
135  #pragma pack(push, 1)
136 #endif
137 
138 
139 /**
140  * @brief ICMP header
141  **/
142 
144 {
145  uint8_t type; //0
146  uint8_t code; //1
147  uint16_t checksum; //2-3
148  uint8_t data[]; //4
150 
151 
152 /**
153  * @brief ICMP Query message
154  **/
155 
156 typedef __packed_struct
157 {
158  uint8_t type; //0
159  uint8_t code; //1
160  uint16_t checksum; //2-3
161  uint16_t identifier; //4-5
162  uint16_t unused; //6-7
163  uint8_t data[]; //8
165 
166 
167 /**
168  * @brief ICMP Echo Request and Echo Reply messages
169  **/
170 
171 typedef __packed_struct
172 {
173  uint8_t type; //0
174  uint8_t code; //1
175  uint16_t checksum; //2-3
176  uint16_t identifier; //4-5
177  uint16_t sequenceNumber; //6-7
178  uint8_t data[]; //8
180 
181 
182 /**
183  * @brief ICMP Error message
184  **/
185 
186 typedef __packed_struct
187 {
188  uint8_t type; //0
189  uint8_t code; //1
190  uint16_t checksum; //2-3
191  uint8_t parameter; //4
192  uint8_t unused[3]; //5-7
193  uint8_t data[]; //8
195 
196 
197 /**
198  * @brief ICMP Destination Unreachable message
199  **/
200 
201 typedef __packed_struct
202 {
203  uint8_t type; //0
204  uint8_t code; //1
205  uint16_t checksum; //2-3
206  uint32_t unused; //4-7
207  uint8_t data[]; //8
209 
210 
211 /**
212  * @brief ICMP Time Exceeded message
213  **/
214 
215 typedef __packed_struct
216 {
217  uint8_t type; //0
218  uint8_t code; //1
219  uint16_t checksum; //2-3
220  uint32_t unused; //4-7
221  uint8_t data[]; //8
223 
224 
225 /**
226  * @brief ICMP Parameter Problem message
227  **/
228 
229 typedef __packed_struct
230 {
231  uint8_t type; //0
232  uint8_t code; //1
233  uint16_t checksum; //2-3
234  uint8_t pointer; //4
235  uint8_t unused[3]; //5-7
236  uint8_t data[]; //8
238 
239 
240 //CC-RX, CodeWarrior or Win32 compiler?
241 #if defined(__CCRX__)
242  #pragma unpack
243 #elif defined(__CWCC__) || defined(_WIN32)
244  #pragma pack(pop)
245 #endif
246 
247 //ICMP related functions
249 
251  bool_t enable);
252 
253 void icmpProcessMessage(NetInterface *interface,
254  const Ipv4PseudoHeader *requestPseudoHeader, const NetBuffer *buffer,
255  size_t offset);
256 
257 void icmpProcessEchoRequest(NetInterface *interface,
258  const Ipv4PseudoHeader *requestPseudoHeader, const NetBuffer *request,
259  size_t requestOffset);
260 
261 error_t icmpSendErrorMessage(NetInterface *interface, uint8_t type,
262  uint8_t code, uint8_t parameter, const NetBuffer *ipPacket,
263  size_t ipPacketOffset);
264 
265 void icmpDumpMessage(const IcmpHeader *message);
268 
269 //C++ guard
270 #ifdef __cplusplus
271 }
272 #endif
273 
274 #endif
uint16_t checksum
Definition: icmp.h:147
uint16_t sequenceNumber
Definition: icmp.h:177
@ ICMP_TYPE_ADDR_MASK_REQUEST
Definition: icmp.h:99
int bool_t
Definition: compiler_port.h:63
uint8_t pointer
Definition: icmp.h:234
error_t icmpEnableEchoRequests(NetInterface *interface, bool_t enable)
Enable support for ICMP Echo Request messages.
Definition: icmp.c:55
Structure describing a buffer that spans multiple chunks.
Definition: net_mem.h:89
uint8_t message[]
Definition: chap.h:154
@ ICMP_CODE_PROTOCOL_UNREACHABLE
Definition: icmp.h:113
uint8_t type
Definition: coap_common.h:176
@ ICMP_TYPE_TIME_EXCEEDED
Definition: icmp.h:93
typedef __packed_struct
ICMP header.
Definition: icmp.h:144
uint8_t parameter
Definition: icmp.h:191
@ ICMP_CODE_NET_UNREACHABLE
Definition: icmp.h:111
@ ICMP_TYPE_PARAM_PROBLEM
Definition: icmp.h:94
IcmpDestUnreachableCode
Destination Unreachable message codes.
Definition: icmp.h:110
@ ICMP_TYPE_ROUTER_SOL
Definition: icmp.h:92
IcmpTimeExceededMessage
Definition: icmp.h:222
uint8_t ipPacket[]
Definition: ndp.h:431
IcmpHeader
Definition: icmp.h:149
error_t icmpEnableBroadcastEchoRequests(NetInterface *interface, bool_t enable)
Enable support for broadcast ICMP Echo Request messages.
Definition: icmp.c:82
error_t
Error codes.
Definition: error.h:43
@ ICMP_TYPE_REDIRECT
Definition: icmp.h:88
@ ICMP_CODE_HOST_UNREACHABLE
Definition: icmp.h:112
uint16_t unused
Definition: icmp.h:162
@ ICMP_TYPE_TIMESTAMP_REPLY
Definition: icmp.h:96
@ ICMP_TYPE_SOURCE_QUENCH
Definition: icmp.h:87
error_t icmpSendErrorMessage(NetInterface *interface, uint8_t type, uint8_t code, uint8_t parameter, const NetBuffer *ipPacket, size_t ipPacketOffset)
Send an ICMP Error message.
Definition: icmp.c:335
#define NetInterface
Definition: net.h:40
IcmpParamProblemMessage
Definition: icmp.h:237
IcmpEchoMessage
Definition: icmp.h:179
@ ICMP_TYPE_ECHO_REQUEST
Definition: icmp.h:90
#define Ipv4PseudoHeader
Definition: ipv4.h:39
void icmpDumpEchoMessage(const IcmpEchoMessage *message)
Dump ICMP Echo Request or Echo Reply message.
Definition: icmp.c:502
IcmpTimeExceededCode
Time Exceeded message codes.
Definition: icmp.h:125
@ ICMP_CODE_SOURCE_ROUTE_FAILED
Definition: icmp.h:116
uint16_t identifier
Definition: icmp.h:161
@ ICMP_TYPE_INFO_REPLY
Definition: icmp.h:98
IcmpErrorMessage
Definition: icmp.h:194
@ ICMP_TYPE_DEST_UNREACHABLE
Definition: icmp.h:86
uint8_t code
Definition: icmp.h:146
@ ICMP_TYPE_ECHO_REPLY
Definition: icmp.h:85
void icmpProcessEchoRequest(NetInterface *interface, const Ipv4PseudoHeader *requestPseudoHeader, const NetBuffer *request, size_t requestOffset)
Echo Request message processing.
Definition: icmp.c:188
void icmpDumpMessage(const IcmpHeader *message)
Dump ICMP message for debugging purpose.
Definition: icmp.c:488
@ ICMP_TYPE_ROUTER_ADV
Definition: icmp.h:91
@ ICMP_CODE_FRAG_NEEDED_AND_DF_SET
Definition: icmp.h:115
@ ICMP_TYPE_ALTERNATE_HOST_ADDR
Definition: icmp.h:89
@ ICMP_TYPE_ADDR_MASK_REPLY
Definition: icmp.h:100
@ ICMP_CODE_PORT_UNREACHABLE
Definition: icmp.h:114
@ ICMP_CODE_TTL_EXCEEDED
Definition: icmp.h:126
IcmpDestUnreachableMessage
Definition: icmp.h:208
void icmpProcessMessage(NetInterface *interface, const Ipv4PseudoHeader *requestPseudoHeader, const NetBuffer *buffer, size_t offset)
Incoming ICMP message processing.
Definition: icmp.c:109
void icmpDumpErrorMessage(const IcmpErrorMessage *message)
Dump generic ICMP Error message.
Definition: icmp.c:518
TCP/IP stack core.
@ ICMP_TYPE_INFO_REQUEST
Definition: icmp.h:97
uint8_t data[]
Definition: icmp.h:148
IcmpType
ICMP message type.
Definition: icmp.h:84
IcmpQueryMessage
Definition: icmp.h:164
@ ICMP_TYPE_TRACEROUTE
Definition: icmp.h:101
@ ICMP_TYPE_TIMESTAMP_REQUEST
Definition: icmp.h:95
@ ICMP_CODE_REASSEMBLY_TIME_EXCEEDED
Definition: icmp.h:127