suricata
decode-ipv4.h
Go to the documentation of this file.
1 /* Copyright (C) 2007-2022 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  * \author Brian Rectanus <brectanu@gmail.com>
23  */
24 
25 #ifndef __DECODE_IPV4_H__
26 #define __DECODE_IPV4_H__
27 
28 #define IPV4_HEADER_LEN 20 /**< Header length */
29 #define IPV4_OPTMAX 40 /**< Max options length */
30 #define IPV4_MAXPACKET_LEN 65535 /**< Maximum packet size */
31 
32 /** IP Option Types */
33 #define IPV4_OPT_EOL 0x00 /**< Option: End of List */
34 #define IPV4_OPT_NOP 0x01 /**< Option: No op */
35 #define IPV4_OPT_RR 0x07 /**< Option: Record Route */
36 #define IPV4_OPT_QS 0x19 /**< Option: Quick Start */
37 #define IPV4_OPT_TS 0x44 /**< Option: Timestamp */
38 #define IPV4_OPT_SEC 0x82 /**< Option: Security */
39 #define IPV4_OPT_LSRR 0x83 /**< Option: Loose Source Route */
40 #define IPV4_OPT_CIPSO 0x86 /**< Option: Commercial IP Security */
41 #define IPV4_OPT_SID 0x88 /**< Option: Stream Identifier */
42 #define IPV4_OPT_SSRR 0x89 /**< Option: Strict Source Route */
43 #define IPV4_OPT_RTRALT 0x94 /**< Option: Router Alert */
44 
45 /** IP Option Lengths (fixed) */
46 #define IPV4_OPT_SEC_LEN 11 /**< SEC Option Fixed Length */
47 #define IPV4_OPT_SID_LEN 4 /**< SID Option Fixed Length */
48 #define IPV4_OPT_RTRALT_LEN 4 /**< RTRALT Option Fixed Length */
49 
50 /** IP Option Lengths (variable) */
51 #define IPV4_OPT_ROUTE_MIN 3 /**< RR, SRR, LTRR Option Min Length */
52 #define IPV4_OPT_QS_MIN 8 /**< QS Option Min Length */
53 #define IPV4_OPT_TS_MIN 5 /**< TS Option Min Length */
54 #define IPV4_OPT_CIPSO_MIN 10 /**< CIPSO Option Min Length */
55 
56 /** IP Option fields */
57 #define IPV4_OPTS ip4vars.ip_opts
58 #define IPV4_OPTS_CNT ip4vars.ip_opt_cnt
59 
60 typedef struct IPV4Opt_ {
61  /** \todo We may want to break type up into its 3 fields
62  * as the reassembler may want to know which options
63  * must be copied to each fragment.
64  */
65  uint8_t type; /**< option type */
66  uint8_t len; /**< option length (type+len+data) */
67  const uint8_t *data; /**< option data */
69 
70 typedef struct IPV4Hdr_
71 {
72  uint8_t ip_verhl; /**< version & header length */
73  uint8_t ip_tos; /**< type of service */
74  uint16_t ip_len; /**< length */
75  uint16_t ip_id; /**< id */
76  uint16_t ip_off; /**< frag offset */
77  uint8_t ip_ttl; /**< time to live */
78  uint8_t ip_proto; /**< protocol (tcp, udp, etc) */
79  uint16_t ip_csum; /**< checksum */
80  union {
81  struct {
82  struct in_addr ip_src;/**< source address */
83  struct in_addr ip_dst;/**< destination address */
85  uint16_t ip_addrs[4];
88 
89 
90 #define s_ip_src ip4_hdrun1.ip4_un1.ip_src
91 #define s_ip_dst ip4_hdrun1.ip4_un1.ip_dst
92 #define s_ip_addrs ip4_hdrun1.ip_addrs
93 
94 #define IPV4_GET_RAW_VER(ip4h) (((ip4h)->ip_verhl & 0xf0) >> 4)
95 #define IPV4_GET_RAW_HLEN(ip4h) ((ip4h)->ip_verhl & 0x0f)
96 #define IPV4_GET_RAW_IPTOS(ip4h) ((ip4h)->ip_tos)
97 #define IPV4_GET_RAW_IPLEN(ip4h) ((ip4h)->ip_len)
98 #define IPV4_GET_RAW_IPID(ip4h) ((ip4h)->ip_id)
99 #define IPV4_GET_RAW_IPOFFSET(ip4h) ((ip4h)->ip_off)
100 #define IPV4_GET_RAW_IPTTL(ip4h) ((ip4h)->ip_ttl)
101 #define IPV4_GET_RAW_IPPROTO(ip4h) ((ip4h)->ip_proto)
102 #define IPV4_GET_RAW_IPSRC(ip4h) ((ip4h)->s_ip_src)
103 #define IPV4_GET_RAW_IPDST(ip4h) ((ip4h)->s_ip_dst)
104 
105 /** return the raw (directly from the header) src ip as uint32_t */
106 #define IPV4_GET_RAW_IPSRC_U32(ip4h) (uint32_t)((ip4h)->s_ip_src.s_addr)
107 /** return the raw (directly from the header) dst ip as uint32_t */
108 #define IPV4_GET_RAW_IPDST_U32(ip4h) (uint32_t)((ip4h)->s_ip_dst.s_addr)
109 
110 /* we need to change them as well as get them */
111 #define IPV4_SET_RAW_VER(ip4h, value) ((ip4h)->ip_verhl = (((ip4h)->ip_verhl & 0x0f) | (value << 4)))
112 #define IPV4_SET_RAW_HLEN(ip4h, value) ((ip4h)->ip_verhl = (((ip4h)->ip_verhl & 0xf0) | (value & 0x0f)))
113 #define IPV4_SET_RAW_IPTOS(ip4h, value) ((ip4h)->ip_tos = value)
114 #define IPV4_SET_RAW_IPLEN(ip4h, value) ((ip4h)->ip_len = value)
115 #define IPV4_SET_RAW_IPPROTO(ip4h, value) ((ip4h)->ip_proto = value)
116 
117 /* ONLY call these functions after making sure that:
118  * 1. p->ip4h is set
119  * 2. p->ip4h is valid (len is correct)
120  */
121 #define IPV4_GET_VER(p) \
122  IPV4_GET_RAW_VER((p)->ip4h)
123 #define IPV4_GET_HLEN(p) ((uint8_t)(IPV4_GET_RAW_HLEN((p)->ip4h) << 2))
124 #define IPV4_GET_IPTOS(p) \
125  IPV4_GET_RAW_IPTOS((p)->ip4h)
126 #define IPV4_GET_IPLEN(p) \
127  (SCNtohs(IPV4_GET_RAW_IPLEN((p)->ip4h)))
128 #define IPV4_GET_IPID(p) \
129  (SCNtohs(IPV4_GET_RAW_IPID((p)->ip4h)))
130 /* _IPV4_GET_IPOFFSET: get the content of the offset header field in host order */
131 #define _IPV4_GET_IPOFFSET(p) \
132  (SCNtohs(IPV4_GET_RAW_IPOFFSET((p)->ip4h)))
133 /* IPV4_GET_IPOFFSET: get the final offset */
134 #define IPV4_GET_IPOFFSET(p) \
135  (_IPV4_GET_IPOFFSET(p) & 0x1fff)
136 /* IPV4_GET_RF: get the RF flag. Use _IPV4_GET_IPOFFSET to save a SCNtohs call. */
137 #define IPV4_GET_RF(p) \
138  (uint8_t)((_IPV4_GET_IPOFFSET((p)) & 0x8000) >> 15)
139 /* IPV4_GET_DF: get the DF flag. Use _IPV4_GET_IPOFFSET to save a SCNtohs call. */
140 #define IPV4_GET_DF(p) \
141  (uint8_t)((_IPV4_GET_IPOFFSET((p)) & 0x4000) >> 14)
142 /* IPV4_GET_MF: get the MF flag. Use _IPV4_GET_IPOFFSET to save a SCNtohs call. */
143 #define IPV4_GET_MF(p) \
144  (uint8_t)((_IPV4_GET_IPOFFSET((p)) & 0x2000) >> 13)
145 #define IPV4_GET_IPTTL(p) \
146  IPV4_GET_RAW_IPTTL(p->ip4h)
147 #define IPV4_GET_IPPROTO(p) \
148  IPV4_GET_RAW_IPPROTO((p)->ip4h)
149 
150 #define CLEAR_IPV4_PACKET(p) do { \
151  (p)->ip4h = NULL; \
152  (p)->level3_comp_csum = -1; \
153  memset(&p->ip4vars, 0x00, sizeof(p->ip4vars)); \
154 } while (0)
155 
168 };
169 
170 /* helper structure with parsed ipv4 info */
171 typedef struct IPV4Vars_
172 {
173  int32_t comp_csum; /* checksum computed over the ipv4 packet */
174 
175  uint16_t opt_cnt;
176  uint16_t opts_set;
178 
179 
180 void DecodeIPV4RegisterTests(void);
181 
182 /** ----- Inline functions ----- */
183 
184 /**
185  * \brief Calculateor validate the checksum for the IP packet
186  *
187  * \param pkt Pointer to the start of the IP packet
188  * \param hlen Length of the IP header
189  * \param init The current checksum if validating, 0 if generating.
190  *
191  * \retval csum For validation 0 will be returned for success, for calculation
192  * this will be the checksum.
193  */
194 static inline uint16_t IPV4Checksum(const uint16_t *pkt, uint16_t hlen, uint16_t init)
195 {
196  uint32_t csum = init;
197 
198  csum += pkt[0] + pkt[1] + pkt[2] + pkt[3] + pkt[4] + pkt[6] + pkt[7] +
199  pkt[8] + pkt[9];
200 
201  hlen -= 20;
202  pkt += 10;
203 
204  if (hlen == 0) {
205  ;
206  } else if (hlen == 4) {
207  csum += pkt[0] + pkt[1];
208  } else if (hlen == 8) {
209  csum += pkt[0] + pkt[1] + pkt[2] + pkt[3];
210  } else if (hlen == 12) {
211  csum += pkt[0] + pkt[1] + pkt[2] + pkt[3] + pkt[4] + pkt[5];
212  } else if (hlen == 16) {
213  csum += pkt[0] + pkt[1] + pkt[2] + pkt[3] + pkt[4] + pkt[5] + pkt[6] +
214  pkt[7];
215  } else if (hlen == 20) {
216  csum += pkt[0] + pkt[1] + pkt[2] + pkt[3] + pkt[4] + pkt[5] + pkt[6] +
217  pkt[7] + pkt[8] + pkt[9];
218  } else if (hlen == 24) {
219  csum += pkt[0] + pkt[1] + pkt[2] + pkt[3] + pkt[4] + pkt[5] + pkt[6] +
220  pkt[7] + pkt[8] + pkt[9] + pkt[10] + pkt[11];
221  } else if (hlen == 28) {
222  csum += pkt[0] + pkt[1] + pkt[2] + pkt[3] + pkt[4] + pkt[5] + pkt[6] +
223  pkt[7] + pkt[8] + pkt[9] + pkt[10] + pkt[11] + pkt[12] + pkt[13];
224  } else if (hlen == 32) {
225  csum += pkt[0] + pkt[1] + pkt[2] + pkt[3] + pkt[4] + pkt[5] + pkt[6] +
226  pkt[7] + pkt[8] + pkt[9] + pkt[10] + pkt[11] + pkt[12] + pkt[13] +
227  pkt[14] + pkt[15];
228  } else if (hlen == 36) {
229  csum += pkt[0] + pkt[1] + pkt[2] + pkt[3] + pkt[4] + pkt[5] + pkt[6] +
230  pkt[7] + pkt[8] + pkt[9] + pkt[10] + pkt[11] + pkt[12] + pkt[13] +
231  pkt[14] + pkt[15] + pkt[16] + pkt[17];
232  } else if (hlen == 40) {
233  csum += pkt[0] + pkt[1] + pkt[2] + pkt[3] + pkt[4] + pkt[5] + pkt[6] +
234  pkt[7] + pkt[8] + pkt[9] + pkt[10] + pkt[11] + pkt[12] + pkt[13] +
235  pkt[14] + pkt[15] + pkt[16] + pkt[17] + pkt[18] + pkt[19];
236  }
237 
238  csum = (csum >> 16) + (csum & 0x0000FFFF);
239  csum += (csum >> 16);
240 
241  return (uint16_t) ~csum;
242 }
243 
244 #endif /* __DECODE_IPV4_H__ */
IPV4Vars_
Definition: decode-ipv4.h:172
IPV4Hdr_::ip_addrs
uint16_t ip_addrs[4]
Definition: decode-ipv4.h:85
IPV4Hdr_::ip_ttl
uint8_t ip_ttl
Definition: decode-ipv4.h:77
IPV4Hdr_::ip_id
uint16_t ip_id
Definition: decode-ipv4.h:75
IPV4_OPT_FLAG_SEC
@ IPV4_OPT_FLAG_SEC
Definition: decode-ipv4.h:165
IPV4Hdr_::ip_tos
uint8_t ip_tos
Definition: decode-ipv4.h:73
DecodeIPV4RegisterTests
void DecodeIPV4RegisterTests(void)
Definition: decode-ipv4.c:1650
IPV4Opt_::len
uint8_t len
Definition: decode-ipv4.h:66
IPV4Hdr
struct IPV4Hdr_ IPV4Hdr
IPV4Hdr_::ip_src
struct in_addr ip_src
Definition: decode-ipv4.h:82
IPV4Vars
struct IPV4Vars_ IPV4Vars
IPV4_OPT_FLAG_NOP
@ IPV4_OPT_FLAG_NOP
Definition: decode-ipv4.h:158
IPV4Hdr_::ip_len
uint16_t ip_len
Definition: decode-ipv4.h:74
IPV4OptionFlags
IPV4OptionFlags
Definition: decode-ipv4.h:156
IPV4Vars_::opt_cnt
uint16_t opt_cnt
Definition: decode-ipv4.h:175
IPV4_OPT_FLAG_SID
@ IPV4_OPT_FLAG_SID
Definition: decode-ipv4.h:164
IPV4_OPT_FLAG_QS
@ IPV4_OPT_FLAG_QS
Definition: decode-ipv4.h:161
IPV4_OPT_FLAG_RR
@ IPV4_OPT_FLAG_RR
Definition: decode-ipv4.h:159
IPV4Hdr_::ip4_un1
struct IPV4Hdr_::@27::@28 ip4_un1
IPV4_OPT_FLAG_CIPSO
@ IPV4_OPT_FLAG_CIPSO
Definition: decode-ipv4.h:166
IPV4Opt_::data
const uint8_t * data
Definition: decode-ipv4.h:67
IPV4Hdr_
Definition: decode-ipv4.h:71
IPV4_OPT_FLAG_TS
@ IPV4_OPT_FLAG_TS
Definition: decode-ipv4.h:160
IPV4_OPT_FLAG_RTRALT
@ IPV4_OPT_FLAG_RTRALT
Definition: decode-ipv4.h:167
IPV4Opt_::type
uint8_t type
Definition: decode-ipv4.h:65
IPV4Opt
struct IPV4Opt_ IPV4Opt
IPV4Hdr_::ip_dst
struct in_addr ip_dst
Definition: decode-ipv4.h:83
IPV4_OPT_FLAG_SSRR
@ IPV4_OPT_FLAG_SSRR
Definition: decode-ipv4.h:163
IPV4Hdr_::ip_csum
uint16_t ip_csum
Definition: decode-ipv4.h:79
IPV4Vars_::opts_set
uint16_t opts_set
Definition: decode-ipv4.h:176
IPV4Hdr_::ip_off
uint16_t ip_off
Definition: decode-ipv4.h:76
IPV4Opt_
Definition: decode-ipv4.h:60
IPV4_OPT_FLAG_LSRR
@ IPV4_OPT_FLAG_LSRR
Definition: decode-ipv4.h:162
IPV4Hdr_::ip_proto
uint8_t ip_proto
Definition: decode-ipv4.h:78
IPV4Hdr_::ip_verhl
uint8_t ip_verhl
Definition: decode-ipv4.h:72
IPV4Vars_::comp_csum
int32_t comp_csum
Definition: decode-ipv4.h:173
IPV4_OPT_FLAG_EOL
@ IPV4_OPT_FLAG_EOL
Definition: decode-ipv4.h:157
IPV4Hdr_::ip4_hdrun1
union IPV4Hdr_::@27 ip4_hdrun1