24 #ifndef SURICATA_DECODE_ICMPV4_H
25 #define SURICATA_DECODE_ICMPV4_H
30 #define ICMPV4_HEADER_LEN 8
32 #ifndef ICMP_ECHOREPLY
33 #define ICMP_ECHOREPLY 0
35 #ifndef ICMP_DEST_UNREACH
36 #define ICMP_DEST_UNREACH 3
38 #ifndef ICMP_SOURCE_QUENCH
39 #define ICMP_SOURCE_QUENCH 4
42 #define ICMP_REDIRECT 5
47 #ifndef ICMP_ROUTERADVERT
48 #define ICMP_ROUTERADVERT 9
50 #ifndef ICMP_ROUTERSOLICIT
51 #define ICMP_ROUTERSOLICIT 10
53 #ifndef ICMP_TIME_EXCEEDED
54 #define ICMP_TIME_EXCEEDED 11
56 #ifndef ICMP_PARAMETERPROB
57 #define ICMP_PARAMETERPROB 12
59 #ifndef ICMP_TIMESTAMP
60 #define ICMP_TIMESTAMP 13
62 #ifndef ICMP_TIMESTAMPREPLY
63 #define ICMP_TIMESTAMPREPLY 14
65 #ifndef ICMP_INFO_REQUEST
66 #define ICMP_INFO_REQUEST 15
68 #ifndef ICMP_INFO_REPLY
69 #define ICMP_INFO_REPLY 16
72 #define ICMP_ADDRESS 17
74 #ifndef ICMP_ADDRESSREPLY
75 #define ICMP_ADDRESSREPLY 18
78 #define NR_ICMP_TYPES 18
83 #ifndef ICMP_NET_UNREACH
84 #define ICMP_NET_UNREACH 0
86 #ifndef ICMP_HOST_UNREACH
87 #define ICMP_HOST_UNREACH 1
89 #ifndef ICMP_PROT_UNREACH
90 #define ICMP_PROT_UNREACH 2
92 #ifndef ICMP_PORT_UNREACH
93 #define ICMP_PORT_UNREACH 3
95 #ifndef ICMP_FRAG_NEEDED
96 #define ICMP_FRAG_NEEDED 4
98 #ifndef ICMP_SR_FAILED
99 #define ICMP_SR_FAILED 5
101 #ifndef ICMP_NET_UNKNOWN
102 #define ICMP_NET_UNKNOWN 6
104 #ifndef ICMP_HOST_UNKNOWN
105 #define ICMP_HOST_UNKNOWN 7
107 #ifndef ICMP_HOST_ISOLATED
108 #define ICMP_HOST_ISOLATED 8
111 #define ICMP_NET_ANO 9
113 #ifndef ICMP_HOST_ANO
114 #define ICMP_HOST_ANO 10
116 #ifndef ICMP_NET_UNR_TOS
117 #define ICMP_NET_UNR_TOS 11
119 #ifndef ICMP_HOST_UNR_TOS
120 #define ICMP_HOST_UNR_TOS 12
122 #ifndef ICMP_PKT_FILTERED
123 #define ICMP_PKT_FILTERED 13
125 #ifndef ICMP_PREC_VIOLATION
126 #define ICMP_PREC_VIOLATION 14
129 #ifndef ICMP_PREC_CUTOFF
130 #define ICMP_PREC_CUTOFF 15
132 #ifndef NR_ICMP_UNREACH
133 #define NR_ICMP_UNREACH 15
137 #ifndef ICMP_REDIR_NET
138 #define ICMP_REDIR_NET 0
140 #ifndef ICMP_REDIR_HOST
141 #define ICMP_REDIR_HOST 1
143 #ifndef ICMP_REDIR_NETTOS
144 #define ICMP_REDIR_NETTOS 2
146 #ifndef ICMP_REDIR_HOSTTOS
147 #define ICMP_REDIR_HOSTTOS 3
152 #define ICMP_EXC_TTL 0
154 #ifndef ICMP_EXC_FRAGTIME
155 #define ICMP_EXC_FRAGTIME 1
159 #define ICMPV4_GET_TYPE(p) (p)->icmpv4h->type
161 #define ICMPV4_GET_CODE(p) (p)->icmpv4h->code
217 #define CLEAR_ICMPV4_PACKET(p) \
219 PACKET_CLEAR_L4VARS((p)); \
220 (p)->icmpv4h = NULL; \
223 #define ICMPV4_HEADER_PKT_OFFSET 8
226 #define ICMPV4_GET_TYPE(p) (p)->icmpv4h->type
228 #define ICMPV4_GET_CODE(p) (p)->icmpv4h->code
230 #define ICMPV4_GET_RAW_CSUM(p) SCNtohs((p)->icmpv4h->checksum)
231 #define ICMPV4_GET_CSUM(p) (p)->icmpv4h->checksum
236 #define ICMPV4_GET_ID(p) ((p)->l4.vars.icmpv4.id)
238 #define ICMPV4_GET_SEQ(p) ((p)->l4.vars.icmpv4.seq)
243 #define ICMPV4_GET_EMB_PROTO(p) (p)->l4.vars.icmpv4.emb_ip4_proto
246 #define ICMPV4_GET_HLEN_ICMPV4H(p) (p)->l4.vars.icmpv4.hlen
253 #define ICMPV4_DEST_UNREACH_IS_VALID(p) \
254 ((!((p)->flags & PKT_IS_INVALID)) && PacketIsICMPv4((p)) && \
255 ((p)->icmp_s.type == ICMP_DEST_UNREACH) && (PacketGetICMPv4EmbIPv4((p)) != NULL) && \
256 (p)->l4.vars.icmpv4.emb_ports_set)
267 #define ICMPV4_IS_ERROR_MSG(type) \
268 ((type) == ICMP_DEST_UNREACH || (type) == ICMP_SOURCE_QUENCH || (type) == ICMP_REDIRECT || \
269 (type) == ICMP_TIME_EXCEEDED || (type) == ICMP_PARAMETERPROB)
283 static inline uint16_t ICMPV4CalculateChecksum(
const uint16_t *pkt, uint16_t tlen)
286 uint32_t csum = pkt[0];
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] +
300 csum += pkt[0] + pkt[1] + pkt[2] + pkt[3];
306 csum += pkt[0] + pkt[1];
318 *(uint8_t *)(&
pad) = (*(uint8_t *)pkt);
322 csum = (csum >> 16) + (csum & 0x0000FFFF);
323 csum += (csum >> 16);
325 return (uint16_t) ~csum;