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