Rev 4722 | Rev 4731 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
3846 | mejdrech | 1 | /* |
3912 | mejdrech | 2 | * Copyright (c) 2009 Lukas Mejdrech |
3846 | 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 | |||
3912 | mejdrech | 29 | /** @addtogroup ip |
30 | * @{ |
||
3846 | mejdrech | 31 | */ |
32 | |||
3912 | mejdrech | 33 | /** @file |
4704 | mejdrech | 34 | * IP header and options definitions. |
4728 | mejdrech | 35 | * Based on the RFC~791. |
3846 | mejdrech | 36 | */ |
37 | |||
38 | #ifndef __NET_IP_HEADER_H__ |
||
39 | #define __NET_IP_HEADER_H__ |
||
40 | |||
41 | #include <byteorder.h> |
||
4505 | mejdrech | 42 | #include <sys/types.h> |
3846 | mejdrech | 43 | |
4728 | mejdrech | 44 | /** Returns the actual IP header length. |
45 | * @param header The IP packet header. Input parameter. |
||
46 | */ |
||
47 | #define IP_HEADER_LENGTH( header ) (( header )->header_length * 4u ) |
||
48 | |||
49 | /** Returns the actual IP header length. |
||
50 | * @param header The IP packet header. Input parameter. |
||
51 | */ |
||
52 | #define IP_COMPUTE_HEADER_LENGTH( length ) (( uint8_t ) (( length ) / 4u )) |
||
53 | |||
54 | /** Returns the actual IP packet total length. |
||
55 | * @param header The IP packet header. Input parameter. |
||
56 | */ |
||
57 | #define IP_TOTAL_LENGTH( header ) ntohs(( header )->total_length ) |
||
58 | |||
59 | /** Returns the actual IP packet data length. |
||
60 | * @param header The IP packet header. Input parameter. |
||
61 | */ |
||
62 | #define IP_HEADER_DATA_LENGTH( header ) ( IP_TOTAL_LENGTH( header ) - IP_HEADER_LENGTH( header )) |
||
63 | |||
64 | /** Returns the IP packet header checksum. |
||
65 | * @param header The IP packet header. Input parameter. |
||
66 | */ |
||
67 | #define IP_HEADER_CHECKSUM( header ) ( htons( ip_checksum(( uint8_t * )( header ), IP_HEADER_LENGTH( header )))) |
||
68 | |||
69 | /** Returns the fragment offest. |
||
70 | * @param length The prefixed data total length. Input parameter. |
||
71 | */ |
||
72 | #define IP_COMPUTE_FRAGMENT_OFFSET( length ) (( length ) / 8 ) |
||
73 | |||
3912 | mejdrech | 74 | /** Type definition of the internet header. |
75 | * @see ip_header |
||
76 | */ |
||
3846 | mejdrech | 77 | typedef struct ip_header ip_header_t; |
3912 | mejdrech | 78 | |
79 | /** Type definition of the internet header pointer. |
||
80 | * @see ip_header |
||
81 | */ |
||
3846 | mejdrech | 82 | typedef ip_header_t * ip_header_ref; |
83 | |||
84 | /** Internet header. |
||
85 | * The variable options should be included after the header itself and indicated by the increased header length value. |
||
86 | */ |
||
87 | struct ip_header{ |
||
88 | #ifdef ARCH_IS_BIG_ENDIAN |
||
89 | /** The Version field indicates the format of the internet header. |
||
90 | */ |
||
4505 | mejdrech | 91 | uint8_t version:4; |
3846 | mejdrech | 92 | /** Internet Header Length is the length of the internet header in 32~bit words, and thus points to the beginning of the data. |
93 | * Note that the minimum value for a~correct header is~5. |
||
94 | */ |
||
4728 | mejdrech | 95 | uint8_t header_length:4; |
3846 | mejdrech | 96 | #else |
97 | /** Internet Header Length is the length of the internet header in 32~bit words, and thus points to the beginning of the data. |
||
98 | * Note that the minimum value for a~correct header is~5. |
||
99 | */ |
||
4728 | mejdrech | 100 | uint8_t header_length:4; |
3846 | mejdrech | 101 | /** The Version field indicates the format of the internet header. |
102 | */ |
||
4505 | mejdrech | 103 | uint8_t version:4; |
3846 | mejdrech | 104 | #endif |
105 | /** The Type of Service provides an indication of the abstract parameters of the quality of service desired. |
||
106 | * These parameters are to be used to guide the selection of the actual service parameters when transmitting a~datagram through a~particular network. |
||
107 | * Several networks offer service precedence, which somehow treats high precedence traffic as more important than other traffic (generally by accepting only traffic above a~certain precedence at time of high load). |
||
108 | * The major choice is a~three way tradeoff between low-delay, high-reliability, and high-throughput. |
||
109 | */ |
||
110 | uint8_t tos; |
||
111 | /** Total Length is the length of the datagram, measured in octets, including internet header and data. |
||
112 | * This field allows the length of a~datagram to be up to 65,535~octets. |
||
113 | */ |
||
114 | uint16_t total_length; |
||
115 | /** An identifying value assigned by the sender to aid in assembling the fragments of a~datagram. |
||
116 | */ |
||
4505 | mejdrech | 117 | uint16_t identification; |
3846 | mejdrech | 118 | #ifdef ARCH_IS_BIG_ENDIAN |
119 | /** Various control flags. |
||
4704 | mejdrech | 120 | * @see |
3846 | mejdrech | 121 | */ |
4505 | mejdrech | 122 | uint16_t flags:3; |
3846 | mejdrech | 123 | /** This field indicates where in the datagram this fragment belongs. |
124 | */ |
||
4505 | mejdrech | 125 | uint16_t fragment_offset:3; |
3846 | mejdrech | 126 | #else |
127 | /** This field indicates where in the datagram this fragment belongs. |
||
128 | */ |
||
4505 | mejdrech | 129 | uint16_t fragment_offset:13; |
3846 | mejdrech | 130 | /** Various control flags. |
131 | */ |
||
4505 | mejdrech | 132 | uint16_t flags:3; |
3846 | mejdrech | 133 | #endif |
134 | /** This field indicates the maximum time the datagram is allowed to remain in the internet system. |
||
135 | * If this field contains the value zero, then the datagram must be destroyed. |
||
136 | * This field is modified in internet header processing. |
||
137 | * The time is measured in units of seconds, but since every module that processes a~datagram must decrease the TTL by at least one even if it process the datagram in less than a~second, the TTL must be thought of only as an upper bound on the time a~datagram may exist. |
||
138 | * The intention is to cause undeliverable datagrams to be discarded, and to bound the maximum datagram lifetime. |
||
139 | */ |
||
140 | uint8_t ttl; |
||
141 | /** This field indicates the next level protocol used in the data portion of the internet datagram. |
||
142 | */ |
||
143 | uint8_t protocol; |
||
4350 | mejdrech | 144 | /** A checksum of the header only. |
3846 | mejdrech | 145 | * Since some header fields change (e.g., time to live), this is recomputed and verified at each point that the internet header is processed. |
146 | * The checksum algorithm is: The checksum field is the 16~bit one's complement of the one's complement sum of all 16~bit words in the header. |
||
147 | * For purposes of computing the checksum, the value of the checksum field is zero. |
||
148 | */ |
||
149 | uint16_t header_checksum; |
||
150 | /** The source address. |
||
151 | */ |
||
152 | uint32_t source_address; |
||
153 | /** The destination address. |
||
154 | */ |
||
155 | uint32_t destination_address; |
||
4243 | mejdrech | 156 | } __attribute__ ((packed)); |
3846 | mejdrech | 157 | |
4350 | mejdrech | 158 | /** Type definition of the internet option header. |
159 | * @see ip_header |
||
160 | */ |
||
3846 | mejdrech | 161 | typedef struct ip_option ip_option_t; |
4350 | mejdrech | 162 | |
163 | /** Type definition of the internet option header pointer. |
||
164 | * @see ip_header |
||
165 | */ |
||
4505 | mejdrech | 166 | typedef ip_option_t * ip_option_ref; |
3846 | mejdrech | 167 | |
168 | /** Internet option header. |
||
169 | * Only type field is always valid. |
||
170 | * Other fields' validity depends on the option type. |
||
171 | */ |
||
172 | struct ip_option{ |
||
173 | /** A single octet of option-type. |
||
174 | */ |
||
175 | uint8_t type; |
||
176 | /** An option length octet. |
||
177 | */ |
||
178 | uint8_t length; |
||
179 | /** A~pointer. |
||
180 | */ |
||
181 | uint8_t pointer; |
||
182 | #ifdef ARCH_IS_BIG_ENDIAN |
||
183 | /** The number of IP modules that cannot register timestamps due to lack of space. |
||
184 | */ |
||
4505 | mejdrech | 185 | uint8_t overflow:4; |
3846 | mejdrech | 186 | /** Various internet timestamp control flags. |
187 | */ |
||
4505 | mejdrech | 188 | uint8_t flags:4; |
3846 | mejdrech | 189 | #else |
190 | /** Various internet timestamp control flags. |
||
191 | */ |
||
4505 | mejdrech | 192 | uint8_t flags:4; |
3846 | mejdrech | 193 | /** The number of IP modules that cannot register timestamps due to lack of space. |
194 | */ |
||
4505 | mejdrech | 195 | uint8_t overflow:4; |
3846 | mejdrech | 196 | #endif |
4243 | mejdrech | 197 | } __attribute__ ((packed)); |
3846 | mejdrech | 198 | |
4704 | mejdrech | 199 | /** @name IP flags definitions |
200 | */ |
||
201 | /*@{*/ |
||
202 | |||
203 | /** Fragment flag field shift. |
||
204 | */ |
||
3846 | mejdrech | 205 | #define IPFLAG_FRAGMENT_SHIFT 1 |
4704 | mejdrech | 206 | |
207 | /** Fragmented flag field shift. |
||
208 | */ |
||
3846 | mejdrech | 209 | #define IPFLAG_FRAGMENTED_SHIFT 0 |
210 | |||
4704 | mejdrech | 211 | /** May fragment flag value. |
212 | * Allows the packet fragmentation. |
||
213 | */ |
||
3846 | mejdrech | 214 | #define IPFLAG_MAY_FRAGMENT ( 0x0 << IPFLAG_FRAGMENT_SHIFT ) |
4704 | mejdrech | 215 | |
216 | /** Don't fragment flag value. |
||
217 | * Permits the packet fragmentation. |
||
218 | */ |
||
3846 | mejdrech | 219 | #define IPFLAG_DONT_FRAGMENT ( 0x1 << IPFLAG_FRAGMENT_SHIFT ) |
220 | |||
4704 | mejdrech | 221 | /** Last fragment flag value. |
222 | * Indicates the last packet fragment. |
||
223 | */ |
||
3846 | mejdrech | 224 | #define IPFLAG_LAST_FRAGMENT ( 0x0 << IPFLAG_FRAGMENTED_SHIFT ) |
4704 | mejdrech | 225 | |
226 | /** More fragments flag value. |
||
227 | * Indicates that more packet fragments follow. |
||
228 | */ |
||
3846 | mejdrech | 229 | #define IPFLAG_MORE_FRAGMENTS ( 0x1 << IPFLAG_FRAGMENTED_SHIFT ) |
230 | |||
4704 | mejdrech | 231 | /*@}*/ |
232 | |||
4722 | mejdrech | 233 | /** Type definition of the internet version 4 pseudo header. |
234 | * @see ipv4_pseudo_header |
||
235 | */ |
||
236 | typedef struct ipv4_pseudo_header ipv4_pseudo_header_t; |
||
237 | |||
238 | /** Type definition of the internet version 4 pseudo header pointer. |
||
239 | * @see ipv4_pseudo_header |
||
240 | */ |
||
241 | typedef ipv4_pseudo_header_t * ipv4_pseudo_header_ref; |
||
242 | |||
243 | /** Internet version 4 pseudo header. |
||
244 | */ |
||
245 | struct ipv4_pseudo_header{ |
||
246 | /** The source address. |
||
247 | */ |
||
248 | uint32_t source_address; |
||
249 | /** The destination address. |
||
250 | */ |
||
251 | uint32_t destination_address; |
||
4728 | mejdrech | 252 | /** Reserved byte. |
253 | * Must be zero. |
||
4722 | mejdrech | 254 | */ |
4728 | mejdrech | 255 | uint8_t reserved; |
4722 | mejdrech | 256 | /** This field indicates the next level protocol used in the data portion of the internet datagram. |
257 | */ |
||
258 | uint8_t protocol; |
||
259 | /** Data length is the length of the datagram, measured in octets. |
||
260 | */ |
||
261 | uint16_t data_length; |
||
262 | } __attribute__ ((packed)); |
||
263 | |||
3846 | mejdrech | 264 | #endif |
265 | |||
266 | /** @} |
||
267 | */ |