Subversion Repositories HelenOS

Rev

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

Rev 4743 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 module implementation.
34
 *  IP module implementation.
35
 *  @see arp.h
35
 *  @see arp.h
36
 *  \todo
-
 
37
 */
36
 */
38
 
37
 
39
#include <async.h>
38
#include <async.h>
40
#include <errno.h>
39
#include <errno.h>
41
#include <fibril_sync.h>
40
#include <fibril_sync.h>
42
#include <stdio.h>
41
#include <stdio.h>
43
#include <string.h>
42
#include <string.h>
44
 
43
 
45
#include <ipc/ipc.h>
44
#include <ipc/ipc.h>
46
#include <ipc/services.h>
45
#include <ipc/services.h>
47
 
46
 
48
#include <sys/types.h>
47
#include <sys/types.h>
49
 
48
 
50
#include "../../err.h"
49
#include "../../err.h"
51
#include "../../messages.h"
50
#include "../../messages.h"
52
#include "../../modules.h"
51
#include "../../modules.h"
53
 
52
 
54
#include "../../include/arp_interface.h"
53
#include "../../include/arp_interface.h"
55
#include "../../include/byteorder.h"
54
#include "../../include/byteorder.h"
56
#include "../../include/checksum.h"
55
#include "../../include/checksum.h"
57
#include "../../include/device.h"
56
#include "../../include/device.h"
58
#include "../../include/icmp_client.h"
57
#include "../../include/icmp_client.h"
59
#include "../../include/icmp_codes.h"
58
#include "../../include/icmp_codes.h"
60
#include "../../include/icmp_interface.h"
59
#include "../../include/icmp_interface.h"
61
#include "../../include/il_interface.h"
60
#include "../../include/il_interface.h"
62
#include "../../include/in.h"
61
#include "../../include/in.h"
63
#include "../../include/in6.h"
62
#include "../../include/in6.h"
64
#include "../../include/inet.h"
63
#include "../../include/inet.h"
65
#include "../../include/ip_client.h"
64
#include "../../include/ip_client.h"
66
#include "../../include/ip_interface.h"
65
#include "../../include/ip_interface.h"
67
#include "../../include/net_interface.h"
66
#include "../../include/net_interface.h"
68
#include "../../include/nil_interface.h"
67
#include "../../include/nil_interface.h"
69
#include "../../include/tl_interface.h"
68
#include "../../include/tl_interface.h"
70
#include "../../include/socket_codes.h"
69
#include "../../include/socket_codes.h"
71
#include "../../include/socket_errno.h"
70
#include "../../include/socket_errno.h"
72
#include "../../structures/measured_strings.h"
71
#include "../../structures/measured_strings.h"
73
#include "../../structures/module_map.h"
72
#include "../../structures/module_map.h"
74
#include "../../structures/packet/packet_client.h"
73
#include "../../structures/packet/packet_client.h"
75
 
74
 
76
#include "../../nil/nil_messages.h"
75
#include "../../nil/nil_messages.h"
77
 
76
 
78
#include "../il_messages.h"
77
#include "../il_messages.h"
79
 
78
 
80
#include "ip.h"
79
#include "ip.h"
81
#include "ip_header.h"
80
#include "ip_header.h"
82
#include "ip_messages.h"
81
#include "ip_messages.h"
83
#include "ip_module.h"
82
#include "ip_module.h"
84
 
83
 
85
/** IP version 4.
84
/** IP version 4.
86
 */
85
 */
87
#define IPV4                4
86
#define IPV4                4
88
 
87
 
89
/** Default network interface IP version.
88
/** Default network interface IP version.
90
 */
89
 */
91
#define NET_DEFAULT_IPV     IPV4
90
#define NET_DEFAULT_IPV     IPV4
92
 
91
 
93
/** Default network interface IP routing.
92
/** Default network interface IP routing.
94
 */
93
 */
95
#define NET_DEFAULT_IP_ROUTING  false
94
#define NET_DEFAULT_IP_ROUTING  false
96
 
95
 
97
/** Minimum IP packet content.
96
/** Minimum IP packet content.
98
 */
97
 */
99
#define IP_MIN_CONTENT  576
98
#define IP_MIN_CONTENT  576
100
 
99
 
101
/** ARP module name.
100
/** ARP module name.
102
 */
101
 */
103
#define ARP_NAME                "arp"
102
#define ARP_NAME                "arp"
104
 
103
 
105
/** ARP module filename.
104
/** ARP module filename.
106
 */
105
 */
107
#define ARP_FILENAME            "/srv/arp"
106
#define ARP_FILENAME            "/srv/arp"
108
 
107
 
109
/** IP packet address length.
108
/** IP packet address length.
110
 */
109
 */
111
#define IP_ADDR                         sizeof( struct sockaddr_in6 )
110
#define IP_ADDR                         sizeof( struct sockaddr_in6 )
112
 
111
 
113
/** IP packet prefix length.
112
/** IP packet prefix length.
114
 */
113
 */
115
#define IP_PREFIX                       sizeof( ip_header_t )
114
#define IP_PREFIX                       sizeof( ip_header_t )
116
 
115
 
117
/** IP packet suffix length.
116
/** IP packet suffix length.
118
 */
117
 */
119
#define IP_SUFFIX                       0
118
#define IP_SUFFIX                       0
120
 
119
 
121
/** IP packet maximum content length.
120
/** IP packet maximum content length.
122
 */
121
 */
123
#define IP_MAX_CONTENT                  65535
122
#define IP_MAX_CONTENT                  65535
124
 
123
 
125
/** The IP localhost address.
124
/** The IP localhost address.
126
 */
125
 */
127
#define IPV4_LOCALHOST_ADDRESS  htonl(( 127 << 24 ) + 1 )
126
#define IPV4_LOCALHOST_ADDRESS  htonl(( 127 << 24 ) + 1 )
128
 
127
 
129
/** IP global data.
128
/** IP global data.
130
 */
129
 */
131
ip_globals_t    ip_globals;
130
ip_globals_t    ip_globals;
132
 
131
 
133
DEVICE_MAP_IMPLEMENT( ip_netifs, ip_netif_t )
132
DEVICE_MAP_IMPLEMENT( ip_netifs, ip_netif_t )
134
 
133
 
135
INT_MAP_IMPLEMENT( ip_protos, ip_proto_t )
134
INT_MAP_IMPLEMENT( ip_protos, ip_proto_t )
136
 
135
 
137
GENERIC_FIELD_IMPLEMENT( ip_routes, ip_route_t )
136
GENERIC_FIELD_IMPLEMENT( ip_routes, ip_route_t )
138
 
137
 
139
/** Updates the device content length according to the new MTU value.
138
/** Updates the device content length according to the new MTU value.
140
 *  @param device_id The device identifier. Input parameter.
139
 *  @param[in] device_id The device identifier.
141
 *  @param mtu The new mtu value. Input parameter.
140
 *  @param[in] mtu The new mtu value.
142
 *  @returns EOK on success.
141
 *  @returns EOK on success.
143
 *  @returns ENOENT if device is not found.
142
 *  @returns ENOENT if device is not found.
144
 */
143
 */
145
int ip_mtu_changed_message( device_id_t device_id, size_t mtu );
144
int ip_mtu_changed_message( device_id_t device_id, size_t mtu );
146
 
145
 
147
/** Updates the device state.
146
/** Updates the device state.
148
 *  @param device_id The device identifier. Input parameter.
147
 *  @param[in] device_id The device identifier.
149
 *  @param state The new state value. Input parameter.
148
 *  @param[in] state The new state value.
150
 *  @returns EOK on success.
149
 *  @returns EOK on success.
151
 *  @returns ENOENT if device is not found.
150
 *  @returns ENOENT if device is not found.
152
 */
151
 */
153
int ip_device_state_message( device_id_t device_id, device_state_t state );
152
int ip_device_state_message( device_id_t device_id, device_state_t state );
154
 
153
 
-
 
154
/** Registers the transport layer protocol.
-
 
155
 *  The traffic of this protocol will be supplied using either the receive function or IPC message.
-
 
156
 *  @param[in] protocol The transport layer module protocol.
-
 
157
 *  @param[in] service The transport layer module service.
-
 
158
 *  @param[in] phone The transport layer module phone.
-
 
159
 *  @param[in] tl_received_msg The receiving function.
-
 
160
 *  @returns EOK on success.
-
 
161
 *  @returns EINVAL if the protocol parameter and/or the service parameter is zero (0).
-
 
162
 *  @returns EINVAL if the phone parameter is not a positive number and the tl_receive_msg is NULL.
-
 
163
 *  @returns ENOMEM if there is not enough memory left.
-
 
164
 */
155
int ip_register( int protocol, services_t service, int phone, tl_received_msg_t tl_received_msg );
165
int ip_register( int protocol, services_t service, int phone, tl_received_msg_t tl_received_msg );
156
 
166
 
157
/** Initializes a new network interface specific data.
167
/** Initializes a new network interface specific data.
158
 *  Connects to the network interface layer module, reads the netif configuration, starts an ARP module if needed and sets the netif routing table.
168
 *  Connects to the network interface layer module, reads the netif configuration, starts an ARP module if needed and sets the netif routing table.
159
 *  The device identifier and the nil service has to be set.
169
 *  The device identifier and the nil service has to be set.
160
 *  @param ip_netif Network interface specific data. Input/output parameter.
170
 *  @param[in,out] ip_netif Network interface specific data.
161
 *  @returns EOK on success.
171
 *  @returns EOK on success.
162
 *  @returns ENOTSUP if DHCP is configured.
172
 *  @returns ENOTSUP if DHCP is configured.
163
 *  @returns ENOTSUP if IPv6 is configured.
173
 *  @returns ENOTSUP if IPv6 is configured.
164
 *  @returns EINVAL if any of the addresses is invalid.
174
 *  @returns EINVAL if any of the addresses is invalid.
165
 *  @returns EINVAL if the used ARP module is not known.
175
 *  @returns EINVAL if the used ARP module is not known.
166
 *  @returns ENOMEM if there is not enough memory left.
176
 *  @returns ENOMEM if there is not enough memory left.
167
 *  @returns Other error codes as defined for the net_get_device_conf_req() function.
177
 *  @returns Other error codes as defined for the net_get_device_conf_req() function.
168
 *  @returns Other error codes as defined for the bind_service() function.
178
 *  @returns Other error codes as defined for the bind_service() function.
169
 *  @returns Other error codes as defined for the specific arp_device_req() function.
179
 *  @returns Other error codes as defined for the specific arp_device_req() function.
170
 *  @returns Other error codes as defined for the nil_packet_size_req() function.
180
 *  @returns Other error codes as defined for the nil_packet_size_req() function.
171
 */
181
 */
172
int ip_netif_initialize( ip_netif_ref ip_netif );
182
int ip_netif_initialize( ip_netif_ref ip_netif );
173
 
183
 
-
 
184
/** Sends the packet or the packet queue via the specified route.
-
 
185
 *  The ICMP_HOST_UNREACH error notification may be sent if route hardware destination address is found.
-
 
186
 *  @param[in,out] packet The packet to be sent.
-
 
187
 *  @param[in] netif The target network interface.
-
 
188
 *  @param[in] route The target route.
-
 
189
 *  @param[in] src The source address.
-
 
190
 *  @param[in] dest The destination address.
-
 
191
 *  @param[in] error The error module service.
-
 
192
 *  @returns EOK on success.
-
 
193
 *  @returns Other error codes as defined for the arp_translate_req() function.
-
 
194
 *  @returns Other error codes as defined for the ip_prepare_packet() function.
-
 
195
 */
174
int ip_send_route( packet_t packet, ip_netif_ref netif, ip_route_ref route, in_addr_t * src, in_addr_t dest, services_t error );
196
int ip_send_route( packet_t packet, ip_netif_ref netif, ip_route_ref route, in_addr_t * src, in_addr_t dest, services_t error );
-
 
197
 
-
 
198
/** Prepares the outgoing packet or the packet queue.
-
 
199
 *  The packet queue is a fragmented packet
-
 
200
 *  Updates the first packet's IP header.
-
 
201
 *  Prefixes the additional packets with fragment headers.
-
 
202
 *  @param[in] source The source address.
-
 
203
 *  @param[in] dest The destination address.
-
 
204
 *  @param[in,out] packet The packet to be sent.
-
 
205
 *  @param[in] destination The destination hardware address.
-
 
206
 *  @returns EOK on success.
-
 
207
 *  @returns EINVAL if the packet is too small to contain the IP header.
-
 
208
 *  @returns EINVAL if the packet is too long than the IP allows.
-
 
209
 *  @returns ENOMEM if there is not enough memory left.
-
 
210
 *  @returns Other error codes as defined for the packet_set_addr() function.
-
 
211
 */
175
int ip_prepare_packet( in_addr_t * source, in_addr_t dest, packet_t packet, measured_string_ref destination );
212
int ip_prepare_packet( in_addr_t * source, in_addr_t dest, packet_t packet, measured_string_ref destination );
176
 
213
 
-
 
214
/** Checks the packet queue lengths and fragments the packets if needed.
-
 
215
 *  The ICMP_FRAG_NEEDED error notification may be sent if the packet needs to be fragmented and the fragmentation is not allowed.
-
 
216
 *  @param[in,out] packet The packet or the packet queue to be checked.
-
 
217
 *  @param[in] prefix The minimum prefix size.
-
 
218
 *  @param[in] content The maximum content size.
-
 
219
 *  @param[in] suffix The minimum suffix size.
-
 
220
 *  @param[in] addr_len The minimum address length.
-
 
221
 *  @param[in] error The error module service.
-
 
222
 *  @returns The packet or the packet queue of the allowed length.
-
 
223
 *  @returns NULL if there are no packets left.
-
 
224
 */
177
packet_t    ip_split_packet( packet_t packet, size_t prefix, size_t content, size_t suffix, socklen_t addr_len, services_t error );
225
packet_t    ip_split_packet( packet_t packet, size_t prefix, size_t content, size_t suffix, socklen_t addr_len, services_t error );
-
 
226
 
-
 
227
/** Checks the packet length and fragments it if needed.
-
 
228
 *  The new fragments are queued before the original packet.
-
 
229
 *  @param[in,out] packet The packet to be checked.
-
 
230
 *  @param[in] length The maximum packet length.
-
 
231
 *  @param[in] prefix The minimum prefix size.
-
 
232
 *  @param[in] suffix The minimum suffix size.
-
 
233
 *  @param[in] addr_len The minimum address length.
-
 
234
 *  @returns EOK on success.
-
 
235
 *  @returns EINVAL if the packet_get_addr() function fails.
-
 
236
 *  @returns EINVAL if the packet does not contain the IP header.
-
 
237
 *  @returns EPERM if the packet needs to be fragmented and the fragmentation is not allowed.
-
 
238
 *  @returns ENOMEM if there is not enough memory left.
-
 
239
 *  @returns ENOMEM if there is no packet available.
-
 
240
 *  @returns ENOMEM if the packet is too small to contain the IP header.
-
 
241
 *  @returns Other error codes as defined for the packet_trim() function.
-
 
242
 *  @returns Other error codes as defined for the ip_create_middle_header() function.
-
 
243
 *  @returns Other error codes as defined for the ip_fragment_packet_data() function.
-
 
244
 */
178
int ip_fragment_packet( packet_t packet, size_t length, size_t prefix, size_t suffix, socklen_t addr_len );
245
int ip_fragment_packet( packet_t packet, size_t length, size_t prefix, size_t suffix, socklen_t addr_len );
-
 
246
 
-
 
247
/** Fragments the packet from the end.
-
 
248
 *  @param[in] packet The packet to be fragmented.
-
 
249
 *  @param[in,out] new_packet The new packet fragment.
-
 
250
 *  @param[in,out] header The original packet header.
-
 
251
 *  @param[in,out] new_header The new packet fragment header.
-
 
252
 *  @param[in] length The new fragment length.
-
 
253
 *  @param[in] src The source address.
-
 
254
 *  @param[in] dest The destiantion address.
-
 
255
 *  @param[in] addrlen The address length.
-
 
256
 *  @returns EOK on success.
-
 
257
 *  @returns ENOMEM if the target packet is too small.
-
 
258
 *  @returns Other error codes as defined for the packet_set_addr() function.
-
 
259
 *  @returns Other error codes as defined for the pq_insert_after() function.
-
 
260
 */
179
int ip_fragment_packet_data( packet_t packet, packet_t new_packet, ip_header_ref header, ip_header_ref new_header, size_t length, const struct sockaddr * src, const struct sockaddr * dest, socklen_t addrlen );
261
int ip_fragment_packet_data( packet_t packet, packet_t new_packet, ip_header_ref header, ip_header_ref new_header, size_t length, const struct sockaddr * src, const struct sockaddr * dest, socklen_t addrlen );
-
 
262
 
-
 
263
/** Prefixes a middle fragment header based on the last fragment header to the packet.
-
 
264
 *  @param[in] packet The packet to be prefixed.
-
 
265
 *  @param[in] last The last header to be copied.
-
 
266
 *  @returns The prefixed middle header.
-
 
267
 *  @returns NULL on error.
-
 
268
 */
180
ip_header_ref   ip_create_middle_header( packet_t packet, ip_header_ref last );
269
ip_header_ref   ip_create_middle_header( packet_t packet, ip_header_ref last );
-
 
270
 
-
 
271
/** Copies the fragment header.
-
 
272
 *  Copies only the header itself and relevant IP options.
-
 
273
 *  @param[out] last The created header.
-
 
274
 *  @param[in] first The original header to be copied.
-
 
275
 */
181
void ip_create_last_header( ip_header_ref last, ip_header_ref first );
276
void ip_create_last_header( ip_header_ref last, ip_header_ref first );
182
 
277
 
-
 
278
/** Returns the network interface's IP address.
-
 
279
 *  @param[in] netif The network interface.
-
 
280
 *  @returns The IP address.
-
 
281
 *  @returns NULL if no IP address was found.
-
 
282
 */
183
in_addr_t * ip_netif_address( ip_netif_ref netif );
283
in_addr_t * ip_netif_address( ip_netif_ref netif );
-
 
284
 
-
 
285
/** Searches all network interfaces if there is a suitable route.
-
 
286
 *  @param[in] destination The destination address.
-
 
287
 *  @returns The found route.
-
 
288
 *  @returns NULL if no route was found.
-
 
289
 */
184
ip_route_ref    ip_find_route( in_addr_t destination );
290
ip_route_ref    ip_find_route( in_addr_t destination );
-
 
291
 
-
 
292
/** Searches the network interfaces if there is a suitable route.
-
 
293
 *  @param[in] netif The network interface to be searched for routes. May be NULL.
-
 
294
 *  @param[in] destination The destination address.
-
 
295
 *  @returns The found route.
-
 
296
 *  @returns NULL if no route was found.
-
 
297
 */
185
ip_route_ref    ip_netif_find_route( ip_netif_ref netif, in_addr_t destination );
298
ip_route_ref    ip_netif_find_route( ip_netif_ref netif, in_addr_t destination );
186
 
299
 
187
/** Processes the received IP packet.
300
/** Processes the received IP packet or the packet queue one by one.
-
 
301
 *  The packet is either passed to another module or released on error.
188
 *  @param device_id The source device identifier. Input parameter.
302
 *  @param[in] device_id The source device identifier.
189
 *  @param packet The received packet. Input/output parameter.
303
 *  @param[in,out] packet The received packet.
190
 *  @returns EOK on success and the packet is no longer needed.
304
 *  @returns EOK on success and the packet is no longer needed.
191
 *  @returns EINVAL if the packet is too small to carry the IP packet.
305
 *  @returns EINVAL if the packet is too small to carry the IP packet.
192
 *  @returns EINVAL if the received address lengths differs from the registered values.
306
 *  @returns EINVAL if the received address lengths differs from the registered values.
193
 *  @returns ENOENT if the device is not found in the cache.
307
 *  @returns ENOENT if the device is not found in the cache.
194
 *  @returns ENOENT if the protocol for the device is not found in the cache.
308
 *  @returns ENOENT if the protocol for the device is not found in the cache.
195
 *  @returns ENOMEM if there is not enough memory left.
309
 *  @returns ENOMEM if there is not enough memory left.
196
 */
310
 */
197
int ip_receive_message( device_id_t device_id, packet_t packet );
311
int ip_receive_message( device_id_t device_id, packet_t packet );
198
 
312
 
-
 
313
/** Processes the received packet.
-
 
314
 *  The packet is either passed to another module or released on error.
-
 
315
 *  The ICMP_PARAM_POINTER error notification may be sent if the checksum is invalid.
-
 
316
 *  The ICMP_EXC_TTL error notification may be sent if the TTL is less than two (2).
-
 
317
 *  The ICMP_HOST_UNREACH error notification may be sent if no route was found.
-
 
318
 *  The ICMP_HOST_UNREACH error notification may be sent if the packet is for another host and the routing is disabled.
-
 
319
 *  @param[in] device_id The source device identifier.
-
 
320
 *  @param[in] packet The received packet to be processed.
-
 
321
 *  @returns EOK on success.
-
 
322
 *  @returns EINVAL if the TTL is less than two (2).
-
 
323
 *  @returns EINVAL if the checksum is invalid.
-
 
324
 *  @returns EAFNOSUPPORT if the address family is not supported.
-
 
325
 *  @returns ENOENT if no route was found.
-
 
326
 *  @returns ENOENT if the packet is for another host and the routing is disabled.
-
 
327
 */
199
int ip_process_packet( device_id_t device_id, packet_t packet );
328
int ip_process_packet( device_id_t device_id, packet_t packet );
-
 
329
 
-
 
330
/** Returns the packet destination address from the IP header.
-
 
331
 *  @param[in] header The packet IP header to be read.
-
 
332
 *  @returns The packet destination address.
-
 
333
 */
200
in_addr_t   ip_get_destination( ip_header_ref header );
334
in_addr_t   ip_get_destination( ip_header_ref header );
-
 
335
 
-
 
336
/** Delivers the packet to the local host.
-
 
337
 *  The packet is either passed to another module or released on error.
-
 
338
 *  The ICMP_PROT_UNREACH error notification may be sent if the protocol is not found.
-
 
339
 *  @param[in] device_id The source device identifier.
-
 
340
 *  @param[in] packet The packet to be delivered.
-
 
341
 *  @param[in] header The first packet IP header. May be NULL.
-
 
342
 *  @param[in] error The packet error service.
-
 
343
 *  @returns EOK on success.
-
 
344
 *  @returns ENOTSUP if the packet is a fragment.
-
 
345
 *  @returns EAFNOSUPPORT if the address family is not supported.
-
 
346
 *  @returns ENOENT if the target protocol is not found.
-
 
347
 *  @returns Other error codes as defined for the packet_set_addr() function.
-
 
348
 *  @returns Other error codes as defined for the packet_trim() function.
-
 
349
 *  @returns Other error codes as defined for the protocol specific tl_received_msg function.
-
 
350
 */
201
int ip_deliver_local( device_id_t device_id, packet_t packet, ip_header_ref header, services_t error );
351
int ip_deliver_local( device_id_t device_id, packet_t packet, ip_header_ref header, services_t error );
202
 
352
 
-
 
353
/** Prepares the ICMP notification packet.
-
 
354
 *  Releases additional packets and keeps only the first one.
-
 
355
 *  All packets is released on error.
-
 
356
 *  @param[in] error The packet error service.
-
 
357
 *  @param[in] packet The packet or the packet queue to be reported as faulty.
-
 
358
 *  @param[in] header The first packet IP header. May be NULL.
-
 
359
 *  @returns The found ICMP phone.
-
 
360
 *  @returns EINVAL if the error parameter is set.
-
 
361
 *  @returns EINVAL if the ICMP phone is not found.
-
 
362
 *  @returns EINVAL if the ip_prepare_icmp() fails.
-
 
363
 */
203
int ip_prepare_icmp_and_get_phone( services_t error, packet_t packet, ip_header_ref header );
364
int ip_prepare_icmp_and_get_phone( services_t error, packet_t packet, ip_header_ref header );
-
 
365
 
-
 
366
/** Returns the ICMP phone.
-
 
367
 *  Searches the registered protocols.
-
 
368
 *  @returns The found ICMP phone.
-
 
369
 *  @returns ENOENT if the ICMP is not registered.
-
 
370
 */
204
int ip_get_icmp_phone( void );
371
int ip_get_icmp_phone( void );
-
 
372
 
-
 
373
/** Prepares the ICMP notification packet.
-
 
374
 *  Releases additional packets and keeps only the first one.
-
 
375
 *  @param[in] packet The packet or the packet queue to be reported as faulty.
-
 
376
 *  @param[in] header The first packet IP header. May be NULL.
-
 
377
 *  @returns EOK on success.
-
 
378
 *  @returns EINVAL if there are no data in the packet.
-
 
379
 *  @returns EINVAL if the packet is a fragment.
-
 
380
 *  @returns ENOMEM if the packet is too short to contain the IP header.
-
 
381
 *  @returns EAFNOSUPPORT if the address family is not supported.
-
 
382
 *  @returns Other error codes as defined for the packet_set_addr().
-
 
383
 */
205
int ip_prepare_icmp( packet_t packet, ip_header_ref header );
384
int ip_prepare_icmp( packet_t packet, ip_header_ref header );
206
 
385
 
-
 
386
/** Releases the packet and returns the result.
-
 
387
 *  @param[in] packet The packet queue to be released.
-
 
388
 *  @param[in] result The result to be returned.
-
 
389
 *  @return The result parameter.
-
 
390
 */
207
int ip_release_and_return( packet_t packet, int result );
391
int ip_release_and_return( packet_t packet, int result );
208
 
392
 
209
int ip_initialize( async_client_conn_t client_connection ){
393
int ip_initialize( async_client_conn_t client_connection ){
210
    ERROR_DECLARE;
394
    ERROR_DECLARE;
211
 
395
 
212
    fibril_rwlock_initialize( & ip_globals.lock );
396
    fibril_rwlock_initialize( & ip_globals.lock );
213
    fibril_rwlock_write_lock( & ip_globals.lock );
397
    fibril_rwlock_write_lock( & ip_globals.lock );
214
    fibril_rwlock_initialize( & ip_globals.protos_lock );
398
    fibril_rwlock_initialize( & ip_globals.protos_lock );
215
    fibril_rwlock_initialize( & ip_globals.netifs_lock );
399
    fibril_rwlock_initialize( & ip_globals.netifs_lock );
216
    ip_globals.packet_counter = 0;
400
    ip_globals.packet_counter = 0;
217
    ip_globals.gateway.address.s_addr = 0;
401
    ip_globals.gateway.address.s_addr = 0;
218
    ip_globals.gateway.netmask.s_addr = 0;
402
    ip_globals.gateway.netmask.s_addr = 0;
219
    ip_globals.gateway.gateway.s_addr = 0;
403
    ip_globals.gateway.gateway.s_addr = 0;
220
    ip_globals.gateway.netif = NULL;
404
    ip_globals.gateway.netif = NULL;
221
    ERROR_PROPAGATE( ip_netifs_initialize( & ip_globals.netifs ));
405
    ERROR_PROPAGATE( ip_netifs_initialize( & ip_globals.netifs ));
222
    ERROR_PROPAGATE( ip_protos_initialize( & ip_globals.protos ));
406
    ERROR_PROPAGATE( ip_protos_initialize( & ip_globals.protos ));
223
    ip_globals.client_connection = client_connection;
407
    ip_globals.client_connection = client_connection;
224
    ERROR_PROPAGATE( modules_initialize( & ip_globals.modules ));
408
    ERROR_PROPAGATE( modules_initialize( & ip_globals.modules ));
225
    ERROR_PROPAGATE( add_module( NULL, & ip_globals.modules, ARP_NAME, ARP_FILENAME, SERVICE_ARP, arp_task_get_id(), arp_connect_module ));
409
    ERROR_PROPAGATE( add_module( NULL, & ip_globals.modules, ARP_NAME, ARP_FILENAME, SERVICE_ARP, arp_task_get_id(), arp_connect_module ));
226
    fibril_rwlock_write_unlock( & ip_globals.lock );
410
    fibril_rwlock_write_unlock( & ip_globals.lock );
227
    return EOK;
411
    return EOK;
228
}
412
}
229
 
413
 
230
int ip_device_req( int il_phone, device_id_t device_id, services_t netif ){
414
int ip_device_req( int il_phone, device_id_t device_id, services_t netif ){
231
    ERROR_DECLARE;
415
    ERROR_DECLARE;
232
 
416
 
233
    ip_netif_ref    ip_netif;
417
    ip_netif_ref    ip_netif;
234
    ip_route_ref    route;
418
    ip_route_ref    route;
235
    int             index;
419
    int             index;
236
    char *          data;
420
    char *          data;
237
 
421
 
238
    ip_netif = ( ip_netif_ref ) malloc( sizeof( ip_netif_t ));
422
    ip_netif = ( ip_netif_ref ) malloc( sizeof( ip_netif_t ));
239
    if( ! ip_netif ) return ENOMEM;
423
    if( ! ip_netif ) return ENOMEM;
240
    if( ERROR_OCCURRED( ip_routes_initialize( & ip_netif->routes ))){
424
    if( ERROR_OCCURRED( ip_routes_initialize( & ip_netif->routes ))){
241
        free( ip_netif );
425
        free( ip_netif );
242
        return ERROR_CODE;
426
        return ERROR_CODE;
243
    }
427
    }
244
    ip_netif->device_id = device_id;
428
    ip_netif->device_id = device_id;
245
    ip_netif->service = netif;
429
    ip_netif->service = netif;
246
    ip_netif->state = NETIF_STOPPED;
430
    ip_netif->state = NETIF_STOPPED;
247
    fibril_rwlock_write_lock( & ip_globals.netifs_lock );
431
    fibril_rwlock_write_lock( & ip_globals.netifs_lock );
248
    if( ERROR_OCCURRED( ip_netif_initialize( ip_netif ))){
432
    if( ERROR_OCCURRED( ip_netif_initialize( ip_netif ))){
249
        fibril_rwlock_write_unlock( & ip_globals.netifs_lock );
433
        fibril_rwlock_write_unlock( & ip_globals.netifs_lock );
250
        ip_routes_destroy( & ip_netif->routes );
434
        ip_routes_destroy( & ip_netif->routes );
251
        free( ip_netif );
435
        free( ip_netif );
252
        return ERROR_CODE;
436
        return ERROR_CODE;
253
    }
437
    }
254
    if( ip_netif->arp ) ++ ip_netif->arp->usage;
438
    if( ip_netif->arp ) ++ ip_netif->arp->usage;
255
    // print the settings
439
    // print the settings
256
    printf( "New device registered:\n\tid\t= %d\n\tphone\t= %d\n\tIPV\t= %d\n", ip_netif->device_id, ip_netif->phone, ip_netif->ipv );
440
    printf( "New device registered:\n\tid\t= %d\n\tphone\t= %d\n\tIPV\t= %d\n", ip_netif->device_id, ip_netif->phone, ip_netif->ipv );
257
    printf( "\tconfiguration\t= %s\n", ip_netif->dhcp ? "dhcp" : "static" );
441
    printf( "\tconfiguration\t= %s\n", ip_netif->dhcp ? "dhcp" : "static" );
258
    // TODO ipv6 addresses
442
    // TODO ipv6 addresses
259
    data = ( char * ) malloc( INET_ADDRSTRLEN );
443
    data = ( char * ) malloc( INET_ADDRSTRLEN );
260
    if( data ){
444
    if( data ){
261
        for( index = 0; index < ip_routes_count( & ip_netif->routes ); ++ index ){
445
        for( index = 0; index < ip_routes_count( & ip_netif->routes ); ++ index ){
262
            route = ip_routes_get_index( & ip_netif->routes, index );
446
            route = ip_routes_get_index( & ip_netif->routes, index );
263
            if( route ){
447
            if( route ){
264
                printf( "\tRouting %d:\n", index );
448
                printf( "\tRouting %d:\n", index );
265
                inet_ntop( AF_INET, ( uint8_t * ) & route->address.s_addr, data, INET_ADDRSTRLEN );
449
                inet_ntop( AF_INET, ( uint8_t * ) & route->address.s_addr, data, INET_ADDRSTRLEN );
266
                printf( "\t\taddress\t= %s\n", data );
450
                printf( "\t\taddress\t= %s\n", data );
267
                inet_ntop( AF_INET, ( uint8_t * ) & route->netmask.s_addr, data, INET_ADDRSTRLEN );
451
                inet_ntop( AF_INET, ( uint8_t * ) & route->netmask.s_addr, data, INET_ADDRSTRLEN );
268
                printf( "\t\tnetmask\t= %s\n", data );
452
                printf( "\t\tnetmask\t= %s\n", data );
269
                inet_ntop( AF_INET, ( uint8_t * ) & route->gateway.s_addr, data, INET_ADDRSTRLEN );
453
                inet_ntop( AF_INET, ( uint8_t * ) & route->gateway.s_addr, data, INET_ADDRSTRLEN );
270
                printf( "\t\tgateway\t= %s\n", data );
454
                printf( "\t\tgateway\t= %s\n", data );
271
            }
455
            }
272
        }
456
        }
273
        inet_ntop( AF_INET, ( uint8_t * ) & ip_netif->broadcast.s_addr, data, INET_ADDRSTRLEN );
457
        inet_ntop( AF_INET, ( uint8_t * ) & ip_netif->broadcast.s_addr, data, INET_ADDRSTRLEN );
274
        printf( "\t\tbroadcast\t= %s\n", data );
458
        printf( "\t\tbroadcast\t= %s\n", data );
275
        free( data );
459
        free( data );
276
    }
460
    }
277
    fibril_rwlock_write_unlock( & ip_globals.netifs_lock );
461
    fibril_rwlock_write_unlock( & ip_globals.netifs_lock );
278
    return EOK;
462
    return EOK;
279
}
463
}
280
 
464
 
281
int ip_netif_initialize( ip_netif_ref ip_netif ){
465
int ip_netif_initialize( ip_netif_ref ip_netif ){
282
    ERROR_DECLARE;
466
    ERROR_DECLARE;
283
 
467
 
284
    measured_string_t   names[] = {{ "IPV", 3 }, { "IP_CONFIG", 9 }, { "IP_ADDR", 7 }, { "IP_NETMASK", 10 }, { "IP_GATEWAY", 10 }, { "IP_BROADCAST", 12 }, { "ARP", 3 }, { "IP_ROUTING", 10 }};
468
    measured_string_t   names[] = {{ "IPV", 3 }, { "IP_CONFIG", 9 }, { "IP_ADDR", 7 }, { "IP_NETMASK", 10 }, { "IP_GATEWAY", 10 }, { "IP_BROADCAST", 12 }, { "ARP", 3 }, { "IP_ROUTING", 10 }};
285
    measured_string_ref configuration;
469
    measured_string_ref configuration;
286
    size_t              count = sizeof( names ) / sizeof( measured_string_t );
470
    size_t              count = sizeof( names ) / sizeof( measured_string_t );
287
    char *              data;
471
    char *              data;
288
    measured_string_t   address;
472
    measured_string_t   address;
289
    int                 index;
473
    int                 index;
290
    ip_route_ref        route;
474
    ip_route_ref        route;
291
    in_addr_t           gateway;
475
    in_addr_t           gateway;
292
 
476
 
293
    ip_netif->arp = NULL;
477
    ip_netif->arp = NULL;
294
    route = NULL;
478
    route = NULL;
295
    ip_netif->ipv = NET_DEFAULT_IPV;
479
    ip_netif->ipv = NET_DEFAULT_IPV;
296
    ip_netif->dhcp = false;
480
    ip_netif->dhcp = false;
297
    ip_netif->routing = NET_DEFAULT_IP_ROUTING;
481
    ip_netif->routing = NET_DEFAULT_IP_ROUTING;
298
    configuration = & names[ 0 ];
482
    configuration = & names[ 0 ];
299
    // get configuration
483
    // get configuration
300
    ERROR_PROPAGATE( net_get_device_conf_req( ip_globals.net_phone, ip_netif->device_id, & configuration, count, & data ));
484
    ERROR_PROPAGATE( net_get_device_conf_req( ip_globals.net_phone, ip_netif->device_id, & configuration, count, & data ));
301
    if( configuration ){
485
    if( configuration ){
302
        if( configuration[ 0 ].value ){
486
        if( configuration[ 0 ].value ){
303
            ip_netif->ipv = strtol( configuration[ 0 ].value, NULL, 0 );
487
            ip_netif->ipv = strtol( configuration[ 0 ].value, NULL, 0 );
304
        }
488
        }
305
        ip_netif->dhcp = ! str_lcmp( configuration[ 1 ].value, "dhcp", configuration[ 1 ].length );
489
        ip_netif->dhcp = ! str_lcmp( configuration[ 1 ].value, "dhcp", configuration[ 1 ].length );
306
        if( ip_netif->dhcp ){
490
        if( ip_netif->dhcp ){
307
            // TODO dhcp
491
            // TODO dhcp
308
            net_free_settings( configuration, data );
492
            net_free_settings( configuration, data );
309
            return ENOTSUP;
493
            return ENOTSUP;
310
        }else if( ip_netif->ipv == IPV4 ){
494
        }else if( ip_netif->ipv == IPV4 ){
311
            route = ( ip_route_ref ) malloc( sizeof( ip_route_t ));
495
            route = ( ip_route_ref ) malloc( sizeof( ip_route_t ));
312
            if( ! route ){
496
            if( ! route ){
313
                net_free_settings( configuration, data );
497
                net_free_settings( configuration, data );
314
                return ENOMEM;
498
                return ENOMEM;
315
            }
499
            }
316
            route->address.s_addr = 0;
500
            route->address.s_addr = 0;
317
            route->netmask.s_addr = 0;
501
            route->netmask.s_addr = 0;
318
            route->gateway.s_addr = 0;
502
            route->gateway.s_addr = 0;
319
            route->netif = ip_netif;
503
            route->netif = ip_netif;
320
            index = ip_routes_add( & ip_netif->routes, route );
504
            index = ip_routes_add( & ip_netif->routes, route );
321
            if( index < 0 ){
505
            if( index < 0 ){
322
                net_free_settings( configuration, data );
506
                net_free_settings( configuration, data );
323
                free( route );
507
                free( route );
324
                return index;
508
                return index;
325
            }
509
            }
326
            if( ERROR_OCCURRED( inet_pton( AF_INET, configuration[ 2 ].value, ( uint8_t * ) & route->address.s_addr ))
510
            if( ERROR_OCCURRED( inet_pton( AF_INET, configuration[ 2 ].value, ( uint8_t * ) & route->address.s_addr ))
327
            || ERROR_OCCURRED( inet_pton( AF_INET, configuration[ 3 ].value, ( uint8_t * ) & route->netmask.s_addr ))
511
            || ERROR_OCCURRED( inet_pton( AF_INET, configuration[ 3 ].value, ( uint8_t * ) & route->netmask.s_addr ))
328
            || ( inet_pton( AF_INET, configuration[ 4 ].value, ( uint8_t * ) & gateway.s_addr ) == EINVAL )
512
            || ( inet_pton( AF_INET, configuration[ 4 ].value, ( uint8_t * ) & gateway.s_addr ) == EINVAL )
329
            || ( inet_pton( AF_INET, configuration[ 5 ].value, ( uint8_t * ) & ip_netif->broadcast.s_addr ) == EINVAL )){
513
            || ( inet_pton( AF_INET, configuration[ 5 ].value, ( uint8_t * ) & ip_netif->broadcast.s_addr ) == EINVAL )){
330
                net_free_settings( configuration, data );
514
                net_free_settings( configuration, data );
331
                return EINVAL;
515
                return EINVAL;
332
            }
516
            }
333
        }else{
517
        }else{
334
            // TODO ipv6 in separate module
518
            // TODO ipv6 in separate module
335
            net_free_settings( configuration, data );
519
            net_free_settings( configuration, data );
336
            return ENOTSUP;
520
            return ENOTSUP;
337
        }
521
        }
338
        if( configuration[ 6 ].value ){
522
        if( configuration[ 6 ].value ){
339
            ip_netif->arp = get_running_module( & ip_globals.modules, configuration[ 6 ].value );
523
            ip_netif->arp = get_running_module( & ip_globals.modules, configuration[ 6 ].value );
340
            if( ! ip_netif->arp ){
524
            if( ! ip_netif->arp ){
341
                printf( "Failed to start the arp %s\n", configuration[ 6 ].value );
525
                printf( "Failed to start the arp %s\n", configuration[ 6 ].value );
342
                net_free_settings( configuration, data );
526
                net_free_settings( configuration, data );
343
                return EINVAL;
527
                return EINVAL;
344
            }
528
            }
345
        }
529
        }
346
        if( configuration[ 7 ].value ){
530
        if( configuration[ 7 ].value ){
347
            ip_netif->routing = ( configuration[ 7 ].value[ 0 ] == 'y' );
531
            ip_netif->routing = ( configuration[ 7 ].value[ 0 ] == 'y' );
348
        }
532
        }
349
        net_free_settings( configuration, data );
533
        net_free_settings( configuration, data );
350
    }
534
    }
351
    // binds the netif service which also initializes the device
535
    // binds the netif service which also initializes the device
352
    ip_netif->phone = nil_bind_service( ip_netif->service, ( ipcarg_t ) ip_netif->device_id, SERVICE_IP, ip_globals.client_connection );
536
    ip_netif->phone = nil_bind_service( ip_netif->service, ( ipcarg_t ) ip_netif->device_id, SERVICE_IP, ip_globals.client_connection );
353
    if( ip_netif->phone < 0 ){
537
    if( ip_netif->phone < 0 ){
354
        printf( "Failed to contact the nil service %d\n", ip_netif->service );
538
        printf( "Failed to contact the nil service %d\n", ip_netif->service );
355
        return ip_netif->phone;
539
        return ip_netif->phone;
356
    }
540
    }
357
    // has to be after the device netif module initialization
541
    // has to be after the device netif module initialization
358
    if( ip_netif->arp ){
542
    if( ip_netif->arp ){
359
        if( route ){
543
        if( route ){
360
            address.value = ( char * ) & route->address.s_addr;
544
            address.value = ( char * ) & route->address.s_addr;
361
            address.length = CONVERT_SIZE( in_addr_t, char, 1 );
545
            address.length = CONVERT_SIZE( in_addr_t, char, 1 );
362
            ERROR_PROPAGATE( arp_device_req( ip_netif->arp->phone, ip_netif->device_id, SERVICE_IP, ip_netif->service, & address ));
546
            ERROR_PROPAGATE( arp_device_req( ip_netif->arp->phone, ip_netif->device_id, SERVICE_IP, ip_netif->service, & address ));
363
        }else{
547
        }else{
364
            ip_netif->arp = 0;
548
            ip_netif->arp = 0;
365
        }
549
        }
366
    }
550
    }
367
    // get packet dimensions
551
    // get packet dimensions
368
    ERROR_PROPAGATE( nil_packet_size_req( ip_netif->phone, ip_netif->device_id, & ip_netif->addr_len, & ip_netif->prefix, & ip_netif->content, & ip_netif->suffix ));
552
    ERROR_PROPAGATE( nil_packet_size_req( ip_netif->phone, ip_netif->device_id, & ip_netif->addr_len, & ip_netif->prefix, & ip_netif->content, & ip_netif->suffix ));
369
    if( ip_netif->content < IP_MIN_CONTENT ){
553
    if( ip_netif->content < IP_MIN_CONTENT ){
370
        printf( "Maximum transmission unit %d bytes is too small, at least %d bytes are needed\n", ip_netif->content, IP_MIN_CONTENT );
554
        printf( "Maximum transmission unit %d bytes is too small, at least %d bytes are needed\n", ip_netif->content, IP_MIN_CONTENT );
371
        ip_netif->content = IP_MIN_CONTENT;
555
        ip_netif->content = IP_MIN_CONTENT;
372
    }
556
    }
373
    index = ip_netifs_add( & ip_globals.netifs, ip_netif->device_id, ip_netif );
557
    index = ip_netifs_add( & ip_globals.netifs, ip_netif->device_id, ip_netif );
374
    if( index < 0 ) return index;
558
    if( index < 0 ) return index;
375
    if( gateway.s_addr ){
559
    if( gateway.s_addr ){
376
        // the default gateway
560
        // the default gateway
377
        ip_globals.gateway.address.s_addr = 0;
561
        ip_globals.gateway.address.s_addr = 0;
378
        ip_globals.gateway.netmask.s_addr = 0;
562
        ip_globals.gateway.netmask.s_addr = 0;
379
        ip_globals.gateway.gateway.s_addr = gateway.s_addr;
563
        ip_globals.gateway.gateway.s_addr = gateway.s_addr;
380
        ip_globals.gateway.netif = ip_netif;
564
        ip_globals.gateway.netif = ip_netif;
381
    }
565
    }
382
    return EOK;
566
    return EOK;
383
}
567
}
384
 
568
 
385
int ip_mtu_changed_message( device_id_t device_id, size_t mtu ){
569
int ip_mtu_changed_message( device_id_t device_id, size_t mtu ){
386
    ip_netif_ref    netif;
570
    ip_netif_ref    netif;
387
 
571
 
388
    fibril_rwlock_write_lock( & ip_globals.netifs_lock );
572
    fibril_rwlock_write_lock( & ip_globals.netifs_lock );
389
    netif = ip_netifs_find( & ip_globals.netifs, device_id );
573
    netif = ip_netifs_find( & ip_globals.netifs, device_id );
390
    if( ! netif ){
574
    if( ! netif ){
391
        fibril_rwlock_write_unlock( & ip_globals.netifs_lock );
575
        fibril_rwlock_write_unlock( & ip_globals.netifs_lock );
392
        return ENOENT;
576
        return ENOENT;
393
    }
577
    }
394
    netif->content = mtu;
578
    netif->content = mtu;
395
    printf( "ip - device %d changed mtu to %d\n\n", device_id, mtu );
579
    printf( "ip - device %d changed mtu to %d\n\n", device_id, mtu );
396
    fibril_rwlock_write_unlock( & ip_globals.netifs_lock );
580
    fibril_rwlock_write_unlock( & ip_globals.netifs_lock );
397
    return EOK;
581
    return EOK;
398
}
582
}
399
 
583
 
400
int ip_device_state_message( device_id_t device_id, device_state_t state ){
584
int ip_device_state_message( device_id_t device_id, device_state_t state ){
401
    ip_netif_ref    netif;
585
    ip_netif_ref    netif;
402
 
586
 
403
    fibril_rwlock_write_lock( & ip_globals.netifs_lock );
587
    fibril_rwlock_write_lock( & ip_globals.netifs_lock );
404
    // find the device
588
    // find the device
405
    netif = ip_netifs_find( & ip_globals.netifs, device_id );
589
    netif = ip_netifs_find( & ip_globals.netifs, device_id );
406
    if( ! netif ){
590
    if( ! netif ){
407
        fibril_rwlock_write_unlock( & ip_globals.netifs_lock );
591
        fibril_rwlock_write_unlock( & ip_globals.netifs_lock );
408
        return ENOENT;
592
        return ENOENT;
409
    }
593
    }
410
    netif->state = state;
594
    netif->state = state;
411
    printf( "ip - device %d changed state to %d\n\n", device_id, state );
595
    printf( "ip - device %d changed state to %d\n\n", device_id, state );
412
    fibril_rwlock_write_unlock( & ip_globals.netifs_lock );
596
    fibril_rwlock_write_unlock( & ip_globals.netifs_lock );
413
    return EOK;
597
    return EOK;
414
}
598
}
415
 
599
 
416
int ip_connect_module( services_t service ){
600
int ip_connect_module( services_t service ){
417
    return EOK;
601
    return EOK;
418
}
602
}
419
 
603
 
420
int ip_bind_service( services_t service, int protocol, services_t me, async_client_conn_t receiver, tl_received_msg_t received_msg ){
604
int ip_bind_service( services_t service, int protocol, services_t me, async_client_conn_t receiver, tl_received_msg_t received_msg ){
421
    return ip_register( protocol, me, 0, received_msg );
605
    return ip_register( protocol, me, 0, received_msg );
422
}
606
}
423
 
607
 
424
int ip_register( int protocol, services_t service, int phone, tl_received_msg_t received_msg ){
608
int ip_register( int protocol, services_t service, int phone, tl_received_msg_t received_msg ){
425
    ip_proto_ref    proto;
609
    ip_proto_ref    proto;
426
    int             index;
610
    int             index;
427
 
611
 
428
    if( !( protocol && service && (( phone > 0 ) || ( received_msg )))) return EINVAL;
612
    if( !( protocol && service && (( phone > 0 ) || ( received_msg )))) return EINVAL;
429
    proto = ( ip_proto_ref ) malloc( sizeof( ip_protos_t ));
613
    proto = ( ip_proto_ref ) malloc( sizeof( ip_protos_t ));
430
    if( ! proto ) return ENOMEM;
614
    if( ! proto ) return ENOMEM;
431
    proto->protocol = protocol;
615
    proto->protocol = protocol;
432
    proto->service = service;
616
    proto->service = service;
433
    proto->phone = phone;
617
    proto->phone = phone;
434
    proto->received_msg = received_msg;
618
    proto->received_msg = received_msg;
435
    fibril_rwlock_write_lock( & ip_globals.protos_lock );
619
    fibril_rwlock_write_lock( & ip_globals.protos_lock );
436
    index = ip_protos_add( & ip_globals.protos, proto->protocol, proto );
620
    index = ip_protos_add( & ip_globals.protos, proto->protocol, proto );
437
    if( index < 0 ){
621
    if( index < 0 ){
438
        fibril_rwlock_write_unlock( & ip_globals.protos_lock );
622
        fibril_rwlock_write_unlock( & ip_globals.protos_lock );
439
        free( proto );
623
        free( proto );
440
        return index;
624
        return index;
441
    }
625
    }
442
    printf( "New protocol registered:\n\tprotocol\t= %d\n\tphone\t= %d\n", proto->protocol, proto->phone );
626
    printf( "New protocol registered:\n\tprotocol\t= %d\n\tphone\t= %d\n", proto->protocol, proto->phone );
443
    fibril_rwlock_write_unlock( & ip_globals.protos_lock );
627
    fibril_rwlock_write_unlock( & ip_globals.protos_lock );
444
    return EOK;
628
    return EOK;
445
}
629
}
446
 
630
 
447
int ip_send_msg( int il_phone, device_id_t device_id, packet_t packet, services_t sender, services_t error ){
631
int ip_send_msg( int il_phone, device_id_t device_id, packet_t packet, services_t sender, services_t error ){
448
    ERROR_DECLARE;
632
    ERROR_DECLARE;
449
 
633
 
450
    int                 addrlen;
634
    int                 addrlen;
451
    ip_netif_ref        netif;
635
    ip_netif_ref        netif;
452
    ip_route_ref        route;
636
    ip_route_ref        route;
453
    struct sockaddr *       addr;
637
    struct sockaddr *       addr;
454
    struct sockaddr_in *    address_in;
638
    struct sockaddr_in *    address_in;
455
//  struct sockaddr_in6 *   address_in6;
639
//  struct sockaddr_in6 *   address_in6;
456
    in_addr_t *         dest;
640
    in_addr_t *         dest;
457
    in_addr_t *         src;
641
    in_addr_t *         src;
458
    int                 phone;
642
    int                 phone;
459
 
643
 
460
    // addresses in the host byte order
644
    // addresses in the host byte order
461
    // should be the next hop address or the target destination address
645
    // should be the next hop address or the target destination address
462
    addrlen = packet_get_addr( packet, NULL, ( uint8_t ** ) & addr );
646
    addrlen = packet_get_addr( packet, NULL, ( uint8_t ** ) & addr );
463
    if( addrlen < 0 ){
647
    if( addrlen < 0 ){
464
        return ip_release_and_return( packet, addrlen );
648
        return ip_release_and_return( packet, addrlen );
465
    }
649
    }
466
    if(( size_t ) addrlen < sizeof( struct sockaddr )){
650
    if(( size_t ) addrlen < sizeof( struct sockaddr )){
467
        return ip_release_and_return( packet, EINVAL );
651
        return ip_release_and_return( packet, EINVAL );
468
    }
652
    }
469
    switch( addr->sa_family ){
653
    switch( addr->sa_family ){
470
        case AF_INET:
654
        case AF_INET:
471
            if( addrlen != sizeof( struct sockaddr_in )){
655
            if( addrlen != sizeof( struct sockaddr_in )){
472
                return ip_release_and_return( packet, EINVAL );
656
                return ip_release_and_return( packet, EINVAL );
473
            }
657
            }
474
            address_in = ( struct sockaddr_in * ) addr;
658
            address_in = ( struct sockaddr_in * ) addr;
475
            dest = & address_in->sin_addr;
659
            dest = & address_in->sin_addr;
476
            break;
660
            break;
477
        // TODO IPv6
661
        // TODO IPv6
478
/*      case AF_INET6:
662
/*      case AF_INET6:
479
            if( addrlen != sizeof( struct sockaddr_in6 )) return EINVAL;
663
            if( addrlen != sizeof( struct sockaddr_in6 )) return EINVAL;
480
            address_in6 = ( struct sockaddr_in6 * ) dest;
664
            address_in6 = ( struct sockaddr_in6 * ) dest;
481
            address_in6.sin6_addr.s6_addr;
665
            address_in6.sin6_addr.s6_addr;
482
*/      default:
666
*/      default:
483
            return ip_release_and_return( packet, EAFNOSUPPORT );
667
            return ip_release_and_return( packet, EAFNOSUPPORT );
484
    }
668
    }
485
    fibril_rwlock_read_lock( & ip_globals.netifs_lock );
669
    fibril_rwlock_read_lock( & ip_globals.netifs_lock );
486
    // device specified?
670
    // device specified?
487
    if( device_id > 0 ){
671
    if( device_id > 0 ){
488
        netif = ip_netifs_find( & ip_globals.netifs, device_id );
672
        netif = ip_netifs_find( & ip_globals.netifs, device_id );
489
        route = ip_netif_find_route( netif, * dest );
673
        route = ip_netif_find_route( netif, * dest );
490
        if( netif && ( ! route ) && ( ip_globals.gateway.netif == netif )){
674
        if( netif && ( ! route ) && ( ip_globals.gateway.netif == netif )){
491
            route = & ip_globals.gateway;
675
            route = & ip_globals.gateway;
492
        }
676
        }
493
    }else{
677
    }else{
494
        route = ip_find_route( * dest );
678
        route = ip_find_route( * dest );
495
        netif = route ? route->netif : NULL;
679
        netif = route ? route->netif : NULL;
496
    }
680
    }
497
    if( !( netif && route )){
681
    if( !( netif && route )){
498
        fibril_rwlock_read_unlock( & ip_globals.netifs_lock );
682
        fibril_rwlock_read_unlock( & ip_globals.netifs_lock );
499
        phone = ip_prepare_icmp_and_get_phone( error, packet, NULL );
683
        phone = ip_prepare_icmp_and_get_phone( error, packet, NULL );
500
        if( phone >= 0 ){
684
        if( phone >= 0 ){
501
            // unreachable ICMP if no routing
685
            // unreachable ICMP if no routing
502
            icmp_destination_unreachable_msg( phone, ICMP_NET_UNREACH, 0, packet );
686
            icmp_destination_unreachable_msg( phone, ICMP_NET_UNREACH, 0, packet );
503
        }
687
        }
504
        return ENOENT;
688
        return ENOENT;
505
    }
689
    }
506
    if( error ){
690
    if( error ){
507
        // do not send for broadcast, anycast packets or network broadcast
691
        // do not send for broadcast, anycast packets or network broadcast
508
        if(( ! dest->s_addr )
692
        if(( ! dest->s_addr )
509
        || ( !( ~ dest->s_addr ))
693
        || ( !( ~ dest->s_addr ))
510
        || ( !( ~(( dest->s_addr & ( ~ route->netmask.s_addr )) | route->netmask.s_addr )))
694
        || ( !( ~(( dest->s_addr & ( ~ route->netmask.s_addr )) | route->netmask.s_addr )))
511
        || ( !( dest->s_addr & ( ~ route->netmask.s_addr )))){
695
        || ( !( dest->s_addr & ( ~ route->netmask.s_addr )))){
512
            return ip_release_and_return( packet, EINVAL );
696
            return ip_release_and_return( packet, EINVAL );
513
        }
697
        }
514
    }
698
    }
515
    if( route->address.s_addr == dest->s_addr ){
699
    if( route->address.s_addr == dest->s_addr ){
516
        // find the loopback device to deliver
700
        // find the loopback device to deliver
517
        dest->s_addr = IPV4_LOCALHOST_ADDRESS;
701
        dest->s_addr = IPV4_LOCALHOST_ADDRESS;
518
        route = ip_find_route( * dest );
702
        route = ip_find_route( * dest );
519
        netif = route ? route->netif : NULL;
703
        netif = route ? route->netif : NULL;
520
        if( !( netif && route )){
704
        if( !( netif && route )){
521
            fibril_rwlock_read_unlock( & ip_globals.netifs_lock );
705
            fibril_rwlock_read_unlock( & ip_globals.netifs_lock );
522
            phone = ip_prepare_icmp_and_get_phone( error, packet, NULL );
706
            phone = ip_prepare_icmp_and_get_phone( error, packet, NULL );
523
            if( phone >= 0 ){
707
            if( phone >= 0 ){
524
                // unreachable ICMP if no routing
708
                // unreachable ICMP if no routing
525
                icmp_destination_unreachable_msg( phone, ICMP_HOST_UNREACH, 0, packet );
709
                icmp_destination_unreachable_msg( phone, ICMP_HOST_UNREACH, 0, packet );
526
            }
710
            }
527
            return ENOENT;
711
            return ENOENT;
528
        }
712
        }
529
    }
713
    }
530
    src = ip_netif_address( netif );
714
    src = ip_netif_address( netif );
531
    if( ! src ){
715
    if( ! src ){
532
        fibril_rwlock_read_unlock( & ip_globals.netifs_lock );
716
        fibril_rwlock_read_unlock( & ip_globals.netifs_lock );
533
        return ip_release_and_return( packet, ENOENT );
717
        return ip_release_and_return( packet, ENOENT );
534
    }
718
    }
535
    ERROR_CODE = ip_send_route( packet, netif, route, src, * dest, error );
719
    ERROR_CODE = ip_send_route( packet, netif, route, src, * dest, error );
536
    fibril_rwlock_read_unlock( & ip_globals.netifs_lock );
720
    fibril_rwlock_read_unlock( & ip_globals.netifs_lock );
537
    return ERROR_CODE;
721
    return ERROR_CODE;
538
}
722
}
539
 
723
 
540
in_addr_t * ip_netif_address( ip_netif_ref netif ){
724
in_addr_t * ip_netif_address( ip_netif_ref netif ){
541
    ip_route_ref    route;
725
    ip_route_ref    route;
542
 
726
 
543
    route = ip_routes_get_index( & netif->routes, 0 );
727
    route = ip_routes_get_index( & netif->routes, 0 );
544
    return route ? & route->address : NULL;
728
    return route ? & route->address : NULL;
545
}
729
}
546
 
730
 
547
int ip_send_route( packet_t packet, ip_netif_ref netif, ip_route_ref route, in_addr_t * src, in_addr_t dest, services_t error ){
731
int ip_send_route( packet_t packet, ip_netif_ref netif, ip_route_ref route, in_addr_t * src, in_addr_t dest, services_t error ){
548
    ERROR_DECLARE;
732
    ERROR_DECLARE;
549
 
733
 
550
    measured_string_t   destination;
734
    measured_string_t   destination;
551
    measured_string_ref translation;
735
    measured_string_ref translation;
552
    char *              data;
736
    char *              data;
553
    int                 phone;
737
    int                 phone;
554
 
738
 
555
    // get destination hardware address
739
    // get destination hardware address
556
    if( netif->arp && ( route->address.s_addr != dest.s_addr )){
740
    if( netif->arp && ( route->address.s_addr != dest.s_addr )){
557
        destination.value = route->gateway.s_addr ? ( char * ) & route->gateway.s_addr : ( char * ) & dest.s_addr;
741
        destination.value = route->gateway.s_addr ? ( char * ) & route->gateway.s_addr : ( char * ) & dest.s_addr;
558
        destination.length = CONVERT_SIZE( dest.s_addr, char, 1 );
742
        destination.length = CONVERT_SIZE( dest.s_addr, char, 1 );
559
        if( ERROR_OCCURRED( arp_translate_req( netif->arp->phone, netif->device_id, SERVICE_IP, & destination, & translation, & data ))){
743
        if( ERROR_OCCURRED( arp_translate_req( netif->arp->phone, netif->device_id, SERVICE_IP, & destination, & translation, & data ))){
560
//          sleep( 1 );
744
//          sleep( 1 );
561
//          ERROR_PROPAGATE( arp_translate_req( netif->arp->phone, netif->device_id, SERVICE_IP, & destination, & translation, & data ));
745
//          ERROR_PROPAGATE( arp_translate_req( netif->arp->phone, netif->device_id, SERVICE_IP, & destination, & translation, & data ));
562
            pq_release( ip_globals.net_phone, packet_get_id( packet ));
746
            pq_release( ip_globals.net_phone, packet_get_id( packet ));
563
            return ERROR_CODE;
747
            return ERROR_CODE;
564
        }
748
        }
565
        if( !( translation && translation->value )){
749
        if( !( translation && translation->value )){
566
            if( translation ){
750
            if( translation ){
567
                free( translation );
751
                free( translation );
568
                free( data );
752
                free( data );
569
            }
753
            }
570
            phone = ip_prepare_icmp_and_get_phone( error, packet, NULL );
754
            phone = ip_prepare_icmp_and_get_phone( error, packet, NULL );
571
            if( phone >= 0 ){
755
            if( phone >= 0 ){
572
                // unreachable ICMP if no routing
756
                // unreachable ICMP if no routing
573
                icmp_destination_unreachable_msg( phone, ICMP_HOST_UNREACH, 0, packet );
757
                icmp_destination_unreachable_msg( phone, ICMP_HOST_UNREACH, 0, packet );
574
            }
758
            }
575
            return EINVAL;
759
            return EINVAL;
576
        }
760
        }
577
    }else translation = NULL;
761
    }else translation = NULL;
578
    if( ERROR_OCCURRED( ip_prepare_packet( src, dest, packet, translation ))){
762
    if( ERROR_OCCURRED( ip_prepare_packet( src, dest, packet, translation ))){
579
        pq_release( ip_globals.net_phone, packet_get_id( packet ));
763
        pq_release( ip_globals.net_phone, packet_get_id( packet ));
580
    }else{
764
    }else{
581
        packet = ip_split_packet( packet, netif->prefix, netif->content, netif->suffix, netif->addr_len, error );
765
        packet = ip_split_packet( packet, netif->prefix, netif->content, netif->suffix, netif->addr_len, error );
582
        if( packet ){
766
        if( packet ){
583
            nil_send_msg( netif->phone, netif->device_id, packet, SERVICE_IP );
767
            nil_send_msg( netif->phone, netif->device_id, packet, SERVICE_IP );
584
        }
768
        }
585
    }
769
    }
586
    if( translation ){
770
    if( translation ){
587
        free( translation );
771
        free( translation );
588
        free( data );
772
        free( data );
589
    }
773
    }
590
    return ERROR_CODE;
774
    return ERROR_CODE;
591
}
775
}
592
 
776
 
593
int ip_prepare_packet( in_addr_t * source, in_addr_t dest, packet_t packet, measured_string_ref destination ){
777
int ip_prepare_packet( in_addr_t * source, in_addr_t dest, packet_t packet, measured_string_ref destination ){
594
    ERROR_DECLARE;
778
    ERROR_DECLARE;
595
 
779
 
596
    size_t              length;
780
    size_t              length;
597
    ip_header_ref       header;
781
    ip_header_ref       header;
598
    ip_header_ref       last_header;
782
    ip_header_ref       last_header;
599
    ip_header_ref       middle_header;
783
    ip_header_ref       middle_header;
600
    packet_t            next;
784
    packet_t            next;
601
 
785
 
602
    length = packet_get_data_length( packet );
786
    length = packet_get_data_length( packet );
603
    if(( length < sizeof( ip_header_t )) || ( length > IP_MAX_CONTENT )) return EINVAL;
787
    if(( length < sizeof( ip_header_t )) || ( length > IP_MAX_CONTENT )) return EINVAL;
604
    header = ( ip_header_ref ) packet_get_data( packet );
788
    header = ( ip_header_ref ) packet_get_data( packet );
605
    if( destination ){
789
    if( destination ){
606
        ERROR_PROPAGATE( packet_set_addr( packet, NULL, ( uint8_t * ) destination->value, CONVERT_SIZE( char, uint8_t, destination->length )));
790
        ERROR_PROPAGATE( packet_set_addr( packet, NULL, ( uint8_t * ) destination->value, CONVERT_SIZE( char, uint8_t, destination->length )));
607
    }else{
791
    }else{
608
        ERROR_PROPAGATE( packet_set_addr( packet, NULL, NULL, 0 ));
792
        ERROR_PROPAGATE( packet_set_addr( packet, NULL, NULL, 0 ));
609
    }
793
    }
610
    header->version = IPV4;
794
    header->version = IPV4;
611
    header->fragment_offset_high = 0;
795
    header->fragment_offset_high = 0;
612
    header->fragment_offset_low = 0;
796
    header->fragment_offset_low = 0;
613
    header->header_checksum = 0;
797
    header->header_checksum = 0;
614
    if( source ) header->source_address = source->s_addr;
798
    if( source ) header->source_address = source->s_addr;
615
    header->destination_address = dest.s_addr;
799
    header->destination_address = dest.s_addr;
616
    fibril_rwlock_write_lock( & ip_globals.lock );
800
    fibril_rwlock_write_lock( & ip_globals.lock );
617
    ++ ip_globals.packet_counter;
801
    ++ ip_globals.packet_counter;
618
    header->identification = htons( ip_globals.packet_counter );
802
    header->identification = htons( ip_globals.packet_counter );
619
    fibril_rwlock_write_unlock( & ip_globals.lock );
803
    fibril_rwlock_write_unlock( & ip_globals.lock );
620
//  length = packet_get_data_length( packet );
804
//  length = packet_get_data_length( packet );
621
    if( pq_next( packet )){
805
    if( pq_next( packet )){
622
        last_header = ( ip_header_ref ) malloc( IP_HEADER_LENGTH( header ));
806
        last_header = ( ip_header_ref ) malloc( IP_HEADER_LENGTH( header ));
623
        if( ! last_header ) return ENOMEM;
807
        if( ! last_header ) return ENOMEM;
624
        ip_create_last_header( last_header, header );
808
        ip_create_last_header( last_header, header );
625
        next = pq_next( packet );
809
        next = pq_next( packet );
626
        while( pq_next( next )){
810
        while( pq_next( next )){
627
            middle_header = ( ip_header_ref ) packet_prefix( next, IP_HEADER_LENGTH( last_header ));
811
            middle_header = ( ip_header_ref ) packet_prefix( next, IP_HEADER_LENGTH( last_header ));
628
            if( ! middle_header ) return ENOMEM;
812
            if( ! middle_header ) return ENOMEM;
629
            memcpy( middle_header, last_header, IP_HEADER_LENGTH( last_header ));
813
            memcpy( middle_header, last_header, IP_HEADER_LENGTH( last_header ));
630
            header->flags |= IPFLAG_MORE_FRAGMENTS;
814
            header->flags |= IPFLAG_MORE_FRAGMENTS;
631
            middle_header->total_length = htons( packet_get_data_length( next ));
815
            middle_header->total_length = htons( packet_get_data_length( next ));
632
            middle_header->fragment_offset_high = IP_COMPUTE_FRAGMENT_OFFSET_HIGH( length );
816
            middle_header->fragment_offset_high = IP_COMPUTE_FRAGMENT_OFFSET_HIGH( length );
633
            middle_header->fragment_offset_low = IP_COMPUTE_FRAGMENT_OFFSET_LOW( length );
817
            middle_header->fragment_offset_low = IP_COMPUTE_FRAGMENT_OFFSET_LOW( length );
634
            middle_header->header_checksum = IP_HEADER_CHECKSUM( middle_header );
818
            middle_header->header_checksum = IP_HEADER_CHECKSUM( middle_header );
635
            if( destination ){
819
            if( destination ){
636
                ERROR_PROPAGATE( packet_set_addr( next, NULL, ( uint8_t * ) destination->value, CONVERT_SIZE( char, uint8_t, destination->length )));
820
                ERROR_PROPAGATE( packet_set_addr( next, NULL, ( uint8_t * ) destination->value, CONVERT_SIZE( char, uint8_t, destination->length )));
637
            }
821
            }
638
            length += packet_get_data_length( next );
822
            length += packet_get_data_length( next );
639
            next = pq_next( next );
823
            next = pq_next( next );
640
        }
824
        }
641
        middle_header = ( ip_header_ref ) packet_prefix( next, IP_HEADER_LENGTH( last_header ));
825
        middle_header = ( ip_header_ref ) packet_prefix( next, IP_HEADER_LENGTH( last_header ));
642
        if( ! middle_header ) return ENOMEM;
826
        if( ! middle_header ) return ENOMEM;
643
        memcpy( middle_header, last_header, IP_HEADER_LENGTH( last_header ));
827
        memcpy( middle_header, last_header, IP_HEADER_LENGTH( last_header ));
644
        middle_header->total_length = htons( packet_get_data_length( next ));
828
        middle_header->total_length = htons( packet_get_data_length( next ));
645
        middle_header->fragment_offset_high = IP_COMPUTE_FRAGMENT_OFFSET_HIGH( length );
829
        middle_header->fragment_offset_high = IP_COMPUTE_FRAGMENT_OFFSET_HIGH( length );
646
        middle_header->fragment_offset_low = IP_COMPUTE_FRAGMENT_OFFSET_LOW( length );
830
        middle_header->fragment_offset_low = IP_COMPUTE_FRAGMENT_OFFSET_LOW( length );
647
        middle_header->header_checksum = IP_HEADER_CHECKSUM( middle_header );
831
        middle_header->header_checksum = IP_HEADER_CHECKSUM( middle_header );
648
        if( destination ){
832
        if( destination ){
649
            ERROR_PROPAGATE( packet_set_addr( next, NULL, ( uint8_t * ) destination->value, CONVERT_SIZE( char, uint8_t, destination->length )));
833
            ERROR_PROPAGATE( packet_set_addr( next, NULL, ( uint8_t * ) destination->value, CONVERT_SIZE( char, uint8_t, destination->length )));
650
        }
834
        }
651
        length += packet_get_data_length( next );
835
        length += packet_get_data_length( next );
652
        free( last_header );
836
        free( last_header );
653
        header->flags |= IPFLAG_MORE_FRAGMENTS;
837
        header->flags |= IPFLAG_MORE_FRAGMENTS;
654
    }
838
    }
655
    header->total_length = htons( length );
839
    header->total_length = htons( length );
656
    // unnecessary for all protocols
840
    // unnecessary for all protocols
657
    header->header_checksum = IP_HEADER_CHECKSUM( header );
841
    header->header_checksum = IP_HEADER_CHECKSUM( header );
658
    return EOK;
842
    return EOK;
659
}
843
}
660
 
844
 
661
int ip_message( ipc_callid_t callid, ipc_call_t * call, ipc_call_t * answer, int * answer_count ){
845
int ip_message( ipc_callid_t callid, ipc_call_t * call, ipc_call_t * answer, int * answer_count ){
662
    ERROR_DECLARE;
846
    ERROR_DECLARE;
663
 
847
 
664
    packet_t                packet;
848
    packet_t                packet;
665
    struct sockaddr *       addr;
849
    struct sockaddr *       addr;
666
    size_t                  addrlen;
850
    size_t                  addrlen;
667
    ip_pseudo_header_ref    header;
851
    ip_pseudo_header_ref    header;
668
    size_t                  headerlen;
852
    size_t                  headerlen;
669
 
853
 
670
    * answer_count = 0;
854
    * answer_count = 0;
671
    switch( IPC_GET_METHOD( * call )){
855
    switch( IPC_GET_METHOD( * call )){
672
        case IPC_M_PHONE_HUNGUP:
856
        case IPC_M_PHONE_HUNGUP:
673
            return EOK;
857
            return EOK;
674
        case NET_IL_DEVICE:
858
        case NET_IL_DEVICE:
675
            return ip_device_req( 0, IPC_GET_DEVICE( call ), IPC_GET_SERVICE( call ));
859
            return ip_device_req( 0, IPC_GET_DEVICE( call ), IPC_GET_SERVICE( call ));
676
        case IPC_M_CONNECT_TO_ME:
860
        case IPC_M_CONNECT_TO_ME:
677
            return ip_register( IL_GET_PROTO( call ), IL_GET_SERVICE( call ), IPC_GET_PHONE( call ), NULL );
861
            return ip_register( IL_GET_PROTO( call ), IL_GET_SERVICE( call ), IPC_GET_PHONE( call ), NULL );
678
        case NET_IL_SEND:
862
        case NET_IL_SEND:
679
            ERROR_PROPAGATE( packet_translate( ip_globals.net_phone, & packet, IPC_GET_PACKET( call )));
863
            ERROR_PROPAGATE( packet_translate( ip_globals.net_phone, & packet, IPC_GET_PACKET( call )));
680
            return ip_send_msg( 0, IPC_GET_DEVICE( call ), packet, 0, IPC_GET_ERROR( call ));
864
            return ip_send_msg( 0, IPC_GET_DEVICE( call ), packet, 0, IPC_GET_ERROR( call ));
681
        case NET_IL_DEVICE_STATE:
865
        case NET_IL_DEVICE_STATE:
682
            return ip_device_state_message( IPC_GET_DEVICE( call ), IPC_GET_STATE( call ));
866
            return ip_device_state_message( IPC_GET_DEVICE( call ), IPC_GET_STATE( call ));
683
        case NET_IL_RECEIVED:
867
        case NET_IL_RECEIVED:
684
            ERROR_PROPAGATE( packet_translate( ip_globals.net_phone, & packet, IPC_GET_PACKET( call )));
868
            ERROR_PROPAGATE( packet_translate( ip_globals.net_phone, & packet, IPC_GET_PACKET( call )));
685
            return ip_receive_message( IPC_GET_DEVICE( call ), packet );
869
            return ip_receive_message( IPC_GET_DEVICE( call ), packet );
686
        case NET_IP_RECEIVED_ERROR:
870
        case NET_IP_RECEIVED_ERROR:
687
            ERROR_PROPAGATE( packet_translate( ip_globals.net_phone, & packet, IPC_GET_PACKET( call )));
871
            ERROR_PROPAGATE( packet_translate( ip_globals.net_phone, & packet, IPC_GET_PACKET( call )));
688
            return ip_received_error_msg( 0, IPC_GET_DEVICE( call ), packet, IPC_GET_TARGET( call ), IPC_GET_ERROR( call ));
872
            return ip_received_error_msg( 0, IPC_GET_DEVICE( call ), packet, IPC_GET_TARGET( call ), IPC_GET_ERROR( call ));
689
        case NET_IP_ADD_ROUTE:
873
        case NET_IP_ADD_ROUTE:
690
            return ip_add_route_req( 0, IPC_GET_DEVICE( call ), IP_GET_ADDRESS( call ), IP_GET_NETMASK( call ), IP_GET_GATEWAY( call ));
874
            return ip_add_route_req( 0, IPC_GET_DEVICE( call ), IP_GET_ADDRESS( call ), IP_GET_NETMASK( call ), IP_GET_GATEWAY( call ));
691
        case NET_IP_SET_GATEWAY:
875
        case NET_IP_SET_GATEWAY:
692
            return ip_set_gateway_req( 0, IPC_GET_DEVICE( call ), IP_GET_GATEWAY( call ));
876
            return ip_set_gateway_req( 0, IPC_GET_DEVICE( call ), IP_GET_GATEWAY( call ));
693
        case NET_IP_GET_ROUTE:
877
        case NET_IP_GET_ROUTE:
694
            ERROR_PROPAGATE( data_receive(( void ** ) & addr, & addrlen ));
878
            ERROR_PROPAGATE( data_receive(( void ** ) & addr, & addrlen ));
695
            ERROR_PROPAGATE( ip_get_route_req( 0, IP_GET_PROTOCOL( call ), addr, ( socklen_t ) addrlen, IPC_SET_DEVICE( answer ), & header, & headerlen ));
879
            ERROR_PROPAGATE( ip_get_route_req( 0, IP_GET_PROTOCOL( call ), addr, ( socklen_t ) addrlen, IPC_SET_DEVICE( answer ), & header, & headerlen ));
696
            * IP_SET_HEADERLEN( answer ) = headerlen;
880
            * IP_SET_HEADERLEN( answer ) = headerlen;
697
            * answer_count = 2;
881
            * answer_count = 2;
698
            if( ! ERROR_OCCURRED( data_reply( & headerlen, sizeof( headerlen )))){
882
            if( ! ERROR_OCCURRED( data_reply( & headerlen, sizeof( headerlen )))){
699
                ERROR_CODE = data_reply( header, headerlen );
883
                ERROR_CODE = data_reply( header, headerlen );
700
            }
884
            }
701
            free( header );
885
            free( header );
702
            return ERROR_CODE;
886
            return ERROR_CODE;
703
        case NET_IL_PACKET_SPACE:
887
        case NET_IL_PACKET_SPACE:
704
            ERROR_PROPAGATE( ip_packet_size_req( 0, IPC_GET_DEVICE( call ), IPC_SET_ADDR( answer ), IPC_SET_PREFIX( answer ), IPC_SET_CONTENT( answer ), IPC_SET_SUFFIX( answer )));
888
            ERROR_PROPAGATE( ip_packet_size_req( 0, IPC_GET_DEVICE( call ), IPC_SET_ADDR( answer ), IPC_SET_PREFIX( answer ), IPC_SET_CONTENT( answer ), IPC_SET_SUFFIX( answer )));
705
            * answer_count = 3;
889
            * answer_count = 3;
706
            return EOK;
890
            return EOK;
707
        case NET_IL_MTU_CHANGED:
891
        case NET_IL_MTU_CHANGED:
708
            return ip_mtu_changed_message( IPC_GET_DEVICE( call ), IPC_GET_MTU( call ));
892
            return ip_mtu_changed_message( IPC_GET_DEVICE( call ), IPC_GET_MTU( call ));
709
    }
893
    }
710
    return ENOTSUP;
894
    return ENOTSUP;
711
}
895
}
712
 
896
 
713
int ip_packet_size_req( int ip_phone, device_id_t device_id, size_t * addr_len, size_t * prefix, size_t * content, size_t * suffix ){
897
int ip_packet_size_req( int ip_phone, device_id_t device_id, size_t * addr_len, size_t * prefix, size_t * content, size_t * suffix ){
714
    ip_netif_ref    netif;
898
    ip_netif_ref    netif;
715
    int             index;
899
    int             index;
716
 
900
 
717
    if( !( addr_len && prefix && content && suffix )) return EBADMEM;
901
    if( !( addr_len && prefix && content && suffix )) return EBADMEM;
718
    * content = IP_MAX_CONTENT - IP_PREFIX;
902
    * content = IP_MAX_CONTENT - IP_PREFIX;
719
    fibril_rwlock_read_lock( & ip_globals.netifs_lock );
903
    fibril_rwlock_read_lock( & ip_globals.netifs_lock );
720
    if( device_id < 0 ){
904
    if( device_id < 0 ){
721
        * addr_len = IP_ADDR;
905
        * addr_len = IP_ADDR;
722
        * prefix = 0;
906
        * prefix = 0;
723
        * suffix = 0;
907
        * suffix = 0;
724
        for( index = ip_netifs_count( & ip_globals.netifs ) - 1; index >= 0; -- index ){
908
        for( index = ip_netifs_count( & ip_globals.netifs ) - 1; index >= 0; -- index ){
725
            netif = ip_netifs_get_index( & ip_globals.netifs, index );
909
            netif = ip_netifs_get_index( & ip_globals.netifs, index );
726
            if( netif ){
910
            if( netif ){
727
                if( netif->addr_len > * addr_len ) * addr_len = netif->addr_len;
911
                if( netif->addr_len > * addr_len ) * addr_len = netif->addr_len;
728
                if( netif->prefix > * prefix ) * prefix = netif->prefix;
912
                if( netif->prefix > * prefix ) * prefix = netif->prefix;
729
                if( netif->suffix > * suffix ) * suffix = netif->suffix;
913
                if( netif->suffix > * suffix ) * suffix = netif->suffix;
730
            }
914
            }
731
        }
915
        }
732
        * prefix = * prefix + IP_PREFIX;
916
        * prefix = * prefix + IP_PREFIX;
733
        * suffix = * suffix + IP_SUFFIX;
917
        * suffix = * suffix + IP_SUFFIX;
734
    }else{
918
    }else{
735
        netif = ip_netifs_find( & ip_globals.netifs, device_id );
919
        netif = ip_netifs_find( & ip_globals.netifs, device_id );
736
        if( ! netif ){
920
        if( ! netif ){
737
            fibril_rwlock_read_unlock( & ip_globals.netifs_lock );
921
            fibril_rwlock_read_unlock( & ip_globals.netifs_lock );
738
            return ENOENT;
922
            return ENOENT;
739
        }
923
        }
740
        * addr_len = ( netif->addr_len > IP_ADDR ) ? netif->addr_len : IP_ADDR;
924
        * addr_len = ( netif->addr_len > IP_ADDR ) ? netif->addr_len : IP_ADDR;
741
        * prefix = netif->prefix + IP_PREFIX;
925
        * prefix = netif->prefix + IP_PREFIX;
742
        * suffix = netif->suffix + IP_SUFFIX;
926
        * suffix = netif->suffix + IP_SUFFIX;
743
    }
927
    }
744
    fibril_rwlock_read_unlock( & ip_globals.netifs_lock );
928
    fibril_rwlock_read_unlock( & ip_globals.netifs_lock );
745
    return EOK;
929
    return EOK;
746
}
930
}
747
 
931
 
748
int ip_add_route_req( int ip_phone, device_id_t device_id, in_addr_t address, in_addr_t netmask, in_addr_t gateway ){
932
int ip_add_route_req( int ip_phone, device_id_t device_id, in_addr_t address, in_addr_t netmask, in_addr_t gateway ){
749
    ip_route_ref    route;
933
    ip_route_ref    route;
750
    ip_netif_ref    netif;
934
    ip_netif_ref    netif;
751
    int             index;
935
    int             index;
752
 
936
 
753
    fibril_rwlock_write_lock( & ip_globals.netifs_lock );
937
    fibril_rwlock_write_lock( & ip_globals.netifs_lock );
754
    netif = ip_netifs_find( & ip_globals.netifs, device_id );
938
    netif = ip_netifs_find( & ip_globals.netifs, device_id );
755
    if( ! netif ){
939
    if( ! netif ){
756
        fibril_rwlock_write_unlock( & ip_globals.netifs_lock );
940
        fibril_rwlock_write_unlock( & ip_globals.netifs_lock );
757
        return ENOENT;
941
        return ENOENT;
758
    }
942
    }
759
    route = ( ip_route_ref ) malloc( sizeof( ip_route_t ));
943
    route = ( ip_route_ref ) malloc( sizeof( ip_route_t ));
760
    if( ! route ){
944
    if( ! route ){
761
        fibril_rwlock_write_unlock( & ip_globals.netifs_lock );
945
        fibril_rwlock_write_unlock( & ip_globals.netifs_lock );
762
        return ENOMEM;
946
        return ENOMEM;
763
    }
947
    }
764
    route->address.s_addr = address.s_addr;
948
    route->address.s_addr = address.s_addr;
765
    route->netmask.s_addr = netmask.s_addr;
949
    route->netmask.s_addr = netmask.s_addr;
766
    route->gateway.s_addr = gateway.s_addr;
950
    route->gateway.s_addr = gateway.s_addr;
767
    route->netif = netif;
951
    route->netif = netif;
768
    index = ip_routes_add( & netif->routes, route );
952
    index = ip_routes_add( & netif->routes, route );
769
    if( index < 0 ) free( route );
953
    if( index < 0 ) free( route );
770
    fibril_rwlock_write_unlock( & ip_globals.netifs_lock );
954
    fibril_rwlock_write_unlock( & ip_globals.netifs_lock );
771
    return index;
955
    return index;
772
}
956
}
773
 
957
 
774
ip_route_ref ip_find_route( in_addr_t destination ){
958
ip_route_ref ip_find_route( in_addr_t destination ){
775
    int             index;
959
    int             index;
776
    ip_route_ref    route;
960
    ip_route_ref    route;
777
    ip_netif_ref    netif;
961
    ip_netif_ref    netif;
778
 
962
 
779
    // start with the last netif - the newest one
963
    // start with the last netif - the newest one
780
    index = ip_netifs_count( & ip_globals.netifs ) - 1;
964
    index = ip_netifs_count( & ip_globals.netifs ) - 1;
781
    while( index >= 0 ){
965
    while( index >= 0 ){
782
        netif = ip_netifs_get_index( & ip_globals.netifs, index );
966
        netif = ip_netifs_get_index( & ip_globals.netifs, index );
783
        if( netif && ( netif->state == NETIF_ACTIVE )){
967
        if( netif && ( netif->state == NETIF_ACTIVE )){
784
            route = ip_netif_find_route( netif, destination );
968
            route = ip_netif_find_route( netif, destination );
785
            if( route ) return route;
969
            if( route ) return route;
786
        }
970
        }
787
        -- index;
971
        -- index;
788
    }
972
    }
789
    return & ip_globals.gateway;
973
    return & ip_globals.gateway;
790
}
974
}
791
 
975
 
792
ip_route_ref ip_netif_find_route( ip_netif_ref netif, in_addr_t destination ){
976
ip_route_ref ip_netif_find_route( ip_netif_ref netif, in_addr_t destination ){
793
    int             index;
977
    int             index;
794
    ip_route_ref    route;
978
    ip_route_ref    route;
795
 
979
 
796
    if( netif ){
980
    if( netif ){
797
        // start with the first one - the direct route
981
        // start with the first one - the direct route
798
        for( index = 0; index < ip_routes_count( & netif->routes ); ++ index ){
982
        for( index = 0; index < ip_routes_count( & netif->routes ); ++ index ){
799
            route = ip_routes_get_index( & netif->routes, index );
983
            route = ip_routes_get_index( & netif->routes, index );
800
            if( route && (( route->address.s_addr & route->netmask.s_addr ) == ( destination.s_addr & route->netmask.s_addr ))){
984
            if( route && (( route->address.s_addr & route->netmask.s_addr ) == ( destination.s_addr & route->netmask.s_addr ))){
801
                return route;
985
                return route;
802
            }
986
            }
803
        }
987
        }
804
    }
988
    }
805
    return NULL;
989
    return NULL;
806
}
990
}
807
 
991
 
808
int ip_set_gateway_req( int ip_phone, device_id_t device_id, in_addr_t gateway ){
992
int ip_set_gateway_req( int ip_phone, device_id_t device_id, in_addr_t gateway ){
809
    ip_netif_ref    netif;
993
    ip_netif_ref    netif;
810
 
994
 
811
    fibril_rwlock_write_lock( & ip_globals.netifs_lock );
995
    fibril_rwlock_write_lock( & ip_globals.netifs_lock );
812
    netif = ip_netifs_find( & ip_globals.netifs, device_id );
996
    netif = ip_netifs_find( & ip_globals.netifs, device_id );
813
    if( ! netif ){
997
    if( ! netif ){
814
        fibril_rwlock_write_unlock( & ip_globals.netifs_lock );
998
        fibril_rwlock_write_unlock( & ip_globals.netifs_lock );
815
        return ENOENT;
999
        return ENOENT;
816
    }
1000
    }
817
    ip_globals.gateway.address.s_addr = 0;
1001
    ip_globals.gateway.address.s_addr = 0;
818
    ip_globals.gateway.netmask.s_addr = 0;
1002
    ip_globals.gateway.netmask.s_addr = 0;
819
    ip_globals.gateway.gateway.s_addr = gateway.s_addr;
1003
    ip_globals.gateway.gateway.s_addr = gateway.s_addr;
820
    ip_globals.gateway.netif = netif;
1004
    ip_globals.gateway.netif = netif;
821
    fibril_rwlock_write_unlock( & ip_globals.netifs_lock );
1005
    fibril_rwlock_write_unlock( & ip_globals.netifs_lock );
822
    return EOK;
1006
    return EOK;
823
}
1007
}
824
 
1008
 
825
packet_t ip_split_packet( packet_t packet, size_t prefix, size_t content, size_t suffix, socklen_t addr_len, services_t error ){
1009
packet_t ip_split_packet( packet_t packet, size_t prefix, size_t content, size_t suffix, socklen_t addr_len, services_t error ){
826
    size_t          length;
1010
    size_t          length;
827
    packet_t        next;
1011
    packet_t        next;
828
    packet_t        new_packet;
1012
    packet_t        new_packet;
829
    int             result;
1013
    int             result;
830
    int             phone;
1014
    int             phone;
831
 
1015
 
832
    next = packet;
1016
    next = packet;
833
    // check all packets
1017
    // check all packets
834
    while( next ){
1018
    while( next ){
835
        length = packet_get_data_length( next );
1019
        length = packet_get_data_length( next );
836
        // too long?
1020
        // too long?
837
        if( length > content ){
1021
        if( length > content ){
838
            result = ip_fragment_packet( next, content, prefix, suffix, addr_len );
1022
            result = ip_fragment_packet( next, content, prefix, suffix, addr_len );
839
            if( result != EOK ){
1023
            if( result != EOK ){
840
                new_packet = pq_detach( next );
1024
                new_packet = pq_detach( next );
841
                if( next == packet ){
1025
                if( next == packet ){
842
                    // the new first packet of the queue
1026
                    // the new first packet of the queue
843
                    packet = new_packet;
1027
                    packet = new_packet;
844
                }
1028
                }
845
                // fragmentation needed?
1029
                // fragmentation needed?
846
                if( result == EPERM ){
1030
                if( result == EPERM ){
847
                    phone = ip_prepare_icmp_and_get_phone( error, next, NULL );
1031
                    phone = ip_prepare_icmp_and_get_phone( error, next, NULL );
848
                    if( phone >= 0 ){
1032
                    if( phone >= 0 ){
849
                        // fragmentation necessary ICMP
1033
                        // fragmentation necessary ICMP
850
                        icmp_destination_unreachable_msg( phone, ICMP_FRAG_NEEDED, content, next );
1034
                        icmp_destination_unreachable_msg( phone, ICMP_FRAG_NEEDED, content, next );
851
                    }
1035
                    }
852
                }else{
1036
                }else{
853
                    pq_release( ip_globals.net_phone, packet_get_id( next ));
1037
                    pq_release( ip_globals.net_phone, packet_get_id( next ));
854
                }
1038
                }
855
                next = new_packet;
1039
                next = new_packet;
856
                continue;
1040
                continue;
857
            }
1041
            }
858
        }
1042
        }
859
        next = pq_next( next );
1043
        next = pq_next( next );
860
    }
1044
    }
861
    return packet;
1045
    return packet;
862
}
1046
}
863
 
1047
 
864
int ip_fragment_packet( packet_t packet, size_t length, size_t prefix, size_t suffix, socklen_t addr_len ){
1048
int ip_fragment_packet( packet_t packet, size_t length, size_t prefix, size_t suffix, socklen_t addr_len ){
865
    ERROR_DECLARE;
1049
    ERROR_DECLARE;
866
 
1050
 
867
    packet_t        new_packet;
1051
    packet_t        new_packet;
868
    ip_header_ref   header;
1052
    ip_header_ref   header;
869
    ip_header_ref   middle_header;
1053
    ip_header_ref   middle_header;
870
    ip_header_ref   last_header;
1054
    ip_header_ref   last_header;
871
    struct sockaddr *       src;
1055
    struct sockaddr *       src;
872
    struct sockaddr *       dest;
1056
    struct sockaddr *       dest;
873
    socklen_t       addrlen;
1057
    socklen_t       addrlen;
874
    int             result;
1058
    int             result;
875
 
1059
 
876
    result = packet_get_addr( packet, ( uint8_t ** ) & src, ( uint8_t ** ) & dest );
1060
    result = packet_get_addr( packet, ( uint8_t ** ) & src, ( uint8_t ** ) & dest );
877
    if( result <= 0 ) return EINVAL;
1061
    if( result <= 0 ) return EINVAL;
878
    addrlen = ( socklen_t ) result;
1062
    addrlen = ( socklen_t ) result;
879
    if( packet_get_data_length( packet ) <= sizeof( ip_header_t )) return ENOMEM;
1063
    if( packet_get_data_length( packet ) <= sizeof( ip_header_t )) return ENOMEM;
880
    // get header
1064
    // get header
881
    header = ( ip_header_ref ) packet_get_data( packet );
1065
    header = ( ip_header_ref ) packet_get_data( packet );
882
    if( ! header ) return EINVAL;
1066
    if( ! header ) return EINVAL;
883
    // fragmentation forbidden?
1067
    // fragmentation forbidden?
884
    if( header->flags & IPFLAG_DONT_FRAGMENT ){
1068
    if( header->flags & IPFLAG_DONT_FRAGMENT ){
885
        return EPERM;
1069
        return EPERM;
886
    }
1070
    }
887
    // create the last fragment
1071
    // create the last fragment
888
    new_packet = packet_get_4( ip_globals.net_phone, prefix, length, suffix, (( addrlen > addr_len ) ? addrlen : addr_len ));
1072
    new_packet = packet_get_4( ip_globals.net_phone, prefix, length, suffix, (( addrlen > addr_len ) ? addrlen : addr_len ));
889
    if( ! new_packet ) return ENOMEM;
1073
    if( ! new_packet ) return ENOMEM;
890
    // allocate as much as originally
1074
    // allocate as much as originally
891
    last_header = ( ip_header_ref ) packet_suffix( new_packet, IP_HEADER_LENGTH( header ));
1075
    last_header = ( ip_header_ref ) packet_suffix( new_packet, IP_HEADER_LENGTH( header ));
892
    if( ! last_header ){
1076
    if( ! last_header ){
893
        return ip_release_and_return( packet, ENOMEM );
1077
        return ip_release_and_return( packet, ENOMEM );
894
    }
1078
    }
895
    ip_create_last_header( last_header, header );
1079
    ip_create_last_header( last_header, header );
896
    // trim the unused space
1080
    // trim the unused space
897
    if( ERROR_OCCURRED( packet_trim( new_packet, 0, IP_HEADER_LENGTH( header ) - IP_HEADER_LENGTH( last_header )))){
1081
    if( ERROR_OCCURRED( packet_trim( new_packet, 0, IP_HEADER_LENGTH( header ) - IP_HEADER_LENGTH( last_header )))){
898
        return ip_release_and_return( packet, ERROR_CODE );
1082
        return ip_release_and_return( packet, ERROR_CODE );
899
    }
1083
    }
900
    // biggest multiple of 8 lower than content
1084
    // biggest multiple of 8 lower than content
901
    // TODO even fragmentation?
1085
    // TODO even fragmentation?
902
    length = length & ( ~ 0x7 );// ( content / 8 ) * 8
1086
    length = length & ( ~ 0x7 );// ( content / 8 ) * 8
903
    if( ERROR_OCCURRED( ip_fragment_packet_data( packet, new_packet, header, last_header, (( IP_HEADER_DATA_LENGTH( header ) - (( length - IP_HEADER_LENGTH( header )) & ( ~ 0x7 ))) % (( length - IP_HEADER_LENGTH( last_header )) & ( ~ 0x7 ))), src, dest, addrlen ))){
1087
    if( ERROR_OCCURRED( ip_fragment_packet_data( packet, new_packet, header, last_header, (( IP_HEADER_DATA_LENGTH( header ) - (( length - IP_HEADER_LENGTH( header )) & ( ~ 0x7 ))) % (( length - IP_HEADER_LENGTH( last_header )) & ( ~ 0x7 ))), src, dest, addrlen ))){
904
        return ip_release_and_return( packet, ERROR_CODE );
1088
        return ip_release_and_return( packet, ERROR_CODE );
905
    }
1089
    }
906
    // mark the first as fragmented
1090
    // mark the first as fragmented
907
    header->flags |= IPFLAG_MORE_FRAGMENTS;
1091
    header->flags |= IPFLAG_MORE_FRAGMENTS;
908
    // create middle framgents
1092
    // create middle framgents
909
    while( IP_TOTAL_LENGTH( header ) > length ){
1093
    while( IP_TOTAL_LENGTH( header ) > length ){
910
        new_packet = packet_get_4( ip_globals.net_phone, prefix, length, suffix, (( addrlen >= addr_len ) ? addrlen : addr_len ));
1094
        new_packet = packet_get_4( ip_globals.net_phone, prefix, length, suffix, (( addrlen >= addr_len ) ? addrlen : addr_len ));
911
        if( ! new_packet ) return ENOMEM;
1095
        if( ! new_packet ) return ENOMEM;
912
        middle_header = ip_create_middle_header( new_packet, last_header );
1096
        middle_header = ip_create_middle_header( new_packet, last_header );
913
        if( ! middle_header ){
1097
        if( ! middle_header ){
914
            return ip_release_and_return( packet, ENOMEM );
1098
            return ip_release_and_return( packet, ENOMEM );
915
        }
1099
        }
916
        if( ERROR_OCCURRED( ip_fragment_packet_data( packet, new_packet, header, middle_header, ( length - IP_HEADER_LENGTH( middle_header )) & ( ~ 0x7 ), src, dest, addrlen ))){
1100
        if( ERROR_OCCURRED( ip_fragment_packet_data( packet, new_packet, header, middle_header, ( length - IP_HEADER_LENGTH( middle_header )) & ( ~ 0x7 ), src, dest, addrlen ))){
917
            return ip_release_and_return( packet, ERROR_CODE );
1101
            return ip_release_and_return( packet, ERROR_CODE );
918
        }
1102
        }
919
    }
1103
    }
920
    // finish the first fragment
1104
    // finish the first fragment
921
    header->header_checksum = IP_HEADER_CHECKSUM( header );
1105
    header->header_checksum = IP_HEADER_CHECKSUM( header );
922
    return EOK;
1106
    return EOK;
923
}
1107
}
924
 
1108
 
925
int ip_fragment_packet_data( packet_t packet, packet_t new_packet, ip_header_ref header, ip_header_ref new_header, size_t length, const struct sockaddr * src, const struct sockaddr * dest, socklen_t addrlen ){
1109
int ip_fragment_packet_data( packet_t packet, packet_t new_packet, ip_header_ref header, ip_header_ref new_header, size_t length, const struct sockaddr * src, const struct sockaddr * dest, socklen_t addrlen ){
926
    ERROR_DECLARE;
1110
    ERROR_DECLARE;
927
 
1111
 
928
    void *          data;
1112
    void *          data;
929
    size_t          offset;
1113
    size_t          offset;
930
 
1114
 
931
    data = packet_suffix( new_packet, length );
1115
    data = packet_suffix( new_packet, length );
932
    if( ! data ) return ENOMEM;
1116
    if( ! data ) return ENOMEM;
933
    memcpy( data, (( void * ) header ) + IP_TOTAL_LENGTH( header ) - length, length );
1117
    memcpy( data, (( void * ) header ) + IP_TOTAL_LENGTH( header ) - length, length );
934
    ERROR_PROPAGATE( packet_trim( packet, 0, length ));
1118
    ERROR_PROPAGATE( packet_trim( packet, 0, length ));
935
    header->total_length = htons( IP_TOTAL_LENGTH( header ) - length );
1119
    header->total_length = htons( IP_TOTAL_LENGTH( header ) - length );
936
    new_header->total_length = htons( IP_HEADER_LENGTH( new_header ) + length );
1120
    new_header->total_length = htons( IP_HEADER_LENGTH( new_header ) + length );
937
    offset = IP_FRAGMENT_OFFSET( header ) + IP_HEADER_DATA_LENGTH( header );
1121
    offset = IP_FRAGMENT_OFFSET( header ) + IP_HEADER_DATA_LENGTH( header );
938
    new_header->fragment_offset_high = IP_COMPUTE_FRAGMENT_OFFSET_HIGH( offset );
1122
    new_header->fragment_offset_high = IP_COMPUTE_FRAGMENT_OFFSET_HIGH( offset );
939
    new_header->fragment_offset_low = IP_COMPUTE_FRAGMENT_OFFSET_LOW( offset );
1123
    new_header->fragment_offset_low = IP_COMPUTE_FRAGMENT_OFFSET_LOW( offset );
940
    new_header->header_checksum = IP_HEADER_CHECKSUM( new_header );
1124
    new_header->header_checksum = IP_HEADER_CHECKSUM( new_header );
941
    ERROR_PROPAGATE( packet_set_addr( new_packet, ( const uint8_t * ) src, ( const uint8_t * ) dest, addrlen ));
1125
    ERROR_PROPAGATE( packet_set_addr( new_packet, ( const uint8_t * ) src, ( const uint8_t * ) dest, addrlen ));
942
    return pq_insert_after( packet, new_packet );
1126
    return pq_insert_after( packet, new_packet );
943
}
1127
}
944
 
1128
 
945
ip_header_ref ip_create_middle_header( packet_t packet, ip_header_ref last ){
1129
ip_header_ref ip_create_middle_header( packet_t packet, ip_header_ref last ){
946
    ip_header_ref   middle;
1130
    ip_header_ref   middle;
947
 
1131
 
948
    middle = ( ip_header_ref ) packet_suffix( packet, IP_HEADER_LENGTH( last ));
1132
    middle = ( ip_header_ref ) packet_suffix( packet, IP_HEADER_LENGTH( last ));
949
    if( ! middle ) return NULL;
1133
    if( ! middle ) return NULL;
950
    memcpy( middle, last, IP_HEADER_LENGTH( last ));
1134
    memcpy( middle, last, IP_HEADER_LENGTH( last ));
951
    middle->flags |= IPFLAG_MORE_FRAGMENTS;
1135
    middle->flags |= IPFLAG_MORE_FRAGMENTS;
952
    return middle;
1136
    return middle;
953
}
1137
}
954
 
1138
 
955
void ip_create_last_header( ip_header_ref last, ip_header_ref first ){
1139
void ip_create_last_header( ip_header_ref last, ip_header_ref first ){
956
    ip_option_ref   option;
1140
    ip_option_ref   option;
957
    size_t          next;
1141
    size_t          next;
958
    size_t          length;
1142
    size_t          length;
959
 
1143
 
960
    // copy first itself
1144
    // copy first itself
961
    memcpy( last, first, sizeof( ip_header_t ));
1145
    memcpy( last, first, sizeof( ip_header_t ));
962
    length = sizeof( ip_header_t );
1146
    length = sizeof( ip_header_t );
963
    next = sizeof( ip_header_t );
1147
    next = sizeof( ip_header_t );
964
    // process all ip options
1148
    // process all ip options
965
    while( next < first->header_length ){
1149
    while( next < first->header_length ){
966
        option = ( ip_option_ref ) ((( uint8_t * ) first ) + next );
1150
        option = ( ip_option_ref ) ((( uint8_t * ) first ) + next );
967
        // skip end or noop
1151
        // skip end or noop
968
        if(( option->type == IPOPT_END ) || ( option->type == IPOPT_NOOP )){
1152
        if(( option->type == IPOPT_END ) || ( option->type == IPOPT_NOOP )){
969
            ++ next;
1153
            ++ next;
970
        }else{
1154
        }else{
971
            // copy if said so or skip
1155
            // copy if said so or skip
972
            if( IPOPT_COPIED( option->type )){
1156
            if( IPOPT_COPIED( option->type )){
973
                memcpy((( uint8_t * ) last ) + length, (( uint8_t * ) first ) + next, option->length );
1157
                memcpy((( uint8_t * ) last ) + length, (( uint8_t * ) first ) + next, option->length );
974
                length += option->length;
1158
                length += option->length;
975
            }
1159
            }
976
            // next option
1160
            // next option
977
            next += option->length;
1161
            next += option->length;
978
        }
1162
        }
979
    }
1163
    }
980
    // align 4 byte boundary
1164
    // align 4 byte boundary
981
    if( length % 4 ){
1165
    if( length % 4 ){
982
        bzero((( uint8_t * ) last ) + length, 4 - ( length % 4 ));
1166
        bzero((( uint8_t * ) last ) + length, 4 - ( length % 4 ));
983
        last->header_length = length / 4 + 1;
1167
        last->header_length = length / 4 + 1;
984
    }else{
1168
    }else{
985
        last->header_length = length / 4;
1169
        last->header_length = length / 4;
986
    }
1170
    }
987
    last->header_checksum = 0;
1171
    last->header_checksum = 0;
988
}
1172
}
989
 
1173
 
990
int ip_receive_message( device_id_t device_id, packet_t packet ){
1174
int ip_receive_message( device_id_t device_id, packet_t packet ){
991
    packet_t        next;
1175
    packet_t        next;
992
 
1176
 
993
    do{
1177
    do{
994
        next = pq_detach( packet );
1178
        next = pq_detach( packet );
995
        ip_process_packet( device_id, packet );
1179
        ip_process_packet( device_id, packet );
996
        packet = next;
1180
        packet = next;
997
    }while( packet );
1181
    }while( packet );
998
    return EOK;
1182
    return EOK;
999
}
1183
}
1000
 
1184
 
1001
int ip_process_packet( device_id_t device_id, packet_t packet ){
1185
int ip_process_packet( device_id_t device_id, packet_t packet ){
1002
    ERROR_DECLARE;
1186
    ERROR_DECLARE;
1003
 
1187
 
1004
    ip_header_ref   header;
1188
    ip_header_ref   header;
1005
    in_addr_t       dest;
1189
    in_addr_t       dest;
1006
    ip_route_ref    route;
1190
    ip_route_ref    route;
1007
    int             phone;
1191
    int             phone;
1008
    struct sockaddr *   addr;
1192
    struct sockaddr *   addr;
1009
    struct sockaddr_in  addr_in;
1193
    struct sockaddr_in  addr_in;
1010
//  struct sockaddr_in  addr_in6;
1194
//  struct sockaddr_in  addr_in6;
1011
    socklen_t       addrlen;
1195
    socklen_t       addrlen;
1012
 
1196
 
1013
    header = ( ip_header_ref ) packet_get_data( packet );
1197
    header = ( ip_header_ref ) packet_get_data( packet );
1014
    if( ! header ){
1198
    if( ! header ){
1015
        return ip_release_and_return( packet, ENOMEM );
1199
        return ip_release_and_return( packet, ENOMEM );
1016
    }
1200
    }
1017
    // checksum
1201
    // checksum
1018
    if(( header->header_checksum ) && ( IP_HEADER_CHECKSUM( header ))){
1202
    if(( header->header_checksum ) && ( IP_HEADER_CHECKSUM( header ))){
1019
        phone = ip_prepare_icmp_and_get_phone( 0, packet, header );
1203
        phone = ip_prepare_icmp_and_get_phone( 0, packet, header );
1020
        if( phone >= 0 ){
1204
        if( phone >= 0 ){
1021
            // checksum error ICMP
1205
            // checksum error ICMP
1022
            icmp_parameter_problem_msg( phone, ICMP_PARAM_POINTER, (( size_t ) (( void * ) & header->header_checksum )) - (( size_t ) (( void * ) header )), packet );
1206
            icmp_parameter_problem_msg( phone, ICMP_PARAM_POINTER, (( size_t ) (( void * ) & header->header_checksum )) - (( size_t ) (( void * ) header )), packet );
1023
        }
1207
        }
1024
        return EINVAL;
1208
        return EINVAL;
1025
    }
1209
    }
1026
    if( header->ttl <= 1 ){
1210
    if( header->ttl <= 1 ){
1027
        phone = ip_prepare_icmp_and_get_phone( 0, packet, header );
1211
        phone = ip_prepare_icmp_and_get_phone( 0, packet, header );
1028
        if( phone >= 0 ){
1212
        if( phone >= 0 ){
1029
            // ttl oxceeded ICMP
1213
            // ttl oxceeded ICMP
1030
            icmp_time_exceeded_msg( phone, ICMP_EXC_TTL, packet );
1214
            icmp_time_exceeded_msg( phone, ICMP_EXC_TTL, packet );
1031
        }
1215
        }
1032
        return EINVAL;
1216
        return EINVAL;
1033
    }
1217
    }
1034
    // process ipopt and get destination
1218
    // process ipopt and get destination
1035
    dest = ip_get_destination( header );
1219
    dest = ip_get_destination( header );
1036
    // set the addrination address
1220
    // set the addrination address
1037
    switch( header->version ){
1221
    switch( header->version ){
1038
        case IPVERSION:
1222
        case IPVERSION:
1039
            addrlen = sizeof( addr_in );
1223
            addrlen = sizeof( addr_in );
1040
            bzero( & addr_in, addrlen );
1224
            bzero( & addr_in, addrlen );
1041
            addr_in.sin_family = AF_INET;
1225
            addr_in.sin_family = AF_INET;
1042
            memcpy( & addr_in.sin_addr.s_addr, & dest, sizeof( dest ));
1226
            memcpy( & addr_in.sin_addr.s_addr, & dest, sizeof( dest ));
1043
            addr = ( struct sockaddr * ) & addr_in;
1227
            addr = ( struct sockaddr * ) & addr_in;
1044
            break;
1228
            break;
1045
/*      case IPv6VERSION:
1229
/*      case IPv6VERSION:
1046
            addrlen = sizeof( dest_in6 );
1230
            addrlen = sizeof( dest_in6 );
1047
            bzero( & dest_in6, addrlen );
1231
            bzero( & dest_in6, addrlen );
1048
            dest_in6.sin6_family = AF_INET6;
1232
            dest_in6.sin6_family = AF_INET6;
1049
            memcpy( & dest_in6.sin6_addr.s6_addr, );
1233
            memcpy( & dest_in6.sin6_addr.s6_addr, );
1050
            dest = ( struct sockaddr * ) & dest_in;
1234
            dest = ( struct sockaddr * ) & dest_in;
1051
            break;
1235
            break;
1052
*/      default:
1236
*/      default:
1053
            return EAFNOSUPPORT;
1237
            return ip_release_and_return( packet, EAFNOSUPPORT );
1054
    }
1238
    }
1055
    ERROR_PROPAGATE( packet_set_addr( packet, NULL, ( uint8_t * ) & addr, addrlen ));
1239
    ERROR_PROPAGATE( packet_set_addr( packet, NULL, ( uint8_t * ) & addr, addrlen ));
1056
    route = ip_find_route( dest );
1240
    route = ip_find_route( dest );
1057
    if( ! route ){
1241
    if( ! route ){
1058
        phone = ip_prepare_icmp_and_get_phone( 0, packet, header );
1242
        phone = ip_prepare_icmp_and_get_phone( 0, packet, header );
1059
        if( phone >= 0 ){
1243
        if( phone >= 0 ){
1060
            // unreachable ICMP
1244
            // unreachable ICMP
1061
            icmp_destination_unreachable_msg( phone, ICMP_HOST_UNREACH, 0, packet );
1245
            icmp_destination_unreachable_msg( phone, ICMP_HOST_UNREACH, 0, packet );
1062
        }
1246
        }
1063
        return ENOENT;
1247
        return ENOENT;
1064
    }
1248
    }
1065
    if( route->address.s_addr == dest.s_addr ){
1249
    if( route->address.s_addr == dest.s_addr ){
1066
        // local delivery
1250
        // local delivery
1067
        return ip_deliver_local( device_id, packet, header, 0 );
1251
        return ip_deliver_local( device_id, packet, header, 0 );
1068
    }else{
1252
    }else{
1069
        // only if routing enabled
1253
        // only if routing enabled
1070
        if( route->netif->routing ){
1254
        if( route->netif->routing ){
1071
            -- header->ttl;
1255
            -- header->ttl;
1072
            return ip_send_route( packet, route->netif, route, NULL, dest, 0 );
1256
            return ip_send_route( packet, route->netif, route, NULL, dest, 0 );
1073
        }else{
1257
        }else{
1074
            phone = ip_prepare_icmp_and_get_phone( 0, packet, header );
1258
            phone = ip_prepare_icmp_and_get_phone( 0, packet, header );
1075
            if( phone >= 0 ){
1259
            if( phone >= 0 ){
1076
                // unreachable ICMP if no routing
1260
                // unreachable ICMP if no routing
1077
                icmp_destination_unreachable_msg( phone, ICMP_HOST_UNREACH, 0, packet );
1261
                icmp_destination_unreachable_msg( phone, ICMP_HOST_UNREACH, 0, packet );
1078
            }
1262
            }
1079
            return ENOENT;
1263
            return ENOENT;
1080
        }
1264
        }
1081
    }
1265
    }
1082
}
1266
}
1083
 
1267
 
1084
int ip_received_error_msg( int ip_phone, device_id_t device_id, packet_t packet, services_t target, services_t error ){
1268
int ip_received_error_msg( int ip_phone, device_id_t device_id, packet_t packet, services_t target, services_t error ){
1085
    uint8_t *           data;
1269
    uint8_t *           data;
1086
    int                 offset;
1270
    int                 offset;
1087
    icmp_type_t         type;
1271
    icmp_type_t         type;
1088
    icmp_code_t         code;
1272
    icmp_code_t         code;
1089
    ip_netif_ref        netif;
1273
    ip_netif_ref        netif;
1090
    measured_string_t   address;
1274
    measured_string_t   address;
1091
    ip_route_ref        route;
1275
    ip_route_ref        route;
1092
    ip_header_ref       header;
1276
    ip_header_ref       header;
1093
 
1277
 
1094
    switch( error ){
1278
    switch( error ){
1095
        case SERVICE_ICMP:
1279
        case SERVICE_ICMP:
1096
            offset = icmp_client_process_packet( packet, & type, & code, NULL, NULL );
1280
            offset = icmp_client_process_packet( packet, & type, & code, NULL, NULL );
1097
            if( offset < 0 ){
1281
            if( offset < 0 ){
1098
                return ip_release_and_return( packet, ENOMEM );
1282
                return ip_release_and_return( packet, ENOMEM );
1099
            }
1283
            }
1100
            data = packet_get_data( packet );
1284
            data = packet_get_data( packet );
1101
            header = ( ip_header_ref )( data + offset );
1285
            header = ( ip_header_ref )( data + offset );
1102
            // destination host unreachable?
1286
            // destination host unreachable?
1103
            if(( type == ICMP_DEST_UNREACH ) && ( code == ICMP_HOST_UNREACH )){
1287
            if(( type == ICMP_DEST_UNREACH ) && ( code == ICMP_HOST_UNREACH )){
1104
                fibril_rwlock_read_lock( & ip_globals.netifs_lock );
1288
                fibril_rwlock_read_lock( & ip_globals.netifs_lock );
1105
                netif = ip_netifs_find( & ip_globals.netifs, device_id );
1289
                netif = ip_netifs_find( & ip_globals.netifs, device_id );
1106
                if( netif && netif->arp ){
1290
                if( netif && netif->arp ){
1107
                    route = ip_routes_get_index( & netif->routes, 0 );
1291
                    route = ip_routes_get_index( & netif->routes, 0 );
1108
                    // from the same network?
1292
                    // from the same network?
1109
                    if( route && (( route->address.s_addr & route->netmask.s_addr ) == ( header->destination_address & route->netmask.s_addr ))){
1293
                    if( route && (( route->address.s_addr & route->netmask.s_addr ) == ( header->destination_address & route->netmask.s_addr ))){
1110
                        // clear the ARP mapping if any
1294
                        // clear the ARP mapping if any
1111
                        address.value = ( char * ) & header->destination_address;
1295
                        address.value = ( char * ) & header->destination_address;
1112
                        address.length = CONVERT_SIZE( uint8_t, char, sizeof( header->destination_address ));
1296
                        address.length = CONVERT_SIZE( uint8_t, char, sizeof( header->destination_address ));
1113
                        arp_clear_address_req( netif->arp->phone, netif->device_id, SERVICE_IP, & address );
1297
                        arp_clear_address_req( netif->arp->phone, netif->device_id, SERVICE_IP, & address );
1114
                    }
1298
                    }
1115
                }
1299
                }
1116
                fibril_rwlock_read_unlock( & ip_globals.netifs_lock );
1300
                fibril_rwlock_read_unlock( & ip_globals.netifs_lock );
1117
            }
1301
            }
1118
            break;
1302
            break;
1119
        default:
1303
        default:
1120
            return ip_release_and_return( packet, ENOTSUP );
1304
            return ip_release_and_return( packet, ENOTSUP );
1121
    }
1305
    }
1122
    return ip_deliver_local( device_id, packet, header, error );
1306
    return ip_deliver_local( device_id, packet, header, error );
1123
}
1307
}
1124
 
1308
 
1125
int ip_deliver_local( device_id_t device_id, packet_t packet, ip_header_ref header, services_t error ){
1309
int ip_deliver_local( device_id_t device_id, packet_t packet, ip_header_ref header, services_t error ){
1126
    ERROR_DECLARE;
1310
    ERROR_DECLARE;
1127
 
1311
 
1128
    ip_proto_ref    proto;
1312
    ip_proto_ref    proto;
1129
    int             phone;
1313
    int             phone;
1130
    services_t      service;
1314
    services_t      service;
1131
    tl_received_msg_t   received_msg;
1315
    tl_received_msg_t   received_msg;
1132
    struct sockaddr *   src;
1316
    struct sockaddr *   src;
1133
    struct sockaddr *   dest;
1317
    struct sockaddr *   dest;
1134
    struct sockaddr_in  src_in;
1318
    struct sockaddr_in  src_in;
1135
    struct sockaddr_in  dest_in;
1319
    struct sockaddr_in  dest_in;
1136
//  struct sockaddr_in  src_in6;
1320
//  struct sockaddr_in  src_in6;
1137
//  struct sockaddr_in  dest_in6;
1321
//  struct sockaddr_in  dest_in6;
1138
    socklen_t       addrlen;
1322
    socklen_t       addrlen;
1139
 
1323
 
1140
    if(( header->flags & IPFLAG_MORE_FRAGMENTS ) || IP_FRAGMENT_OFFSET( header )){
1324
    if(( header->flags & IPFLAG_MORE_FRAGMENTS ) || IP_FRAGMENT_OFFSET( header )){
1141
        // TODO fragmented
1325
        // TODO fragmented
1142
        return ENOTSUP;
1326
        return ENOTSUP;
1143
    }else{
1327
    }else{
1144
        switch( header->version ){
1328
        switch( header->version ){
1145
            case IPVERSION:
1329
            case IPVERSION:
1146
                addrlen = sizeof( src_in );
1330
                addrlen = sizeof( src_in );
1147
                bzero( & src_in, addrlen );
1331
                bzero( & src_in, addrlen );
1148
                src_in.sin_family = AF_INET;
1332
                src_in.sin_family = AF_INET;
1149
                memcpy( & dest_in, & src_in, addrlen );
1333
                memcpy( & dest_in, & src_in, addrlen );
1150
                memcpy( & src_in.sin_addr.s_addr, & header->source_address, sizeof( header->source_address ));
1334
                memcpy( & src_in.sin_addr.s_addr, & header->source_address, sizeof( header->source_address ));
1151
                memcpy( & dest_in.sin_addr.s_addr, & header->destination_address, sizeof( header->destination_address ));
1335
                memcpy( & dest_in.sin_addr.s_addr, & header->destination_address, sizeof( header->destination_address ));
1152
                src = ( struct sockaddr * ) & src_in;
1336
                src = ( struct sockaddr * ) & src_in;
1153
                dest = ( struct sockaddr * ) & dest_in;
1337
                dest = ( struct sockaddr * ) & dest_in;
1154
                break;
1338
                break;
1155
/*          case IPv6VERSION:
1339
/*          case IPv6VERSION:
1156
                addrlen = sizeof( src_in6 );
1340
                addrlen = sizeof( src_in6 );
1157
                bzero( & src_in6, addrlen );
1341
                bzero( & src_in6, addrlen );
1158
                src_in6.sin6_family = AF_INET6;
1342
                src_in6.sin6_family = AF_INET6;
1159
                memcpy( & dest_in6, & src_in6, addrlen );
1343
                memcpy( & dest_in6, & src_in6, addrlen );
1160
                memcpy( & src_in6.sin6_addr.s6_addr, );
1344
                memcpy( & src_in6.sin6_addr.s6_addr, );
1161
                memcpy( & dest_in6.sin6_addr.s6_addr, );
1345
                memcpy( & dest_in6.sin6_addr.s6_addr, );
1162
                src = ( struct sockaddr * ) & src_in;
1346
                src = ( struct sockaddr * ) & src_in;
1163
                dest = ( struct sockaddr * ) & dest_in;
1347
                dest = ( struct sockaddr * ) & dest_in;
1164
                break;
1348
                break;
1165
*/          default:
1349
*/          default:
1166
                return ip_release_and_return( packet, EAFNOSUPPORT );
1350
                return ip_release_and_return( packet, EAFNOSUPPORT );
1167
        }
1351
        }
1168
        if( ERROR_OCCURRED( packet_set_addr( packet, ( uint8_t * ) src, ( uint8_t * ) dest, addrlen ))){
1352
        if( ERROR_OCCURRED( packet_set_addr( packet, ( uint8_t * ) src, ( uint8_t * ) dest, addrlen ))){
1169
            return ip_release_and_return( packet, ERROR_CODE );
1353
            return ip_release_and_return( packet, ERROR_CODE );
1170
        }
1354
        }
1171
        // trim padding if present
1355
        // trim padding if present
1172
        if(( ! error ) && ( IP_TOTAL_LENGTH( header ) < packet_get_data_length( packet ))){
1356
        if(( ! error ) && ( IP_TOTAL_LENGTH( header ) < packet_get_data_length( packet ))){
1173
            if( ERROR_OCCURRED( packet_trim( packet, 0, packet_get_data_length( packet ) - IP_TOTAL_LENGTH( header )))){
1357
            if( ERROR_OCCURRED( packet_trim( packet, 0, packet_get_data_length( packet ) - IP_TOTAL_LENGTH( header )))){
1174
                return ip_release_and_return( packet, ERROR_CODE );
1358
                return ip_release_and_return( packet, ERROR_CODE );
1175
            }
1359
            }
1176
        }
1360
        }
1177
        fibril_rwlock_read_lock( & ip_globals.protos_lock );
1361
        fibril_rwlock_read_lock( & ip_globals.protos_lock );
1178
        proto = ip_protos_find( & ip_globals.protos, header->protocol );
1362
        proto = ip_protos_find( & ip_globals.protos, header->protocol );
1179
        if( ! proto ){
1363
        if( ! proto ){
1180
            fibril_rwlock_read_unlock( & ip_globals.protos_lock );
1364
            fibril_rwlock_read_unlock( & ip_globals.protos_lock );
1181
            phone = ip_prepare_icmp_and_get_phone( error, packet, header );
1365
            phone = ip_prepare_icmp_and_get_phone( error, packet, header );
1182
            if( phone >= 0 ){
1366
            if( phone >= 0 ){
1183
                // unreachable ICMP
1367
                // unreachable ICMP
1184
                icmp_destination_unreachable_msg( phone, ICMP_PROT_UNREACH, 0, packet );
1368
                icmp_destination_unreachable_msg( phone, ICMP_PROT_UNREACH, 0, packet );
1185
            }
1369
            }
1186
            return ENOENT;
1370
            return ENOENT;
1187
        }
1371
        }
1188
        if( proto->received_msg ){
1372
        if( proto->received_msg ){
1189
            service = proto->service;
1373
            service = proto->service;
1190
            received_msg = proto->received_msg;
1374
            received_msg = proto->received_msg;
1191
            fibril_rwlock_read_unlock( & ip_globals.protos_lock );
1375
            fibril_rwlock_read_unlock( & ip_globals.protos_lock );
1192
            ERROR_CODE = received_msg( device_id, packet, service, error );
1376
            ERROR_CODE = received_msg( device_id, packet, service, error );
1193
        }else{
1377
        }else{
1194
            ERROR_CODE = tl_received_msg( proto->phone, device_id, packet, proto->service, error );
1378
            ERROR_CODE = tl_received_msg( proto->phone, device_id, packet, proto->service, error );
1195
            fibril_rwlock_read_unlock( & ip_globals.protos_lock );
1379
            fibril_rwlock_read_unlock( & ip_globals.protos_lock );
1196
        }
1380
        }
1197
        return ERROR_CODE;
1381
        return ERROR_CODE;
1198
    }
1382
    }
1199
}
1383
}
1200
 
1384
 
1201
in_addr_t ip_get_destination( ip_header_ref header ){
1385
in_addr_t ip_get_destination( ip_header_ref header ){
1202
    in_addr_t   destination;
1386
    in_addr_t   destination;
1203
 
1387
 
1204
    // TODO search set ipopt route?
1388
    // TODO search set ipopt route?
1205
    destination.s_addr = header->destination_address;
1389
    destination.s_addr = header->destination_address;
1206
    return destination;
1390
    return destination;
1207
}
1391
}
1208
 
1392
 
1209
int ip_prepare_icmp( packet_t packet, ip_header_ref header ){
1393
int ip_prepare_icmp( packet_t packet, ip_header_ref header ){
1210
    packet_t    next;
1394
    packet_t    next;
1211
    struct sockaddr *   dest;
1395
    struct sockaddr *   dest;
1212
    struct sockaddr_in  dest_in;
1396
    struct sockaddr_in  dest_in;
1213
//  struct sockaddr_in  dest_in6;
1397
//  struct sockaddr_in  dest_in6;
1214
    socklen_t       addrlen;
1398
    socklen_t       addrlen;
1215
 
1399
 
1216
    // detach the first packet and release the others
1400
    // detach the first packet and release the others
1217
    next = pq_detach( packet );
1401
    next = pq_detach( packet );
1218
    if( next ){
1402
    if( next ){
1219
        pq_release( ip_globals.net_phone, packet_get_id( next ));
1403
        pq_release( ip_globals.net_phone, packet_get_id( next ));
1220
    }
1404
    }
1221
    if( ! header ){
1405
    if( ! header ){
1222
        if( packet_get_data_length( packet ) <= sizeof( ip_header_t )) return ENOMEM;
1406
        if( packet_get_data_length( packet ) <= sizeof( ip_header_t )) return ENOMEM;
1223
        // get header
1407
        // get header
1224
        header = ( ip_header_ref ) packet_get_data( packet );
1408
        header = ( ip_header_ref ) packet_get_data( packet );
1225
        if( ! header ) return EINVAL;
1409
        if( ! header ) return EINVAL;
1226
    }
1410
    }
1227
    // only for the first fragment
1411
    // only for the first fragment
1228
    if( IP_FRAGMENT_OFFSET( header )) return EINVAL;
1412
    if( IP_FRAGMENT_OFFSET( header )) return EINVAL;
1229
    // set the destination address
1413
    // set the destination address
1230
    switch( header->version ){
1414
    switch( header->version ){
1231
        case IPVERSION:
1415
        case IPVERSION:
1232
            addrlen = sizeof( dest_in );
1416
            addrlen = sizeof( dest_in );
1233
            bzero( & dest_in, addrlen );
1417
            bzero( & dest_in, addrlen );
1234
            dest_in.sin_family = AF_INET;
1418
            dest_in.sin_family = AF_INET;
1235
            memcpy( & dest_in.sin_addr.s_addr, & header->source_address, sizeof( header->source_address ));
1419
            memcpy( & dest_in.sin_addr.s_addr, & header->source_address, sizeof( header->source_address ));
1236
            dest = ( struct sockaddr * ) & dest_in;
1420
            dest = ( struct sockaddr * ) & dest_in;
1237
            break;
1421
            break;
1238
/*      case IPv6VERSION:
1422
/*      case IPv6VERSION:
1239
            addrlen = sizeof( dest_in6 );
1423
            addrlen = sizeof( dest_in6 );
1240
            bzero( & dest_in6, addrlen );
1424
            bzero( & dest_in6, addrlen );
1241
            dest_in6.sin6_family = AF_INET6;
1425
            dest_in6.sin6_family = AF_INET6;
1242
            memcpy( & dest_in6.sin6_addr.s6_addr, );
1426
            memcpy( & dest_in6.sin6_addr.s6_addr, );
1243
            dest = ( struct sockaddr * ) & dest_in;
1427
            dest = ( struct sockaddr * ) & dest_in;
1244
            break;
1428
            break;
1245
*/      default:
1429
*/      default:
1246
            return EAFNOSUPPORT;
1430
            return EAFNOSUPPORT;
1247
    }
1431
    }
1248
    return packet_set_addr( packet, NULL, ( uint8_t * ) dest, addrlen );
1432
    return packet_set_addr( packet, NULL, ( uint8_t * ) dest, addrlen );
1249
}
1433
}
1250
 
1434
 
1251
int ip_get_icmp_phone( void ){
1435
int ip_get_icmp_phone( void ){
1252
    ip_proto_ref    proto;
1436
    ip_proto_ref    proto;
1253
    int             phone;
1437
    int             phone;
1254
 
1438
 
1255
    fibril_rwlock_read_lock( & ip_globals.protos_lock );
1439
    fibril_rwlock_read_lock( & ip_globals.protos_lock );
1256
    proto = ip_protos_find( & ip_globals.protos, IPPROTO_ICMP );
1440
    proto = ip_protos_find( & ip_globals.protos, IPPROTO_ICMP );
1257
    phone = proto ? proto->phone : ENOENT;
1441
    phone = proto ? proto->phone : ENOENT;
1258
    fibril_rwlock_read_unlock( & ip_globals.protos_lock );
1442
    fibril_rwlock_read_unlock( & ip_globals.protos_lock );
1259
    return phone;
1443
    return phone;
1260
}
1444
}
1261
 
1445
 
1262
int ip_prepare_icmp_and_get_phone( services_t error, packet_t packet, ip_header_ref header ){
1446
int ip_prepare_icmp_and_get_phone( services_t error, packet_t packet, ip_header_ref header ){
1263
    int phone;
1447
    int phone;
1264
 
1448
 
1265
    phone = ip_get_icmp_phone();
1449
    phone = ip_get_icmp_phone();
1266
    if( error || ( phone < 0 ) || ip_prepare_icmp( packet, header )){
1450
    if( error || ( phone < 0 ) || ip_prepare_icmp( packet, header )){
1267
        return ip_release_and_return( packet, EINVAL );
1451
        return ip_release_and_return( packet, EINVAL );
1268
    }
1452
    }
1269
    return phone;
1453
    return phone;
1270
}
1454
}
1271
 
1455
 
1272
int ip_release_and_return( packet_t packet, int result ){
1456
int ip_release_and_return( packet_t packet, int result ){
1273
    pq_release( ip_globals.net_phone, packet_get_id( packet ));
1457
    pq_release( ip_globals.net_phone, packet_get_id( packet ));
1274
    return result;
1458
    return result;
1275
}
1459
}
1276
 
1460
 
1277
int ip_get_route_req( int ip_phone, ip_protocol_t protocol, const struct sockaddr * destination, socklen_t addrlen, device_id_t * device_id, ip_pseudo_header_ref * header, size_t * headerlen ){
1461
int ip_get_route_req( int ip_phone, ip_protocol_t protocol, const struct sockaddr * destination, socklen_t addrlen, device_id_t * device_id, ip_pseudo_header_ref * header, size_t * headerlen ){
1278
    struct sockaddr_in *    address_in;
1462
    struct sockaddr_in *    address_in;
1279
//  struct sockaddr_in6 *   address_in6;
1463
//  struct sockaddr_in6 *   address_in6;
1280
    in_addr_t *             dest;
1464
    in_addr_t *             dest;
1281
    in_addr_t *             src;
1465
    in_addr_t *             src;
1282
    ip_route_ref            route;
1466
    ip_route_ref            route;
1283
    ipv4_pseudo_header_ref  header_in;
1467
    ipv4_pseudo_header_ref  header_in;
1284
 
1468
 
1285
    if( !( destination && ( addrlen > 0 ))) return EINVAL;
1469
    if( !( destination && ( addrlen > 0 ))) return EINVAL;
1286
    if( !( device_id && header && headerlen )) return EBADMEM;
1470
    if( !( device_id && header && headerlen )) return EBADMEM;
1287
    if(( size_t ) addrlen < sizeof( struct sockaddr )){
1471
    if(( size_t ) addrlen < sizeof( struct sockaddr )){
1288
        return EINVAL;
1472
        return EINVAL;
1289
    }
1473
    }
1290
    switch( destination->sa_family ){
1474
    switch( destination->sa_family ){
1291
        case AF_INET:
1475
        case AF_INET:
1292
            if( addrlen != sizeof( struct sockaddr_in )){
1476
            if( addrlen != sizeof( struct sockaddr_in )){
1293
                return EINVAL;
1477
                return EINVAL;
1294
            }
1478
            }
1295
            address_in = ( struct sockaddr_in * ) destination;
1479
            address_in = ( struct sockaddr_in * ) destination;
1296
            dest = & address_in->sin_addr;
1480
            dest = & address_in->sin_addr;
1297
            break;
1481
            break;
1298
        // TODO IPv6
1482
        // TODO IPv6
1299
/*      case AF_INET6:
1483
/*      case AF_INET6:
1300
            if( addrlen != sizeof( struct sockaddr_in6 )) return EINVAL;
1484
            if( addrlen != sizeof( struct sockaddr_in6 )) return EINVAL;
1301
            address_in6 = ( struct sockaddr_in6 * ) dest;
1485
            address_in6 = ( struct sockaddr_in6 * ) dest;
1302
            address_in6.sin6_addr.s6_addr;
1486
            address_in6.sin6_addr.s6_addr;
1303
*/      default:
1487
*/      default:
1304
            return EAFNOSUPPORT;
1488
            return EAFNOSUPPORT;
1305
    }
1489
    }
1306
    fibril_rwlock_read_lock( & ip_globals.lock );
1490
    fibril_rwlock_read_lock( & ip_globals.lock );
1307
    route = ip_find_route( * dest );
1491
    route = ip_find_route( * dest );
1308
    if( !( route && route->netif )){
1492
    if( !( route && route->netif )){
1309
        fibril_rwlock_read_unlock( & ip_globals.lock );
1493
        fibril_rwlock_read_unlock( & ip_globals.lock );
1310
        return ENOENT;
1494
        return ENOENT;
1311
    }
1495
    }
1312
    * device_id = route->netif->device_id;
1496
    * device_id = route->netif->device_id;
1313
    src = ip_netif_address( route->netif );
1497
    src = ip_netif_address( route->netif );
1314
    fibril_rwlock_read_unlock( & ip_globals.lock );
1498
    fibril_rwlock_read_unlock( & ip_globals.lock );
1315
    * headerlen = sizeof( * header_in );
1499
    * headerlen = sizeof( * header_in );
1316
    header_in = ( ipv4_pseudo_header_ref ) malloc( * headerlen );
1500
    header_in = ( ipv4_pseudo_header_ref ) malloc( * headerlen );
1317
    if( ! header_in ) return ENOMEM;
1501
    if( ! header_in ) return ENOMEM;
1318
    bzero( header_in, * headerlen );
1502
    bzero( header_in, * headerlen );
1319
    header_in->destination_address = dest->s_addr;
1503
    header_in->destination_address = dest->s_addr;
1320
    header_in->source_address = src->s_addr;
1504
    header_in->source_address = src->s_addr;
1321
    header_in->protocol = protocol;
1505
    header_in->protocol = protocol;
1322
    header_in->data_length = 0;
1506
    header_in->data_length = 0;
1323
    * header = ( ip_pseudo_header_ref ) header_in;
1507
    * header = ( ip_pseudo_header_ref ) header_in;
1324
    return EOK;
1508
    return EOK;
1325
}
1509
}
1326
 
1510
 
1327
/** @}
1511
/** @}
1328
 */
1512
 */
1329
 
1513