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-2025 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.5.0
29  **/
30 
31 #ifndef _ICMP_H
32 #define _ICMP_H
33 
34 //Dependencies
35 #include "core/net.h"
36 
37 //ICMP query identifier range (lower limit)
38 #ifndef ICMP_QUERY_ID_MIN
39  #define ICMP_QUERY_ID_MIN 0
40 #elif (ICMP_QUERY_ID_MIN < 0)
41  #error ICMP_QUERY_ID_MIN parameter is not valid
42 #endif
43 
44 //ICMP query identifier range (upper limit)
45 #ifndef ICMP_QUERY_ID_MAX
46  #define ICMP_QUERY_ID_MAX 32767
47 #elif (ICMP_QUERY_ID_MAX <= ICMP_QUERY_ID_MIN || ICMP_QUERY_ID_MAX > 65535)
48  #error ICMP_QUERY_ID_MAX parameter is not valid
49 #endif
50 
51 //C++ guard
52 #ifdef __cplusplus
53 extern "C" {
54 #endif
55 
56 
57 /**
58  * @brief ICMP message type
59  *
60  * The type field indicates the type of the message. Its
61  * value determines the format of the remaining data
62  *
63  **/
64 
65 typedef enum
66 {
85 
86 
87 /**
88  * @brief Destination Unreachable message codes
89  **/
90 
91 typedef enum
92 {
100 
101 
102 /**
103  * @brief Time Exceeded message codes
104  **/
105 
106 typedef enum
107 {
111 
112 
113 //CC-RX, CodeWarrior or Win32 compiler?
114 #if defined(__CCRX__)
115  #pragma pack
116 #elif defined(__CWCC__) || defined(_WIN32)
117  #pragma pack(push, 1)
118 #endif
119 
120 
121 /**
122  * @brief ICMP header
123  **/
124 
126 {
127  uint8_t type; //0
128  uint8_t code; //1
129  uint16_t checksum; //2-3
130  uint8_t data[]; //4
132 
133 
134 /**
135  * @brief ICMP Query message
136  **/
137 
138 typedef __packed_struct
139 {
140  uint8_t type; //0
141  uint8_t code; //1
142  uint16_t checksum; //2-3
143  uint16_t identifier; //4-5
144  uint16_t unused; //6-7
145  uint8_t data[]; //8
147 
148 
149 /**
150  * @brief ICMP Echo Request and Echo Reply messages
151  **/
152 
153 typedef __packed_struct
154 {
155  uint8_t type; //0
156  uint8_t code; //1
157  uint16_t checksum; //2-3
158  uint16_t identifier; //4-5
159  uint16_t sequenceNumber; //6-7
160  uint8_t data[]; //8
162 
163 
164 /**
165  * @brief ICMP Error message
166  **/
167 
168 typedef __packed_struct
169 {
170  uint8_t type; //0
171  uint8_t code; //1
172  uint16_t checksum; //2-3
173  uint8_t parameter; //4
174  uint8_t unused[3]; //5-7
175  uint8_t data[]; //8
177 
178 
179 /**
180  * @brief ICMP Destination Unreachable message
181  **/
182 
183 typedef __packed_struct
184 {
185  uint8_t type; //0
186  uint8_t code; //1
187  uint16_t checksum; //2-3
188  uint32_t unused; //4-7
189  uint8_t data[]; //8
191 
192 
193 /**
194  * @brief ICMP Time Exceeded message
195  **/
196 
197 typedef __packed_struct
198 {
199  uint8_t type; //0
200  uint8_t code; //1
201  uint16_t checksum; //2-3
202  uint32_t unused; //4-7
203  uint8_t data[]; //8
205 
206 
207 /**
208  * @brief ICMP Parameter Problem message
209  **/
210 
211 typedef __packed_struct
212 {
213  uint8_t type; //0
214  uint8_t code; //1
215  uint16_t checksum; //2-3
216  uint8_t pointer; //4
217  uint8_t unused[3]; //5-7
218  uint8_t data[]; //8
220 
221 
222 //CC-RX, CodeWarrior or Win32 compiler?
223 #if defined(__CCRX__)
224  #pragma unpack
225 #elif defined(__CWCC__) || defined(_WIN32)
226  #pragma pack(pop)
227 #endif
228 
229 //ICMP related functions
231 
233  bool_t enable);
234 
235 void icmpProcessMessage(NetInterface *interface,
236  const Ipv4PseudoHeader *requestPseudoHeader, const NetBuffer *buffer,
237  size_t offset);
238 
239 void icmpProcessEchoRequest(NetInterface *interface,
240  const Ipv4PseudoHeader *requestPseudoHeader, const NetBuffer *request,
241  size_t requestOffset);
242 
243 error_t icmpSendErrorMessage(NetInterface *interface, uint8_t type,
244  uint8_t code, uint8_t parameter, const NetBuffer *ipPacket,
245  size_t ipPacketOffset);
246 
247 void icmpUpdateInStats(uint8_t type);
248 void icmpUpdateOutStats(uint8_t type);
249 
250 void icmpDumpMessage(const IcmpHeader *message);
253 
254 //C++ guard
255 #ifdef __cplusplus
256 }
257 #endif
258 
259 #endif
uint16_t checksum
Definition: icmp.h:129
uint16_t sequenceNumber
Definition: icmp.h:159
@ ICMP_TYPE_ADDR_MASK_REQUEST
Definition: icmp.h:81
int bool_t
Definition: compiler_port.h:61
uint8_t pointer
Definition: icmp.h:216
error_t icmpEnableEchoRequests(NetInterface *interface, bool_t enable)
Enable support for ICMP Echo Request messages.
Definition: icmp.c:57
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:95
uint8_t type
Definition: coap_common.h:176
@ ICMP_TYPE_TIME_EXCEEDED
Definition: icmp.h:75
typedef __packed_struct
ICMP header.
Definition: icmp.h:126
uint8_t parameter
Definition: icmp.h:173
@ ICMP_CODE_NET_UNREACHABLE
Definition: icmp.h:93
@ ICMP_TYPE_PARAM_PROBLEM
Definition: icmp.h:76
IcmpDestUnreachableCode
Destination Unreachable message codes.
Definition: icmp.h:92
@ ICMP_TYPE_ROUTER_SOL
Definition: icmp.h:74
IcmpTimeExceededMessage
Definition: icmp.h:204
uint8_t ipPacket[]
Definition: ndp.h:431
IcmpHeader
Definition: icmp.h:131
error_t icmpEnableBroadcastEchoRequests(NetInterface *interface, bool_t enable)
Enable support for broadcast ICMP Echo Request messages.
Definition: icmp.c:84
error_t
Error codes.
Definition: error.h:43
@ ICMP_TYPE_REDIRECT
Definition: icmp.h:70
@ ICMP_CODE_HOST_UNREACHABLE
Definition: icmp.h:94
uint16_t unused
Definition: icmp.h:144
@ ICMP_TYPE_TIMESTAMP_REPLY
Definition: icmp.h:78
@ ICMP_TYPE_SOURCE_QUENCH
Definition: icmp.h:69
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:338
#define NetInterface
Definition: net.h:36
IcmpParamProblemMessage
Definition: icmp.h:219
void icmpUpdateInStats(uint8_t type)
Update ICMP input statistics.
Definition: icmp.c:489
IcmpEchoMessage
Definition: icmp.h:161
void icmpUpdateOutStats(uint8_t type)
Update ICMP output statistics.
Definition: icmp.c:564
@ ICMP_TYPE_ECHO_REQUEST
Definition: icmp.h:72
#define Ipv4PseudoHeader
Definition: ipv4.h:39
void icmpDumpEchoMessage(const IcmpEchoMessage *message)
Dump ICMP Echo Request or Echo Reply message.
Definition: icmp.c:657
IcmpTimeExceededCode
Time Exceeded message codes.
Definition: icmp.h:107
@ ICMP_CODE_SOURCE_ROUTE_FAILED
Definition: icmp.h:98
uint16_t identifier
Definition: icmp.h:143
@ ICMP_TYPE_INFO_REPLY
Definition: icmp.h:80
IcmpErrorMessage
Definition: icmp.h:176
@ ICMP_TYPE_DEST_UNREACHABLE
Definition: icmp.h:68
uint8_t code
Definition: icmp.h:128
@ ICMP_TYPE_ECHO_REPLY
Definition: icmp.h:67
void icmpProcessEchoRequest(NetInterface *interface, const Ipv4PseudoHeader *requestPseudoHeader, const NetBuffer *request, size_t requestOffset)
Echo Request message processing.
Definition: icmp.c:193
void icmpDumpMessage(const IcmpHeader *message)
Dump ICMP message for debugging purpose.
Definition: icmp.c:643
@ ICMP_TYPE_ROUTER_ADV
Definition: icmp.h:73
@ ICMP_CODE_FRAG_NEEDED_AND_DF_SET
Definition: icmp.h:97
@ ICMP_TYPE_ALTERNATE_HOST_ADDR
Definition: icmp.h:71
@ ICMP_TYPE_ADDR_MASK_REPLY
Definition: icmp.h:82
@ ICMP_CODE_PORT_UNREACHABLE
Definition: icmp.h:96
@ ICMP_CODE_TTL_EXCEEDED
Definition: icmp.h:108
IcmpDestUnreachableMessage
Definition: icmp.h:190
void icmpProcessMessage(NetInterface *interface, const Ipv4PseudoHeader *requestPseudoHeader, const NetBuffer *buffer, size_t offset)
Incoming ICMP message processing.
Definition: icmp.c:111
void icmpDumpErrorMessage(const IcmpErrorMessage *message)
Dump generic ICMP Error message.
Definition: icmp.c:673
TCP/IP stack core.
@ ICMP_TYPE_INFO_REQUEST
Definition: icmp.h:79
uint8_t data[]
Definition: icmp.h:130
IcmpType
ICMP message type.
Definition: icmp.h:66
IcmpQueryMessage
Definition: icmp.h:146
@ ICMP_TYPE_TRACEROUTE
Definition: icmp.h:83
@ ICMP_TYPE_TIMESTAMP_REQUEST
Definition: icmp.h:77
@ ICMP_CODE_REASSEMBLY_TIME_EXCEEDED
Definition: icmp.h:109