Subversion Repositories HelenOS

Rev

Rev 4728 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 4728 Rev 4731
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.
45
 *  @param header The IP packet header. Input parameter.
45
 *  @param header The IP packet header. Input parameter.
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 actual IP header length.
50
 *  @param header The IP packet header. Input parameter.
50
 *  @param header The IP packet header. Input parameter.
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 header The IP packet header. Input parameter.
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 header The IP packet header. Input parameter.
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 header The IP packet header. Input parameter.
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.
-
 
71
 */
-
 
72
#define IP_FRAGMENT_OFFSET( header ) (((( header )->fragment_offset_high << 8 ) + ( header )->fragment_offset_low ) * 8u )
-
 
73
 
-
 
74
/** Returns the fragment offest high bits.
70
 *  @param length The prefixed data total length. Input parameter.
75
 *  @param length The prefixed data total length. Input parameter.
71
 */
76
 */
-
 
77
#define IP_COMPUTE_FRAGMENT_OFFSET_HIGH( length ) (((( length ) / 8u ) & 0x1F00 ) >> 8 )
-
 
78
 
-
 
79
/** Returns the fragment offest low bits.
-
 
80
 *  @param length The prefixed data total length. Input parameter.
-
 
81
 */
72
#define IP_COMPUTE_FRAGMENT_OFFSET( length ) (( length ) / 8 )
82
#define IP_COMPUTE_FRAGMENT_OFFSET_LOW( length ) ((( length ) / 8u ) & 0xFF )
73
 
83
 
74
/** Type definition of the internet header.
84
/** Type definition of the internet header.
75
 *  @see ip_header
85
 *  @see ip_header
76
 */
86
 */
77
typedef struct ip_header    ip_header_t;
87
typedef struct ip_header    ip_header_t;
78
 
88
 
79
/** Type definition of the internet header pointer.
89
/** Type definition of the internet header pointer.
80
 *  @see ip_header
90
 *  @see ip_header
81
 */
91
 */
82
typedef ip_header_t *       ip_header_ref;
92
typedef ip_header_t *       ip_header_ref;
83
 
93
 
84
/** Internet header.
94
/** Internet header.
85
 *  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.
86
 */
96
 */
87
struct ip_header{
97
struct ip_header{
88
#ifdef ARCH_IS_BIG_ENDIAN
98
#ifdef ARCH_IS_BIG_ENDIAN
89
    /** The Version field indicates the format of the internet header.
99
    /** The Version field indicates the format of the internet header.
90
     */
100
     */
91
    uint8_t     version:4;
101
    uint8_t     version:4;
92
    /** 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.
93
     *  Note that the minimum value for a~correct header is~5.
103
     *  Note that the minimum value for a~correct header is~5.
94
     */
104
     */
95
    uint8_t     header_length:4;
105
    uint8_t     header_length:4;
96
#else
106
#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.
107
    /** 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.
108
     *  Note that the minimum value for a~correct header is~5.
99
     */
109
     */
100
    uint8_t     header_length:4;
110
    uint8_t     header_length:4;
101
    /** The Version field indicates the format of the internet header.
111
    /** The Version field indicates the format of the internet header.
102
     */
112
     */
103
    uint8_t     version:4;
113
    uint8_t     version:4;
104
#endif
114
#endif
105
    /** 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.
106
     *  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.
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).
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).
108
     *  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.
109
     */
119
     */
110
    uint8_t     tos;
120
    uint8_t     tos;
111
    /** 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.
112
     *  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.
113
     */
123
     */
114
    uint16_t    total_length;
124
    uint16_t    total_length;
115
    /** 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.
116
     */
126
     */
117
    uint16_t    identification;
127
    uint16_t    identification;
118
#ifdef ARCH_IS_BIG_ENDIAN
128
#ifdef ARCH_IS_BIG_ENDIAN
119
    /** Various control flags.
129
    /** Various control flags.
120
     *  @see
-
 
121
     */
130
     */
122
    uint16_t    flags:3;
131
    uint8_t flags:3;
123
    /** This field indicates where in the datagram this fragment belongs.
132
    /** This field indicates where in the datagram this fragment belongs.
-
 
133
     *  High bits.
124
     */
134
     */
125
    uint16_t    fragment_offset:3;
135
    uint8_t fragment_offset_high:5;
126
#else
136
#else
127
    /** This field indicates where in the datagram this fragment belongs.
137
    /** This field indicates where in the datagram this fragment belongs.
-
 
138
     *  High bits.
128
     */
139
     */
129
    uint16_t    fragment_offset:13;
140
    uint8_t fragment_offset_high:5;
130
    /** Various control flags.
141
    /** Various control flags.
131
     */
142
     */
132
    uint16_t    flags:3;
143
    uint8_t flags:3;
133
#endif
144
#endif
-
 
145
    /** This field indicates where in the datagram this fragment belongs.
-
 
146
     *  Low bits.
-
 
147
     */
-
 
148
    uint8_t fragment_offset_low;
134
    /** 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.
135
     *  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.
136
     *  This field is modified in internet header processing.
151
     *  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.
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.
138
     *  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.
139
     */
154
     */
140
    uint8_t     ttl;
155
    uint8_t     ttl;
141
    /** 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.
142
     */
157
     */
143
    uint8_t     protocol;
158
    uint8_t     protocol;
144
    /** A checksum of the header only.
159
    /** A checksum of the header only.
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.
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.
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.
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.
147
     *  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.
148
     */
163
     */
149
    uint16_t    header_checksum;
164
    uint16_t    header_checksum;
150
    /** The source address.
165
    /** The source address.
151
     */
166
     */
152
    uint32_t    source_address;
167
    uint32_t    source_address;
153
    /** The destination address.
168
    /** The destination address.
154
     */
169
     */
155
    uint32_t    destination_address;
170
    uint32_t    destination_address;
156
} __attribute__ ((packed));
171
} __attribute__ ((packed));
157
 
172
 
158
/** Type definition of the internet option header.
173
/** Type definition of the internet option header.
159
 *  @see ip_header
174
 *  @see ip_header
160
 */
175
 */
161
typedef struct ip_option    ip_option_t;
176
typedef struct ip_option    ip_option_t;
162
 
177
 
163
/** Type definition of the internet option header pointer.
178
/** Type definition of the internet option header pointer.
164
 *  @see ip_header
179
 *  @see ip_header
165
 */
180
 */
166
typedef ip_option_t *       ip_option_ref;
181
typedef ip_option_t *       ip_option_ref;
167
 
182
 
168
/** Internet option header.
183
/** Internet option header.
169
 *  Only type field is always valid.
184
 *  Only type field is always valid.
170
 *  Other fields' validity depends on the option type.
185
 *  Other fields' validity depends on the option type.
171
 */
186
 */
172
struct ip_option{
187
struct ip_option{
173
    /** A single octet of option-type.
188
    /** A single octet of option-type.
174
     */
189
     */
175
    uint8_t     type;
190
    uint8_t     type;
176
    /** An option length octet.
191
    /** An option length octet.
177
     */
192
     */
178
    uint8_t     length;
193
    uint8_t     length;
179
    /** A~pointer.
194
    /** A~pointer.
180
     */
195
     */
181
    uint8_t     pointer;
196
    uint8_t     pointer;
182
#ifdef ARCH_IS_BIG_ENDIAN
197
#ifdef ARCH_IS_BIG_ENDIAN
183
    /** 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.
184
     */
199
     */
185
    uint8_t overflow:4;
200
    uint8_t overflow:4;
186
    /** Various internet timestamp control flags.
201
    /** Various internet timestamp control flags.
187
     */
202
     */
188
    uint8_t flags:4;
203
    uint8_t flags:4;
189
#else
204
#else
190
    /** Various internet timestamp control flags.
205
    /** Various internet timestamp control flags.
191
     */
206
     */
192
    uint8_t flags:4;
207
    uint8_t flags:4;
193
    /** 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.
194
     */
209
     */
195
    uint8_t overflow:4;
210
    uint8_t overflow:4;
196
#endif
211
#endif
197
} __attribute__ ((packed));
212
} __attribute__ ((packed));
198
 
213
 
199
/** @name IP flags definitions
214
/** @name IP flags definitions
200
 */
215
 */
201
/*@{*/
216
/*@{*/
202
 
217
 
203
/** Fragment flag field shift.
218
/** Fragment flag field shift.
204
 */
219
 */
205
#define IPFLAG_FRAGMENT_SHIFT       1
220
#define IPFLAG_FRAGMENT_SHIFT       1
206
 
221
 
207
/** Fragmented flag field shift.
222
/** Fragmented flag field shift.
208
 */
223
 */
209
#define IPFLAG_FRAGMENTED_SHIFT     0
224
#define IPFLAG_FRAGMENTED_SHIFT     0
210
 
225
 
211
/** May fragment flag value.
226
/** May fragment flag value.
212
 *  Allows the packet fragmentation.
227
 *  Allows the packet fragmentation.
213
 */
228
 */
214
#define IPFLAG_MAY_FRAGMENT         ( 0x0 << IPFLAG_FRAGMENT_SHIFT )
229
#define IPFLAG_MAY_FRAGMENT         ( 0x0 << IPFLAG_FRAGMENT_SHIFT )
215
 
230
 
216
/** Don't fragment flag value.
231
/** Don't fragment flag value.
217
 *  Permits the packet fragmentation.
232
 *  Permits the packet fragmentation.
218
 */
233
 */
219
#define IPFLAG_DONT_FRAGMENT        ( 0x1 << IPFLAG_FRAGMENT_SHIFT )
234
#define IPFLAG_DONT_FRAGMENT        ( 0x1 << IPFLAG_FRAGMENT_SHIFT )
220
 
235
 
221
/** Last fragment flag value.
236
/** Last fragment flag value.
222
 *  Indicates the last packet fragment.
237
 *  Indicates the last packet fragment.
223
 */
238
 */
224
#define IPFLAG_LAST_FRAGMENT        ( 0x0 << IPFLAG_FRAGMENTED_SHIFT )
239
#define IPFLAG_LAST_FRAGMENT        ( 0x0 << IPFLAG_FRAGMENTED_SHIFT )
225
 
240
 
226
/** More fragments flag value.
241
/** More fragments flag value.
227
 *  Indicates that more packet fragments follow.
242
 *  Indicates that more packet fragments follow.
228
 */
243
 */
229
#define IPFLAG_MORE_FRAGMENTS       ( 0x1 << IPFLAG_FRAGMENTED_SHIFT )
244
#define IPFLAG_MORE_FRAGMENTS       ( 0x1 << IPFLAG_FRAGMENTED_SHIFT )
230
 
245
 
231
/*@}*/
246
/*@}*/
232
 
247
 
233
/** Type definition of the internet version 4 pseudo header.
248
/** Type definition of the internet version 4 pseudo header.
234
 *  @see ipv4_pseudo_header
249
 *  @see ipv4_pseudo_header
235
 */
250
 */
236
typedef struct ipv4_pseudo_header   ipv4_pseudo_header_t;
251
typedef struct ipv4_pseudo_header   ipv4_pseudo_header_t;
237
 
252
 
238
/** Type definition of the internet version 4 pseudo header pointer.
253
/** Type definition of the internet version 4 pseudo header pointer.
239
 *  @see ipv4_pseudo_header
254
 *  @see ipv4_pseudo_header
240
 */
255
 */
241
typedef ipv4_pseudo_header_t *      ipv4_pseudo_header_ref;
256
typedef ipv4_pseudo_header_t *      ipv4_pseudo_header_ref;
242
 
257
 
243
/** Internet version 4 pseudo header.
258
/** Internet version 4 pseudo header.
244
 */
259
 */
245
struct ipv4_pseudo_header{
260
struct ipv4_pseudo_header{
246
    /** The source address.
261
    /** The source address.
247
     */
262
     */
248
    uint32_t    source_address;
263
    uint32_t    source_address;
249
    /** The destination address.
264
    /** The destination address.
250
     */
265
     */
251
    uint32_t    destination_address;
266
    uint32_t    destination_address;
252
    /** Reserved byte.
267
    /** Reserved byte.
253
     *  Must be zero.
268
     *  Must be zero.
254
     */
269
     */
255
    uint8_t     reserved;
270
    uint8_t     reserved;
256
    /** 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.
257
     */
272
     */
258
    uint8_t     protocol;
273
    uint8_t     protocol;
259
    /** Data length is the length of the datagram, measured in octets.
274
    /** Data length is the length of the datagram, measured in octets.
260
     */
275
     */
261
    uint16_t    data_length;
276
    uint16_t    data_length;
262
} __attribute__ ((packed));
277
} __attribute__ ((packed));
263
 
278
 
264
#endif
279
#endif
265
 
280
 
266
/** @}
281
/** @}
267
 */
282
 */
268
 
283