Changes in uspace/srv/net/tl/icmp/icmp.c [ffa2c8ef:014dd57b] in mainline
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
uspace/srv/net/tl/icmp/icmp.c
rffa2c8ef r014dd57b 33 33 /** @file 34 34 * ICMP module implementation. 35 * @see icmp.h 35 36 */ 36 37 … … 41 42 #include <stdint.h> 42 43 #include <str.h> 44 #include <ipc/ipc.h> 43 45 #include <ipc/services.h> 44 46 #include <ipc/net.h> … … 49 51 #include <byteorder.h> 50 52 #include <errno.h> 51 #include <adt/hash_table.h>52 53 53 54 #include <net/socket_codes.h> … … 63 64 #include <net_checksum.h> 64 65 #include <icmp_client.h> 65 #include <icmp_ remote.h>66 #include <icmp_interface.h> 66 67 #include <il_remote.h> 67 68 #include <ip_client.h> … … 72 73 #include <icmp_header.h> 73 74 74 /** ICMP module name */ 75 #include "icmp.h" 76 77 /** ICMP module name. */ 75 78 #define NAME "icmp" 76 79 77 /** Number of replies hash table keys */ 78 #define REPLY_KEYS 2 79 80 /** Number of replies hash table buckets */ 81 #define REPLY_BUCKETS 1024 82 83 /** 84 * Original datagram length in bytes transfered to the error 85 * notification message. 86 */ 87 #define ICMP_KEEP_LENGTH 8 88 89 /** Compute the ICMP datagram checksum. 90 * 91 * @param[in,out] header ICMP datagram header. 92 * @param[in] length Total datagram length. 93 * 94 * @return Computed checksum. 95 * 80 /** Default ICMP error reporting. */ 81 #define NET_DEFAULT_ICMP_ERROR_REPORTING true 82 83 /** Default ICMP echo replying. */ 84 #define NET_DEFAULT_ICMP_ECHO_REPLYING true 85 86 /** Original datagram length in bytes transfered to the error notification 87 * message. 88 */ 89 #define ICMP_KEEP_LENGTH 8 90 91 /** Free identifier numbers pool start. */ 92 #define ICMP_FREE_IDS_START 1 93 94 /** Free identifier numbers pool end. */ 95 #define ICMP_FREE_IDS_END UINT16_MAX 96 97 /** Computes the ICMP datagram checksum. 98 * 99 * @param[in,out] header The ICMP datagram header. 100 * @param[in] length The total datagram length. 101 * @return The computed checksum. 96 102 */ 97 103 #define ICMP_CHECKSUM(header, length) \ … … 99 105 100 106 /** An echo request datagrams pattern. */ 101 #define ICMP_ECHO_TEXT "ICMP hello from HelenOS." 102 103 /** ICMP reply data. */ 104 typedef struct { 105 /** Hash table link */ 106 link_t link; 107 108 /** Reply identification and sequence */ 109 icmp_param_t id; 110 icmp_param_t sequence; 111 112 /** Reply signaling */ 113 fibril_condvar_t condvar; 114 115 /** Reply result */ 116 int result; 117 } icmp_reply_t; 118 119 /** Global data */ 120 static int phone_net = -1; 121 static int phone_ip = -1; 122 static bool error_reporting = true; 123 static bool echo_replying = true; 124 static packet_dimension_t icmp_dimension; 125 126 /** ICMP client identification counter */ 127 static atomic_t icmp_client; 128 129 /** ICMP identifier and sequence number (client-specific) */ 130 static fibril_local icmp_param_t icmp_id; 131 static fibril_local icmp_param_t icmp_seq; 132 133 /** Reply hash table */ 134 static fibril_mutex_t reply_lock; 135 static hash_table_t replies; 136 137 static hash_index_t replies_hash(unsigned long key[]) 138 { 139 /* 140 * ICMP identifier and sequence numbers 141 * are 16-bit values. 142 */ 143 hash_index_t index = ((key[0] & 0xffff) << 16) | (key[1] & 0xffff); 144 return (index % REPLY_BUCKETS); 145 } 146 147 static int replies_compare(unsigned long key[], hash_count_t keys, link_t *item) 148 { 149 icmp_reply_t *reply = 150 hash_table_get_instance(item, icmp_reply_t, link); 151 152 if (keys == 1) 153 return (reply->id == key[0]); 154 else 155 return ((reply->id == key[0]) && (reply->sequence == key[1])); 156 } 157 158 static void replies_remove_callback(link_t *item) 159 { 160 } 161 162 static hash_table_operations_t reply_ops = { 163 .hash = replies_hash, 164 .compare = replies_compare, 165 .remove_callback = replies_remove_callback 166 }; 167 168 /** Release the packet and return the result. 169 * 170 * @param[in] packet Packet queue to be released. 171 * 172 */ 173 static void icmp_release(packet_t *packet) 174 { 175 pq_release_remote(phone_net, packet_get_id(packet)); 176 } 177 178 /** Send the ICMP message. 179 * 180 * Set the message type and code and compute the checksum. 107 #define ICMP_ECHO_TEXT "Hello from HelenOS." 108 109 /** Computes an ICMP reply data key. 110 * 111 * @param[in] id The message identifier. 112 * @param[in] sequence The message sequence number. 113 * @return The computed ICMP reply data key. 114 */ 115 #define ICMP_GET_REPLY_KEY(id, sequence) \ 116 (((id) << 16) | (sequence & 0xFFFF)) 117 118 119 /** ICMP global data. */ 120 icmp_globals_t icmp_globals; 121 122 INT_MAP_IMPLEMENT(icmp_replies, icmp_reply_t); 123 INT_MAP_IMPLEMENT(icmp_echo_data, icmp_echo_t); 124 125 /** Releases the packet and returns the result. 126 * 127 * @param[in] packet The packet queue to be released. 128 * @param[in] result The result to be returned. 129 * @return The result parameter. 130 */ 131 static int icmp_release_and_return(packet_t *packet, int result) 132 { 133 pq_release_remote(icmp_globals.net_phone, packet_get_id(packet)); 134 return result; 135 } 136 137 /** Sends the ICMP message. 138 * 139 * Sets the message type and code and computes the checksum. 181 140 * Error messages are sent only if allowed in the configuration. 182 * Release the packet on errors. 183 * 184 * @param[in] type Message type. 185 * @param[in] code Message code. 186 * @param[in] packet Message packet to be sent. 187 * @param[in] header ICMP header. 188 * @param[in] error Error service to be announced. Should be 189 * SERVICE_ICMP or zero. 190 * @param[in] ttl Time to live. 191 * @param[in] tos Type of service. 192 * @param[in] dont_fragment Disable fragmentation. 193 * 194 * @return EOK on success. 195 * @return EPERM if the error message is not allowed. 196 * 197 */ 198 static int icmp_send_packet(icmp_type_t type, icmp_code_t code, 199 packet_t *packet, icmp_header_t *header, services_t error, ip_ttl_t ttl, 200 ip_tos_t tos, bool dont_fragment) 201 { 141 * Releases the packet on errors. 142 * 143 * @param[in] type The message type. 144 * @param[in] code The message code. 145 * @param[in] packet The message packet to be sent. 146 * @param[in] header The ICMP header. 147 * @param[in] error The error service to be announced. Should be 148 * SERVICE_ICMP or zero. 149 * @param[in] ttl The time to live. 150 * @param[in] tos The type of service. 151 * @param[in] dont_fragment The value indicating whether the datagram must not 152 * be fragmented. Is used as a MTU discovery. 153 * @return EOK on success. 154 * @return EPERM if the error message is not allowed. 155 */ 156 static int icmp_send_packet(icmp_type_t type, icmp_code_t code, packet_t *packet, 157 icmp_header_t *header, services_t error, ip_ttl_t ttl, ip_tos_t tos, 158 int dont_fragment) 159 { 160 int rc; 161 202 162 /* Do not send an error if disabled */ 203 if ((error) && (!error_reporting)) { 204 icmp_release(packet); 205 return EPERM; 206 } 207 163 if (error && !icmp_globals.error_reporting) 164 return icmp_release_and_return(packet, EPERM); 165 208 166 header->type = type; 209 167 header->code = code; 210 211 /*212 * The checksum needs to be calculated213 * with a virtual checksum field set to214 * zero.215 */216 168 header->checksum = 0; 217 169 header->checksum = ICMP_CHECKSUM(header, 218 170 packet_get_data_length(packet)); 219 171 220 intrc = ip_client_prepare_packet(packet, IPPROTO_ICMP, ttl, tos,172 rc = ip_client_prepare_packet(packet, IPPROTO_ICMP, ttl, tos, 221 173 dont_fragment, 0); 222 if (rc != EOK) { 223 icmp_release(packet); 224 return rc; 225 } 226 227 return ip_send_msg(phone_ip, -1, packet, SERVICE_ICMP, error); 228 } 229 230 /** Prepare the ICMP error packet. 231 * 232 * Truncate the original packet if longer than ICMP_KEEP_LENGTH bytes. 233 * Prefix and return the ICMP header. 234 * 235 * @param[in,out] packet Original packet. 236 * 174 if (rc != EOK) 175 return icmp_release_and_return(packet, rc); 176 177 return ip_send_msg(icmp_globals.ip_phone, -1, packet, SERVICE_ICMP, 178 error); 179 } 180 181 /** Prepares the ICMP error packet. 182 * 183 * Truncates the original packet if longer than ICMP_KEEP_LENGTH bytes. 184 * Prefixes and returns the ICMP header. 185 * 186 * @param[in,out] packet The original packet. 237 187 * @return The prefixed ICMP header. 238 188 * @return NULL on errors. 239 *240 189 */ 241 190 static icmp_header_t *icmp_prepare_packet(packet_t *packet) 242 191 { 243 size_t total_length = packet_get_data_length(packet); 192 icmp_header_t *header; 193 size_t header_length; 194 size_t total_length; 195 196 total_length = packet_get_data_length(packet); 244 197 if (total_length <= 0) 245 198 return NULL; 246 247 size_theader_length = ip_client_header_length(packet);199 200 header_length = ip_client_header_length(packet); 248 201 if (header_length <= 0) 249 202 return NULL; 250 203 251 204 /* Truncate if longer than 64 bits (without the IP header) */ 252 205 if ((total_length > header_length + ICMP_KEEP_LENGTH) && 253 206 (packet_trim(packet, 0, 254 total_length - header_length - ICMP_KEEP_LENGTH) != EOK)) 207 total_length - header_length - ICMP_KEEP_LENGTH) != EOK)) { 255 208 return NULL; 256 257 icmp_header_t *header = PACKET_PREFIX(packet, icmp_header_t); 209 } 210 211 header = PACKET_PREFIX(packet, icmp_header_t); 258 212 if (!header) 259 213 return NULL; 260 214 261 215 bzero(header, sizeof(*header)); 262 216 return header; 263 217 } 264 218 265 /** Request an echo message.266 * 267 * Send a packet with specified parameters to the target host268 * and wait forthe reply upto the given timeout.269 * Block the caller until the reply or the timeout occurs.270 * 271 * @param[in] id Message identifier.272 * @param[in] sequence Message sequence parameter.273 * @param[in] size Message data length in bytes.274 * @param[in] timeout Timeout in miliseconds.275 * @param[in] ttl Time to live.276 * @param[in] tos Type of service.277 * @param[in] dont_fragment Disable fragmentation.278 * @param[in] addr Target host address.279 * @param[in] addr len Torget host address length.280 * 281 * @return 282 * @return 283 * 284 * @return 285 * @return 286 * 287 * @return 288 * 219 /** Requests an echo message. 220 * 221 * Sends a packet with specified parameters to the target host and waits for 222 * the reply upto the given timeout. 223 * Blocks the caller until the reply or the timeout occurs. 224 * 225 * @param[in] id The message identifier. 226 * @param[in] sequence The message sequence parameter. 227 * @param[in] size The message data length in bytes. 228 * @param[in] timeout The timeout in miliseconds. 229 * @param[in] ttl The time to live. 230 * @param[in] tos The type of service. 231 * @param[in] dont_fragment The value indicating whether the datagram must not 232 * be fragmented. Is used as a MTU discovery. 233 * @param[in] addr The target host address. 234 * @param[in] addrlen The torget host address length. 235 * @return ICMP_ECHO on success. 236 * @return ETIMEOUT if the reply has not arrived before the 237 * timeout. 238 * @return ICMP type of the received error notification. 239 * @return EINVAL if the addrlen parameter is less or equal to 240 * zero. 241 * @return ENOMEM if there is not enough memory left. 242 * @return EPARTY if there was an internal error. 289 243 */ 290 244 static int icmp_echo(icmp_param_t id, icmp_param_t sequence, size_t size, 291 mseconds_t timeout, ip_ttl_t ttl, ip_tos_t tos, bool dont_fragment, 292 const struct sockaddr *addr, socklen_t addrlen) 293 { 245 mseconds_t timeout, ip_ttl_t ttl, ip_tos_t tos, int dont_fragment, 246 const struct sockaddr * addr, socklen_t addrlen) 247 { 248 icmp_header_t *header; 249 packet_t *packet; 250 size_t length; 251 uint8_t *data; 252 icmp_reply_t *reply; 253 int reply_key; 254 int index; 255 int rc; 256 294 257 if (addrlen <= 0) 295 258 return EINVAL; 296 297 size_t length = (size_t) addrlen; 298 299 packet_t *packet = packet_get_4_remote(phone_net, size, 300 icmp_dimension.addr_len, ICMP_HEADER_SIZE + icmp_dimension.prefix, 301 icmp_dimension.suffix); 259 260 length = (size_t) addrlen; 261 /* TODO do not ask all the time */ 262 rc = ip_packet_size_req(icmp_globals.ip_phone, -1, 263 &icmp_globals.packet_dimension); 264 if (rc != EOK) 265 return rc; 266 267 packet = packet_get_4_remote(icmp_globals.net_phone, size, 268 icmp_globals.packet_dimension.addr_len, 269 ICMP_HEADER_SIZE + icmp_globals.packet_dimension.prefix, 270 icmp_globals.packet_dimension.suffix); 302 271 if (!packet) 303 272 return ENOMEM; 304 273 305 274 /* Prepare the requesting packet, set the destination address. */ 306 int rc = packet_set_addr(packet, NULL, (const uint8_t *) addr, length); 307 if (rc != EOK) { 308 icmp_release(packet); 309 return rc; 310 } 311 275 rc = packet_set_addr(packet, NULL, (const uint8_t *) addr, length); 276 if (rc != EOK) 277 return icmp_release_and_return(packet, rc); 278 312 279 /* Allocate space in the packet */ 313 uint8_t *data = (uint8_t *) packet_suffix(packet, size); 314 if (!data) { 315 icmp_release(packet); 316 return ENOMEM; 317 } 318 280 data = (uint8_t *) packet_suffix(packet, size); 281 if (!data) 282 return icmp_release_and_return(packet, ENOMEM); 283 319 284 /* Fill the data */ 320 285 length = 0; … … 324 289 } 325 290 memcpy(data + length, ICMP_ECHO_TEXT, size - length); 326 291 327 292 /* Prefix the header */ 328 icmp_header_t *header = PACKET_PREFIX(packet, icmp_header_t); 329 if (!header) { 330 icmp_release(packet); 331 return ENOMEM; 332 } 333 334 bzero(header, sizeof(icmp_header_t)); 293 header = PACKET_PREFIX(packet, icmp_header_t); 294 if (!header) 295 return icmp_release_and_return(packet, ENOMEM); 296 297 bzero(header, sizeof(*header)); 335 298 header->un.echo.identifier = id; 336 299 header->un.echo.sequence_number = sequence; 337 300 338 301 /* Prepare the reply structure */ 339 icmp_reply_t *reply = malloc(sizeof(icmp_reply_t)); 340 if (!reply) { 341 icmp_release(packet); 342 return ENOMEM; 343 } 344 345 reply->id = id; 346 reply->sequence = sequence; 302 reply = malloc(sizeof(*reply)); 303 if (!reply) 304 return icmp_release_and_return(packet, ENOMEM); 305 306 fibril_mutex_initialize(&reply->mutex); 307 fibril_mutex_lock(&reply->mutex); 347 308 fibril_condvar_initialize(&reply->condvar); 348 349 /* Add the reply to the replies hash table */ 350 fibril_mutex_lock(&reply_lock); 351 352 unsigned long key[REPLY_KEYS] = {id, sequence}; 353 hash_table_insert(&replies, key, &reply->link); 354 309 reply_key = ICMP_GET_REPLY_KEY(header->un.echo.identifier, 310 header->un.echo.sequence_number); 311 index = icmp_replies_add(&icmp_globals.replies, reply_key, reply); 312 if (index < 0) { 313 free(reply); 314 return icmp_release_and_return(packet, index); 315 } 316 317 /* Unlock the globals so that we can wait for the reply */ 318 fibril_rwlock_write_unlock(&icmp_globals.lock); 319 355 320 /* Send the request */ 356 321 icmp_send_packet(ICMP_ECHO, 0, packet, header, 0, ttl, tos, 357 322 dont_fragment); 358 323 359 324 /* Wait for the reply. Timeout in microseconds. */ 360 rc = fibril_condvar_wait_timeout(&reply->condvar, &reply _lock,325 rc = fibril_condvar_wait_timeout(&reply->condvar, &reply->mutex, 361 326 timeout * 1000); 362 327 if (rc == EOK) 363 328 rc = reply->result; 364 365 /* Remove the reply from the replies hash table*/366 hash_table_remove(&replies, key, REPLY_KEYS);367 368 fibril_mutex_unlock(&reply_lock); 369 370 free(reply);371 329 330 /* Drop the reply mutex before locking the globals again */ 331 fibril_mutex_unlock(&reply->mutex); 332 fibril_rwlock_write_lock(&icmp_globals.lock); 333 334 /* Destroy the reply structure */ 335 icmp_replies_exclude_index(&icmp_globals.replies, index); 336 372 337 return rc; 373 338 } 374 339 375 static int icmp_destination_unreachable (icmp_code_t code, icmp_param_t mtu,376 packet_t *packet)377 { 378 icmp_header_t *header = icmp_prepare_packet(packet);379 if (!header) { 380 icmp_release(packet);381 return ENOMEM;382 }383 340 static int icmp_destination_unreachable_msg_local(int icmp_phone, 341 icmp_code_t code, icmp_param_t mtu, packet_t *packet) 342 { 343 icmp_header_t *header; 344 345 header = icmp_prepare_packet(packet); 346 if (!header) 347 return icmp_release_and_return(packet, ENOMEM); 348 384 349 if (mtu) 385 350 header->un.frag.mtu = mtu; 386 351 387 352 return icmp_send_packet(ICMP_DEST_UNREACH, code, packet, header, 388 SERVICE_ICMP, 0, 0, false);389 } 390 391 static int icmp_source_quench (packet_t *packet)392 { 393 icmp_header_t *header = icmp_prepare_packet(packet);394 if (!header) { 395 icmp_release(packet);396 return ENOMEM;397 }398 353 SERVICE_ICMP, 0, 0, 0); 354 } 355 356 static int icmp_source_quench_msg_local(int icmp_phone, packet_t *packet) 357 { 358 icmp_header_t *header; 359 360 header = icmp_prepare_packet(packet); 361 if (!header) 362 return icmp_release_and_return(packet, ENOMEM); 363 399 364 return icmp_send_packet(ICMP_SOURCE_QUENCH, 0, packet, header, 400 SERVICE_ICMP, 0, 0, false); 401 } 402 403 static int icmp_time_exceeded(icmp_code_t code, packet_t *packet) 404 { 405 icmp_header_t *header = icmp_prepare_packet(packet); 406 if (!header) { 407 icmp_release(packet); 408 return ENOMEM; 409 } 410 365 SERVICE_ICMP, 0, 0, 0); 366 } 367 368 static int icmp_time_exceeded_msg_local(int icmp_phone, icmp_code_t code, 369 packet_t *packet) 370 { 371 icmp_header_t *header; 372 373 header = icmp_prepare_packet(packet); 374 if (!header) 375 return icmp_release_and_return(packet, ENOMEM); 376 411 377 return icmp_send_packet(ICMP_TIME_EXCEEDED, code, packet, header, 412 SERVICE_ICMP, 0, 0, false);413 } 414 415 static int icmp_parameter_problem (icmp_code_t code, icmp_param_t pointer,416 packet_t *packet)417 { 418 icmp_header_t *header = icmp_prepare_packet(packet);419 if (!header) { 420 icmp_release(packet);421 return ENOMEM;422 }423 378 SERVICE_ICMP, 0, 0, 0); 379 } 380 381 static int icmp_parameter_problem_msg_local(int icmp_phone, icmp_code_t code, 382 icmp_param_t pointer, packet_t *packet) 383 { 384 icmp_header_t *header; 385 386 header = icmp_prepare_packet(packet); 387 if (!header) 388 return icmp_release_and_return(packet, ENOMEM); 389 424 390 header->un.param.pointer = pointer; 425 391 return icmp_send_packet(ICMP_PARAMETERPROB, code, packet, header, 426 SERVICE_ICMP, 0, 0, false);427 } 428 429 /** Tr yto set the pending reply result as the received message type.392 SERVICE_ICMP, 0, 0, 0); 393 } 394 395 /** Tries to set the pending reply result as the received message type. 430 396 * 431 397 * If the reply data is not present, the reply timed out and the other fibril 432 * is already awake. The packet is released.433 * 434 * @param[in] packet The received reply message.435 * @param[in] header The ICMP message header.436 * @param[in] type The received reply message type.437 * @param[in] code The received reply message code.438 * 439 */ 440 static void icmp_process_echo_reply(packet_t *packet, icmp_header_t *header,398 * is already awake. 399 * Releases the packet. 400 * 401 * @param[in] packet The received reply message. 402 * @param[in] header The ICMP message header. 403 * @param[in] type The received reply message type. 404 * @param[in] code The received reply message code. 405 */ 406 static void icmp_process_echo_reply(packet_t *packet, icmp_header_t *header, 441 407 icmp_type_t type, icmp_code_t code) 442 408 { 443 unsigned long key[REPLY_KEYS] = 444 {header->un.echo.identifier, header->un.echo.sequence_number}; 445 446 /* The packet is no longer needed */ 447 icmp_release(packet); 448 409 int reply_key; 410 icmp_reply_t *reply; 411 412 /* Compute the reply key */ 413 reply_key = ICMP_GET_REPLY_KEY(header->un.echo.identifier, 414 header->un.echo.sequence_number); 415 pq_release_remote(icmp_globals.net_phone, packet_get_id(packet)); 416 449 417 /* Find the pending reply */ 450 fibril_mutex_lock(&reply_lock); 451 452 link_t *link = hash_table_find(&replies, key); 453 if (link != NULL) { 454 icmp_reply_t *reply = 455 hash_table_get_instance(link, icmp_reply_t, link); 456 418 fibril_rwlock_write_lock(&icmp_globals.lock); 419 reply = icmp_replies_find(&icmp_globals.replies, reply_key); 420 if (reply) { 457 421 reply->result = type; 458 422 fibril_condvar_signal(&reply->condvar); 459 423 } 460 461 fibril_mutex_unlock(&reply_lock); 462 } 463 464 /** Process the received ICMP packet. 465 * 466 * Notify the destination socket application. 467 * 468 * @param[in,out] packet Received packet. 469 * @param[in] error Packet error reporting service to prefix 470 * the received packet. 471 * 472 * @return EOK on success. 473 * @return EINVAL if the packet is not valid. 474 * @return EINVAL if the stored packet address is not the an_addr_t. 475 * @return EINVAL if the packet does not contain any data. 476 * @return NO_DATA if the packet content is shorter than the user 477 * datagram header. 478 * @return ENOMEM if there is not enough memory left. 479 * @return EADDRNOTAVAIL if the destination socket does not exist. 480 * @return Other error codes as defined for the 481 * ip_client_process_packet() function. 482 * 424 fibril_rwlock_write_unlock(&icmp_globals.lock); 425 } 426 427 /** Processes the received ICMP packet. 428 * 429 * Notifies the destination socket application. 430 * 431 * @param[in,out] packet The received packet. 432 * @param[in] error The packet error reporting service. Prefixes the 433 * received packet. 434 * @return EOK on success. 435 * @return EINVAL if the packet is not valid. 436 * @return EINVAL if the stored packet address is not the an_addr_t. 437 * @return EINVAL if the packet does not contain any data. 438 * @return NO_DATA if the packet content is shorter than the user 439 * datagram header. 440 * @return ENOMEM if there is not enough memory left. 441 * @return EADDRNOTAVAIL if the destination socket does not exist. 442 * @return Other error codes as defined for the 443 * ip_client_process_packet() function. 483 444 */ 484 445 static int icmp_process_packet(packet_t *packet, services_t error) 485 446 { 447 size_t length; 448 uint8_t *src; 449 int addrlen; 450 int result; 451 void *data; 452 icmp_header_t *header; 486 453 icmp_type_t type; 487 454 icmp_code_t code; … … 493 460 case SERVICE_ICMP: 494 461 /* Process error */ 495 rc = icmp_client_process_packet(packet, &type, &code, NULL, NULL); 496 if (rc < 0) 497 return rc; 498 462 result = icmp_client_process_packet(packet, &type, &code, NULL, 463 NULL); 464 if (result < 0) 465 return result; 466 length = (size_t) result; 499 467 /* Remove the error header */ 500 rc = packet_trim(packet, (size_t) rc, 0);468 rc = packet_trim(packet, length, 0); 501 469 if (rc != EOK) 502 470 return rc; 503 504 471 break; 505 472 default: 506 473 return ENOTSUP; 507 474 } 508 475 509 476 /* Get rid of the IP header */ 510 size_tlength = ip_client_header_length(packet);477 length = ip_client_header_length(packet); 511 478 rc = packet_trim(packet, length, 0); 512 479 if (rc != EOK) 513 480 return rc; 514 481 515 482 length = packet_get_data_length(packet); 516 483 if (length <= 0) 517 484 return EINVAL; 518 485 519 486 if (length < ICMP_HEADER_SIZE) 520 487 return EINVAL; 521 522 void *data = packet_get_data(packet);488 489 data = packet_get_data(packet); 523 490 if (!data) 524 491 return EINVAL; 525 492 526 493 /* Get ICMP header */ 527 icmp_header_t *header = (icmp_header_t *) data;528 494 header = (icmp_header_t *) data; 495 529 496 if (header->checksum) { 530 497 while (ICMP_CHECKSUM(header, length) != IP_CHECKSUM_ZERO) { … … 540 507 } 541 508 } 542 543 509 return EINVAL; 544 510 } 545 511 } 546 512 547 513 switch (header->type) { 548 514 case ICMP_ECHOREPLY: … … 551 517 else 552 518 icmp_process_echo_reply(packet, header, ICMP_ECHO, 0); 553 519 554 520 return EOK; 555 521 556 522 case ICMP_ECHO: 557 523 if (error) { … … 561 527 562 528 /* Do not send a reply if disabled */ 563 if (echo_replying) { 564 uint8_t *src; 565 int addrlen = packet_get_addr(packet, &src, NULL); 566 529 if (icmp_globals.echo_replying) { 530 addrlen = packet_get_addr(packet, &src, NULL); 531 567 532 /* 568 * Set both addresses to the source one (avoid the533 * Set both addresses to the source one (avoids the 569 534 * source address deletion before setting the 570 535 * destination one). … … 577 542 return EOK; 578 543 } 579 544 580 545 return EINVAL; 581 546 } 582 547 583 548 return EPERM; 584 549 585 550 case ICMP_DEST_UNREACH: 586 551 case ICMP_SOURCE_QUENCH: … … 595 560 case ICMP_SKIP: 596 561 case ICMP_PHOTURIS: 597 ip_received_error_msg( phone_ip, -1, packet,562 ip_received_error_msg(icmp_globals.ip_phone, -1, packet, 598 563 SERVICE_IP, SERVICE_ICMP); 599 564 return EOK; 600 565 601 566 default: 602 567 return ENOTSUP; … … 604 569 } 605 570 571 /** Processes the received ICMP packet. 572 * 573 * Is used as an entry point from the underlying IP module. 574 * Releases the packet on error. 575 * 576 * @param device_id The device identifier. Ignored parameter. 577 * @param[in,out] packet The received packet. 578 * @param receiver The target service. Ignored parameter. 579 * @param[in] error The packet error reporting service. Prefixes the 580 * received packet. 581 * @return EOK on success. 582 * @return Other error codes as defined for the 583 * icmp_process_packet() function. 584 */ 585 static int icmp_received_msg_local(device_id_t device_id, packet_t *packet, 586 services_t receiver, services_t error) 587 { 588 int rc; 589 590 rc = icmp_process_packet(packet, error); 591 if (rc != EOK) 592 return icmp_release_and_return(packet, rc); 593 594 return EOK; 595 } 596 606 597 /** Process IPC messages from the IP module 607 598 * … … 612 603 static void icmp_receiver(ipc_callid_t iid, ipc_call_t *icall) 613 604 { 614 bool loop = true;615 605 packet_t *packet; 616 606 int rc; 617 607 618 while ( loop) {608 while (true) { 619 609 switch (IPC_GET_IMETHOD(*icall)) { 620 610 case NET_TL_RECEIVED: 621 rc = packet_translate_remote( phone_net, &packet,611 rc = packet_translate_remote(icmp_globals.net_phone, &packet, 622 612 IPC_GET_PACKET(*icall)); 623 if (rc == EOK) { 624 rc = icmp_process_packet(packet, IPC_GET_ERROR(*icall)); 625 if (rc != EOK) 626 icmp_release(packet); 627 } 613 if (rc == EOK) 614 rc = icmp_received_msg_local(IPC_GET_DEVICE(*icall), packet, 615 SERVICE_ICMP, IPC_GET_ERROR(*icall)); 628 616 629 async_answer_0(iid, (sysarg_t) rc);617 ipc_answer_0(iid, (sysarg_t) rc); 630 618 break; 631 case IPC_M_PHONE_HUNGUP:632 loop = false;633 continue;634 619 default: 635 async_answer_0(iid, (sysarg_t) ENOTSUP);620 ipc_answer_0(iid, (sysarg_t) ENOTSUP); 636 621 } 637 622 … … 664 649 uint8_t *data; 665 650 666 if (!hash_table_create(&replies, REPLY_BUCKETS, REPLY_KEYS, &reply_ops)) 667 return ENOMEM; 668 669 fibril_mutex_initialize(&reply_lock); 670 atomic_set(&icmp_client, 0); 671 672 phone_net = net_phone; 673 phone_ip = ip_bind_service(SERVICE_IP, IPPROTO_ICMP, SERVICE_ICMP, 674 icmp_receiver); 675 if (phone_ip < 0) 676 return phone_ip; 677 678 int rc = ip_packet_size_req(phone_ip, -1, &icmp_dimension); 679 if (rc != EOK) 651 fibril_rwlock_initialize(&icmp_globals.lock); 652 fibril_rwlock_write_lock(&icmp_globals.lock); 653 icmp_replies_initialize(&icmp_globals.replies); 654 icmp_echo_data_initialize(&icmp_globals.echo_data); 655 656 icmp_globals.net_phone = net_phone; 657 658 icmp_globals.ip_phone = ip_bind_service(SERVICE_IP, IPPROTO_ICMP, 659 SERVICE_ICMP, icmp_receiver); 660 if (icmp_globals.ip_phone < 0) { 661 fibril_rwlock_write_unlock(&icmp_globals.lock); 662 return icmp_globals.ip_phone; 663 } 664 665 int rc = ip_packet_size_req(icmp_globals.ip_phone, -1, 666 &icmp_globals.packet_dimension); 667 if (rc != EOK) { 668 fibril_rwlock_write_unlock(&icmp_globals.lock); 680 669 return rc; 681 682 icmp_dimension.prefix += ICMP_HEADER_SIZE; 683 icmp_dimension.content -= ICMP_HEADER_SIZE; 670 } 671 672 icmp_globals.packet_dimension.prefix += ICMP_HEADER_SIZE; 673 icmp_globals.packet_dimension.content -= ICMP_HEADER_SIZE; 674 675 icmp_globals.error_reporting = NET_DEFAULT_ICMP_ERROR_REPORTING; 676 icmp_globals.echo_replying = NET_DEFAULT_ICMP_ECHO_REPLYING; 684 677 685 678 /* Get configuration */ 686 679 configuration = &names[0]; 687 rc = net_get_conf_req(phone_net, &configuration, count, &data); 688 if (rc != EOK) 680 rc = net_get_conf_req(icmp_globals.net_phone, &configuration, count, 681 &data); 682 if (rc != EOK) { 683 fibril_rwlock_write_unlock(&icmp_globals.lock); 689 684 return rc; 685 } 690 686 691 687 if (configuration) { 692 if (configuration[0].value) 693 error_reporting = (configuration[0].value[0] == 'y'); 694 695 if (configuration[1].value) 696 echo_replying = (configuration[1].value[0] == 'y'); 697 688 if (configuration[0].value) { 689 icmp_globals.error_reporting = 690 (configuration[0].value[0] == 'y'); 691 } 692 if (configuration[1].value) { 693 icmp_globals.echo_replying = 694 (configuration[1].value[0] == 'y'); 695 } 698 696 net_free_settings(configuration, data); 699 697 } 700 698 699 fibril_rwlock_write_unlock(&icmp_globals.lock); 701 700 return EOK; 702 701 } 703 702 704 /** Per-connection initialization 705 * 706 * Initialize client-specific global variables. 707 * 708 */ 709 void tl_connection(void) 710 { 711 icmp_id = (icmp_param_t) atomic_postinc(&icmp_client); 712 icmp_seq = 1; 713 } 714 715 /** Process the ICMP message. 716 * 717 * @param[in] callid Message identifier. 718 * @param[in] call Message parameters. 719 * @param[out] answer Answer. 720 * @param[out] count Number of arguments of the answer. 721 * 722 * @return EOK on success. 723 * @return ENOTSUP if the message is not known. 724 * @return Other error codes as defined for the packet_translate() 725 * function. 726 * @return Other error codes as defined for the 727 * icmp_destination_unreachable() function. 728 * @return Other error codes as defined for the 729 * icmp_source_quench() function. 730 * @return Other error codes as defined for the 731 * icmp_time_exceeded() function. 732 * @return Other error codes as defined for the 733 * icmp_parameter_problem() function. 734 * 735 * @see icmp_remote.h 736 * @see IS_NET_ICMP_MESSAGE() 737 * 738 */ 739 int tl_message(ipc_callid_t callid, ipc_call_t *call, 740 ipc_call_t *answer, size_t *count) 741 { 742 struct sockaddr *addr; 743 size_t size; 703 /** Processes the generic client messages. 704 * 705 * @param[in] call The message parameters. 706 * @return EOK on success. 707 * @return ENOTSUP if the message is not known. 708 * @return Other error codes as defined for the packet_translate() 709 * function. 710 * @return Other error codes as defined for the 711 * icmp_destination_unreachable_msg_local() function. 712 * @return Other error codes as defined for the 713 * icmp_source_quench_msg_local() function. 714 * @return Other error codes as defined for the 715 * icmp_time_exceeded_msg_local() function. 716 * @return Other error codes as defined for the 717 * icmp_parameter_problem_msg_local() function. 718 * 719 * @see icmp_interface.h 720 */ 721 static int icmp_process_message(ipc_call_t *call) 722 { 744 723 packet_t *packet; 745 724 int rc; 746 747 *count = 0; 748 725 749 726 switch (IPC_GET_IMETHOD(*call)) { 750 case NET_ICMP_ECHO:751 rc = async_data_write_accept((void **) &addr, false, 0, 0, 0, &size);752 if (rc != EOK)753 return rc;754 755 rc = icmp_echo(icmp_id, icmp_seq, ICMP_GET_SIZE(*call),756 ICMP_GET_TIMEOUT(*call), ICMP_GET_TTL(*call),757 ICMP_GET_TOS(*call), ICMP_GET_DONT_FRAGMENT(*call),758 addr, (socklen_t) size);759 760 free(addr);761 icmp_seq++;762 return rc;763 764 727 case NET_ICMP_DEST_UNREACH: 765 rc = packet_translate_remote( phone_net, &packet,728 rc = packet_translate_remote(icmp_globals.net_phone, &packet, 766 729 IPC_GET_PACKET(*call)); 767 730 if (rc != EOK) 768 731 return rc; 769 770 return icmp_destination_unreachable(ICMP_GET_CODE(*call), 771 ICMP_GET_MTU(*call), packet); 772 732 return icmp_destination_unreachable_msg_local(0, 733 ICMP_GET_CODE(*call), ICMP_GET_MTU(*call), packet); 773 734 case NET_ICMP_SOURCE_QUENCH: 774 rc = packet_translate_remote( phone_net, &packet,735 rc = packet_translate_remote(icmp_globals.net_phone, &packet, 775 736 IPC_GET_PACKET(*call)); 776 737 if (rc != EOK) 777 738 return rc; 778 779 return icmp_source_quench(packet); 780 739 return icmp_source_quench_msg_local(0, packet); 781 740 case NET_ICMP_TIME_EXCEEDED: 782 rc = packet_translate_remote( phone_net, &packet,741 rc = packet_translate_remote(icmp_globals.net_phone, &packet, 783 742 IPC_GET_PACKET(*call)); 784 743 if (rc != EOK) 785 744 return rc; 786 787 return icmp_time_exceeded(ICMP_GET_CODE(*call), packet); 788 745 return icmp_time_exceeded_msg_local(0, ICMP_GET_CODE(*call), 746 packet); 789 747 case NET_ICMP_PARAMETERPROB: 790 rc = packet_translate_remote( phone_net, &packet,748 rc = packet_translate_remote(icmp_globals.net_phone, &packet, 791 749 IPC_GET_PACKET(*call)); 792 750 if (rc != EOK) 793 751 return rc; 752 return icmp_parameter_problem_msg_local(0, ICMP_GET_CODE(*call), 753 ICMP_GET_POINTER(*call), packet); 754 default: 755 return ENOTSUP; 756 } 757 } 758 759 /** Assigns a new identifier for the connection. 760 * 761 * Fills the echo data parameter with the assigned values. 762 * 763 * @param[in,out] echo_data The echo data to be bound. 764 * @return Index of the inserted echo data. 765 * @return EBADMEM if the echo_data parameter is NULL. 766 * @return ENOTCONN if no free identifier have been found. 767 */ 768 static int icmp_bind_free_id(icmp_echo_t *echo_data) 769 { 770 icmp_param_t index; 771 772 if (!echo_data) 773 return EBADMEM; 774 775 /* From the last used one */ 776 index = icmp_globals.last_used_id; 777 do { 778 index++; 779 /* til the range end */ 780 if (index >= ICMP_FREE_IDS_END) { 781 /* start from the range beginning */ 782 index = ICMP_FREE_IDS_START - 1; 783 do { 784 index++; 785 /* til the last used one */ 786 if (index >= icmp_globals.last_used_id) { 787 /* none found */ 788 return ENOTCONN; 789 } 790 } while(icmp_echo_data_find(&icmp_globals.echo_data, 791 index) != NULL); 792 793 /* Found, break immediately */ 794 break; 795 } 796 } while (icmp_echo_data_find(&icmp_globals.echo_data, index) != NULL); 797 798 echo_data->identifier = index; 799 echo_data->sequence_number = 0; 800 801 return icmp_echo_data_add(&icmp_globals.echo_data, index, echo_data); 802 } 803 804 /** Processes the client messages. 805 * 806 * Remembers the assigned identifier and sequence numbers. 807 * Runs until the client module disconnects. 808 * 809 * @param[in] callid The message identifier. 810 * @param[in] call The message parameters. 811 * @return EOK. 812 * 813 * @see icmp_interface.h 814 * @see icmp_api.h 815 */ 816 static int icmp_process_client_messages(ipc_callid_t callid, ipc_call_t call) 817 { 818 bool keep_on_going = true; 819 ipc_call_t answer; 820 size_t answer_count; 821 size_t length; 822 struct sockaddr *addr; 823 ipc_callid_t data_callid; 824 icmp_echo_t *echo_data; 825 int rc = EOK; 826 827 /* 828 * Accept the connection 829 * - Answer the first NET_ICMP_INIT call. 830 */ 831 answer_count = 0; 832 833 echo_data = (icmp_echo_t *) malloc(sizeof(*echo_data)); 834 if (!echo_data) 835 return ENOMEM; 836 837 /* Assign a new identifier */ 838 fibril_rwlock_write_lock(&icmp_globals.lock); 839 rc = icmp_bind_free_id(echo_data); 840 fibril_rwlock_write_unlock(&icmp_globals.lock); 841 if (rc < 0) { 842 free(echo_data); 843 return rc; 844 } 845 846 while (keep_on_going) { 847 /* Answer the call */ 848 answer_call(callid, rc, &answer, answer_count); 849 850 /* Refresh data */ 851 refresh_answer(&answer, &answer_count); 852 853 /* Get the next call */ 854 callid = async_get_call(&call); 855 856 /* Process the call */ 857 switch (IPC_GET_IMETHOD(call)) { 858 case IPC_M_PHONE_HUNGUP: 859 keep_on_going = false; 860 rc = EHANGUP; 861 break; 794 862 795 return icmp_parameter_problem(ICMP_GET_CODE(*call), 796 ICMP_GET_POINTER(*call), packet); 863 case NET_ICMP_ECHO: 864 if (!async_data_write_receive(&data_callid, &length)) { 865 rc = EINVAL; 866 break; 867 } 868 869 addr = malloc(length); 870 if (!addr) { 871 rc = ENOMEM; 872 break; 873 } 874 875 rc = async_data_write_finalize(data_callid, addr, 876 length); 877 if (rc != EOK) { 878 free(addr); 879 break; 880 } 881 882 fibril_rwlock_write_lock(&icmp_globals.lock); 883 rc = icmp_echo(echo_data->identifier, 884 echo_data->sequence_number, ICMP_GET_SIZE(call), 885 ICMP_GET_TIMEOUT(call), ICMP_GET_TTL(call), 886 ICMP_GET_TOS(call), ICMP_GET_DONT_FRAGMENT(call), 887 addr, (socklen_t) length); 888 fibril_rwlock_write_unlock(&icmp_globals.lock); 889 890 free(addr); 891 892 if (echo_data->sequence_number < UINT16_MAX) 893 echo_data->sequence_number++; 894 else 895 echo_data->sequence_number = 0; 896 897 break; 898 899 default: 900 rc = icmp_process_message(&call); 901 } 902 903 } 904 905 /* Release the identifier */ 906 fibril_rwlock_write_lock(&icmp_globals.lock); 907 icmp_echo_data_exclude(&icmp_globals.echo_data, echo_data->identifier); 908 fibril_rwlock_write_unlock(&icmp_globals.lock); 909 910 return rc; 911 } 912 913 /** Processes the ICMP message. 914 * 915 * @param[in] callid The message identifier. 916 * @param[in] call The message parameters. 917 * @param[out] answer The message answer parameters. 918 * @param[out] answer_count The last parameter for the actual answer in the 919 * answer parameter. 920 * @return EOK on success. 921 * @return ENOTSUP if the message is not known. 922 * 923 * @see icmp_interface.h 924 * @see IS_NET_ICMP_MESSAGE() 925 */ 926 int tl_module_message (ipc_callid_t callid, ipc_call_t *call, 927 ipc_call_t *answer, size_t *answer_count) 928 { 929 *answer_count = 0; 930 switch (IPC_GET_IMETHOD(*call)) { 931 case NET_ICMP_INIT: 932 return icmp_process_client_messages(callid, *call); 933 default: 934 return icmp_process_message(call); 797 935 } 798 936
Note:
See TracChangeset
for help on using the changeset viewer.