Subversion Repositories HelenOS

Rev

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

Rev 4730 Rev 4731
1
/*
1
/*
2
 * Copyright (c) 2008 Lukas Mejdrech
2
 * Copyright (c) 2008 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 icmp
29
/** @addtogroup icmp
30
 *  @{
30
 *  @{
31
 */
31
 */
32
 
32
 
33
/** @file
33
/** @file
34
 *  ICMP module implementation.
34
 *  ICMP module implementation.
35
 *  @see icmp.h
35
 *  @see icmp.h
36
 */
36
 */
37
 
37
 
38
#include <async.h>
38
#include <async.h>
39
#include <atomic.h>
39
#include <atomic.h>
40
#include <fibril.h>
40
#include <fibril.h>
41
#include <fibril_sync.h>
41
#include <fibril_sync.h>
42
#include <stdint.h>
42
#include <stdint.h>
43
 
43
 
44
#include <ipc/ipc.h>
44
#include <ipc/ipc.h>
45
#include <ipc/services.h>
45
#include <ipc/services.h>
46
 
46
 
47
#include <sys/types.h>
47
#include <sys/types.h>
48
 
48
 
49
#include "../../err.h"
49
#include "../../err.h"
50
#include "../../messages.h"
50
#include "../../messages.h"
51
#include "../../modules.h"
51
#include "../../modules.h"
52
 
52
 
53
#include "../../structures/packet/packet_client.h"
53
#include "../../structures/packet/packet_client.h"
54
 
54
 
55
#include "../../include/byteorder.h"
55
#include "../../include/byteorder.h"
56
#include "../../include/crc.h"
56
#include "../../include/crc.h"
57
#include "../../include/icmp_api.h"
57
#include "../../include/icmp_api.h"
58
#include "../../include/icmp_client.h"
58
#include "../../include/icmp_client.h"
59
#include "../../include/icmp_codes.h"
59
#include "../../include/icmp_codes.h"
60
#include "../../include/icmp_common.h"
60
#include "../../include/icmp_common.h"
61
#include "../../include/icmp_interface.h"
61
#include "../../include/icmp_interface.h"
62
#include "../../include/il_interface.h"
62
#include "../../include/il_interface.h"
63
#include "../../include/inet.h"
63
#include "../../include/inet.h"
64
#include "../../include/ip_client.h"
64
#include "../../include/ip_client.h"
65
#include "../../include/ip_interface.h"
65
#include "../../include/ip_interface.h"
66
#include "../../include/ip_protocols.h"
66
#include "../../include/ip_protocols.h"
67
#include "../../include/net_interface.h"
67
#include "../../include/net_interface.h"
68
#include "../../include/socket_codes.h"
68
#include "../../include/socket_codes.h"
69
#include "../../include/socket_errno.h"
69
#include "../../include/socket_errno.h"
70
 
70
 
71
#include "../../tl/tl_messages.h"
71
#include "../../tl/tl_messages.h"
72
 
72
 
73
#include "icmp.h"
73
#include "icmp.h"
74
#include "icmp_header.h"
74
#include "icmp_header.h"
75
#include "icmp_messages.h"
75
#include "icmp_messages.h"
76
#include "icmp_module.h"
76
#include "icmp_module.h"
77
 
77
 
78
/** Default ICMP error reporting.
78
/** Default ICMP error reporting.
79
 */
79
 */
80
#define NET_DEFAULT_ICMP_ERROR_REPORTING    true
80
#define NET_DEFAULT_ICMP_ERROR_REPORTING    true
81
 
81
 
82
/** Default ICMP echo replying.
82
/** Default ICMP echo replying.
83
 */
83
 */
84
#define NET_DEFAULT_ICMP_ECHO_REPLYING      true
84
#define NET_DEFAULT_ICMP_ECHO_REPLYING      true
85
 
85
 
86
/** Original datagram length in bytes transfered to the error notification message.
86
/** Original datagram length in bytes transfered to the error notification message.
87
 */
87
 */
88
#define ICMP_KEEP_LENGTH    8
88
#define ICMP_KEEP_LENGTH    8
89
 
89
 
90
/** Free identifier numbers pool start.
90
/** Free identifier numbers pool start.
91
 */
91
 */
92
#define ICMP_FREE_IDS_START 1
92
#define ICMP_FREE_IDS_START 1
93
 
93
 
94
/** Free identifier numbers pool end.
94
/** Free identifier numbers pool end.
95
 */
95
 */
96
#define ICMP_FREE_IDS_END   MAX_UINT16
96
#define ICMP_FREE_IDS_END   MAX_UINT16
97
 
97
 
98
/** Computes the ICMP datagram checksum.
98
/** Computes the ICMP datagram checksum.
99
 *  @param header The ICMP datagram header. Input/output parameter.
99
 *  @param header The ICMP datagram header. Input/output parameter.
100
 *  @param length The total datagram length. Input parameter.
100
 *  @param length The total datagram length. Input parameter.
101
 *  @returns The computed checksum.
101
 *  @returns The computed checksum.
102
 */
102
 */
103
#define ICMP_CHECKSUM( header, length )     htons( ip_checksum(( uint8_t * ) ( header ), ( length )))
103
#define ICMP_CHECKSUM( header, length )     htons( ip_checksum(( uint8_t * ) ( header ), ( length )))
104
 
104
 
105
/** An echo request datagrams pattern.
105
/** An echo request datagrams pattern.
106
 */
106
 */
107
#define ICMP_ECHO_TEXT                  "Hello from HelenOS."
107
#define ICMP_ECHO_TEXT                  "Hello from HelenOS."
108
 
108
 
109
/** Computes an ICMP reply data key.
109
/** Computes an ICMP reply data key.
110
 *  @param id The message identifier. Input parameter.
110
 *  @param id The message identifier. Input parameter.
111
 *  @param sequence The message sequence number. Input parameter.
111
 *  @param sequence The message sequence number. Input parameter.
112
 *  @returns The computed ICMP reply data key.
112
 *  @returns The computed ICMP reply data key.
113
 */
113
 */
114
#define ICMP_GET_REPLY_KEY( id, sequence )  ((( id ) << 16 ) | ( sequence & 0xFFFF ))
114
#define ICMP_GET_REPLY_KEY( id, sequence )  ((( id ) << 16 ) | ( sequence & 0xFFFF ))
115
 
115
 
116
/** Type definition of the ICMP reply timeout.
116
/** Type definition of the ICMP reply timeout.
117
 *  @see icmp_reply_timeout
117
 *  @see icmp_reply_timeout
118
 */
118
 */
119
typedef struct icmp_reply_timeout   icmp_reply_timeout_t;
119
typedef struct icmp_reply_timeout   icmp_reply_timeout_t;
120
 
120
 
121
/** Type definition of the ICMP reply timeout pointer.
121
/** Type definition of the ICMP reply timeout pointer.
122
 *  @see icmp_reply_timeout
122
 *  @see icmp_reply_timeout
123
 */
123
 */
124
typedef icmp_reply_timeout_t *  icmp_reply_timeout_ref;
124
typedef icmp_reply_timeout_t *  icmp_reply_timeout_ref;
125
 
125
 
126
/** ICMP reply timeout data.
126
/** ICMP reply timeout data.
127
 *  Used as a timeouting fibril argument.
127
 *  Used as a timeouting fibril argument.
128
 *  @see icmp_timeout_for_reply()
128
 *  @see icmp_timeout_for_reply()
129
 */
129
 */
130
struct icmp_reply_timeout{
130
struct icmp_reply_timeout{
131
    /** Reply data key.
131
    /** Reply data key.
132
     */
132
     */
133
    int         reply_key;
133
    int         reply_key;
134
    /** Timeout in microseconds.
134
    /** Timeout in microseconds.
135
     */
135
     */
136
    suseconds_t timeout;
136
    suseconds_t timeout;
137
};
137
};
138
 
138
 
139
/** Processes the received ICMP packet.
139
/** Processes the received ICMP packet.
140
 *  Is used as an entry point from the underlying IP module.
140
 *  Is used as an entry point from the underlying IP module.
141
 *  Releases the packet on error.
141
 *  Releases the packet on error.
142
 *  @param device_id The device identifier. Ignored parameter.
142
 *  @param device_id The device identifier. Ignored parameter.
143
 *  @param packet The received packet. Input/output parameter.
143
 *  @param packet The received packet. Input/output parameter.
144
 *  @param receiver The target service. Ignored parameter.
144
 *  @param receiver The target service. Ignored parameter.
145
 *  @param error The packet error reporting service. Prefixes the received packet. Input parameter.
145
 *  @param error The packet error reporting service. Prefixes the received packet. Input parameter.
146
 *  @returns EOK on success.
146
 *  @returns EOK on success.
147
 *  @returns Other error codes as defined for the icmp_process_packet() function.
147
 *  @returns Other error codes as defined for the icmp_process_packet() function.
148
 */
148
 */
149
int icmp_received_msg( device_id_t device_id, packet_t packet, services_t receiver, services_t error );
149
int icmp_received_msg( device_id_t device_id, packet_t packet, services_t receiver, services_t error );
150
 
150
 
151
/** Processes the received ICMP packet.
151
/** Processes the received ICMP packet.
152
 *  Notifies the destination socket application.
152
 *  Notifies the destination socket application.
153
 *  @param packet The received packet. Input/output parameter.
153
 *  @param packet The received packet. Input/output parameter.
154
 *  @param error The packet error reporting service. Prefixes the received packet. Input parameter.
154
 *  @param error The packet error reporting service. Prefixes the received packet. Input parameter.
155
 *  @returns EOK on success.
155
 *  @returns EOK on success.
156
 *  @returns EINVAL if the packet is not valid.
156
 *  @returns EINVAL if the packet is not valid.
157
 *  @returns EINVAL if the stored packet address is not the an_addr_t.
157
 *  @returns EINVAL if the stored packet address is not the an_addr_t.
158
 *  @returns EINVAL if the packet does not contain any data.
158
 *  @returns EINVAL if the packet does not contain any data.
159
 *  @returns NO_DATA if the packet content is shorter than the user datagram header.
159
 *  @returns NO_DATA if the packet content is shorter than the user datagram header.
160
 *  @returns ENOMEM if there is not enough memory left.
160
 *  @returns ENOMEM if there is not enough memory left.
161
 *  @returns EADDRNOTAVAIL if the destination socket does not exist.
161
 *  @returns EADDRNOTAVAIL if the destination socket does not exist.
162
 *  @returns Other error codes as defined for the ip_client_process_packet() function.
162
 *  @returns Other error codes as defined for the ip_client_process_packet() function.
163
 */
163
 */
164
int icmp_process_packet( packet_t packet, services_t error );
164
int icmp_process_packet( packet_t packet, services_t error );
165
 
165
 
166
/** Processes the client messages.
166
/** Processes the client messages.
167
 *  Remembers the assigned identifier and sequence numbers.
167
 *  Remembers the assigned identifier and sequence numbers.
168
 *  Runs until the client module disconnects.
168
 *  Runs until the client module disconnects.
169
 *  @param callid The message identifier. Input parameter.
169
 *  @param callid The message identifier. Input parameter.
170
 *  @param call The message parameters. Input parameter.
170
 *  @param call The message parameters. Input parameter.
171
 *  @returns EOK.
171
 *  @returns EOK.
172
 *  @see icmp_interface.h
172
 *  @see icmp_interface.h
173
 *  @see icmp_api.h
173
 *  @see icmp_api.h
174
 */
174
 */
175
int icmp_process_client_messages( ipc_callid_t callid, ipc_call_t call );
175
int icmp_process_client_messages( ipc_callid_t callid, ipc_call_t call );
176
 
176
 
177
/** Processes the generic client messages.
177
/** Processes the generic client messages.
178
 *  @param call The message parameters. Input parameter.
178
 *  @param call The message parameters. Input parameter.
179
 *  @returns EOK on success.
179
 *  @returns EOK on success.
180
 *  @returns ENOTSUP if the message is not known.
180
 *  @returns ENOTSUP if the message is not known.
181
 *  @returns Other error codes as defined for the packet_translate() function.
181
 *  @returns Other error codes as defined for the packet_translate() function.
182
 *  @returns Other error codes as defined for the icmp_destination_unreachable_msg() function.
182
 *  @returns Other error codes as defined for the icmp_destination_unreachable_msg() function.
183
 *  @returns Other error codes as defined for the icmp_source_quench_msg() function.
183
 *  @returns Other error codes as defined for the icmp_source_quench_msg() function.
184
 *  @returns Other error codes as defined for the icmp_time_exceeded_msg() function.
184
 *  @returns Other error codes as defined for the icmp_time_exceeded_msg() function.
185
 *  @returns Other error codes as defined for the icmp_parameter_problem_msg() function.
185
 *  @returns Other error codes as defined for the icmp_parameter_problem_msg() function.
186
 *  @see icmp_interface.h
186
 *  @see icmp_interface.h
187
 */
187
 */
188
int icmp_process_message( ipc_call_t * call );
188
int icmp_process_message( ipc_call_t * call );
189
 
189
 
190
/** Releases the packet and returns the result.
190
/** Releases the packet and returns the result.
191
 *  @param packet The packet queue to be released. Input parameter.
191
 *  @param packet The packet queue to be released. Input parameter.
192
 *  @param result The result to be returned. Input parameter.
192
 *  @param result The result to be returned. Input parameter.
193
 *  @returns The result parameter.
193
 *  @returns The result parameter.
194
 */
194
 */
195
int icmp_release_and_return( packet_t packet, int result );
195
int icmp_release_and_return( packet_t packet, int result );
196
 
196
 
197
/** Requests an echo message.
197
/** Requests an echo message.
198
 *  Sends a packet with specified parameters to the target host and waits for the reply upto the given timeout.
198
 *  Sends a packet with specified parameters to the target host and waits for the reply upto the given timeout.
199
 *  Blocks the caller until the reply or the timeout occurres.
199
 *  Blocks the caller until the reply or the timeout occurres.
200
 *  @param id The message identifier. Input parameter.
200
 *  @param id The message identifier. Input parameter.
201
 *  @param sequence The message sequence parameter. Input parameter.
201
 *  @param sequence The message sequence parameter. Input parameter.
202
 *  @param size The message data length in bytes. Input parameter.
202
 *  @param size The message data length in bytes. Input parameter.
203
 *  @param timeout The timeout in miliseconds. Input parameter.
203
 *  @param timeout The timeout in miliseconds. Input parameter.
204
 *  @param ttl The time to live. Input parameter.
204
 *  @param ttl The time to live. Input parameter.
205
 *  @param tos The type of service. Input parameter.
205
 *  @param tos The type of service. Input parameter.
206
 *  @param dont_fragment The value indicating whether the datagram must not be fragmented. Is used as a MTU discovery. Input parameter.
206
 *  @param dont_fragment The value indicating whether the datagram must not be fragmented. Is used as a MTU discovery. Input parameter.
207
 *  @param addr The target host address. Input parameter.
207
 *  @param addr The target host address. Input parameter.
208
 *  @param addrlen The torget host address length. Input parameter.
208
 *  @param addrlen The torget host address length. Input parameter.
209
 *  @returns ICMP_ECHO on success.
209
 *  @returns ICMP_ECHO on success.
210
 *  @returns ETIMEOUT if the reply has not arrived before the timeout.
210
 *  @returns ETIMEOUT if the reply has not arrived before the timeout.
211
 *  @returns ICMP type of the received error notification.
211
 *  @returns ICMP type of the received error notification.
212
 *  @returns EINVAL if the addrlen parameter is less or equal to zero (<=0).
212
 *  @returns EINVAL if the addrlen parameter is less or equal to zero (<=0).
213
 *  @returns ENOMEM if there is not enough memory left.
213
 *  @returns ENOMEM if there is not enough memory left.
214
 *  @returns EPARTY if there was an internal error.
214
 *  @returns EPARTY if there was an internal error.
215
 */
215
 */
216
int icmp_echo( icmp_param_t id, icmp_param_t sequence, size_t size, mseconds_t timeout, ip_ttl_t ttl, ip_tos_t tos, int dont_fragment, const struct sockaddr * addr, socklen_t addrlen );
216
int icmp_echo( icmp_param_t id, icmp_param_t sequence, size_t size, mseconds_t timeout, ip_ttl_t ttl, ip_tos_t tos, int dont_fragment, const struct sockaddr * addr, socklen_t addrlen );
217
 
217
 
218
/** Prepares the ICMP error packet.
218
/** Prepares the ICMP error packet.
219
 *  Truncates the original packet if longer than ICMP_KEEP_LENGTH bytes.
219
 *  Truncates the original packet if longer than ICMP_KEEP_LENGTH bytes.
220
 *  Prefixes and returns the ICMP header.
220
 *  Prefixes and returns the ICMP header.
221
 *  @param packet The original packet. Input/output parameter.
221
 *  @param packet The original packet. Input/output parameter.
222
 *  @returns The prefixed ICMP header.
222
 *  @returns The prefixed ICMP header.
223
 *  @returns NULL on errors.
223
 *  @returns NULL on errors.
224
 */
224
 */
225
icmp_header_ref icmp_prepare_packet( packet_t packet );
225
icmp_header_ref icmp_prepare_packet( packet_t packet );
226
 
226
 
227
/** Sends the ICMP message.
227
/** Sends the ICMP message.
228
 *  Sets the message type and code and computes the checksum.
228
 *  Sets the message type and code and computes the checksum.
229
 *  Error messages are sent only if allowed in the configuration.
229
 *  Error messages are sent only if allowed in the configuration.
230
 *  Releases the packet on errors.
230
 *  Releases the packet on errors.
-
 
231
 *  @param type The message type. Input parameter.
-
 
232
 *  @param code The message code. Input parameter.
-
 
233
 *  @param packet The message packet to be sent. Input parameter.
-
 
234
 *  @param header The ICMP header. Input parameter.
-
 
235
 *  @param error The error service to be announced. Should be SERVICE_ICMP or zero (0). Input parametr.
-
 
236
 *  @param ttl The time to live. Input parameter.
-
 
237
 *  @param tos The type of service. Input parameter.
-
 
238
 *  @param dont_fragment The value indicating whether the datagram must not be fragmented. Is used as a MTU discovery. Input parameter.
231
 *  @returns EOK on success.
239
 *  @returns EOK on success.
232
 *  @returns EPERM if the error message is not allowed.
240
 *  @returns EPERM if the error message is not allowed.
233
 */
241
 */
234
int icmp_send_packet( icmp_type_t type, icmp_code_t code, packet_t packet, icmp_header_ref header, services_t error );
242
int icmp_send_packet( icmp_type_t type, icmp_code_t code, packet_t packet, icmp_header_ref header, services_t error, ip_ttl_t ttl, ip_tos_t tos, int dont_fragment );
235
 
243
 
236
/** Tries to set the pending reply result as the received message type.
244
/** Tries to set the pending reply result as the received message type.
237
 *  If the reply data are still present, the reply timeouted and the parent fibril is awaken.
245
 *  If the reply data are still present, the reply timeouted and the parent fibril is awaken.
238
 *  The global lock is not released in this case to be reused by the parent fibril.
246
 *  The global lock is not released in this case to be reused by the parent fibril.
239
 *  Releases the packet.
247
 *  Releases the packet.
240
 *  @param packet The received reply message. Input parameter.
248
 *  @param packet The received reply message. Input parameter.
241
 *  @param header The ICMP message header. Input parameter.
249
 *  @param header The ICMP message header. Input parameter.
242
 *  @param type The received reply message type. Input parameter.
250
 *  @param type The received reply message type. Input parameter.
243
 *  @param code The received reply message code. Input parameter.
251
 *  @param code The received reply message code. Input parameter.
244
 *  @returns EOK.
252
 *  @returns EOK.
245
 */
253
 */
246
int icmp_process_echo_reply( packet_t packet, icmp_header_ref header, icmp_type_t type, icmp_code_t code );
254
int icmp_process_echo_reply( packet_t packet, icmp_header_ref header, icmp_type_t type, icmp_code_t code );
247
 
255
 
248
/** Tries to set the pending reply result as timeouted.
256
/** Tries to set the pending reply result as timeouted.
249
 *  Sleeps the timeout period of time and then tries to obtain and set the pending reply result as timeouted and signals the reply result.
257
 *  Sleeps the timeout period of time and then tries to obtain and set the pending reply result as timeouted and signals the reply result.
250
 *  If the reply data are still present, the reply timeouted and the parent fibril is awaken.
258
 *  If the reply data are still present, the reply timeouted and the parent fibril is awaken.
251
 *  The global lock is not released in this case to be reused by the parent fibril.
259
 *  The global lock is not released in this case to be reused by the parent fibril.
252
 *  Should run in a searate fibril.
260
 *  Should run in a searate fibril.
253
 *  @param data The icmp_reply_timeout structure. Input parameter.
261
 *  @param data The icmp_reply_timeout structure. Input parameter.
254
 *  @returns EOK on success.
262
 *  @returns EOK on success.
255
 *  @returns EINVAL if the data parameter is NULL.
263
 *  @returns EINVAL if the data parameter is NULL.
256
 */
264
 */
257
int icmp_timeout_for_reply( void * data );
265
int icmp_timeout_for_reply( void * data );
258
 
266
 
259
/** Assigns a new identifier for the connection.
267
/** Assigns a new identifier for the connection.
260
 *  Fills the echo data parameter with the assigned values.
268
 *  Fills the echo data parameter with the assigned values.
261
 *  @param echo_data The echo data to be bound. Input/output parameter.
269
 *  @param echo_data The echo data to be bound. Input/output parameter.
262
 *  @returns Index of the inserted echo data.
270
 *  @returns Index of the inserted echo data.
263
 *  @returns EBADMEM if the echo_data parameter is NULL.
271
 *  @returns EBADMEM if the echo_data parameter is NULL.
264
 *  @returns ENOTCONN if no free identifier have been found.
272
 *  @returns ENOTCONN if no free identifier have been found.
265
 */
273
 */
266
int icmp_bind_free_id( icmp_echo_ref echo_data );
274
int icmp_bind_free_id( icmp_echo_ref echo_data );
267
 
275
 
268
/** ICMP global data.
276
/** ICMP global data.
269
 */
277
 */
270
icmp_globals_t  icmp_globals;
278
icmp_globals_t  icmp_globals;
271
 
279
 
272
INT_MAP_IMPLEMENT( icmp_replies, icmp_reply_t );
280
INT_MAP_IMPLEMENT( icmp_replies, icmp_reply_t );
273
 
281
 
274
INT_MAP_IMPLEMENT( icmp_echo_data, icmp_echo_t );
282
INT_MAP_IMPLEMENT( icmp_echo_data, icmp_echo_t );
275
 
283
 
276
int icmp_echo_msg( int icmp_phone, size_t size, mseconds_t timeout, ip_ttl_t ttl, ip_tos_t tos, int dont_fragment, const struct sockaddr * addr, socklen_t addrlen ){
284
int icmp_echo_msg( int icmp_phone, size_t size, mseconds_t timeout, ip_ttl_t ttl, ip_tos_t tos, int dont_fragment, const struct sockaddr * addr, socklen_t addrlen ){
277
    icmp_echo_ref   echo_data;
285
    icmp_echo_ref   echo_data;
278
    int             res;
286
    int             res;
279
 
287
 
280
    fibril_rwlock_write_lock( & icmp_globals.lock );
288
    fibril_rwlock_write_lock( & icmp_globals.lock );
281
    // use the phone as the echo data index
289
    // use the phone as the echo data index
282
    echo_data = icmp_echo_data_find( & icmp_globals.echo_data, icmp_phone );
290
    echo_data = icmp_echo_data_find( & icmp_globals.echo_data, icmp_phone );
283
    if( ! echo_data ){
291
    if( ! echo_data ){
284
        res = ENOENT;
292
        res = ENOENT;
285
    }else{
293
    }else{
286
        res = icmp_echo( echo_data->identifier, echo_data->sequence_number, size, timeout, ttl, tos, dont_fragment, addr, addrlen );
294
        res = icmp_echo( echo_data->identifier, echo_data->sequence_number, size, timeout, ttl, tos, dont_fragment, addr, addrlen );
287
        if( echo_data->sequence_number < MAX_UINT16 ){
295
        if( echo_data->sequence_number < MAX_UINT16 ){
288
            ++ echo_data->sequence_number;
296
            ++ echo_data->sequence_number;
289
        }else{
297
        }else{
290
            echo_data->sequence_number = 0;
298
            echo_data->sequence_number = 0;
291
        }
299
        }
292
    }
300
    }
293
    fibril_rwlock_write_unlock( & icmp_globals.lock );
301
    fibril_rwlock_write_unlock( & icmp_globals.lock );
294
    return res;
302
    return res;
295
}
303
}
296
 
304
 
297
int icmp_timeout_for_reply( void * data ){
305
int icmp_timeout_for_reply( void * data ){
298
    icmp_reply_ref          reply;
306
    icmp_reply_ref          reply;
299
    icmp_reply_timeout_ref  timeout = data;
307
    icmp_reply_timeout_ref  timeout = data;
300
 
308
 
301
    if( ! timeout ){
309
    if( ! timeout ){
302
        return EINVAL;
310
        return EINVAL;
303
    }
311
    }
304
    // sleep the given timeout
312
    // sleep the given timeout
305
    async_usleep( timeout->timeout );
313
    async_usleep( timeout->timeout );
306
    // lock the globals
314
    // lock the globals
307
    fibril_rwlock_write_lock( & icmp_globals.lock );
315
    fibril_rwlock_write_lock( & icmp_globals.lock );
308
    // find the pending reply
316
    // find the pending reply
309
    reply = icmp_replies_find( & icmp_globals.replies, timeout->reply_key );
317
    reply = icmp_replies_find( & icmp_globals.replies, timeout->reply_key );
310
    if( reply ){
318
    if( reply ){
311
        // set the timeout result
319
        // set the timeout result
312
        reply->result = ETIMEOUT;
320
        reply->result = ETIMEOUT;
313
        // notify the main fibril
321
        // notify the main fibril
314
        fibril_condvar_signal( & reply->condvar );
322
        fibril_condvar_signal( & reply->condvar );
315
    }else{
323
    }else{
316
        // unlock only if no reply
324
        // unlock only if no reply
317
        fibril_rwlock_write_unlock( & icmp_globals.lock );
325
        fibril_rwlock_write_unlock( & icmp_globals.lock );
318
    }
326
    }
319
    // release the timeout structure
327
    // release the timeout structure
320
    free( timeout );
328
    free( timeout );
321
    return EOK;
329
    return EOK;
322
}
330
}
323
 
331
 
324
int icmp_echo( icmp_param_t id, icmp_param_t sequence, size_t size, mseconds_t timeout, ip_ttl_t ttl, ip_tos_t tos, int dont_fragment, const struct sockaddr * addr, socklen_t addrlen ){
332
int icmp_echo( icmp_param_t id, icmp_param_t sequence, size_t size, mseconds_t timeout, ip_ttl_t ttl, ip_tos_t tos, int dont_fragment, const struct sockaddr * addr, socklen_t addrlen ){
325
    ERROR_DECLARE;
333
    ERROR_DECLARE;
326
 
334
 
327
    icmp_header_ref header;
335
    icmp_header_ref header;
328
    packet_t        packet;
336
    packet_t        packet;
329
    size_t          length;
337
    size_t          length;
330
    uint8_t *       data;
338
    uint8_t *       data;
331
    icmp_reply_ref          reply;
339
    icmp_reply_ref          reply;
332
    icmp_reply_timeout_ref  reply_timeout;
340
    icmp_reply_timeout_ref  reply_timeout;
333
    int             result;
341
    int             result;
334
    int             index;
342
    int             index;
335
    fid_t           fibril;
343
    fid_t           fibril;
336
 
344
 
337
    if( addrlen <= 0 ){
345
    if( addrlen <= 0 ){
338
        return EINVAL;
346
        return EINVAL;
339
    }
347
    }
340
    length = ( size_t ) addrlen;
348
    length = ( size_t ) addrlen;
341
    // TODO do not ask all the time
349
    // TODO do not ask all the time
342
    ERROR_PROPAGATE( ip_packet_size_req( icmp_globals.ip_phone, -1, & icmp_globals.addr_len, & icmp_globals.prefix, & icmp_globals.content, & icmp_globals.suffix ));
350
    ERROR_PROPAGATE( ip_packet_size_req( icmp_globals.ip_phone, -1, & icmp_globals.addr_len, & icmp_globals.prefix, & icmp_globals.content, & icmp_globals.suffix ));
343
    packet = packet_get_4( icmp_globals.net_phone, size, icmp_globals.addr_len, sizeof( icmp_header_t ) + icmp_globals.prefix, icmp_globals.suffix );
351
    packet = packet_get_4( icmp_globals.net_phone, size, icmp_globals.addr_len, sizeof( icmp_header_t ) + icmp_globals.prefix, icmp_globals.suffix );
344
    if( ! packet ) return ENOMEM;
352
    if( ! packet ) return ENOMEM;
345
 
353
 
346
    // prepare the requesting packet
354
    // prepare the requesting packet
347
    // set the destination address
355
    // set the destination address
348
    if( ERROR_OCCURRED( packet_set_addr( packet, NULL, ( const uint8_t * ) addr, length ))){
356
    if( ERROR_OCCURRED( packet_set_addr( packet, NULL, ( const uint8_t * ) addr, length ))){
349
        return icmp_release_and_return( packet, ERROR_CODE );
357
        return icmp_release_and_return( packet, ERROR_CODE );
350
    }
358
    }
351
    // allocate space in the packet
359
    // allocate space in the packet
352
    data = ( uint8_t * ) packet_suffix( packet, size );
360
    data = ( uint8_t * ) packet_suffix( packet, size );
353
    if( ! data ){
361
    if( ! data ){
354
        return icmp_release_and_return( packet, ENOMEM );
362
        return icmp_release_and_return( packet, ENOMEM );
355
    }
363
    }
356
    // fill the data
364
    // fill the data
357
    length = 0;
365
    length = 0;
358
    while( size > length + sizeof( ICMP_ECHO_TEXT )){
366
    while( size > length + sizeof( ICMP_ECHO_TEXT )){
359
        memcpy( data + length, ICMP_ECHO_TEXT, sizeof( ICMP_ECHO_TEXT ));
367
        memcpy( data + length, ICMP_ECHO_TEXT, sizeof( ICMP_ECHO_TEXT ));
360
        length += sizeof( ICMP_ECHO_TEXT );
368
        length += sizeof( ICMP_ECHO_TEXT );
361
    }
369
    }
362
    memcpy( data + length, ICMP_ECHO_TEXT, size - length );
370
    memcpy( data + length, ICMP_ECHO_TEXT, size - length );
363
    // prefix the header
371
    // prefix the header
364
    header = PACKET_PREFIX( packet, icmp_header_t );
372
    header = PACKET_PREFIX( packet, icmp_header_t );
365
    if( ! header ){
373
    if( ! header ){
366
        return icmp_release_and_return( packet, ENOMEM );
374
        return icmp_release_and_return( packet, ENOMEM );
367
    }
375
    }
368
    bzero( header, sizeof( * header ));
376
    bzero( header, sizeof( * header ));
369
    header->un.echo.identifier = id;
377
    header->un.echo.identifier = id;
370
    header->un.echo.sequence_number = sequence;
378
    header->un.echo.sequence_number = sequence;
371
 
379
 
372
    // prepare the reply and the reply timeout structures
380
    // prepare the reply and the reply timeout structures
373
    reply_timeout = malloc( sizeof( * reply_timeout ));
381
    reply_timeout = malloc( sizeof( * reply_timeout ));
374
    if( ! reply_timeout ){
382
    if( ! reply_timeout ){
375
        return icmp_release_and_return( packet, ENOMEM );
383
        return icmp_release_and_return( packet, ENOMEM );
376
    }
384
    }
377
    reply = malloc( sizeof( * reply ));
385
    reply = malloc( sizeof( * reply ));
378
    if( ! reply ){
386
    if( ! reply ){
379
        free( reply_timeout );
387
        free( reply_timeout );
380
        return icmp_release_and_return( packet, ENOMEM );
388
        return icmp_release_and_return( packet, ENOMEM );
381
    }
389
    }
382
    // prepare the timeouting thread
390
    // prepare the timeouting thread
383
    fibril = fibril_create( icmp_timeout_for_reply, reply_timeout );
391
    fibril = fibril_create( icmp_timeout_for_reply, reply_timeout );
384
    if( ! fibril ){
392
    if( ! fibril ){
385
        free( reply );
393
        free( reply );
386
        free( reply_timeout );
394
        free( reply_timeout );
387
        return icmp_release_and_return( packet, EPARTY );
395
        return icmp_release_and_return( packet, EPARTY );
388
    }
396
    }
389
    reply_timeout->reply_key = ICMP_GET_REPLY_KEY( header->un.echo.identifier, header->un.echo.sequence_number );
397
    reply_timeout->reply_key = ICMP_GET_REPLY_KEY( header->un.echo.identifier, header->un.echo.sequence_number );
390
    // timeout in microseconds
398
    // timeout in microseconds
391
    reply_timeout->timeout = timeout * 1000;
399
    reply_timeout->timeout = timeout * 1000;
392
    fibril_mutex_initialize( & reply->mutex );
400
    fibril_mutex_initialize( & reply->mutex );
393
    fibril_mutex_lock( & reply->mutex );
401
    fibril_mutex_lock( & reply->mutex );
394
    fibril_condvar_initialize( & reply->condvar );
402
    fibril_condvar_initialize( & reply->condvar );
395
    // start the timeouting fibril
403
    // start the timeouting fibril
396
    fibril_add_ready( fibril );
404
    fibril_add_ready( fibril );
397
    index = icmp_replies_add( & icmp_globals.replies, reply_timeout->reply_key, reply );
405
    index = icmp_replies_add( & icmp_globals.replies, reply_timeout->reply_key, reply );
398
    if( index < 0 ){
406
    if( index < 0 ){
399
        free( reply );
407
        free( reply );
400
        return icmp_release_and_return( packet, index );
408
        return icmp_release_and_return( packet, index );
401
    }
409
    }
402
 
410
 
403
    // unlock the globals and wait for a reply
411
    // unlock the globals and wait for a reply
404
    fibril_rwlock_write_unlock( & icmp_globals.lock );
412
    fibril_rwlock_write_unlock( & icmp_globals.lock );
405
 
413
 
406
    // send the request
414
    // send the request
407
    icmp_send_packet( ICMP_ECHO, 0, packet, header, 0 );
415
    icmp_send_packet( ICMP_ECHO, 0, packet, header, 0, ttl, tos, dont_fragment );
408
 
416
 
409
    // wait for a reply
417
    // wait for a reply
410
    fibril_condvar_wait( & reply->condvar, & reply->mutex );
418
    fibril_condvar_wait( & reply->condvar, & reply->mutex );
411
 
419
 
412
    // read the result
420
    // read the result
413
    result = reply->result;
421
    result = reply->result;
414
 
422
 
415
    // destroy the reply structure
423
    // destroy the reply structure
416
    fibril_mutex_unlock( & reply->mutex );
424
    fibril_mutex_unlock( & reply->mutex );
417
    icmp_replies_exclude_index( & icmp_globals.replies, index );
425
    icmp_replies_exclude_index( & icmp_globals.replies, index );
418
    return result;
426
    return result;
419
}
427
}
420
 
428
 
421
int icmp_destination_unreachable_msg( int icmp_phone, icmp_code_t code, icmp_param_t mtu, packet_t packet ){
429
int icmp_destination_unreachable_msg( int icmp_phone, icmp_code_t code, icmp_param_t mtu, packet_t packet ){
422
    icmp_header_ref header;
430
    icmp_header_ref header;
423
 
431
 
424
    header = icmp_prepare_packet( packet );
432
    header = icmp_prepare_packet( packet );
425
    if( ! header ){
433
    if( ! header ){
426
        return icmp_release_and_return( packet, ENOMEM );
434
        return icmp_release_and_return( packet, ENOMEM );
427
    }
435
    }
428
    if( mtu ){
436
    if( mtu ){
429
        header->un.frag.mtu = mtu;
437
        header->un.frag.mtu = mtu;
430
    }
438
    }
431
    return icmp_send_packet( ICMP_DEST_UNREACH, code, packet, header, SERVICE_ICMP );
439
    return icmp_send_packet( ICMP_DEST_UNREACH, code, packet, header, SERVICE_ICMP, 0, 0, 0 );
432
}
440
}
433
 
441
 
434
int icmp_source_quench_msg( int icmp_phone, packet_t packet ){
442
int icmp_source_quench_msg( int icmp_phone, packet_t packet ){
435
    icmp_header_ref header;
443
    icmp_header_ref header;
436
 
444
 
437
    header = icmp_prepare_packet( packet );
445
    header = icmp_prepare_packet( packet );
438
    if( ! header ){
446
    if( ! header ){
439
        return icmp_release_and_return( packet, ENOMEM );
447
        return icmp_release_and_return( packet, ENOMEM );
440
    }
448
    }
441
    return icmp_send_packet( ICMP_SOURCE_QUENCH, 0, packet, header, SERVICE_ICMP );
449
    return icmp_send_packet( ICMP_SOURCE_QUENCH, 0, packet, header, SERVICE_ICMP, 0, 0, 0 );
442
}
450
}
443
 
451
 
444
int icmp_time_exceeded_msg( int icmp_phone, icmp_code_t code, packet_t packet ){
452
int icmp_time_exceeded_msg( int icmp_phone, icmp_code_t code, packet_t packet ){
445
    icmp_header_ref header;
453
    icmp_header_ref header;
446
 
454
 
447
    header = icmp_prepare_packet( packet );
455
    header = icmp_prepare_packet( packet );
448
    if( ! header ){
456
    if( ! header ){
449
        return icmp_release_and_return( packet, ENOMEM );
457
        return icmp_release_and_return( packet, ENOMEM );
450
    }
458
    }
451
    return icmp_send_packet( ICMP_TIME_EXCEEDED, code, packet, header, SERVICE_ICMP );
459
    return icmp_send_packet( ICMP_TIME_EXCEEDED, code, packet, header, SERVICE_ICMP, 0, 0, 0 );
452
}
460
}
453
 
461
 
454
int icmp_parameter_problem_msg( int icmp_phone, icmp_code_t code, icmp_param_t pointer, packet_t packet ){
462
int icmp_parameter_problem_msg( int icmp_phone, icmp_code_t code, icmp_param_t pointer, packet_t packet ){
455
    icmp_header_ref header;
463
    icmp_header_ref header;
456
 
464
 
457
    header = icmp_prepare_packet( packet );
465
    header = icmp_prepare_packet( packet );
458
    if( ! header ){
466
    if( ! header ){
459
        return icmp_release_and_return( packet, ENOMEM );
467
        return icmp_release_and_return( packet, ENOMEM );
460
    }
468
    }
461
    header->un.param.pointer = pointer;
469
    header->un.param.pointer = pointer;
462
    return icmp_send_packet( ICMP_PARAMETERPROB, code, packet, header, SERVICE_ICMP );
470
    return icmp_send_packet( ICMP_PARAMETERPROB, code, packet, header, SERVICE_ICMP, 0, 0, 0 );
463
}
471
}
464
 
472
 
465
icmp_header_ref icmp_prepare_packet( packet_t packet ){
473
icmp_header_ref icmp_prepare_packet( packet_t packet ){
466
    icmp_header_ref header;
474
    icmp_header_ref header;
467
    size_t          header_length;
475
    size_t          header_length;
468
    size_t          total_length;
476
    size_t          total_length;
469
 
477
 
470
    total_length = packet_get_data_length( packet );
478
    total_length = packet_get_data_length( packet );
471
    if( total_length <= 0 ) return NULL;
479
    if( total_length <= 0 ) return NULL;
472
    header_length = ip_client_header_length( packet );
480
    header_length = ip_client_header_length( packet );
473
    if( header_length <= 0 ) return NULL;
481
    if( header_length <= 0 ) return NULL;
474
    // truncate if longer than 64 bits (without the IP header)
482
    // truncate if longer than 64 bits (without the IP header)
475
    if(( total_length > header_length + ICMP_KEEP_LENGTH )
483
    if(( total_length > header_length + ICMP_KEEP_LENGTH )
476
    && ( packet_trim( packet, 0, total_length - header_length - ICMP_KEEP_LENGTH ) != EOK )){
484
    && ( packet_trim( packet, 0, total_length - header_length - ICMP_KEEP_LENGTH ) != EOK )){
477
        return NULL;
485
        return NULL;
478
    }
486
    }
479
    header = PACKET_PREFIX( packet, icmp_header_t );
487
    header = PACKET_PREFIX( packet, icmp_header_t );
480
    if( ! header ) return NULL;
488
    if( ! header ) return NULL;
481
    bzero( header, sizeof( * header ));
489
    bzero( header, sizeof( * header ));
482
    return header;
490
    return header;
483
}
491
}
484
 
492
 
485
int icmp_send_packet( icmp_type_t type, icmp_code_t code, packet_t packet, icmp_header_ref header, services_t error ){
493
int icmp_send_packet( icmp_type_t type, icmp_code_t code, packet_t packet, icmp_header_ref header, services_t error, ip_ttl_t ttl, ip_tos_t tos, int dont_fragment ){
486
    ERROR_DECLARE;
494
    ERROR_DECLARE;
487
 
495
 
488
    // do not send an error if disabled
496
    // do not send an error if disabled
489
    if( error && ( ! icmp_globals.error_reporting )){
497
    if( error && ( ! icmp_globals.error_reporting )){
490
        return icmp_release_and_return( packet, EPERM );
498
        return icmp_release_and_return( packet, EPERM );
491
    }
499
    }
492
    header->type = type;
500
    header->type = type;
493
    header->code = code;
501
    header->code = code;
494
    header->checksum = 0;
502
    header->checksum = 0;
495
    header->checksum = ICMP_CHECKSUM( header, packet_get_data_length( packet ));
503
    header->checksum = ICMP_CHECKSUM( header, packet_get_data_length( packet ));
496
    if( ERROR_OCCURRED( ip_client_prepare_packet( packet, IPPROTO_ICMP, 0, 0, 0, 0 ))){
504
    if( ERROR_OCCURRED( ip_client_prepare_packet( packet, IPPROTO_ICMP, ttl, tos, dont_fragment, 0 ))){
497
        return icmp_release_and_return( packet, ERROR_CODE );
505
        return icmp_release_and_return( packet, ERROR_CODE );
498
    }
506
    }
499
    return ip_send_msg( icmp_globals.ip_phone, -1, packet, SERVICE_ICMP, error );
507
    return ip_send_msg( icmp_globals.ip_phone, -1, packet, SERVICE_ICMP, error );
500
}
508
}
501
 
509
 
502
int icmp_connect_module( services_t service ){
510
int icmp_connect_module( services_t service ){
503
    icmp_echo_ref   echo_data;
511
    icmp_echo_ref   echo_data;
504
    icmp_param_t    id;
512
    icmp_param_t    id;
505
    int             index;
513
    int             index;
506
 
514
 
507
    echo_data = ( icmp_echo_ref ) malloc( sizeof( * echo_data ));
515
    echo_data = ( icmp_echo_ref ) malloc( sizeof( * echo_data ));
508
    if( ! echo_data ) return ENOMEM;
516
    if( ! echo_data ) return ENOMEM;
509
    // assign a new identifier
517
    // assign a new identifier
510
    fibril_rwlock_write_lock( & icmp_globals.lock );
518
    fibril_rwlock_write_lock( & icmp_globals.lock );
511
    index = icmp_bind_free_id( echo_data );
519
    index = icmp_bind_free_id( echo_data );
512
    if( index < 0 ){
520
    if( index < 0 ){
513
        free( echo_data );
521
        free( echo_data );
514
        fibril_rwlock_write_unlock( & icmp_globals.lock );
522
        fibril_rwlock_write_unlock( & icmp_globals.lock );
515
        return index;
523
        return index;
516
    }else{
524
    }else{
517
        id = echo_data->identifier;
525
        id = echo_data->identifier;
518
        fibril_rwlock_write_unlock( & icmp_globals.lock );
526
        fibril_rwlock_write_unlock( & icmp_globals.lock );
519
        // return the echo data identifier as the ICMP phone
527
        // return the echo data identifier as the ICMP phone
520
        return id;
528
        return id;
521
    }
529
    }
522
}
530
}
523
 
531
 
524
int icmp_initialize( async_client_conn_t client_connection ){
532
int icmp_initialize( async_client_conn_t client_connection ){
525
    ERROR_DECLARE;
533
    ERROR_DECLARE;
526
 
534
 
527
    measured_string_t   names[] = {{ "ICMP_ERROR_REPORTING", 20 }, { "ICMP_ECHO_REPLYING", 18 }};
535
    measured_string_t   names[] = {{ "ICMP_ERROR_REPORTING", 20 }, { "ICMP_ECHO_REPLYING", 18 }};
528
    measured_string_ref configuration;
536
    measured_string_ref configuration;
529
    size_t              count = sizeof( names ) / sizeof( measured_string_t );
537
    size_t              count = sizeof( names ) / sizeof( measured_string_t );
530
    char *              data;
538
    char *              data;
531
 
539
 
532
    fibril_rwlock_initialize( & icmp_globals.lock );
540
    fibril_rwlock_initialize( & icmp_globals.lock );
533
    fibril_rwlock_write_lock( & icmp_globals.lock );
541
    fibril_rwlock_write_lock( & icmp_globals.lock );
534
    icmp_replies_initialize( & icmp_globals.replies );
542
    icmp_replies_initialize( & icmp_globals.replies );
535
    icmp_echo_data_initialize( & icmp_globals.echo_data );
543
    icmp_echo_data_initialize( & icmp_globals.echo_data );
536
    icmp_globals.ip_phone = ip_bind_service( SERVICE_IP, IPPROTO_ICMP, SERVICE_ICMP, client_connection, icmp_received_msg );
544
    icmp_globals.ip_phone = ip_bind_service( SERVICE_IP, IPPROTO_ICMP, SERVICE_ICMP, client_connection, icmp_received_msg );
537
    if( icmp_globals.ip_phone < 0 ){
545
    if( icmp_globals.ip_phone < 0 ){
538
        return icmp_globals.ip_phone;
546
        return icmp_globals.ip_phone;
539
    }
547
    }
540
    ERROR_PROPAGATE( ip_packet_size_req( icmp_globals.ip_phone, -1, & icmp_globals.addr_len, & icmp_globals.prefix, & icmp_globals.content, & icmp_globals.suffix ));
548
    ERROR_PROPAGATE( ip_packet_size_req( icmp_globals.ip_phone, -1, & icmp_globals.addr_len, & icmp_globals.prefix, & icmp_globals.content, & icmp_globals.suffix ));
541
    icmp_globals.prefix += sizeof( icmp_header_t );
549
    icmp_globals.prefix += sizeof( icmp_header_t );
542
    icmp_globals.content -= sizeof( icmp_header_t );
550
    icmp_globals.content -= sizeof( icmp_header_t );
543
    // get configuration
551
    // get configuration
544
    icmp_globals.error_reporting = NET_DEFAULT_ICMP_ERROR_REPORTING;
552
    icmp_globals.error_reporting = NET_DEFAULT_ICMP_ERROR_REPORTING;
545
    icmp_globals.echo_replying = NET_DEFAULT_ICMP_ECHO_REPLYING;
553
    icmp_globals.echo_replying = NET_DEFAULT_ICMP_ECHO_REPLYING;
546
    configuration = & names[ 0 ];
554
    configuration = & names[ 0 ];
547
    ERROR_PROPAGATE( net_get_conf_req( icmp_globals.net_phone, & configuration, count, & data ));
555
    ERROR_PROPAGATE( net_get_conf_req( icmp_globals.net_phone, & configuration, count, & data ));
548
    if( configuration ){
556
    if( configuration ){
549
        if( configuration[ 0 ].value ){
557
        if( configuration[ 0 ].value ){
550
            icmp_globals.error_reporting = ( configuration[ 0 ].value[ 0 ] == 'y' );
558
            icmp_globals.error_reporting = ( configuration[ 0 ].value[ 0 ] == 'y' );
551
        }
559
        }
552
        if( configuration[ 1 ].value ){
560
        if( configuration[ 1 ].value ){
553
            icmp_globals.echo_replying = ( configuration[ 1 ].value[ 0 ] == 'y' );
561
            icmp_globals.echo_replying = ( configuration[ 1 ].value[ 0 ] == 'y' );
554
        }
562
        }
555
        net_free_settings( configuration, data );
563
        net_free_settings( configuration, data );
556
    }
564
    }
557
    fibril_rwlock_write_unlock( & icmp_globals.lock );
565
    fibril_rwlock_write_unlock( & icmp_globals.lock );
558
    return EOK;
566
    return EOK;
559
}
567
}
560
 
568
 
561
int icmp_received_msg( device_id_t device_id, packet_t packet, services_t receiver, services_t error ){
569
int icmp_received_msg( device_id_t device_id, packet_t packet, services_t receiver, services_t error ){
562
    ERROR_DECLARE;
570
    ERROR_DECLARE;
563
 
571
 
564
    if( ERROR_OCCURRED( icmp_process_packet( packet, error ))){
572
    if( ERROR_OCCURRED( icmp_process_packet( packet, error ))){
565
        return icmp_release_and_return( packet, ERROR_CODE );
573
        return icmp_release_and_return( packet, ERROR_CODE );
566
    }
574
    }
567
 
575
 
568
    return EOK;
576
    return EOK;
569
}
577
}
570
 
578
 
571
int icmp_process_packet( packet_t packet, services_t error ){
579
int icmp_process_packet( packet_t packet, services_t error ){
572
    ERROR_DECLARE;
580
    ERROR_DECLARE;
573
 
581
 
574
    size_t          length;
582
    size_t          length;
575
    uint8_t *       src;
583
    uint8_t *       src;
576
    int             addrlen;
584
    int             addrlen;
577
    int             result;
585
    int             result;
578
    void *          data;
586
    void *          data;
579
    icmp_header_ref header;
587
    icmp_header_ref header;
580
    icmp_type_t     type;
588
    icmp_type_t     type;
581
    icmp_code_t     code;
589
    icmp_code_t     code;
582
 
590
 
583
    if( error ){
591
    if( error ){
584
        switch( error ){
592
        switch( error ){
585
            case SERVICE_ICMP:
593
            case SERVICE_ICMP:
586
                // process error
594
                // process error
587
                result = icmp_client_process_packet( packet, & type, & code, NULL, NULL );
595
                result = icmp_client_process_packet( packet, & type, & code, NULL, NULL );
588
                if( result < 0 ) return result;
596
                if( result < 0 ) return result;
589
                length = ( size_t ) result;
597
                length = ( size_t ) result;
590
                // TODO remove debug dump
-
 
591
                printf( "ICMP error %d (%d) in packet %d\n", type, code, packet_get_id( packet ) );
-
 
592
                // remove the error header
598
                // remove the error header
593
                ERROR_PROPAGATE( packet_trim( packet, length, 0 ));
599
                ERROR_PROPAGATE( packet_trim( packet, length, 0 ));
594
                break;
600
                break;
595
            default:
601
            default:
596
                return ENOTSUP;
602
                return ENOTSUP;
597
        }
603
        }
598
    }
604
    }
599
    // get rid of the ip header
605
    // get rid of the ip header
600
    length = ip_client_header_length( packet );
606
    length = ip_client_header_length( packet );
601
    ERROR_PROPAGATE( packet_trim( packet, length, 0 ));
607
    ERROR_PROPAGATE( packet_trim( packet, length, 0 ));
602
 
608
 
603
    length = packet_get_data_length( packet );
609
    length = packet_get_data_length( packet );
604
    if( length <= 0 ) return EINVAL;
610
    if( length <= 0 ) return EINVAL;
605
    if( length < sizeof( icmp_header_t )) return EINVAL;
611
    if( length < sizeof( icmp_header_t )) return EINVAL;
606
    data = packet_get_data( packet );
612
    data = packet_get_data( packet );
607
    if( ! data ) return EINVAL;
613
    if( ! data ) return EINVAL;
608
    // get icmp header
614
    // get icmp header
609
    header = ( icmp_header_ref ) data;
615
    header = ( icmp_header_ref ) data;
610
    // checksum
616
    // checksum
611
    if( header->checksum ){
617
    if( header->checksum ){
612
        while( ICMP_CHECKSUM( header, length )){
618
        while( ICMP_CHECKSUM( header, length )){
613
            // set the original message type on error notification
619
            // set the original message type on error notification
614
            // type swap observed in Qemu
620
            // type swap observed in Qemu
615
            if( error ){
621
            if( error ){
616
                switch( header->type ){
622
                switch( header->type ){
617
                    case ICMP_ECHOREPLY:
623
                    case ICMP_ECHOREPLY:
618
                        header->type = ICMP_ECHO;
624
                        header->type = ICMP_ECHO;
619
                        continue;
625
                        continue;
620
                }
626
                }
621
            }
627
            }
622
            return EINVAL;
628
            return EINVAL;
623
        }
629
        }
624
    }
630
    }
625
    switch( header->type ){
631
    switch( header->type ){
626
        case ICMP_ECHOREPLY:
632
        case ICMP_ECHOREPLY:
627
            if( error ){
633
            if( error ){
628
                return icmp_process_echo_reply( packet, header, type, code );
634
                return icmp_process_echo_reply( packet, header, type, code );
629
            }else{
635
            }else{
630
                return icmp_process_echo_reply( packet, header, ICMP_ECHO, 0 );
636
                return icmp_process_echo_reply( packet, header, ICMP_ECHO, 0 );
631
            }
637
            }
632
        case ICMP_ECHO:
638
        case ICMP_ECHO:
633
            if( error ){
639
            if( error ){
634
                return icmp_process_echo_reply( packet, header, type, code );
640
                return icmp_process_echo_reply( packet, header, type, code );
635
            // do not send a reply if disabled
641
            // do not send a reply if disabled
636
            }else if( icmp_globals.echo_replying ){
642
            }else if( icmp_globals.echo_replying ){
637
                addrlen = packet_get_addr( packet, & src, NULL );
643
                addrlen = packet_get_addr( packet, & src, NULL );
638
                if(( addrlen > 0 )
644
                if(( addrlen > 0 )
639
                // set both addresses to the source one (avoids the source address deletion before setting the destination one)
645
                // set both addresses to the source one (avoids the source address deletion before setting the destination one)
640
                && ( packet_set_addr( packet, src, src, ( size_t ) addrlen ) == EOK )){
646
                && ( packet_set_addr( packet, src, src, ( size_t ) addrlen ) == EOK )){
641
                    // send the reply
647
                    // send the reply
642
                    icmp_send_packet( ICMP_ECHOREPLY, 0, packet, header, 0 );
648
                    icmp_send_packet( ICMP_ECHOREPLY, 0, packet, header, 0, 0, 0, 0 );
643
                    return EOK;
649
                    return EOK;
644
                }else{
650
                }else{
645
                    return EINVAL;
651
                    return EINVAL;
646
                }
652
                }
647
            }else{
653
            }else{
648
                return EPERM;
654
                return EPERM;
649
            }
655
            }
650
        case ICMP_DEST_UNREACH:
656
        case ICMP_DEST_UNREACH:
651
        case ICMP_SOURCE_QUENCH:
657
        case ICMP_SOURCE_QUENCH:
652
        case ICMP_REDIRECT:
658
        case ICMP_REDIRECT:
653
        case ICMP_ALTERNATE_ADDR:
659
        case ICMP_ALTERNATE_ADDR:
654
        case ICMP_ROUTER_ADV:
660
        case ICMP_ROUTER_ADV:
655
        case ICMP_ROUTER_SOL:
661
        case ICMP_ROUTER_SOL:
656
        case ICMP_TIME_EXCEEDED:
662
        case ICMP_TIME_EXCEEDED:
657
        case ICMP_PARAMETERPROB:
663
        case ICMP_PARAMETERPROB:
658
        case ICMP_CONVERSION_ERROR:
664
        case ICMP_CONVERSION_ERROR:
659
        case ICMP_REDIRECT_MOBILE:
665
        case ICMP_REDIRECT_MOBILE:
660
        case ICMP_SKIP:
666
        case ICMP_SKIP:
661
        case ICMP_PHOTURIS:
667
        case ICMP_PHOTURIS:
662
            ip_received_error_msg( icmp_globals.ip_phone, -1, packet, SERVICE_IP, SERVICE_ICMP );
668
            ip_received_error_msg( icmp_globals.ip_phone, -1, packet, SERVICE_IP, SERVICE_ICMP );
663
            return EOK;
669
            return EOK;
664
        default:
670
        default:
665
            return ENOTSUP;
671
            return ENOTSUP;
666
    }
672
    }
667
}
673
}
668
 
674
 
669
int icmp_process_echo_reply( packet_t packet, icmp_header_ref header, icmp_type_t type, icmp_code_t code ){
675
int icmp_process_echo_reply( packet_t packet, icmp_header_ref header, icmp_type_t type, icmp_code_t code ){
670
    int             reply_key;
676
    int             reply_key;
671
    icmp_reply_ref  reply;
677
    icmp_reply_ref  reply;
672
 
678
 
673
    // compute the reply key
679
    // compute the reply key
674
    reply_key = ICMP_GET_REPLY_KEY( header->un.echo.identifier, header->un.echo.sequence_number );
680
    reply_key = ICMP_GET_REPLY_KEY( header->un.echo.identifier, header->un.echo.sequence_number );
675
    pq_release( icmp_globals.net_phone, packet_get_id( packet ));
681
    pq_release( icmp_globals.net_phone, packet_get_id( packet ));
676
    // lock the globals
682
    // lock the globals
677
    fibril_rwlock_write_lock( & icmp_globals.lock );
683
    fibril_rwlock_write_lock( & icmp_globals.lock );
678
    // find the pending reply
684
    // find the pending reply
679
    reply = icmp_replies_find( & icmp_globals.replies, reply_key );
685
    reply = icmp_replies_find( & icmp_globals.replies, reply_key );
680
    if( reply ){
686
    if( reply ){
681
        // set the result
687
        // set the result
682
        reply->result = type;
688
        reply->result = type;
683
        // notify the main fibril
689
        // notify the main fibril
684
        fibril_condvar_signal( & reply->condvar );
690
        fibril_condvar_signal( & reply->condvar );
685
    }else{
691
    }else{
686
        // unlock only if no reply
692
        // unlock only if no reply
687
        fibril_rwlock_write_unlock( & icmp_globals.lock );
693
        fibril_rwlock_write_unlock( & icmp_globals.lock );
688
    }
694
    }
689
    return EOK;
695
    return EOK;
690
}
696
}
691
 
697
 
692
int icmp_message( ipc_callid_t callid, ipc_call_t * call, ipc_call_t * answer, int * answer_count ){
698
int icmp_message( ipc_callid_t callid, ipc_call_t * call, ipc_call_t * answer, int * answer_count ){
693
    ERROR_DECLARE;
699
    ERROR_DECLARE;
694
 
700
 
695
    packet_t            packet;
701
    packet_t            packet;
696
 
702
 
697
    * answer_count = 0;
703
    * answer_count = 0;
698
    switch( IPC_GET_METHOD( * call )){
704
    switch( IPC_GET_METHOD( * call )){
699
        case NET_TL_RECEIVED:
705
        case NET_TL_RECEIVED:
700
            if( ! ERROR_OCCURRED( packet_translate( icmp_globals.net_phone, & packet, IPC_GET_PACKET( call )))){
706
            if( ! ERROR_OCCURRED( packet_translate( icmp_globals.net_phone, & packet, IPC_GET_PACKET( call )))){
701
                ERROR_CODE = icmp_received_msg( IPC_GET_DEVICE( call ), packet, SERVICE_ICMP, IPC_GET_ERROR( call ));
707
                ERROR_CODE = icmp_received_msg( IPC_GET_DEVICE( call ), packet, SERVICE_ICMP, IPC_GET_ERROR( call ));
702
            }
708
            }
703
            return ERROR_CODE;
709
            return ERROR_CODE;
704
        case NET_ICMP_INIT:
710
        case NET_ICMP_INIT:
705
            return icmp_process_client_messages( callid, * call );
711
            return icmp_process_client_messages( callid, * call );
706
        default:
712
        default:
707
            return icmp_process_message( call );
713
            return icmp_process_message( call );
708
    }
714
    }
709
    return ENOTSUP;
715
    return ENOTSUP;
710
}
716
}
711
 
717
 
712
int icmp_process_client_messages( ipc_callid_t callid, ipc_call_t call ){
718
int icmp_process_client_messages( ipc_callid_t callid, ipc_call_t call ){
713
    ERROR_DECLARE;
719
    ERROR_DECLARE;
714
 
720
 
715
    bool                    keep_on_going = true;
721
    bool                    keep_on_going = true;
716
//  fibril_rwlock_t         lock;
722
//  fibril_rwlock_t         lock;
717
    ipc_call_t              answer;
723
    ipc_call_t              answer;
718
    int                     answer_count;
724
    int                     answer_count;
719
    size_t                  length;
725
    size_t                  length;
720
    struct sockaddr *       addr;
726
    struct sockaddr *       addr;
721
    ipc_callid_t            data_callid;
727
    ipc_callid_t            data_callid;
722
    icmp_echo_ref           echo_data;
728
    icmp_echo_ref           echo_data;
723
 
729
 
724
    /*
730
    /*
725
     * Accept the connection
731
     * Accept the connection
726
     *  - Answer the first NET_ICMP_INIT call.
732
     *  - Answer the first NET_ICMP_INIT call.
727
     */
733
     */
728
    ipc_answer_0( callid, EOK );
734
    ipc_answer_0( callid, EOK );
729
 
735
 
730
//  fibril_rwlock_initialize( & lock );
736
//  fibril_rwlock_initialize( & lock );
731
 
737
 
732
    echo_data = ( icmp_echo_ref ) malloc( sizeof( * echo_data ));
738
    echo_data = ( icmp_echo_ref ) malloc( sizeof( * echo_data ));
733
    if( ! echo_data ) return ENOMEM;
739
    if( ! echo_data ) return ENOMEM;
734
    // assign a new identifier
740
    // assign a new identifier
735
    fibril_rwlock_write_lock( & icmp_globals.lock );
741
    fibril_rwlock_write_lock( & icmp_globals.lock );
736
    ERROR_CODE = icmp_bind_free_id( echo_data );
742
    ERROR_CODE = icmp_bind_free_id( echo_data );
737
    fibril_rwlock_write_unlock( & icmp_globals.lock );
743
    fibril_rwlock_write_unlock( & icmp_globals.lock );
738
    if( ERROR_CODE < 0 ){
744
    if( ERROR_CODE < 0 ){
739
        free( echo_data );
745
        free( echo_data );
740
        return ERROR_CODE;
746
        return ERROR_CODE;
741
    }
747
    }
742
 
748
 
743
    while( keep_on_going ){
749
    while( keep_on_going ){
744
        refresh_answer( & answer, & answer_count );
750
        refresh_answer( & answer, & answer_count );
745
 
751
 
746
        callid = async_get_call( & call );
752
        callid = async_get_call( & call );
747
 
753
 
748
        switch( IPC_GET_METHOD( call )){
754
        switch( IPC_GET_METHOD( call )){
749
            case IPC_M_PHONE_HUNGUP:
755
            case IPC_M_PHONE_HUNGUP:
750
                keep_on_going = false;
756
                keep_on_going = false;
751
                ERROR_CODE = EOK;
757
                ERROR_CODE = EOK;
752
                break;
758
                break;
753
            case NET_ICMP_ECHO:
759
            case NET_ICMP_ECHO:
754
//              fibril_rwlock_write_lock( & lock );
760
//              fibril_rwlock_write_lock( & lock );
755
                if( ! ipc_data_write_receive( & data_callid, & length )){
761
                if( ! ipc_data_write_receive( & data_callid, & length )){
756
                    ERROR_CODE = EINVAL;
762
                    ERROR_CODE = EINVAL;
757
                }else{
763
                }else{
758
                    addr = malloc( length );
764
                    addr = malloc( length );
759
                    if( ! addr ){
765
                    if( ! addr ){
760
                        ERROR_CODE = ENOMEM;
766
                        ERROR_CODE = ENOMEM;
761
                    }else{
767
                    }else{
762
                        if( ! ERROR_OCCURRED( ipc_data_write_finalize( data_callid, addr, length ))){
768
                        if( ! ERROR_OCCURRED( ipc_data_write_finalize( data_callid, addr, length ))){
763
                            fibril_rwlock_write_lock( & icmp_globals.lock );
769
                            fibril_rwlock_write_lock( & icmp_globals.lock );
764
                            ERROR_CODE = icmp_echo( echo_data->identifier, echo_data->sequence_number, ICMP_GET_SIZE( call ), ICMP_GET_TIMEOUT( call ), ICMP_GET_TTL( call ), ICMP_GET_TOS( call ), ICMP_GET_DONT_FRAGMENT( call ), addr, ( socklen_t ) length );
770
                            ERROR_CODE = icmp_echo( echo_data->identifier, echo_data->sequence_number, ICMP_GET_SIZE( call ), ICMP_GET_TIMEOUT( call ), ICMP_GET_TTL( call ), ICMP_GET_TOS( call ), ICMP_GET_DONT_FRAGMENT( call ), addr, ( socklen_t ) length );
765
                            fibril_rwlock_write_unlock( & icmp_globals.lock );
771
                            fibril_rwlock_write_unlock( & icmp_globals.lock );
766
                            free( addr );
772
                            free( addr );
767
                            if( echo_data->sequence_number < MAX_UINT16 ){
773
                            if( echo_data->sequence_number < MAX_UINT16 ){
768
                                ++ echo_data->sequence_number;
774
                                ++ echo_data->sequence_number;
769
                            }else{
775
                            }else{
770
                                echo_data->sequence_number = 0;
776
                                echo_data->sequence_number = 0;
771
                            }
777
                            }
772
                        }
778
                        }
773
                    }
779
                    }
774
                }
780
                }
775
//              fibril_rwlock_write_unlock( & lock );
781
//              fibril_rwlock_write_unlock( & lock );
776
                break;
782
                break;
777
            default:
783
            default:
778
                ERROR_CODE = icmp_process_message( & call );
784
                ERROR_CODE = icmp_process_message( & call );
779
        }
785
        }
780
 
786
 
781
        answer_call( callid, ERROR_CODE, & answer, answer_count );
787
        answer_call( callid, ERROR_CODE, & answer, answer_count );
782
    }
788
    }
783
 
789
 
784
    // release the identifier
790
    // release the identifier
785
    fibril_rwlock_write_lock( & icmp_globals.lock );
791
    fibril_rwlock_write_lock( & icmp_globals.lock );
786
    icmp_echo_data_exclude( & icmp_globals.echo_data, echo_data->identifier );
792
    icmp_echo_data_exclude( & icmp_globals.echo_data, echo_data->identifier );
787
    fibril_rwlock_write_unlock( & icmp_globals.lock );
793
    fibril_rwlock_write_unlock( & icmp_globals.lock );
788
    return EOK;
794
    return EOK;
789
}
795
}
790
 
796
 
791
int icmp_process_message( ipc_call_t * call ){
797
int icmp_process_message( ipc_call_t * call ){
792
    ERROR_DECLARE;
798
    ERROR_DECLARE;
793
 
799
 
794
    packet_t    packet;
800
    packet_t    packet;
795
 
801
 
796
    switch( IPC_GET_METHOD( * call )){
802
    switch( IPC_GET_METHOD( * call )){
797
        case NET_ICMP_DEST_UNREACH:
803
        case NET_ICMP_DEST_UNREACH:
798
            if( ! ERROR_OCCURRED( packet_translate( icmp_globals.net_phone, & packet, IPC_GET_PACKET( call )))){
804
            if( ! ERROR_OCCURRED( packet_translate( icmp_globals.net_phone, & packet, IPC_GET_PACKET( call )))){
799
                ERROR_CODE = icmp_destination_unreachable_msg( 0, ICMP_GET_CODE( call ), ICMP_GET_MTU( call ), packet );
805
                ERROR_CODE = icmp_destination_unreachable_msg( 0, ICMP_GET_CODE( call ), ICMP_GET_MTU( call ), packet );
800
            }
806
            }
801
            return ERROR_CODE;
807
            return ERROR_CODE;
802
        case NET_ICMP_SOURCE_QUENCH:
808
        case NET_ICMP_SOURCE_QUENCH:
803
            if( ! ERROR_OCCURRED( packet_translate( icmp_globals.net_phone, & packet, IPC_GET_PACKET( call )))){
809
            if( ! ERROR_OCCURRED( packet_translate( icmp_globals.net_phone, & packet, IPC_GET_PACKET( call )))){
804
                ERROR_CODE = icmp_source_quench_msg( 0, packet );
810
                ERROR_CODE = icmp_source_quench_msg( 0, packet );
805
            }
811
            }
806
            return ERROR_CODE;
812
            return ERROR_CODE;
807
        case NET_ICMP_TIME_EXCEEDED:
813
        case NET_ICMP_TIME_EXCEEDED:
808
            if( ! ERROR_OCCURRED( packet_translate( icmp_globals.net_phone, & packet, IPC_GET_PACKET( call )))){
814
            if( ! ERROR_OCCURRED( packet_translate( icmp_globals.net_phone, & packet, IPC_GET_PACKET( call )))){
809
                ERROR_CODE = icmp_time_exceeded_msg( 0, ICMP_GET_CODE( call ), packet );
815
                ERROR_CODE = icmp_time_exceeded_msg( 0, ICMP_GET_CODE( call ), packet );
810
            }
816
            }
811
            return ERROR_CODE;
817
            return ERROR_CODE;
812
        case NET_ICMP_PARAMETERPROB:
818
        case NET_ICMP_PARAMETERPROB:
813
            if( ! ERROR_OCCURRED( packet_translate( icmp_globals.net_phone, & packet, IPC_GET_PACKET( call )))){
819
            if( ! ERROR_OCCURRED( packet_translate( icmp_globals.net_phone, & packet, IPC_GET_PACKET( call )))){
814
                ERROR_CODE = icmp_parameter_problem_msg( 0, ICMP_GET_CODE( call ), ICMP_GET_POINTER( call ), packet );
820
                ERROR_CODE = icmp_parameter_problem_msg( 0, ICMP_GET_CODE( call ), ICMP_GET_POINTER( call ), packet );
815
            }
821
            }
816
            return ERROR_CODE;
822
            return ERROR_CODE;
817
        default:
823
        default:
818
            return ENOTSUP;
824
            return ENOTSUP;
819
    }
825
    }
820
}
826
}
821
 
827
 
822
int icmp_release_and_return( packet_t packet, int result ){
828
int icmp_release_and_return( packet_t packet, int result ){
823
    pq_release( icmp_globals.net_phone, packet_get_id( packet ));
829
    pq_release( icmp_globals.net_phone, packet_get_id( packet ));
824
    return result;
830
    return result;
825
}
831
}
826
 
832
 
827
int icmp_bind_free_id( icmp_echo_ref echo_data ){
833
int icmp_bind_free_id( icmp_echo_ref echo_data ){
828
    icmp_param_t    index;
834
    icmp_param_t    index;
829
 
835
 
830
    if( ! echo_data ) return EBADMEM;
836
    if( ! echo_data ) return EBADMEM;
831
    // from the last used one
837
    // from the last used one
832
    index = icmp_globals.last_used_id;
838
    index = icmp_globals.last_used_id;
833
    do{
839
    do{
834
        ++ index;
840
        ++ index;
835
        // til the range end
841
        // til the range end
836
        if( index >= ICMP_FREE_IDS_END ){
842
        if( index >= ICMP_FREE_IDS_END ){
837
            // start from the range beginning
843
            // start from the range beginning
838
            index = ICMP_FREE_IDS_START - 1;
844
            index = ICMP_FREE_IDS_START - 1;
839
            do{
845
            do{
840
                ++ index;
846
                ++ index;
841
                // til the last used one
847
                // til the last used one
842
                if( index >= icmp_globals.last_used_id ){
848
                if( index >= icmp_globals.last_used_id ){
843
                    // none found
849
                    // none found
844
                    return ENOTCONN;
850
                    return ENOTCONN;
845
                }
851
                }
846
            }while( icmp_echo_data_find( & icmp_globals.echo_data, index ) != NULL );
852
            }while( icmp_echo_data_find( & icmp_globals.echo_data, index ) != NULL );
847
            // found, break immediately
853
            // found, break immediately
848
            break;
854
            break;
849
        }
855
        }
850
    }while( icmp_echo_data_find( & icmp_globals.echo_data, index ) != NULL );
856
    }while( icmp_echo_data_find( & icmp_globals.echo_data, index ) != NULL );
851
    echo_data->identifier = index;
857
    echo_data->identifier = index;
852
    echo_data->sequence_number = 0;
858
    echo_data->sequence_number = 0;
853
    return icmp_echo_data_add( & icmp_globals.echo_data, index, echo_data );
859
    return icmp_echo_data_add( & icmp_globals.echo_data, index, echo_data );
854
}
860
}
855
 
861
 
856
/** @}
862
/** @}
857
 */
863
 */
858
 
864