Rev 4708 | Go to most recent revision | Details | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
4707 | mejdrech | 1 | /* |
2 | * Copyright (c) 2008 Lukas Mejdrech |
||
3 | * All rights reserved. |
||
4 | * |
||
5 | * Redistribution and use in source and binary forms, with or without |
||
6 | * modification, are permitted provided that the following conditions |
||
7 | * are met: |
||
8 | * |
||
9 | * - Redistributions of source code must retain the above copyright |
||
10 | * notice, this list of conditions and the following disclaimer. |
||
11 | * - Redistributions in binary form must reproduce the above copyright |
||
12 | * notice, this list of conditions and the following disclaimer in the |
||
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 |
||
15 | * derived from this software without specific prior written permission. |
||
16 | * |
||
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 |
||
19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
||
20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
||
21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
||
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 |
||
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 |
||
26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
||
27 | */ |
||
28 | |||
29 | /** @addtogroup icmp |
||
30 | * @{ |
||
31 | */ |
||
32 | |||
33 | /** @file |
||
34 | * ICMP module implementation. |
||
35 | * @see icmp.h |
||
36 | */ |
||
37 | |||
38 | #include <async.h> |
||
39 | #include <fibril_sync.h> |
||
40 | |||
41 | #include <ipc/ipc.h> |
||
42 | #include <ipc/services.h> |
||
43 | |||
44 | #include "../../err.h" |
||
45 | #include "../../messages.h" |
||
46 | #include "../../modules.h" |
||
47 | |||
48 | #include "../../structures/packet/packet_client.h" |
||
49 | |||
50 | #include "../../include/crc.h" |
||
51 | #include "../../include/icmp_codes.h" |
||
52 | #include "../../include/icmp_client.h" |
||
53 | #include "../../include/icmp_interface.h" |
||
54 | #include "../../include/ip_client.h" |
||
55 | #include "../../include/ip_interface.h" |
||
56 | #include "../../include/il_interface.h" |
||
57 | #include "../../include/ip_protocols.h" |
||
58 | |||
59 | #include "../../tl/tl_messages.h" |
||
60 | |||
61 | #include "icmp.h" |
||
62 | #include "icmp_header.h" |
||
63 | #include "icmp_messages.h" |
||
64 | #include "icmp_module.h" |
||
65 | |||
66 | #define ICMP_KEEP_LENGTH 8 |
||
67 | |||
68 | #define ICMP_HEADER_CHECKSUM( header ) compact_checksum(compute_checksum( 0, ( uint8_t * ) header, sizeof( icmp_header_t ))) |
||
69 | |||
70 | /** Processes the received ICMP packet. |
||
71 | * Is used as an entry point from the underlying IP module. |
||
72 | * Releases the packet on error. |
||
73 | * @param device_id The device identifier. Ignored parameter. |
||
74 | * @param packet The received packet. Input/output parameter. |
||
75 | * @param receiver The target service. Ignored parameter. |
||
76 | * @param error The packet error reporting service. Prefixes the received packet. Input parameter. |
||
77 | * @returns EOK on success. |
||
78 | * @returns Other error codes as defined for the icmp_process_packet() function. |
||
79 | */ |
||
80 | int icmp_received_msg( device_id_t device_id, packet_t packet, services_t receiver, services_t error ); |
||
81 | |||
82 | /** Processes the received ICMP packet. |
||
83 | * Notifies the destination socket application. |
||
84 | * @param packet The received packet. Input/output parameter. |
||
85 | * @returns EOK on success. |
||
86 | * @returns EINVAL if the packet is not valid. |
||
87 | * @returns EINVAL if the stored packet address is not the an_addr_t. |
||
88 | * @returns EINVAL if the packet does not contain any data. |
||
89 | * @returns NO_DATA if the packet content is shorter than the user datagram header. |
||
90 | * @returns ENOMEM if there is not enough memory left. |
||
91 | * @returns EADDRNOTAVAIL if the destination socket does not exist. |
||
92 | * @returns Other error codes as defined for the ip_client_process_packet() function. |
||
93 | */ |
||
94 | int icmp_process_packet( packet_t packet ); |
||
95 | |||
96 | icmp_header_ref icmp_prepare_packet( packet_t packet ); |
||
97 | int icmp_send_packet( icmp_type_t type, icmp_code_t code, packet_t packet, icmp_header_ref header ); |
||
98 | |||
99 | /** ICMP global data. |
||
100 | */ |
||
101 | icmp_globals_t icmp_globals; |
||
102 | |||
103 | int icmp_echo_msg( int icmp_phone, icmp_param_t identifier, icmp_param_t sequence_number, const void * data, size_t length ){ |
||
104 | // TODO echo - with timeout |
||
105 | // waits for the reply up to the timeout |
||
106 | return ENOTSUP; |
||
107 | } |
||
108 | |||
109 | int icmp_destination_unreachable_msg( int icmp_phone, icmp_code_t code, icmp_param_t mtu, packet_t packet ){ |
||
110 | icmp_header_ref header; |
||
111 | |||
112 | header = icmp_prepare_packet( packet ); |
||
113 | if( ! header ) return ENOMEM; |
||
114 | if( mtu ){ |
||
115 | header->un.frag.mtu = mtu; |
||
116 | } |
||
117 | return icmp_send_packet( ICMP_DEST_UNREACH, code, packet, header ); |
||
118 | } |
||
119 | |||
120 | int icmp_source_quench_msg( int icmp_phone, packet_t packet ){ |
||
121 | icmp_header_ref header; |
||
122 | |||
123 | header = icmp_prepare_packet( packet ); |
||
124 | if( ! header ) return ENOMEM; |
||
125 | return icmp_send_packet( ICMP_SOURCE_QUENCH, ICMP_SOURCE_QUENCH, packet, header ); |
||
126 | } |
||
127 | |||
128 | int icmp_time_exceeded_msg( int icmp_phone, icmp_code_t code, packet_t packet ){ |
||
129 | icmp_header_ref header; |
||
130 | |||
131 | header = icmp_prepare_packet( packet ); |
||
132 | if( ! header ) return ENOMEM; |
||
133 | return icmp_send_packet( ICMP_TIME_EXCEEDED, code, packet, header ); |
||
134 | } |
||
135 | |||
136 | int icmp_parameter_problem_msg( int icmp_phone, icmp_code_t code, icmp_param_t pointer, packet_t packet ){ |
||
137 | icmp_header_ref header; |
||
138 | |||
139 | header = icmp_prepare_packet( packet ); |
||
140 | if( ! header ) return ENOMEM; |
||
141 | header->un.param.pointer = pointer; |
||
142 | return icmp_send_packet( ICMP_PARAMETERPROB, code, packet, header ); |
||
143 | } |
||
144 | |||
145 | icmp_header_ref icmp_prepare_packet( packet_t packet ){ |
||
146 | icmp_header_ref header; |
||
147 | int header_length; |
||
148 | size_t total_length; |
||
149 | |||
150 | total_length = packet_get_data_length( packet ); |
||
151 | if( total_length <= 0 ) return NULL; |
||
152 | header_length = ip_client_header_length( packet ); |
||
153 | if( header_length <= 0 ) return NULL; |
||
154 | // truncate if longer than 64 bits (without the IP header) |
||
155 | if( total_length - header_length > ICMP_KEEP_LENGTH ){ |
||
156 | if( packet_trim( packet, 0, total_length - header_length - ICMP_KEEP_LENGTH ) != EOK ) return NULL; |
||
157 | } |
||
158 | header = PACKET_PREFIX( packet, icmp_header_t ); |
||
159 | if( ! header ){ |
||
160 | pq_release( icmp_globals.net_phone, packet_get_id( packet )); |
||
161 | return NULL; |
||
162 | } |
||
163 | bzero( header, sizeof( * header )); |
||
164 | return header; |
||
165 | } |
||
166 | |||
167 | int icmp_send_packet( icmp_type_t type, icmp_code_t code, packet_t packet, icmp_header_ref header ){ |
||
168 | ERROR_DECLARE; |
||
169 | |||
170 | header->type = type; |
||
171 | header->code = code; |
||
172 | header->checksum = 0; |
||
173 | header->checksum = ICMP_HEADER_CHECKSUM( header ); |
||
174 | if( ERROR_OCCURRED( ip_client_prepare_packet( packet, IPPROTO_ICMP, 0, 0, 0, 0 ))){ |
||
175 | pq_release( icmp_globals.net_phone, packet_get_id( packet )); |
||
176 | return ERROR_CODE; |
||
177 | } |
||
178 | return ip_send_msg( icmp_globals.ip_phone, -1, packet, SERVICE_ICMP, SERVICE_ICMP ); |
||
179 | } |
||
180 | |||
181 | int icmp_connect_module( services_t service ){ |
||
182 | return EOK; |
||
183 | } |
||
184 | |||
185 | int icmp_initialize( async_client_conn_t client_connection ){ |
||
186 | ERROR_DECLARE; |
||
187 | |||
188 | fibril_rwlock_initialize( & icmp_globals.lock ); |
||
189 | fibril_rwlock_write_lock( & icmp_globals.lock ); |
||
190 | icmp_globals.ip_phone = ip_bind_service( SERVICE_IP, IPPROTO_ICMP, SERVICE_ICMP, client_connection, icmp_received_msg ); |
||
191 | if( icmp_globals.ip_phone < 0 ){ |
||
192 | return icmp_globals.ip_phone; |
||
193 | } |
||
194 | ERROR_PROPAGATE( ip_packet_size_req( icmp_globals.ip_phone, -1, & icmp_globals.addr_len, & icmp_globals.prefix, & icmp_globals.content, & icmp_globals.suffix )); |
||
195 | icmp_globals.prefix += sizeof( icmp_header_t ); |
||
196 | icmp_globals.content -= sizeof( icmp_header_t ); |
||
197 | fibril_rwlock_write_unlock( & icmp_globals.lock ); |
||
198 | return EOK; |
||
199 | } |
||
200 | |||
201 | int icmp_received_msg( device_id_t device_id, packet_t packet, services_t receiver, services_t error ){ |
||
202 | ERROR_DECLARE; |
||
203 | |||
204 | if( error ){ |
||
205 | pq_release( icmp_globals.net_phone, packet_get_id( packet )); |
||
206 | return EINVAL; |
||
207 | } |
||
208 | if( ERROR_OCCURRED( icmp_process_packet( packet ))){ |
||
209 | pq_release( icmp_globals.net_phone, packet_get_id( packet )); |
||
210 | return ERROR_CODE; |
||
211 | } |
||
212 | |||
213 | return EOK; |
||
214 | } |
||
215 | |||
216 | int icmp_process_packet( packet_t packet ){ |
||
217 | int length; |
||
218 | void * data; |
||
219 | icmp_header_ref header; |
||
220 | |||
221 | // get rid of the ip header |
||
222 | length = ip_client_process_packet( packet, NULL, NULL, NULL, NULL, NULL ); |
||
223 | if( length < 0 ) return length; |
||
224 | packet_trim( packet, length, 0 ); |
||
225 | |||
226 | length = packet_get_data_length( packet ); |
||
227 | if( length <= 0 ) return EINVAL; |
||
228 | if( length < sizeof( icmp_header_t )) return EINVAL; |
||
229 | data = packet_get_data( packet ); |
||
230 | if( ! data ) return EINVAL; |
||
231 | // get icmp header |
||
232 | header = ( icmp_header_ref ) data; |
||
233 | // checksum |
||
234 | if(( header->checksum ) && ( ICMP_HEADER_CHECKSUM( header ))){ |
||
235 | return EINVAL; |
||
236 | } |
||
237 | // TODO process requests and replies |
||
238 | ip_received_error_msg( icmp_globals.ip_phone, -1, packet, SERVICE_IP, SERVICE_ICMP ); |
||
239 | return EOK; |
||
240 | } |
||
241 | |||
242 | int icmp_message( ipc_callid_t callid, ipc_call_t * call, ipc_call_t * answer, int * answer_count ){ |
||
243 | ERROR_DECLARE; |
||
244 | |||
245 | packet_t packet; |
||
246 | |||
247 | * answer_count = 0; |
||
248 | switch( IPC_GET_METHOD( * call )){ |
||
249 | case NET_TL_RECEIVED: |
||
250 | fibril_rwlock_read_lock( & icmp_globals.lock ); |
||
251 | if( ! ERROR_OCCURRED( packet_translate( icmp_globals.net_phone, & packet, IPC_GET_PACKET( call )))){ |
||
252 | ERROR_CODE = icmp_received_msg( IPC_GET_DEVICE( call ), packet, SERVICE_ICMP, IPC_GET_ERROR( call )); |
||
253 | } |
||
254 | fibril_rwlock_read_unlock( & icmp_globals.lock ); |
||
255 | return ERROR_CODE; |
||
256 | case NET_ICMP_DEST_UNREACH: |
||
257 | ERROR_PROPAGATE( packet_translate( icmp_globals.net_phone, & packet, IPC_GET_PACKET( call ))); |
||
258 | return icmp_destination_unreachable_msg( 0, ICMP_GET_CODE( call ), ICMP_GET_MTU( call ), packet ); |
||
259 | case NET_ICMP_SOURCE_QUENCH: |
||
260 | ERROR_PROPAGATE( packet_translate( icmp_globals.net_phone, & packet, IPC_GET_PACKET( call ))); |
||
261 | return icmp_source_quench_msg( 0, packet ); |
||
262 | case NET_ICMP_TIME_EXCEEDED: |
||
263 | ERROR_PROPAGATE( packet_translate( icmp_globals.net_phone, & packet, IPC_GET_PACKET( call ))); |
||
264 | return icmp_time_exceeded_msg( 0, ICMP_GET_CODE( call ), packet ); |
||
265 | case NET_ICMP_PARAMETERPROB: |
||
266 | ERROR_PROPAGATE( packet_translate( icmp_globals.net_phone, & packet, IPC_GET_PACKET( call ))); |
||
267 | return icmp_parameter_problem_msg( 0, ICMP_GET_CODE( call ), ICMP_GET_POINTER( call ), packet ); |
||
268 | } |
||
269 | return ENOTSUP; |
||
270 | } |
||
271 | |||
272 | /** @} |
||
273 | */ |