Rev 4731 | Rev 4743 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed
| Rev | Author | Line No. | Line |
|---|---|---|---|
| 4499 | 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 udp |
||
| 30 | * @{ |
||
| 31 | */ |
||
| 32 | |||
| 33 | /** @file |
||
| 4701 | mejdrech | 34 | * UDP module implementation. |
| 35 | * @see udp.h |
||
| 4499 | mejdrech | 36 | */ |
| 37 | |||
| 38 | #include <async.h> |
||
| 4700 | mejdrech | 39 | #include <fibril_sync.h> |
| 4579 | mejdrech | 40 | #include <malloc.h> |
| 4499 | mejdrech | 41 | |
| 42 | #include <ipc/ipc.h> |
||
| 43 | #include <ipc/services.h> |
||
| 44 | |||
| 45 | #include "../../err.h" |
||
| 46 | #include "../../messages.h" |
||
| 47 | #include "../../modules.h" |
||
| 4589 | mejdrech | 48 | |
| 49 | #include "../../structures/dynamic_fifo.h" |
||
| 4499 | mejdrech | 50 | #include "../../structures/packet/packet_client.h" |
| 51 | |||
| 4722 | mejdrech | 52 | #include "../../include/crc.h" |
| 4579 | mejdrech | 53 | #include "../../include/in.h" |
| 4720 | mejdrech | 54 | #include "../../include/in6.h" |
| 4579 | mejdrech | 55 | #include "../../include/inet.h" |
| 4499 | mejdrech | 56 | #include "../../include/ip_client.h" |
| 57 | #include "../../include/ip_interface.h" |
||
| 58 | #include "../../include/ip_protocols.h" |
||
| 4707 | mejdrech | 59 | #include "../../include/icmp_client.h" |
| 60 | #include "../../include/icmp_interface.h" |
||
| 4722 | mejdrech | 61 | #include "../../include/net_interface.h" |
| 4713 | mejdrech | 62 | #include "../../include/socket_codes.h" |
| 4579 | mejdrech | 63 | #include "../../include/socket_errno.h" |
| 4499 | mejdrech | 64 | |
| 4579 | mejdrech | 65 | #include "../../socket/socket_core.h" |
| 66 | #include "../../socket/socket_messages.h" |
||
| 67 | |||
| 4726 | mejdrech | 68 | #include "../tl_common.h" |
| 4499 | mejdrech | 69 | #include "../tl_messages.h" |
| 70 | |||
| 71 | #include "udp.h" |
||
| 4579 | mejdrech | 72 | #include "udp_header.h" |
| 4499 | mejdrech | 73 | #include "udp_module.h" |
| 74 | |||
| 4722 | mejdrech | 75 | /** Default UDP checksum computing. |
| 76 | */ |
||
| 77 | #define NET_DEFAULT_UDP_CHECKSUM_COMPUTING true |
||
| 78 | |||
| 4730 | mejdrech | 79 | /** Default UDP autobind when sending via unbound sockets. |
| 80 | */ |
||
| 81 | #define NET_DEFAULT_UDP_AUTOBINDING true |
||
| 82 | |||
| 4701 | mejdrech | 83 | /** Maximum UDP fragment size. |
| 84 | */ |
||
| 4589 | mejdrech | 85 | #define MAX_UDP_FRAGMENT_SIZE 65535 |
| 86 | |||
| 4701 | mejdrech | 87 | /** Free ports pool start. |
| 88 | */ |
||
| 4700 | mejdrech | 89 | #define UDP_FREE_PORTS_START 1025 |
| 4701 | mejdrech | 90 | |
| 91 | /** Free ports pool end. |
||
| 92 | */ |
||
| 4700 | mejdrech | 93 | #define UDP_FREE_PORTS_END 65535 |
| 94 | |||
| 4707 | mejdrech | 95 | /** Processes the received UDP packet queue. |
| 4701 | mejdrech | 96 | * Is used as an entry point from the underlying IP module. |
| 4730 | mejdrech | 97 | * Locks the global lock and calls udp_process_packet() function. |
| 4701 | mejdrech | 98 | * @param device_id The device identifier. Ignored parameter. |
| 4707 | mejdrech | 99 | * @param packet The received packet queue. Input/output parameter. |
| 4701 | mejdrech | 100 | * @param receiver The target service. Ignored parameter. |
| 4707 | mejdrech | 101 | * @param error The packet error reporting service. Prefixes the received packet. Input parameter. |
| 4701 | mejdrech | 102 | * @returns EOK on success. |
| 4730 | mejdrech | 103 | * @returns Other error codes as defined for the udp_process_packet() function. |
| 104 | */ |
||
| 105 | int udp_received_msg( device_id_t device_id, packet_t packet, services_t receiver, services_t error ); |
||
| 106 | |||
| 107 | /** Processes the received UDP packet queue. |
||
| 108 | * Notifies the destination socket application. |
||
| 109 | * Releases the packet on error or sends an ICMP error notification.. |
||
| 110 | * @param packet The received packet queue. Input/output parameter. |
||
| 111 | * @param error The packet error reporting service. Prefixes the received packet. Input parameter. |
||
| 112 | * @returns EOK on success. |
||
| 4701 | mejdrech | 113 | * @returns EINVAL if the packet is not valid. |
| 114 | * @returns EINVAL if the stored packet address is not the an_addr_t. |
||
| 115 | * @returns EINVAL if the packet does not contain any data. |
||
| 116 | * @returns NO_DATA if the packet content is shorter than the user datagram header. |
||
| 117 | * @returns ENOMEM if there is not enough memory left. |
||
| 118 | * @returns EADDRNOTAVAIL if the destination socket does not exist. |
||
| 119 | * @returns Other error codes as defined for the ip_client_process_packet() function. |
||
| 120 | */ |
||
| 4730 | mejdrech | 121 | int udp_process_packet( packet_t packet, services_t error ); |
| 4701 | mejdrech | 122 | |
| 4707 | mejdrech | 123 | /** Releases the packet and returns the result. |
| 124 | * @param packet The packet queue to be released. Input parameter. |
||
| 125 | * @param result The result to be returned. Input parameter. |
||
| 126 | * @return The result parameter. |
||
| 127 | */ |
||
| 4713 | mejdrech | 128 | int udp_release_and_return( packet_t packet, int result ); |
| 4707 | mejdrech | 129 | |
| 4701 | mejdrech | 130 | /** @name Socket messages processing functions |
| 131 | */ |
||
| 132 | /*@{*/ |
||
| 133 | |||
| 134 | /** Processes the socket client messages. |
||
| 135 | * Runs until the client module disconnects. |
||
| 136 | * @param callid The message identifier. Input parameter. |
||
| 137 | * @param call The message parameters. Input parameter. |
||
| 138 | * @returns EOK on success. |
||
| 139 | * @see socket.h |
||
| 140 | */ |
||
| 4713 | mejdrech | 141 | int udp_process_client_messages( ipc_callid_t callid, ipc_call_t call ); |
| 4701 | mejdrech | 142 | |
| 143 | /** Sends data from the socket to the remote address. |
||
| 144 | * Binds the socket to a free port if not already connected/bound. |
||
| 145 | * Handles the NET_SOCKET_SENDTO message. |
||
| 4720 | mejdrech | 146 | * Supports AF_INET and AF_INET6 address families. |
| 4701 | mejdrech | 147 | * @param local_sockets The application local sockets. Input/output parameter. |
| 148 | * @param socket_id Socket identifier. Input parameter. |
||
| 149 | * @param addr The destination address. Input parameter. |
||
| 150 | * @param addrlen The address length. Input parameter. |
||
| 151 | * @param fragments The number of data fragments. Input parameter. |
||
| 4726 | mejdrech | 152 | * @param data_fragment_size The data fragment size in bytes. Input parameter. |
| 4701 | mejdrech | 153 | * @param flags Various send flags. Input parameter. |
| 154 | * @returns EOK on success. |
||
| 155 | * @returns EAFNOTSUPPORT if the address family is not supported. |
||
| 156 | * @returns ENOTSOCK if the socket is not found. |
||
| 157 | * @returns EINVAL if the address is invalid. |
||
| 158 | * @returns ENOTCONN if the sending socket is not and cannot be bound. |
||
| 159 | * @returns ENOMEM if there is not enough memory left. |
||
| 160 | * @returns Other error codes as defined for the socket_read_packet_data() function. |
||
| 161 | * @returns Other error codes as defined for the ip_client_prepare_packet() function. |
||
| 162 | * @returns Other error codes as defined for the ip_send_msg() function. |
||
| 163 | */ |
||
| 4726 | mejdrech | 164 | int udp_sendto_message( socket_cores_ref local_sockets, int socket_id, const struct sockaddr * addr, socklen_t addrlen, int fragments, size_t data_fragment_size, int flags ); |
| 4701 | mejdrech | 165 | |
| 166 | /** Receives data to the socket. |
||
| 167 | * Handles the NET_SOCKET_RECVFROM message. |
||
| 4720 | mejdrech | 168 | * Replies the source address as well. |
| 4701 | mejdrech | 169 | * @param local_sockets The application local sockets. Input parameter. |
| 170 | * @param socket_id Socket identifier. Input parameter. |
||
| 171 | * @param flags Various receive flags. Input parameter. |
||
| 4720 | mejdrech | 172 | * @param addrlen The source address length. Output parameter. |
| 4701 | mejdrech | 173 | * @returns The number of bytes received. |
| 174 | * @returns ENOTSOCK if the socket is not found. |
||
| 175 | * @returns NO_DATA if there are no received packets or data. |
||
| 176 | * @returns ENOMEM if there is not enough memory left. |
||
| 177 | * @returns EINVAL if the received address is not an IP address. |
||
| 178 | * @returns Other error codes as defined for the packet_translate() function. |
||
| 4722 | mejdrech | 179 | * @returns Other error codes as defined for the data_reply() function. |
| 4701 | mejdrech | 180 | */ |
| 4720 | mejdrech | 181 | int udp_recvfrom_message( socket_cores_ref local_sockets, int socket_id, int flags, size_t * addrlen ); |
| 4701 | mejdrech | 182 | |
| 183 | /*@}*/ |
||
| 184 | |||
| 185 | /** UDP global data. |
||
| 186 | */ |
||
| 4499 | mejdrech | 187 | udp_globals_t udp_globals; |
| 188 | |||
| 189 | int udp_initialize( async_client_conn_t client_connection ){ |
||
| 4579 | mejdrech | 190 | ERROR_DECLARE; |
| 4499 | mejdrech | 191 | |
| 4730 | mejdrech | 192 | measured_string_t names[] = {{ "UDP_CHECKSUM_COMPUTING", 22 }, { "UDP_AUTOBINDING", 15 }}; |
| 4722 | mejdrech | 193 | measured_string_ref configuration; |
| 194 | size_t count = sizeof( names ) / sizeof( measured_string_t ); |
||
| 195 | char * data; |
||
| 196 | |||
| 4700 | mejdrech | 197 | fibril_rwlock_initialize( & udp_globals.lock ); |
| 198 | fibril_rwlock_write_lock( & udp_globals.lock ); |
||
| 4707 | mejdrech | 199 | udp_globals.icmp_phone = icmp_connect_module( SERVICE_ICMP ); |
| 200 | if( udp_globals.icmp_phone < 0 ){ |
||
| 201 | return udp_globals.icmp_phone; |
||
| 202 | } |
||
| 4499 | mejdrech | 203 | udp_globals.ip_phone = ip_bind_service( SERVICE_IP, IPPROTO_UDP, SERVICE_UDP, client_connection, udp_received_msg ); |
| 4579 | mejdrech | 204 | if( udp_globals.ip_phone < 0 ){ |
| 205 | return udp_globals.ip_phone; |
||
| 206 | } |
||
| 4726 | mejdrech | 207 | ERROR_PROPAGATE( ip_packet_size_req( udp_globals.ip_phone, -1, & udp_globals.packet_dimension.addr_len, & udp_globals.packet_dimension.prefix, & udp_globals.packet_dimension.content, & udp_globals.packet_dimension.suffix )); |
| 4579 | mejdrech | 208 | ERROR_PROPAGATE( socket_ports_initialize( & udp_globals.sockets )); |
| 4726 | mejdrech | 209 | udp_globals.packet_dimension.prefix += sizeof( udp_header_t ); |
| 210 | udp_globals.packet_dimension.content -= sizeof( udp_header_t ); |
||
| 4700 | mejdrech | 211 | udp_globals.last_used_port = UDP_FREE_PORTS_START - 1; |
| 4722 | mejdrech | 212 | // get configuration |
| 213 | udp_globals.checksum_computing = NET_DEFAULT_UDP_CHECKSUM_COMPUTING; |
||
| 4730 | mejdrech | 214 | udp_globals.autobinding = NET_DEFAULT_UDP_AUTOBINDING; |
| 4722 | mejdrech | 215 | configuration = & names[ 0 ]; |
| 216 | ERROR_PROPAGATE( net_get_conf_req( udp_globals.net_phone, & configuration, count, & data )); |
||
| 217 | if( configuration ){ |
||
| 218 | if( configuration[ 0 ].value ){ |
||
| 219 | udp_globals.checksum_computing = ( configuration[ 0 ].value[ 0 ] == 'y' ); |
||
| 220 | } |
||
| 4730 | mejdrech | 221 | if( configuration[ 1 ].value ){ |
| 222 | udp_globals.autobinding = ( configuration[ 1 ].value[ 0 ] == 'y' ); |
||
| 223 | } |
||
| 4722 | mejdrech | 224 | net_free_settings( configuration, data ); |
| 225 | } |
||
| 4700 | mejdrech | 226 | fibril_rwlock_write_unlock( & udp_globals.lock ); |
| 4579 | mejdrech | 227 | return EOK; |
| 4499 | mejdrech | 228 | } |
| 229 | |||
| 4707 | mejdrech | 230 | int udp_received_msg( device_id_t device_id, packet_t packet, services_t receiver, services_t error ){ |
| 4730 | mejdrech | 231 | int result; |
| 232 | |||
| 4738 | mejdrech | 233 | fibril_rwlock_write_lock( & udp_globals.lock ); |
| 4730 | mejdrech | 234 | result = udp_process_packet( packet, error ); |
| 4738 | mejdrech | 235 | fibril_rwlock_write_unlock( & udp_globals.lock ); |
| 4730 | mejdrech | 236 | |
| 237 | return result; |
||
| 238 | } |
||
| 239 | |||
| 240 | int udp_process_packet( packet_t packet, services_t error ){ |
||
| 4589 | mejdrech | 241 | ERROR_DECLARE; |
| 242 | |||
| 4708 | mejdrech | 243 | size_t length; |
| 244 | size_t offset; |
||
| 245 | int result; |
||
| 4589 | mejdrech | 246 | udp_header_ref header; |
| 4738 | mejdrech | 247 | socket_core_ref socket; |
| 4589 | mejdrech | 248 | packet_t next_packet; |
| 4708 | mejdrech | 249 | size_t total_length; |
| 4722 | mejdrech | 250 | uint32_t checksum; |
| 4589 | mejdrech | 251 | int fragments; |
| 252 | packet_t tmp_packet; |
||
| 4707 | mejdrech | 253 | icmp_type_t type; |
| 254 | icmp_code_t code; |
||
| 4722 | mejdrech | 255 | ip_pseudo_header_ref ip_header; |
| 256 | struct sockaddr * src; |
||
| 257 | struct sockaddr * dest; |
||
| 4589 | mejdrech | 258 | |
| 4707 | mejdrech | 259 | if( error ){ |
| 260 | switch( error ){ |
||
| 261 | case SERVICE_ICMP: |
||
| 4731 | mejdrech | 262 | // ignore error |
| 263 | // length = icmp_client_header_length( packet ); |
||
| 4707 | mejdrech | 264 | // process error |
| 4708 | mejdrech | 265 | result = icmp_client_process_packet( packet, & type, & code, NULL, NULL ); |
| 266 | if( result < 0 ){ |
||
| 4713 | mejdrech | 267 | return udp_release_and_return( packet, result ); |
| 4707 | mejdrech | 268 | } |
| 4708 | mejdrech | 269 | length = ( size_t ) result; |
| 4707 | mejdrech | 270 | if( ERROR_OCCURRED( packet_trim( packet, length, 0 ))){ |
| 4713 | mejdrech | 271 | return udp_release_and_return( packet, ERROR_CODE ); |
| 4707 | mejdrech | 272 | } |
| 273 | break; |
||
| 274 | default: |
||
| 4713 | mejdrech | 275 | return udp_release_and_return( packet, ENOTSUP ); |
| 4707 | mejdrech | 276 | } |
| 277 | } |
||
| 4701 | mejdrech | 278 | // TODO process received ipopts? |
| 4708 | mejdrech | 279 | result = ip_client_process_packet( packet, NULL, NULL, NULL, NULL, NULL ); |
| 280 | if( result < 0 ){ |
||
| 4713 | mejdrech | 281 | return udp_release_and_return( packet, result ); |
| 4707 | mejdrech | 282 | } |
| 4708 | mejdrech | 283 | offset = ( size_t ) result; |
| 4589 | mejdrech | 284 | |
| 285 | length = packet_get_data_length( packet ); |
||
| 4707 | mejdrech | 286 | if( length <= 0 ){ |
| 4713 | mejdrech | 287 | return udp_release_and_return( packet, EINVAL ); |
| 4707 | mejdrech | 288 | } |
| 289 | if( length < sizeof( udp_header_t ) + offset ){ |
||
| 4713 | mejdrech | 290 | return udp_release_and_return( packet, NO_DATA ); |
| 4707 | mejdrech | 291 | } |
| 4731 | mejdrech | 292 | |
| 293 | // trim all but UDP header |
||
| 294 | if( ERROR_OCCURRED( packet_trim( packet, offset, 0 ))){ |
||
| 295 | return udp_release_and_return( packet, ERROR_CODE ); |
||
| 296 | } |
||
| 297 | |||
| 298 | // get udp header |
||
| 299 | header = ( udp_header_ref ) packet_get_data( packet ); |
||
| 300 | if( ! header ){ |
||
| 4713 | mejdrech | 301 | return udp_release_and_return( packet, NO_DATA ); |
| 4707 | mejdrech | 302 | } |
| 4589 | mejdrech | 303 | // find the destination socket |
| 4738 | mejdrech | 304 | socket = socket_port_find( & udp_globals.sockets, ntohs( header->destination_port ), SOCKET_MAP_KEY_LISTENING, 0 ); |
| 4707 | mejdrech | 305 | if( ! socket ){ |
| 4731 | mejdrech | 306 | if( tl_prepare_icmp_packet( udp_globals.net_phone, udp_globals.icmp_phone, packet, error ) == EOK ){ |
| 307 | icmp_destination_unreachable_msg( udp_globals.icmp_phone, ICMP_PORT_UNREACH, 0, packet ); |
||
| 308 | } |
||
| 4707 | mejdrech | 309 | return EADDRNOTAVAIL; |
| 310 | } |
||
| 4730 | mejdrech | 311 | |
| 4589 | mejdrech | 312 | // count the received packet fragments |
| 313 | next_packet = packet; |
||
| 314 | fragments = 0; |
||
| 4728 | mejdrech | 315 | total_length = ntohs( header->total_length ); |
| 4722 | mejdrech | 316 | // compute header checksum if set |
| 4728 | mejdrech | 317 | if( header->checksum && ( ! error )){ |
| 4722 | mejdrech | 318 | result = packet_get_addr( packet, ( uint8_t ** ) & src, ( uint8_t ** ) & dest ); |
| 319 | if( result <= 0 ){ |
||
| 320 | return udp_release_and_return( packet, result ); |
||
| 321 | } |
||
| 322 | if( ERROR_OCCURRED( ip_client_get_pseudo_header( IPPROTO_UDP, src, result, dest, result, total_length, & ip_header, & length ))){ |
||
| 323 | return udp_release_and_return( packet, ERROR_CODE ); |
||
| 324 | }else{ |
||
| 325 | checksum = compute_checksum( 0, ip_header, length ); |
||
| 326 | // the udp header checksum will be added with the first fragment later |
||
| 327 | free( ip_header ); |
||
| 328 | } |
||
| 329 | }else{ |
||
| 4728 | mejdrech | 330 | header->checksum = 0; |
| 4722 | mejdrech | 331 | checksum = 0; |
| 332 | } |
||
| 4730 | mejdrech | 333 | |
| 4589 | mejdrech | 334 | do{ |
| 335 | ++ fragments; |
||
| 4722 | mejdrech | 336 | length = packet_get_data_length( next_packet ); |
| 4708 | mejdrech | 337 | if( length <= 0 ){ |
| 4713 | mejdrech | 338 | return udp_release_and_return( packet, NO_DATA ); |
| 4707 | mejdrech | 339 | } |
| 4589 | mejdrech | 340 | if( total_length < length ){ |
| 4707 | mejdrech | 341 | if( ERROR_OCCURRED( packet_trim( next_packet, 0, length - total_length ))){ |
| 4713 | mejdrech | 342 | return udp_release_and_return( packet, ERROR_CODE ); |
| 4707 | mejdrech | 343 | } |
| 4722 | mejdrech | 344 | // add partial checksum if set |
| 4728 | mejdrech | 345 | if( header->checksum ){ |
| 4722 | mejdrech | 346 | checksum = compute_checksum( checksum, packet_get_data( packet ), packet_get_data_length( packet )); |
| 347 | } |
||
| 4589 | mejdrech | 348 | // relese the rest of the packet fragments |
| 349 | tmp_packet = pq_next( next_packet ); |
||
| 350 | while( tmp_packet ){ |
||
| 351 | next_packet = pq_detach( tmp_packet ); |
||
| 352 | pq_release( udp_globals.net_phone, packet_get_id( tmp_packet )); |
||
| 353 | tmp_packet = next_packet; |
||
| 354 | } |
||
| 4722 | mejdrech | 355 | // exit the loop |
| 4589 | mejdrech | 356 | break; |
| 357 | } |
||
| 358 | total_length -= length; |
||
| 4722 | mejdrech | 359 | // add partial checksum if set |
| 4728 | mejdrech | 360 | if( header->checksum ){ |
| 4722 | mejdrech | 361 | checksum = compute_checksum( checksum, packet_get_data( packet ), packet_get_data_length( packet )); |
| 4589 | mejdrech | 362 | } |
| 363 | }while(( next_packet = pq_next( next_packet )) && ( total_length > 0 )); |
||
| 4730 | mejdrech | 364 | |
| 4722 | mejdrech | 365 | // check checksum |
| 4728 | mejdrech | 366 | if( header->checksum ){ |
| 4722 | mejdrech | 367 | if( flip_checksum( compact_checksum( checksum ))){ |
| 4731 | mejdrech | 368 | if( tl_prepare_icmp_packet( udp_globals.net_phone, udp_globals.icmp_phone, packet, error ) == EOK ){ |
| 369 | // checksum error ICMP |
||
| 370 | icmp_parameter_problem_msg( udp_globals.icmp_phone, ICMP_PARAM_POINTER, (( size_t ) (( void * ) & header->checksum )) - (( size_t ) (( void * ) header )), packet ); |
||
| 371 | } |
||
| 372 | return EINVAL; |
||
| 4722 | mejdrech | 373 | } |
| 374 | } |
||
| 4730 | mejdrech | 375 | |
| 4589 | mejdrech | 376 | // queue the received packet |
| 4738 | mejdrech | 377 | if( ERROR_OCCURRED( dyn_fifo_push( & socket->received, packet_get_id( packet ), SOCKET_MAX_RECEIVED_SIZE ))){ |
| 4713 | mejdrech | 378 | return udp_release_and_return( packet, ERROR_CODE ); |
| 4707 | mejdrech | 379 | } |
| 4499 | mejdrech | 380 | |
| 4589 | mejdrech | 381 | // notify the destination socket |
| 4738 | mejdrech | 382 | async_msg_5( socket->phone, NET_SOCKET_RECEIVED, ( ipcarg_t ) socket->socket_id, 0, 0, 0, ( ipcarg_t ) fragments ); |
| 4499 | mejdrech | 383 | return EOK; |
| 384 | } |
||
| 385 | |||
| 386 | int udp_message( ipc_callid_t callid, ipc_call_t * call, ipc_call_t * answer, int * answer_count ){ |
||
| 4505 | mejdrech | 387 | ERROR_DECLARE; |
| 388 | |||
| 389 | packet_t packet; |
||
| 390 | |||
| 4499 | mejdrech | 391 | * answer_count = 0; |
| 392 | switch( IPC_GET_METHOD( * call )){ |
||
| 393 | case NET_TL_RECEIVED: |
||
| 4700 | mejdrech | 394 | if( ! ERROR_OCCURRED( packet_translate( udp_globals.net_phone, & packet, IPC_GET_PACKET( call )))){ |
| 4707 | mejdrech | 395 | ERROR_CODE = udp_received_msg( IPC_GET_DEVICE( call ), packet, SERVICE_UDP, IPC_GET_ERROR( call )); |
| 4700 | mejdrech | 396 | } |
| 397 | return ERROR_CODE; |
||
| 4579 | mejdrech | 398 | case IPC_M_CONNECT_TO_ME: |
| 4713 | mejdrech | 399 | return udp_process_client_messages( callid, * call ); |
| 4499 | mejdrech | 400 | } |
| 401 | return ENOTSUP; |
||
| 402 | } |
||
| 403 | |||
| 4713 | mejdrech | 404 | int udp_process_client_messages( ipc_callid_t callid, ipc_call_t call ){ |
| 4579 | mejdrech | 405 | int res; |
| 406 | bool keep_on_going = true; |
||
| 407 | socket_cores_t local_sockets; |
||
| 4701 | mejdrech | 408 | int app_phone = IPC_GET_PHONE( & call ); |
| 4720 | mejdrech | 409 | struct sockaddr * addr; |
| 4579 | mejdrech | 410 | size_t addrlen; |
| 4738 | mejdrech | 411 | fibril_rwlock_t lock; |
| 4701 | mejdrech | 412 | ipc_call_t answer; |
| 413 | int answer_count; |
||
| 4579 | mejdrech | 414 | |
| 415 | /* |
||
| 416 | * Accept the connection |
||
| 417 | * - Answer the first IPC_M_CONNECT_ME_TO call. |
||
| 418 | */ |
||
| 419 | ipc_answer_0( callid, EOK ); |
||
| 420 | |||
| 4726 | mejdrech | 421 | // The client connection is only in one fibril and therefore no additional locks are needed. |
| 422 | |||
| 4579 | mejdrech | 423 | socket_cores_initialize( & local_sockets ); |
| 4738 | mejdrech | 424 | fibril_rwlock_initialize( & lock ); |
| 4579 | mejdrech | 425 | |
| 426 | while( keep_on_going ){ |
||
| 427 | // refresh data |
||
| 4713 | mejdrech | 428 | refresh_answer( & answer, & answer_count ); |
| 4579 | mejdrech | 429 | |
| 4701 | mejdrech | 430 | callid = async_get_call( & call ); |
| 4589 | mejdrech | 431 | // printf( "message %d\n", IPC_GET_METHOD( * call )); |
| 4579 | mejdrech | 432 | |
| 4701 | mejdrech | 433 | switch( IPC_GET_METHOD( call )){ |
| 4579 | mejdrech | 434 | case IPC_M_PHONE_HUNGUP: |
| 435 | keep_on_going = false; |
||
| 436 | res = EOK; |
||
| 437 | break; |
||
| 438 | case NET_SOCKET: |
||
| 4738 | mejdrech | 439 | fibril_rwlock_write_lock( & lock ); |
| 4726 | mejdrech | 440 | res = socket_create( & local_sockets, app_phone, NULL, SOCKET_SET_SOCKET_ID( answer )); |
| 4738 | mejdrech | 441 | fibril_rwlock_write_unlock( & lock ); |
| 4731 | mejdrech | 442 | // TODO max fragment size |
| 4726 | mejdrech | 443 | * SOCKET_SET_DATA_FRAGMENT_SIZE( answer ) = MAX_UDP_FRAGMENT_SIZE; |
| 4589 | mejdrech | 444 | * SOCKET_SET_HEADER_SIZE( answer ) = sizeof( udp_header_t ); |
| 4701 | mejdrech | 445 | answer_count = 3; |
| 4579 | mejdrech | 446 | break; |
| 447 | case NET_SOCKET_BIND: |
||
| 4722 | mejdrech | 448 | res = data_receive(( void ** ) & addr, & addrlen ); |
| 4713 | mejdrech | 449 | if( res == EOK ){ |
| 4738 | mejdrech | 450 | fibril_rwlock_read_lock( & lock ); |
| 4713 | mejdrech | 451 | fibril_rwlock_write_lock( & udp_globals.lock ); |
| 452 | res = socket_bind( & local_sockets, & udp_globals.sockets, SOCKET_GET_SOCKET_ID( call ), addr, addrlen, UDP_FREE_PORTS_START, UDP_FREE_PORTS_END, udp_globals.last_used_port ); |
||
| 453 | fibril_rwlock_write_unlock( & udp_globals.lock ); |
||
| 4738 | mejdrech | 454 | fibril_rwlock_read_unlock( & lock ); |
| 4713 | mejdrech | 455 | free( addr ); |
| 4579 | mejdrech | 456 | } |
| 4713 | mejdrech | 457 | break; |
| 4579 | mejdrech | 458 | case NET_SOCKET_SENDTO: |
| 4722 | mejdrech | 459 | res = data_receive(( void ** ) & addr, & addrlen ); |
| 4713 | mejdrech | 460 | if( res == EOK ){ |
| 4738 | mejdrech | 461 | fibril_rwlock_read_lock( & lock ); |
| 462 | fibril_rwlock_write_lock( & udp_globals.lock ); |
||
| 4726 | mejdrech | 463 | res = udp_sendto_message( & local_sockets, SOCKET_GET_SOCKET_ID( call ), addr, addrlen, SOCKET_GET_DATA_FRAGMENTS( call ), SOCKET_GET_DATA_FRAGMENT_SIZE( call ), SOCKET_GET_FLAGS( call )); |
| 4738 | mejdrech | 464 | fibril_rwlock_write_unlock( & udp_globals.lock ); |
| 465 | fibril_rwlock_read_unlock( & lock ); |
||
| 4713 | mejdrech | 466 | free( addr ); |
| 4579 | mejdrech | 467 | } |
| 468 | break; |
||
| 469 | case NET_SOCKET_RECVFROM: |
||
| 4738 | mejdrech | 470 | fibril_rwlock_read_lock( & lock ); |
| 471 | fibril_rwlock_write_lock( & udp_globals.lock ); |
||
| 4720 | mejdrech | 472 | res = udp_recvfrom_message( & local_sockets, SOCKET_GET_SOCKET_ID( call ), SOCKET_GET_FLAGS( call ), & addrlen ); |
| 4738 | mejdrech | 473 | fibril_rwlock_write_unlock( & udp_globals.lock ); |
| 474 | fibril_rwlock_read_unlock( & lock ); |
||
| 4589 | mejdrech | 475 | if( res > 0 ){ |
| 476 | * SOCKET_SET_READ_DATA_LENGTH( answer ) = res; |
||
| 4720 | mejdrech | 477 | * SOCKET_SET_ADDRESS_LENGTH( answer ) = addrlen; |
| 4701 | mejdrech | 478 | answer_count = 2; |
| 4589 | mejdrech | 479 | res = EOK; |
| 480 | } |
||
| 4579 | mejdrech | 481 | break; |
| 482 | case NET_SOCKET_CLOSE: |
||
| 4738 | mejdrech | 483 | fibril_rwlock_write_lock( & lock ); |
| 4700 | mejdrech | 484 | fibril_rwlock_write_lock( & udp_globals.lock ); |
| 4738 | mejdrech | 485 | res = socket_destroy( udp_globals.net_phone, SOCKET_GET_SOCKET_ID( call ), & local_sockets, & udp_globals.sockets, NULL ); |
| 4700 | mejdrech | 486 | fibril_rwlock_write_unlock( & udp_globals.lock ); |
| 4738 | mejdrech | 487 | fibril_rwlock_write_unlock( & lock ); |
| 4579 | mejdrech | 488 | break; |
| 489 | case NET_SOCKET_GETSOCKOPT: |
||
| 490 | case NET_SOCKET_SETSOCKOPT: |
||
| 491 | default: |
||
| 492 | res = ENOTSUP; |
||
| 493 | break; |
||
| 494 | } |
||
| 495 | |||
| 4589 | mejdrech | 496 | // printf( "res = %d\n", res ); |
| 4579 | mejdrech | 497 | |
| 4713 | mejdrech | 498 | answer_call( callid, res, & answer, answer_count ); |
| 4579 | mejdrech | 499 | } |
| 500 | |||
| 4738 | mejdrech | 501 | // release all local sockets |
| 502 | socket_cores_release( udp_globals.net_phone, & local_sockets, & udp_globals.sockets, NULL ); |
||
| 4579 | mejdrech | 503 | |
| 504 | return EOK; |
||
| 505 | } |
||
| 506 | |||
| 4726 | mejdrech | 507 | int udp_sendto_message( socket_cores_ref local_sockets, int socket_id, const struct sockaddr * addr, socklen_t addrlen, int fragments, size_t data_fragment_size, int flags ){ |
| 4579 | mejdrech | 508 | ERROR_DECLARE; |
| 509 | |||
| 510 | socket_core_ref socket; |
||
| 511 | packet_t packet; |
||
| 4589 | mejdrech | 512 | packet_t next_packet; |
| 4579 | mejdrech | 513 | udp_header_ref header; |
| 4589 | mejdrech | 514 | int index; |
| 4708 | mejdrech | 515 | size_t total_length; |
| 516 | int result; |
||
| 4720 | mejdrech | 517 | uint16_t dest_port; |
| 4722 | mejdrech | 518 | uint32_t checksum; |
| 519 | ip_pseudo_header_ref ip_header; |
||
| 520 | size_t headerlen; |
||
| 521 | device_id_t device_id; |
||
| 4579 | mejdrech | 522 | |
| 4726 | mejdrech | 523 | ERROR_PROPAGATE( tl_get_address_port( addr, addrlen, & dest_port )); |
| 4589 | mejdrech | 524 | |
| 4720 | mejdrech | 525 | socket = socket_cores_find( local_sockets, socket_id ); |
| 526 | if( ! socket ) return ENOTSOCK; |
||
| 4589 | mejdrech | 527 | |
| 4730 | mejdrech | 528 | if(( socket->port <= 0 ) && udp_globals.autobinding ){ |
| 529 | // bind the socket to a random free port if not bound |
||
| 530 | do{ |
||
| 531 | // try to find a free port |
||
| 4738 | mejdrech | 532 | // fibril_rwlock_read_unlock( & udp_globals.lock ); |
| 533 | // fibril_rwlock_write_lock( & udp_globals.lock ); |
||
| 4730 | mejdrech | 534 | // might be changed in the meantime |
| 535 | if( socket->port <= 0 ){ |
||
| 4738 | mejdrech | 536 | if( ERROR_OCCURRED( socket_bind_free_port( & udp_globals.sockets, socket, UDP_FREE_PORTS_START, UDP_FREE_PORTS_END, udp_globals.last_used_port ))){ |
| 537 | fibril_rwlock_write_unlock( & udp_globals.lock ); |
||
| 538 | fibril_rwlock_read_lock( & udp_globals.lock ); |
||
| 539 | return ERROR_CODE; |
||
| 540 | } |
||
| 4730 | mejdrech | 541 | // set the next port as the search starting port number |
| 542 | udp_globals.last_used_port = socket->port; |
||
| 543 | } |
||
| 4738 | mejdrech | 544 | // fibril_rwlock_write_unlock( & udp_globals.lock ); |
| 545 | // fibril_rwlock_read_lock( & udp_globals.lock ); |
||
| 4730 | mejdrech | 546 | // might be changed in the meantime |
| 547 | }while( socket->port <= 0 ); |
||
| 4579 | mejdrech | 548 | } |
| 4720 | mejdrech | 549 | |
| 550 | // TODO do not ask all the time |
||
| 4726 | mejdrech | 551 | ERROR_PROPAGATE( ip_packet_size_req( udp_globals.ip_phone, -1, & udp_globals.packet_dimension.addr_len, & udp_globals.packet_dimension.prefix, & udp_globals.packet_dimension.content, & udp_globals.packet_dimension.suffix )); |
| 4720 | mejdrech | 552 | |
| 553 | // read the first packet fragment |
||
| 4726 | mejdrech | 554 | result = tl_socket_read_packet_data( udp_globals.net_phone, & packet, sizeof( udp_header_t ), & udp_globals.packet_dimension, addr, addrlen ); |
| 4720 | mejdrech | 555 | if( result < 0 ) return result; |
| 556 | total_length = ( size_t ) result; |
||
| 4722 | mejdrech | 557 | if( udp_globals.checksum_computing ){ |
| 558 | checksum = compute_checksum( 0, packet_get_data( packet ), packet_get_data_length( packet )); |
||
| 559 | }else{ |
||
| 560 | checksum = 0; |
||
| 561 | } |
||
| 4720 | mejdrech | 562 | // prefix the udp header |
| 563 | header = PACKET_PREFIX( packet, udp_header_t ); |
||
| 564 | if( ! header ){ |
||
| 4722 | mejdrech | 565 | return udp_release_and_return( packet, ENOMEM ); |
| 4720 | mejdrech | 566 | } |
| 4722 | mejdrech | 567 | bzero( header, sizeof( * header )); |
| 4720 | mejdrech | 568 | // read the rest of the packet fragments |
| 569 | for( index = 1; index < fragments; ++ index ){ |
||
| 4726 | mejdrech | 570 | result = tl_socket_read_packet_data( udp_globals.net_phone, & next_packet, 0, & udp_globals.packet_dimension, addr, addrlen ); |
| 4720 | mejdrech | 571 | if( result < 0 ){ |
| 572 | return udp_release_and_return( packet, result ); |
||
| 573 | } |
||
| 574 | packet = pq_add( packet, next_packet, index, 0 ); |
||
| 575 | total_length += ( size_t ) result; |
||
| 4722 | mejdrech | 576 | if( udp_globals.checksum_computing ){ |
| 577 | checksum = compute_checksum( checksum, packet_get_data( next_packet ), packet_get_data_length( next_packet )); |
||
| 578 | } |
||
| 4720 | mejdrech | 579 | } |
| 580 | // set the udp header |
||
| 4730 | mejdrech | 581 | header->source_port = htons(( socket->port > 0 ) ? socket->port : 0 ); |
| 4728 | mejdrech | 582 | header->destination_port = htons( dest_port ); |
| 4730 | mejdrech | 583 | header->total_length = htons( total_length + sizeof( * header )); |
| 4728 | mejdrech | 584 | header->checksum = 0; |
| 4722 | mejdrech | 585 | if( udp_globals.checksum_computing ){ |
| 586 | if( ERROR_OCCURRED( ip_get_route_req( udp_globals.ip_phone, IPPROTO_UDP, addr, addrlen, & device_id, & ip_header, & headerlen ))){ |
||
| 587 | return udp_release_and_return( packet, ERROR_CODE ); |
||
| 588 | } |
||
| 589 | if( ERROR_OCCURRED( ip_client_set_pseudo_header_data_length( ip_header, headerlen, total_length + sizeof( udp_header_t )))){ |
||
| 590 | free( ip_header ); |
||
| 591 | return udp_release_and_return( packet, ERROR_CODE ); |
||
| 592 | } |
||
| 4731 | mejdrech | 593 | checksum = compute_checksum( checksum, ip_header, headerlen ); |
| 4722 | mejdrech | 594 | checksum = compute_checksum( checksum, ( uint8_t * ) header, sizeof( * header )); |
| 4728 | mejdrech | 595 | header->checksum = htons( flip_checksum( compact_checksum( checksum ))); |
| 4722 | mejdrech | 596 | free( ip_header ); |
| 597 | }else{ |
||
| 4726 | mejdrech | 598 | device_id = -1; |
| 4722 | mejdrech | 599 | } |
| 4720 | mejdrech | 600 | // prepare the first packet fragment |
| 601 | if( ERROR_OCCURRED( ip_client_prepare_packet( packet, IPPROTO_UDP, 0, 0, 0, 0 ))){ |
||
| 4722 | mejdrech | 602 | return udp_release_and_return( packet, ERROR_CODE ); |
| 4720 | mejdrech | 603 | } |
| 604 | // send the packet |
||
| 4722 | mejdrech | 605 | return ip_send_msg( udp_globals.ip_phone, device_id, packet, SERVICE_UDP, 0 ); |
| 4579 | mejdrech | 606 | } |
| 607 | |||
| 4720 | mejdrech | 608 | int udp_recvfrom_message( socket_cores_ref local_sockets, int socket_id, int flags, size_t * addrlen ){ |
| 4579 | mejdrech | 609 | ERROR_DECLARE; |
| 610 | |||
| 4589 | mejdrech | 611 | socket_core_ref socket; |
| 612 | int packet_id; |
||
| 613 | packet_t packet; |
||
| 614 | udp_header_ref header; |
||
| 4720 | mejdrech | 615 | struct sockaddr * addr; |
| 4708 | mejdrech | 616 | size_t length; |
| 617 | uint8_t * data; |
||
| 618 | int result; |
||
| 4589 | mejdrech | 619 | |
| 620 | // find the socket |
||
| 621 | socket = socket_cores_find( local_sockets, socket_id ); |
||
| 622 | if( ! socket ) return ENOTSOCK; |
||
| 623 | // get the next received packet |
||
| 624 | packet_id = dyn_fifo_value( & socket->received ); |
||
| 4701 | mejdrech | 625 | if( packet_id < 0 ) return NO_DATA; |
| 4589 | mejdrech | 626 | ERROR_PROPAGATE( packet_translate( udp_globals.net_phone, & packet, packet_id )); |
| 627 | // get udp header |
||
| 628 | data = packet_get_data( packet ); |
||
| 629 | if( ! data ){ |
||
| 630 | pq_release( udp_globals.net_phone, packet_id ); |
||
| 631 | return NO_DATA; |
||
| 632 | } |
||
| 633 | header = ( udp_header_ref ) data; |
||
| 4720 | mejdrech | 634 | |
| 635 | // set the source address port |
||
| 636 | result = packet_get_addr( packet, ( uint8_t ** ) & addr, NULL ); |
||
| 4728 | mejdrech | 637 | if( ERROR_OCCURRED( tl_set_address_port( addr, result, ntohs( header->source_port )))){ |
| 4589 | mejdrech | 638 | pq_release( udp_globals.net_phone, packet_id ); |
| 4720 | mejdrech | 639 | return ERROR_CODE; |
| 4589 | mejdrech | 640 | } |
| 4720 | mejdrech | 641 | * addrlen = ( size_t ) result; |
| 4589 | mejdrech | 642 | // send the source address |
| 4722 | mejdrech | 643 | ERROR_PROPAGATE( data_reply( addr, * addrlen )); |
| 4720 | mejdrech | 644 | |
| 4738 | mejdrech | 645 | // trim the header |
| 646 | ERROR_PROPAGATE( packet_trim( packet, sizeof( udp_header_t ), 0 )); |
||
| 647 | |||
| 648 | // reply the packets |
||
| 649 | ERROR_PROPAGATE( socket_reply_packets( packet, & length )); |
||
| 650 | |||
| 4589 | mejdrech | 651 | // release the packet |
| 652 | dyn_fifo_pop( & socket->received ); |
||
| 653 | pq_release( udp_globals.net_phone, packet_get_id( packet )); |
||
| 654 | // return the total length |
||
| 4708 | mejdrech | 655 | return ( int ) length; |
| 4589 | mejdrech | 656 | } |
| 657 | |||
| 4713 | mejdrech | 658 | int udp_release_and_return( packet_t packet, int result ){ |
| 4707 | mejdrech | 659 | pq_release( udp_globals.net_phone, packet_get_id( packet )); |
| 660 | return result; |
||
| 661 | } |
||
| 662 | |||
| 4499 | mejdrech | 663 | /** @} |
| 664 | */ |