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