1 | /*
|
---|
2 | * Copyright (c) 2009 Lukas Mejdrech
|
---|
3 | * All rights reserved.
|
---|
4 | *
|
---|
5 | * Redistribution and use in source and binary forms, with or without
|
---|
6 | * modification, are permitted provided that the following conditions
|
---|
7 | * are met:
|
---|
8 | *
|
---|
9 | * - Redistributions of source code must retain the above copyright
|
---|
10 | * notice, this list of conditions and the following disclaimer.
|
---|
11 | * - Redistributions in binary form must reproduce the above copyright
|
---|
12 | * notice, this list of conditions and the following disclaimer in the
|
---|
13 | * documentation and/or other materials provided with the distribution.
|
---|
14 | * - The name of the author may not be used to endorse or promote products
|
---|
15 | * derived from this software without specific prior written permission.
|
---|
16 | *
|
---|
17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
---|
18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
---|
19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
---|
26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
27 | */
|
---|
28 |
|
---|
29 | /** @addtogroup libnet
|
---|
30 | * @{
|
---|
31 | */
|
---|
32 |
|
---|
33 | /** @file
|
---|
34 | * IP header and options definitions.
|
---|
35 | * Based on the RFC 791.
|
---|
36 | */
|
---|
37 |
|
---|
38 | #ifndef LIBNET_IP_HEADER_H_
|
---|
39 | #define LIBNET_IP_HEADER_H_
|
---|
40 |
|
---|
41 | #include <byteorder.h>
|
---|
42 | #include <sys/types.h>
|
---|
43 |
|
---|
44 | /** Returns the fragment offest high bits.
|
---|
45 | * @param[in] length The prefixed data total length.
|
---|
46 | */
|
---|
47 | #define IP_COMPUTE_FRAGMENT_OFFSET_HIGH(length) \
|
---|
48 | ((((length) / 8U) & 0x1f00) >> 8)
|
---|
49 |
|
---|
50 | /** Returns the fragment offest low bits.
|
---|
51 | * @param[in] length The prefixed data total length.
|
---|
52 | */
|
---|
53 | #define IP_COMPUTE_FRAGMENT_OFFSET_LOW(length) \
|
---|
54 | (((length) / 8U) & 0xff)
|
---|
55 |
|
---|
56 | /** Returns the IP header length.
|
---|
57 | * @param[in] length The IP header length in bytes.
|
---|
58 | */
|
---|
59 | #define IP_COMPUTE_HEADER_LENGTH(length) \
|
---|
60 | ((uint8_t) ((length) / 4U))
|
---|
61 |
|
---|
62 | /** Returns the fragment offest.
|
---|
63 | * @param[in] header The IP packet header.
|
---|
64 | */
|
---|
65 | #define IP_FRAGMENT_OFFSET(header) \
|
---|
66 | ((((header)->fragment_offset_high << 8) + \
|
---|
67 | (header)->fragment_offset_low) * 8U)
|
---|
68 |
|
---|
69 | /** Returns the IP packet header checksum.
|
---|
70 | * @param[in] header The IP packet header.
|
---|
71 | */
|
---|
72 | #define IP_HEADER_CHECKSUM(header) \
|
---|
73 | (htons(ip_checksum((uint8_t *) (header), IP_HEADER_LENGTH(header))))
|
---|
74 |
|
---|
75 | /** Returns the actual IP packet data length.
|
---|
76 | * @param[in] header The IP packet header.
|
---|
77 | */
|
---|
78 | #define IP_HEADER_DATA_LENGTH(header) \
|
---|
79 | (IP_TOTAL_LENGTH(header) - IP_HEADER_LENGTH(header))
|
---|
80 |
|
---|
81 | /** Returns the actual IP header length in bytes.
|
---|
82 | * @param[in] header The IP packet header.
|
---|
83 | */
|
---|
84 | #define IP_HEADER_LENGTH(header) \
|
---|
85 | ((header)->header_length * 4U)
|
---|
86 |
|
---|
87 | /** Returns the actual IP packet total length.
|
---|
88 | * @param[in] header The IP packet header.
|
---|
89 | */
|
---|
90 | #define IP_TOTAL_LENGTH(header) \
|
---|
91 | ntohs((header)->total_length)
|
---|
92 |
|
---|
93 | /** @name IP flags definitions */
|
---|
94 | /*@{*/
|
---|
95 |
|
---|
96 | /** Fragment flag field shift. */
|
---|
97 | #define IPFLAG_FRAGMENT_SHIFT 1
|
---|
98 |
|
---|
99 | /** Fragmented flag field shift. */
|
---|
100 | #define IPFLAG_FRAGMENTED_SHIFT 0
|
---|
101 |
|
---|
102 | /** Don't fragment flag value.
|
---|
103 | * Permits the packet fragmentation.
|
---|
104 | */
|
---|
105 | #define IPFLAG_DONT_FRAGMENT (0x1 << IPFLAG_FRAGMENT_SHIFT)
|
---|
106 |
|
---|
107 | /** Last fragment flag value.
|
---|
108 | * Indicates the last packet fragment.
|
---|
109 | */
|
---|
110 | #define IPFLAG_LAST_FRAGMENT (0x0 << IPFLAG_FRAGMENTED_SHIFT)
|
---|
111 |
|
---|
112 | /** May fragment flag value.
|
---|
113 | * Allows the packet fragmentation.
|
---|
114 | */
|
---|
115 | #define IPFLAG_MAY_FRAGMENT (0x0 << IPFLAG_FRAGMENT_SHIFT)
|
---|
116 |
|
---|
117 | /** More fragments flag value.
|
---|
118 | * Indicates that more packet fragments follow.
|
---|
119 | */
|
---|
120 | #define IPFLAG_MORE_FRAGMENTS (0x1 << IPFLAG_FRAGMENTED_SHIFT)
|
---|
121 |
|
---|
122 | /*@}*/
|
---|
123 |
|
---|
124 | /** Type definition of the internet header.
|
---|
125 | * @see ip_header
|
---|
126 | */
|
---|
127 | typedef struct ip_header ip_header_t;
|
---|
128 |
|
---|
129 | /** Type definition of the internet header pointer.
|
---|
130 | * @see ip_header
|
---|
131 | */
|
---|
132 | typedef ip_header_t *ip_header_ref;
|
---|
133 |
|
---|
134 | /** Type definition of the internet option header.
|
---|
135 | * @see ip_header
|
---|
136 | */
|
---|
137 | typedef struct ip_option ip_option_t;
|
---|
138 |
|
---|
139 | /** Type definition of the internet option header pointer.
|
---|
140 | * @see ip_header
|
---|
141 | */
|
---|
142 | typedef ip_option_t *ip_option_ref;
|
---|
143 |
|
---|
144 | /** Type definition of the internet version 4 pseudo header.
|
---|
145 | * @see ipv4_pseudo_header
|
---|
146 | */
|
---|
147 | typedef struct ipv4_pseudo_header ipv4_pseudo_header_t;
|
---|
148 |
|
---|
149 | /** Type definition of the internet version 4 pseudo header pointer.
|
---|
150 | * @see ipv4_pseudo_header
|
---|
151 | */
|
---|
152 | typedef ipv4_pseudo_header_t *ipv4_pseudo_header_ref;
|
---|
153 |
|
---|
154 | /** Internet header.
|
---|
155 | *
|
---|
156 | * The variable options should be included after the header itself and
|
---|
157 | * indicated by the increased header length value.
|
---|
158 | */
|
---|
159 | struct ip_header {
|
---|
160 | #ifdef ARCH_IS_BIG_ENDIAN
|
---|
161 | uint8_t version : 4;
|
---|
162 | uint8_t header_length : 4;
|
---|
163 | #else
|
---|
164 | uint8_t header_length : 4;
|
---|
165 | uint8_t version : 4;
|
---|
166 | #endif
|
---|
167 |
|
---|
168 | uint8_t tos;
|
---|
169 | uint16_t total_length;
|
---|
170 | uint16_t identification;
|
---|
171 |
|
---|
172 | #ifdef ARCH_IS_BIG_ENDIAN
|
---|
173 | uint8_t flags : 3;
|
---|
174 | uint8_t fragment_offset_high : 5;
|
---|
175 | #else
|
---|
176 | uint8_t fragment_offset_high : 5;
|
---|
177 | uint8_t flags : 3;
|
---|
178 | #endif
|
---|
179 |
|
---|
180 | uint8_t fragment_offset_low;
|
---|
181 | uint8_t ttl;
|
---|
182 | uint8_t protocol;
|
---|
183 | uint16_t header_checksum;
|
---|
184 | uint32_t source_address;
|
---|
185 | uint32_t destination_address;
|
---|
186 | } __attribute__ ((packed));
|
---|
187 |
|
---|
188 | /** Internet option header.
|
---|
189 | *
|
---|
190 | * Only type field is always valid.
|
---|
191 | * Other fields' validity depends on the option type.
|
---|
192 | */
|
---|
193 | struct ip_option {
|
---|
194 | uint8_t type;
|
---|
195 | uint8_t length;
|
---|
196 | uint8_t pointer;
|
---|
197 |
|
---|
198 | #ifdef ARCH_IS_BIG_ENDIAN
|
---|
199 | uint8_t overflow : 4;
|
---|
200 | uint8_t flags : 4;
|
---|
201 | #else
|
---|
202 | uint8_t flags : 4;
|
---|
203 | uint8_t overflow : 4;
|
---|
204 | #endif
|
---|
205 | } __attribute__ ((packed));
|
---|
206 |
|
---|
207 | /** Internet version 4 pseudo header. */
|
---|
208 | struct ipv4_pseudo_header {
|
---|
209 | uint32_t source_address;
|
---|
210 | uint32_t destination_address;
|
---|
211 | uint8_t reserved;
|
---|
212 | uint8_t protocol;
|
---|
213 | uint16_t data_length;
|
---|
214 | } __attribute__ ((packed));
|
---|
215 |
|
---|
216 | #endif
|
---|
217 |
|
---|
218 | /** @}
|
---|
219 | */
|
---|