suricata
decode-icmpv4.h
Go to the documentation of this file.
1 /* Copyright (C) 2007-2010 Open Information Security Foundation
2  *
3  * You can copy, redistribute or modify this Program under the terms of
4  * the GNU General Public License version 2 as published by the Free
5  * Software Foundation.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10  * GNU General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License
13  * version 2 along with this program; if not, write to the Free Software
14  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
15  * 02110-1301, USA.
16  */
17 
18 /**
19  * \file
20  *
21  * \author Victor Julien <victor@inliniac.net>
22  */
23 
24 #ifndef __DECODE_ICMPV4_H__
25 #define __DECODE_ICMPV4_H__
26 
27 #include "decode.h"
28 #include "decode-tcp.h"
29 #include "decode-sctp.h"
30 #include "decode-udp.h"
31 
32 #define ICMPV4_HEADER_LEN 8
33 
34 #ifndef ICMP_ECHOREPLY
35 #define ICMP_ECHOREPLY 0 /* Echo Reply */
36 #endif
37 #ifndef ICMP_DEST_UNREACH
38 #define ICMP_DEST_UNREACH 3 /* Destination Unreachable */
39 #endif
40 #ifndef ICMP_SOURCE_QUENCH
41 #define ICMP_SOURCE_QUENCH 4 /* Source Quench */
42 #endif
43 #ifndef ICMP_REDIRECT
44 #define ICMP_REDIRECT 5 /* Redirect (change route) */
45 #endif
46 #ifndef ICMP_ECHO
47 #define ICMP_ECHO 8 /* Echo Request */
48 #endif
49 #ifndef ICMP_ROUTERADVERT
50 #define ICMP_ROUTERADVERT 9
51 #endif
52 #ifndef ICMP_ROUTERSOLICIT
53 #define ICMP_ROUTERSOLICIT 10
54 #endif
55 #ifndef ICMP_TIME_EXCEEDED
56 #define ICMP_TIME_EXCEEDED 11 /* Time Exceeded */
57 #endif
58 #ifndef ICMP_PARAMETERPROB
59 #define ICMP_PARAMETERPROB 12 /* Parameter Problem */
60 #endif
61 #ifndef ICMP_TIMESTAMP
62 #define ICMP_TIMESTAMP 13 /* Timestamp Request */
63 #endif
64 #ifndef ICMP_TIMESTAMPREPLY
65 #define ICMP_TIMESTAMPREPLY 14 /* Timestamp Reply */
66 #endif
67 #ifndef ICMP_INFO_REQUEST
68 #define ICMP_INFO_REQUEST 15 /* Information Request */
69 #endif
70 #ifndef ICMP_INFO_REPLY
71 #define ICMP_INFO_REPLY 16 /* Information Reply */
72 #endif
73 #ifndef ICMP_ADDRESS
74 #define ICMP_ADDRESS 17 /* Address Mask Request */
75 #endif
76 #ifndef ICMP_ADDRESSREPLY
77 #define ICMP_ADDRESSREPLY 18 /* Address Mask Reply */
78 #endif
79 #ifndef NR_ICMP_TYPES
80 #define NR_ICMP_TYPES 18
81 #endif
82 
83 
84 /* Codes for UNREACH. */
85 #ifndef ICMP_NET_UNREACH
86 #define ICMP_NET_UNREACH 0 /* Network Unreachable */
87 #endif
88 #ifndef ICMP_HOST_UNREACH
89 #define ICMP_HOST_UNREACH 1 /* Host Unreachable */
90 #endif
91 #ifndef ICMP_PROT_UNREACH
92 #define ICMP_PROT_UNREACH 2 /* Protocol Unreachable */
93 #endif
94 #ifndef ICMP_PORT_UNREACH
95 #define ICMP_PORT_UNREACH 3 /* Port Unreachable */
96 #endif
97 #ifndef ICMP_FRAG_NEEDED
98 #define ICMP_FRAG_NEEDED 4 /* Fragmentation Needed/DF set */
99 #endif
100 #ifndef ICMP_SR_FAILED
101 #define ICMP_SR_FAILED 5 /* Source Route failed */
102 #endif
103 #ifndef ICMP_NET_UNKNOWN
104 #define ICMP_NET_UNKNOWN 6
105 #endif
106 #ifndef ICMP_HOST_UNKNOWN
107 #define ICMP_HOST_UNKNOWN 7
108 #endif
109 #ifndef ICMP_HOST_ISOLATED
110 #define ICMP_HOST_ISOLATED 8
111 #endif
112 #ifndef ICMP_NET_ANO
113 #define ICMP_NET_ANO 9
114 #endif
115 #ifndef ICMP_HOST_ANO
116 #define ICMP_HOST_ANO 10
117 #endif
118 #ifndef ICMP_NET_UNR_TOS
119 #define ICMP_NET_UNR_TOS 11
120 #endif
121 #ifndef ICMP_HOST_UNR_TOS
122 #define ICMP_HOST_UNR_TOS 12
123 #endif
124 #ifndef ICMP_PKT_FILTERED
125 #define ICMP_PKT_FILTERED 13 /* Packet filtered */
126 #endif
127 #ifndef ICMP_PREC_VIOLATION
128 #define ICMP_PREC_VIOLATION 14 /* Precedence violation */
129 
130 #endif
131 #ifndef ICMP_PREC_CUTOFF
132 #define ICMP_PREC_CUTOFF 15 /* Precedence cut off */
133 #endif
134 #ifndef NR_ICMP_UNREACH
135 #define NR_ICMP_UNREACH 15 /* instead of hardcoding immediate value */
136 #endif
137 
138 /* Codes for REDIRECT. */
139 #ifndef ICMP_REDIR_NET
140 #define ICMP_REDIR_NET 0 /* Redirect Net */
141 #endif
142 #ifndef ICMP_REDIR_HOST
143 #define ICMP_REDIR_HOST 1 /* Redirect Host */
144 #endif
145 #ifndef ICMP_REDIR_NETTOS
146 #define ICMP_REDIR_NETTOS 2 /* Redirect Net for TOS */
147 #endif
148 #ifndef ICMP_REDIR_HOSTTOS
149 #define ICMP_REDIR_HOSTTOS 3 /* Redirect Host for TOS */
150 #endif
151 
152 /* Codes for TIME_EXCEEDED. */
153 #ifndef ICMP_EXC_TTL
154 #define ICMP_EXC_TTL 0 /* TTL count exceeded */
155 #endif
156 #ifndef ICMP_EXC_FRAGTIME
157 #define ICMP_EXC_FRAGTIME 1 /* Fragment Reass time exceeded */
158 #endif
159 
160 /** marco for icmpv4 type access */
161 #define ICMPV4_GET_TYPE(p) (p)->icmpv4h->type
162 /** marco for icmpv4 code access */
163 #define ICMPV4_GET_CODE(p) (p)->icmpv4h->code
164 
165 /* ICMPv4 header structure */
166 typedef struct ICMPV4Hdr_
167 {
168  uint8_t type;
169  uint8_t code;
170  uint16_t checksum;
171 } __attribute__((__packed__)) ICMPV4Hdr;
172 
173 /* ICMPv4 header structure */
174 typedef struct ICMPV4ExtHdr_
175 {
176  uint8_t type;
177  uint8_t code;
178  uint16_t checksum;
179  uint16_t id;
180  uint16_t seq;
181 } ICMPV4ExtHdr;
182 
183 /* ICMPv4 vars */
184 typedef struct ICMPV4Vars_
185 {
186  uint16_t id;
187  uint16_t seq;
188 
189  /** Pointers to the embedded packet headers */
191  TCPHdr *emb_tcph;
192  UDPHdr *emb_udph;
193  ICMPV4Hdr *emb_icmpv4h;
194 
195  /** IPv4 src and dst address */
196  struct in_addr emb_ip4_src;
197  struct in_addr emb_ip4_dst;
198  uint8_t emb_ip4_hlen;
199  uint8_t emb_ip4_proto;
200 
201  /** TCP/UDP ports */
202  uint16_t emb_sport;
203  uint16_t emb_dport;
204 } ICMPV4Vars;
205 
206 #define CLEAR_ICMPV4_PACKET(p) do { \
207  (p)->level4_comp_csum = -1; \
208  PACKET_CLEAR_L4VARS((p)); \
209  (p)->icmpv4h = NULL; \
210 } while(0)
211 
212 #define ICMPV4_HEADER_PKT_OFFSET 8
213 
214 /** macro for icmpv4 "type" access */
215 #define ICMPV4_GET_TYPE(p) (p)->icmpv4h->type
216 /** macro for icmpv4 "code" access */
217 #define ICMPV4_GET_CODE(p) (p)->icmpv4h->code
218 /** macro for icmpv4 "csum" access */
219 #define ICMPV4_GET_RAW_CSUM(p) SCNtohs((p)->icmpv4h->checksum)
220 #define ICMPV4_GET_CSUM(p) (p)->icmpv4h->checksum
221 
222 /* If message is informational */
223 
224 /** macro for icmpv4 "id" access */
225 #define ICMPV4_GET_ID(p) ((p)->icmpv4vars.id)
226 /** macro for icmpv4 "seq" access */
227 #define ICMPV4_GET_SEQ(p) ((p)->icmpv4vars.seq)
228 
229 /* If message is Error */
230 
231 /** macro for icmpv4 embedded "protocol" access */
232 #define ICMPV4_GET_EMB_PROTO(p) (p)->icmpv4vars.emb_ip4_proto
233 /** macro for icmpv4 embedded "ipv4h" header access */
234 #define ICMPV4_GET_EMB_IPV4(p) (p)->icmpv4vars.emb_ipv4h
235 /** macro for icmpv4 embedded "tcph" header access */
236 #define ICMPV4_GET_EMB_TCP(p) (p)->icmpv4vars.emb_tcph
237 /** macro for icmpv4 embedded "udph" header access */
238 #define ICMPV4_GET_EMB_UDP(p) (p)->icmpv4vars.emb_udph
239 /** macro for icmpv4 embedded "icmpv4h" header access */
240 #define ICMPV4_GET_EMB_ICMPV4H(p) (p)->icmpv4vars.emb_icmpv4h
241 
242 /** macro for checking if a ICMP DEST UNREACH packet is valid for use
243  * in other parts of the engine, such as the flow engine.
244  *
245  * \warning use only _after_ the decoder has processed the packet
246  */
247 #define ICMPV4_DEST_UNREACH_IS_VALID(p) ( \
248  (!((p)->flags & PKT_IS_INVALID)) && \
249  ((p)->icmpv4h != NULL) && \
250  (ICMPV4_GET_TYPE((p)) == ICMP_DEST_UNREACH) && \
251  (ICMPV4_GET_EMB_IPV4((p)) != NULL) && \
252  ((ICMPV4_GET_EMB_TCP((p)) != NULL) || \
253  (ICMPV4_GET_EMB_UDP((p)) != NULL)))
254 
255 /**
256  * marco for checking if a ICMP packet is an error message or an
257  * query message.
258  *
259  * \todo This check is used in the flow engine and needs to be as
260  * cheap as possible. Consider setting a bitflag at the decoder
261  * stage so we can to a bit check instead of the more expensive
262  * check below.
263  */
264 #define ICMPV4_IS_ERROR_MSG(p) (ICMPV4_GET_TYPE((p)) == ICMP_DEST_UNREACH || \
265  ICMPV4_GET_TYPE((p)) == ICMP_SOURCE_QUENCH || \
266  ICMPV4_GET_TYPE((p)) == ICMP_REDIRECT || \
267  ICMPV4_GET_TYPE((p)) == ICMP_TIME_EXCEEDED || \
268  ICMPV4_GET_TYPE((p)) == ICMP_PARAMETERPROB)
269 
270 void DecodeICMPV4RegisterTests(void);
271 
272 /** ------ Inline functions ------ */
273 static inline uint16_t ICMPV4CalculateChecksum(uint16_t *, uint16_t);
274 
275 /**
276  * \brief Calculates the checksum for the ICMP packet
277  *
278  * \param pkt Pointer to the start of the ICMP packet
279  * \param hlen Total length of the ICMP packet(header + payload)
280  *
281  * \retval csum Checksum for the ICMP packet
282  */
283 static inline uint16_t ICMPV4CalculateChecksum(uint16_t *pkt, uint16_t tlen)
284 {
285  uint16_t pad = 0;
286  uint32_t csum = pkt[0];
287 
288  tlen -= 4;
289  pkt += 2;
290 
291  while (tlen >= 32) {
292  csum += pkt[0] + pkt[1] + pkt[2] + pkt[3] + pkt[4] + pkt[5] + pkt[6] +
293  pkt[7] + pkt[8] + pkt[9] + pkt[10] + pkt[11] + pkt[12] + pkt[13] +
294  pkt[14] + pkt[15];
295  tlen -= 32;
296  pkt += 16;
297  }
298 
299  while(tlen >= 8) {
300  csum += pkt[0] + pkt[1] + pkt[2] + pkt[3];
301  tlen -= 8;
302  pkt += 4;
303  }
304 
305  while(tlen >= 4) {
306  csum += pkt[0] + pkt[1];
307  tlen -= 4;
308  pkt += 2;
309  }
310 
311  while (tlen > 1) {
312  csum += pkt[0];
313  tlen -= 2;
314  pkt += 1;
315  }
316 
317  if (tlen == 1) {
318  *(uint8_t *)(&pad) = (*(uint8_t *)pkt);
319  csum += pad;
320  }
321 
322  csum = (csum >> 16) + (csum & 0x0000FFFF);
323  csum += (csum >> 16);
324 
325  return (uint16_t) ~csum;
326 }
327 
328 int ICMPv4GetCounterpart(uint8_t type);
329 
330 #endif /* __DECODE_ICMPV4_H__ */
331 
uint16_t emb_sport
struct ICMPV4Vars_ ICMPV4Vars
TCPHdr * emb_tcph
uint16_t pad
uint8_t emb_ip4_hlen
struct ICMPV4ExtHdr_ ICMPV4ExtHdr
uint16_t checksum
void DecodeICMPV4RegisterTests(void)
Registers ICMPV4 unit test.
ICMPV4Hdr * emb_icmpv4h
int ICMPv4GetCounterpart(uint8_t type)
uint8_t code
uint16_t checksum
uint8_t type
uint8_t emb_ip4_proto
uint16_t emb_dport
uint16_t id
uint16_t seq
IPV4Hdr * emb_ipv4h
UDPHdr * emb_udph
struct ICMPV4Hdr_ __attribute__((__packed__)) ICMPV4Hdr
DNP3 link header.