Subversion Repositories HelenOS

Rev

Rev 4731 | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 4731 Rev 4756
1
/*
1
/*
2
 * Copyright (c) 2009 Lukas Mejdrech
2
 * Copyright (c) 2009 Lukas Mejdrech
3
 * All rights reserved.
3
 * All rights reserved.
4
 *
4
 *
5
 * Redistribution and use in source and binary forms, with or without
5
 * Redistribution and use in source and binary forms, with or without
6
 * modification, are permitted provided that the following conditions
6
 * modification, are permitted provided that the following conditions
7
 * are met:
7
 * are met:
8
 *
8
 *
9
 * - Redistributions of source code must retain the above copyright
9
 * - Redistributions of source code must retain the above copyright
10
 *   notice, this list of conditions and the following disclaimer.
10
 *   notice, this list of conditions and the following disclaimer.
11
 * - Redistributions in binary form must reproduce the above copyright
11
 * - Redistributions in binary form must reproduce the above copyright
12
 *   notice, this list of conditions and the following disclaimer in the
12
 *   notice, this list of conditions and the following disclaimer in the
13
 *   documentation and/or other materials provided with the distribution.
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
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.
15
 *   derived from this software without specific prior written permission.
16
 *
16
 *
17
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
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
18
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20
 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20
 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
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
23
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
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
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.
26
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
 */
27
 */
28
 
28
 
29
/** @addtogroup ip
29
/** @addtogroup ip
30
 *  @{
30
 *  @{
31
 */
31
 */
32
 
32
 
33
/** @file
33
/** @file
34
 *  IP header and options definitions.
34
 *  IP header and options definitions.
35
 *  Based on the RFC~791.
35
 *  Based on the RFC~791.
36
 */
36
 */
37
 
37
 
38
#ifndef __NET_IP_HEADER_H__
38
#ifndef __NET_IP_HEADER_H__
39
#define __NET_IP_HEADER_H__
39
#define __NET_IP_HEADER_H__
40
 
40
 
41
#include <byteorder.h>
41
#include <byteorder.h>
42
#include <sys/types.h>
42
#include <sys/types.h>
43
 
43
 
44
/** Returns the actual IP header length.
44
/** Returns the actual IP header length in bytes.
45
 *  @param header The IP packet header. Input parameter.
45
 *  @param[in] header The IP packet header.
46
 */
46
 */
47
#define IP_HEADER_LENGTH( header )      (( header )->header_length * 4u )
47
#define IP_HEADER_LENGTH( header )      (( header )->header_length * 4u )
48
 
48
 
49
/** Returns the actual IP header length.
49
/** Returns the IP header length.
50
 *  @param header The IP packet header. Input parameter.
50
 *  @param[in] length The IP header length in bytes.
51
 */
51
 */
52
#define IP_COMPUTE_HEADER_LENGTH( length )      (( uint8_t ) (( length ) / 4u ))
52
#define IP_COMPUTE_HEADER_LENGTH( length )      (( uint8_t ) (( length ) / 4u ))
53
 
53
 
54
/** Returns the actual IP packet total length.
54
/** Returns the actual IP packet total length.
55
 *  @param header The IP packet header. Input parameter.
55
 *  @param[in] header The IP packet header.
56
 */
56
 */
57
#define IP_TOTAL_LENGTH( header )       ntohs(( header )->total_length )
57
#define IP_TOTAL_LENGTH( header )       ntohs(( header )->total_length )
58
 
58
 
59
/** Returns the actual IP packet data length.
59
/** Returns the actual IP packet data length.
60
 *  @param header The IP packet header. Input parameter.
60
 *  @param[in] header The IP packet header.
61
 */
61
 */
62
#define IP_HEADER_DATA_LENGTH( header ) ( IP_TOTAL_LENGTH( header ) - IP_HEADER_LENGTH( header ))
62
#define IP_HEADER_DATA_LENGTH( header ) ( IP_TOTAL_LENGTH( header ) - IP_HEADER_LENGTH( header ))
63
 
63
 
64
/** Returns the IP packet header checksum.
64
/** Returns the IP packet header checksum.
65
 *  @param header The IP packet header. Input parameter.
65
 *  @param[in] header The IP packet header.
66
 */
66
 */
67
#define IP_HEADER_CHECKSUM( header )    ( htons( ip_checksum(( uint8_t * )( header ), IP_HEADER_LENGTH( header ))))
67
#define IP_HEADER_CHECKSUM( header )    ( htons( ip_checksum(( uint8_t * )( header ), IP_HEADER_LENGTH( header ))))
68
 
68
 
69
/** Returns the fragment offest.
69
/** Returns the fragment offest.
70
 *  @param header The IP packet header. Input parameter.
70
 *  @param[in] header The IP packet header.
71
 */
71
 */
72
#define IP_FRAGMENT_OFFSET( header ) (((( header )->fragment_offset_high << 8 ) + ( header )->fragment_offset_low ) * 8u )
72
#define IP_FRAGMENT_OFFSET( header ) (((( header )->fragment_offset_high << 8 ) + ( header )->fragment_offset_low ) * 8u )
73
 
73
 
74
/** Returns the fragment offest high bits.
74
/** Returns the fragment offest high bits.
75
 *  @param length The prefixed data total length. Input parameter.
75
 *  @param[in] length The prefixed data total length.
76
 */
76
 */
77
#define IP_COMPUTE_FRAGMENT_OFFSET_HIGH( length ) (((( length ) / 8u ) & 0x1F00 ) >> 8 )
77
#define IP_COMPUTE_FRAGMENT_OFFSET_HIGH( length ) (((( length ) / 8u ) & 0x1F00 ) >> 8 )
78
 
78
 
79
/** Returns the fragment offest low bits.
79
/** Returns the fragment offest low bits.
80
 *  @param length The prefixed data total length. Input parameter.
80
 *  @param[in] length The prefixed data total length.
81
 */
81
 */
82
#define IP_COMPUTE_FRAGMENT_OFFSET_LOW( length ) ((( length ) / 8u ) & 0xFF )
82
#define IP_COMPUTE_FRAGMENT_OFFSET_LOW( length ) ((( length ) / 8u ) & 0xFF )
83
 
83
 
84
/** Type definition of the internet header.
84
/** Type definition of the internet header.
85
 *  @see ip_header
85
 *  @see ip_header
86
 */
86
 */
87
typedef struct ip_header    ip_header_t;
87
typedef struct ip_header    ip_header_t;
88
 
88
 
89
/** Type definition of the internet header pointer.
89
/** Type definition of the internet header pointer.
90
 *  @see ip_header
90
 *  @see ip_header
91
 */
91
 */
92
typedef ip_header_t *       ip_header_ref;
92
typedef ip_header_t *       ip_header_ref;
93
 
93
 
94
/** Internet header.
94
/** Internet header.
95
 *  The variable options should be included after the header itself and indicated by the increased header length value.
95
 *  The variable options should be included after the header itself and indicated by the increased header length value.
96
 */
96
 */
97
struct ip_header{
97
struct ip_header{
98
#ifdef ARCH_IS_BIG_ENDIAN
98
#ifdef ARCH_IS_BIG_ENDIAN
99
    /** The Version field indicates the format of the internet header.
99
    /** The Version field indicates the format of the internet header.
100
     */
100
     */
101
    uint8_t     version:4;
101
    uint8_t     version:4;
102
    /** Internet Header Length is the length of the internet header in 32~bit words, and thus points to the beginning of the data.
102
    /** Internet Header Length is the length of the internet header in 32~bit words, and thus points to the beginning of the data.
103
     *  Note that the minimum value for a~correct header is~5.
103
     *  Note that the minimum value for a~correct header is~5.
104
     */
104
     */
105
    uint8_t     header_length:4;
105
    uint8_t     header_length:4;
106
#else
106
#else
107
    /** Internet Header Length is the length of the internet header in 32~bit words, and thus points to the beginning of the data.
107
    /** Internet Header Length is the length of the internet header in 32~bit words, and thus points to the beginning of the data.
108
     *  Note that the minimum value for a~correct header is~5.
108
     *  Note that the minimum value for a~correct header is~5.
109
     */
109
     */
110
    uint8_t     header_length:4;
110
    uint8_t     header_length:4;
111
    /** The Version field indicates the format of the internet header.
111
    /** The Version field indicates the format of the internet header.
112
     */
112
     */
113
    uint8_t     version:4;
113
    uint8_t     version:4;
114
#endif
114
#endif
115
    /** The Type of Service provides an indication of the abstract parameters of the quality of service desired.
115
    /** The Type of Service provides an indication of the abstract parameters of the quality of service desired.
116
     *  These parameters are to be used to guide the selection of the actual service parameters when transmitting a~datagram through a~particular network.
116
     *  These parameters are to be used to guide the selection of the actual service parameters when transmitting a~datagram through a~particular network.
117
     *  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).
117
     *  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).
118
     *  The major choice is a~three way tradeoff between low-delay, high-reliability, and high-throughput.
118
     *  The major choice is a~three way tradeoff between low-delay, high-reliability, and high-throughput.
119
     */
119
     */
120
    uint8_t     tos;
120
    uint8_t     tos;
121
    /** Total Length is the length of the datagram, measured in octets, including internet header and data.
121
    /** Total Length is the length of the datagram, measured in octets, including internet header and data.
122
     *  This field allows the length of a~datagram to be up to 65,535~octets.
122
     *  This field allows the length of a~datagram to be up to 65,535~octets.
123
     */
123
     */
124
    uint16_t    total_length;
124
    uint16_t    total_length;
125
    /** An identifying value assigned by the sender to aid in assembling the fragments of a~datagram.
125
    /** An identifying value assigned by the sender to aid in assembling the fragments of a~datagram.
126
     */
126
     */
127
    uint16_t    identification;
127
    uint16_t    identification;
128
#ifdef ARCH_IS_BIG_ENDIAN
128
#ifdef ARCH_IS_BIG_ENDIAN
129
    /** Various control flags.
129
    /** Various control flags.
130
     */
130
     */
131
    uint8_t flags:3;
131
    uint8_t flags:3;
132
    /** This field indicates where in the datagram this fragment belongs.
132
    /** This field indicates where in the datagram this fragment belongs.
133
     *  High bits.
133
     *  High bits.
134
     */
134
     */
135
    uint8_t fragment_offset_high:5;
135
    uint8_t fragment_offset_high:5;
136
#else
136
#else
137
    /** This field indicates where in the datagram this fragment belongs.
137
    /** This field indicates where in the datagram this fragment belongs.
138
     *  High bits.
138
     *  High bits.
139
     */
139
     */
140
    uint8_t fragment_offset_high:5;
140
    uint8_t fragment_offset_high:5;
141
    /** Various control flags.
141
    /** Various control flags.
142
     */
142
     */
143
    uint8_t flags:3;
143
    uint8_t flags:3;
144
#endif
144
#endif
145
    /** This field indicates where in the datagram this fragment belongs.
145
    /** This field indicates where in the datagram this fragment belongs.
146
     *  Low bits.
146
     *  Low bits.
147
     */
147
     */
148
    uint8_t fragment_offset_low;
148
    uint8_t fragment_offset_low;
149
    /** This field indicates the maximum time the datagram is allowed to remain in the internet system.
149
    /** This field indicates the maximum time the datagram is allowed to remain in the internet system.
150
     *  If this field contains the value zero, then the datagram must be destroyed.
150
     *  If this field contains the value zero, then the datagram must be destroyed.
151
     *  This field is modified in internet header processing.
151
     *  This field is modified in internet header processing.
152
     *  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.
152
     *  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.
153
     *  The intention is to cause undeliverable datagrams to be discarded, and to bound the maximum datagram lifetime.
153
     *  The intention is to cause undeliverable datagrams to be discarded, and to bound the maximum datagram lifetime.
154
     */
154
     */
155
    uint8_t     ttl;
155
    uint8_t     ttl;
156
    /** This field indicates the next level protocol used in the data portion of the internet datagram.
156
    /** This field indicates the next level protocol used in the data portion of the internet datagram.
157
     */
157
     */
158
    uint8_t     protocol;
158
    uint8_t     protocol;
159
    /** A checksum of the header only.
159
    /** A checksum of the header only.
160
     *  Since some header fields change (e.g., time to live), this is recomputed and verified at each point that the internet header is processed.
160
     *  Since some header fields change (e.g., time to live), this is recomputed and verified at each point that the internet header is processed.
161
     *  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.
161
     *  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.
162
     *  For purposes of computing the checksum, the value of the checksum field is zero.
162
     *  For purposes of computing the checksum, the value of the checksum field is zero.
163
     */
163
     */
164
    uint16_t    header_checksum;
164
    uint16_t    header_checksum;
165
    /** The source address.
165
    /** The source address.
166
     */
166
     */
167
    uint32_t    source_address;
167
    uint32_t    source_address;
168
    /** The destination address.
168
    /** The destination address.
169
     */
169
     */
170
    uint32_t    destination_address;
170
    uint32_t    destination_address;
171
} __attribute__ ((packed));
171
} __attribute__ ((packed));
172
 
172
 
173
/** Type definition of the internet option header.
173
/** Type definition of the internet option header.
174
 *  @see ip_header
174
 *  @see ip_header
175
 */
175
 */
176
typedef struct ip_option    ip_option_t;
176
typedef struct ip_option    ip_option_t;
177
 
177
 
178
/** Type definition of the internet option header pointer.
178
/** Type definition of the internet option header pointer.
179
 *  @see ip_header
179
 *  @see ip_header
180
 */
180
 */
181
typedef ip_option_t *       ip_option_ref;
181
typedef ip_option_t *       ip_option_ref;
182
 
182
 
183
/** Internet option header.
183
/** Internet option header.
184
 *  Only type field is always valid.
184
 *  Only type field is always valid.
185
 *  Other fields' validity depends on the option type.
185
 *  Other fields' validity depends on the option type.
186
 */
186
 */
187
struct ip_option{
187
struct ip_option{
188
    /** A single octet of option-type.
188
    /** A single octet of option-type.
189
     */
189
     */
190
    uint8_t     type;
190
    uint8_t     type;
191
    /** An option length octet.
191
    /** An option length octet.
192
     */
192
     */
193
    uint8_t     length;
193
    uint8_t     length;
194
    /** A~pointer.
194
    /** A~pointer.
195
     */
195
     */
196
    uint8_t     pointer;
196
    uint8_t     pointer;
197
#ifdef ARCH_IS_BIG_ENDIAN
197
#ifdef ARCH_IS_BIG_ENDIAN
198
    /** The number of IP modules that cannot register timestamps due to lack of space.
198
    /** The number of IP modules that cannot register timestamps due to lack of space.
199
     */
199
     */
200
    uint8_t overflow:4;
200
    uint8_t overflow:4;
201
    /** Various internet timestamp control flags.
201
    /** Various internet timestamp control flags.
202
     */
202
     */
203
    uint8_t flags:4;
203
    uint8_t flags:4;
204
#else
204
#else
205
    /** Various internet timestamp control flags.
205
    /** Various internet timestamp control flags.
206
     */
206
     */
207
    uint8_t flags:4;
207
    uint8_t flags:4;
208
    /** The number of IP modules that cannot register timestamps due to lack of space.
208
    /** The number of IP modules that cannot register timestamps due to lack of space.
209
     */
209
     */
210
    uint8_t overflow:4;
210
    uint8_t overflow:4;
211
#endif
211
#endif
212
} __attribute__ ((packed));
212
} __attribute__ ((packed));
213
 
213
 
214
/** @name IP flags definitions
214
/** @name IP flags definitions
215
 */
215
 */
216
/*@{*/
216
/*@{*/
217
 
217
 
218
/** Fragment flag field shift.
218
/** Fragment flag field shift.
219
 */
219
 */
220
#define IPFLAG_FRAGMENT_SHIFT       1
220
#define IPFLAG_FRAGMENT_SHIFT       1
221
 
221
 
222
/** Fragmented flag field shift.
222
/** Fragmented flag field shift.
223
 */
223
 */
224
#define IPFLAG_FRAGMENTED_SHIFT     0
224
#define IPFLAG_FRAGMENTED_SHIFT     0
225
 
225
 
226
/** May fragment flag value.
226
/** May fragment flag value.
227
 *  Allows the packet fragmentation.
227
 *  Allows the packet fragmentation.
228
 */
228
 */
229
#define IPFLAG_MAY_FRAGMENT         ( 0x0 << IPFLAG_FRAGMENT_SHIFT )
229
#define IPFLAG_MAY_FRAGMENT         ( 0x0 << IPFLAG_FRAGMENT_SHIFT )
230
 
230
 
231
/** Don't fragment flag value.
231
/** Don't fragment flag value.
232
 *  Permits the packet fragmentation.
232
 *  Permits the packet fragmentation.
233
 */
233
 */
234
#define IPFLAG_DONT_FRAGMENT        ( 0x1 << IPFLAG_FRAGMENT_SHIFT )
234
#define IPFLAG_DONT_FRAGMENT        ( 0x1 << IPFLAG_FRAGMENT_SHIFT )
235
 
235
 
236
/** Last fragment flag value.
236
/** Last fragment flag value.
237
 *  Indicates the last packet fragment.
237
 *  Indicates the last packet fragment.
238
 */
238
 */
239
#define IPFLAG_LAST_FRAGMENT        ( 0x0 << IPFLAG_FRAGMENTED_SHIFT )
239
#define IPFLAG_LAST_FRAGMENT        ( 0x0 << IPFLAG_FRAGMENTED_SHIFT )
240
 
240
 
241
/** More fragments flag value.
241
/** More fragments flag value.
242
 *  Indicates that more packet fragments follow.
242
 *  Indicates that more packet fragments follow.
243
 */
243
 */
244
#define IPFLAG_MORE_FRAGMENTS       ( 0x1 << IPFLAG_FRAGMENTED_SHIFT )
244
#define IPFLAG_MORE_FRAGMENTS       ( 0x1 << IPFLAG_FRAGMENTED_SHIFT )
245
 
245
 
246
/*@}*/
246
/*@}*/
247
 
247
 
248
/** Type definition of the internet version 4 pseudo header.
248
/** Type definition of the internet version 4 pseudo header.
249
 *  @see ipv4_pseudo_header
249
 *  @see ipv4_pseudo_header
250
 */
250
 */
251
typedef struct ipv4_pseudo_header   ipv4_pseudo_header_t;
251
typedef struct ipv4_pseudo_header   ipv4_pseudo_header_t;
252
 
252
 
253
/** Type definition of the internet version 4 pseudo header pointer.
253
/** Type definition of the internet version 4 pseudo header pointer.
254
 *  @see ipv4_pseudo_header
254
 *  @see ipv4_pseudo_header
255
 */
255
 */
256
typedef ipv4_pseudo_header_t *      ipv4_pseudo_header_ref;
256
typedef ipv4_pseudo_header_t *      ipv4_pseudo_header_ref;
257
 
257
 
258
/** Internet version 4 pseudo header.
258
/** Internet version 4 pseudo header.
259
 */
259
 */
260
struct ipv4_pseudo_header{
260
struct ipv4_pseudo_header{
261
    /** The source address.
261
    /** The source address.
262
     */
262
     */
263
    uint32_t    source_address;
263
    uint32_t    source_address;
264
    /** The destination address.
264
    /** The destination address.
265
     */
265
     */
266
    uint32_t    destination_address;
266
    uint32_t    destination_address;
267
    /** Reserved byte.
267
    /** Reserved byte.
268
     *  Must be zero.
268
     *  Must be zero.
269
     */
269
     */
270
    uint8_t     reserved;
270
    uint8_t     reserved;
271
    /** This field indicates the next level protocol used in the data portion of the internet datagram.
271
    /** This field indicates the next level protocol used in the data portion of the internet datagram.
272
     */
272
     */
273
    uint8_t     protocol;
273
    uint8_t     protocol;
274
    /** Data length is the length of the datagram, measured in octets.
274
    /** Data length is the length of the datagram, measured in octets.
275
     */
275
     */
276
    uint16_t    data_length;
276
    uint16_t    data_length;
277
} __attribute__ ((packed));
277
} __attribute__ ((packed));
278
 
278
 
279
#endif
279
#endif
280
 
280
 
281
/** @}
281
/** @}
282
 */
282
 */
283
 
283