source: mainline/uspace/srv/net/tl/udp/udp.c@ a347a11

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since a347a11 was 6b82009, checked in by Martin Decky <martin@…>, 15 years ago

networking stack: convert to the new async framework

  • Property mode set to 100644
File size: 25.3 KB
RevLine 
[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
[457a6f5]30 * @{
[21580dd]31 */
32
33/** @file
[457a6f5]34 * UDP module implementation.
35 * @see udp.h
[21580dd]36 */
37
38#include <async.h>
39#include <fibril_synch.h>
40#include <malloc.h>
41#include <stdio.h>
42#include <ipc/services.h>
[514ee46]43#include <ipc/net.h>
[8e3a65c]44#include <ipc/tl.h>
[88e127ee]45#include <ipc/socket.h>
[457a6f5]46#include <adt/dynamic_fifo.h>
[e98b1d5]47#include <errno.h>
[21580dd]48
[058edb6]49#include <net/socket_codes.h>
[fe5d3c1b]50#include <net/ip_protocols.h>
[e4554d4]51#include <net/in.h>
52#include <net/in6.h>
53#include <net/inet.h>
[c7a8442]54#include <net/modules.h>
[058edb6]55
[0a866eeb]56#include <packet_client.h>
[14f1db0]57#include <packet_remote.h>
[849ed54]58#include <net_checksum.h>
59#include <ip_client.h>
60#include <ip_interface.h>
61#include <icmp_client.h>
[f1938c6]62#include <icmp_remote.h>
[849ed54]63#include <net_interface.h>
64#include <socket_core.h>
65#include <tl_common.h>
[014dd57b]66#include <tl_remote.h>
67#include <tl_skel.h>
68
69#include "udp.h"
70#include "udp_header.h"
[21580dd]71
[7282582]72/** UDP module name. */
[014dd57b]73#define NAME "udp"
[849ed54]74
[7282582]75/** Default UDP checksum computing. */
[21580dd]76#define NET_DEFAULT_UDP_CHECKSUM_COMPUTING true
77
[7282582]78/** Default UDP autobind when sending via unbound sockets. */
[21580dd]79#define NET_DEFAULT_UDP_AUTOBINDING true
80
[7282582]81/** Maximum UDP fragment size. */
82#define MAX_UDP_FRAGMENT_SIZE 65535
[21580dd]83
[7282582]84/** Free ports pool start. */
85#define UDP_FREE_PORTS_START 1025
[21580dd]86
[7282582]87/** Free ports pool end. */
[21580dd]88#define UDP_FREE_PORTS_END 65535
89
[457a6f5]90/** UDP global data. */
91udp_globals_t udp_globals;
[21580dd]92
[457a6f5]93/** Releases the packet and returns the result.
94 *
95 * @param[in] packet The packet queue to be released.
96 * @param[in] result The result to be returned.
97 * @return The result parameter.
98 */
[46d4d9f]99static int udp_release_and_return(packet_t *packet, int result)
[7282582]100{
[6b82009]101 pq_release_remote(udp_globals.net_sess, packet_get_id(packet));
[21580dd]102 return result;
103}
104
[457a6f5]105/** Processes the received UDP packet queue.
106 *
107 * Notifies the destination socket application.
108 * Releases the packet on error or sends an ICMP error notification.
109 *
110 * @param[in] device_id The receiving device identifier.
111 * @param[in,out] packet The received packet queue.
112 * @param[in] error The packet error reporting service. Prefixes the
113 * received packet.
[1bfd3d3]114 * @return EOK on success.
115 * @return EINVAL if the packet is not valid.
116 * @return EINVAL if the stored packet address is not the
[457a6f5]117 * an_addr_t.
[1bfd3d3]118 * @return EINVAL if the packet does not contain any data.
119 * @return NO_DATA if the packet content is shorter than the user
[457a6f5]120 * datagram header.
[1bfd3d3]121 * @return ENOMEM if there is not enough memory left.
122 * @return EADDRNOTAVAIL if the destination socket does not exist.
123 * @return Other error codes as defined for the
[457a6f5]124 * ip_client_process_packet() function.
125 */
[46d4d9f]126static int udp_process_packet(device_id_t device_id, packet_t *packet,
[fb04cba8]127 services_t error)
[7282582]128{
[aadf01e]129 size_t length;
130 size_t offset;
131 int result;
[4e5c7ba]132 udp_header_t *header;
[88a1bb9]133 socket_core_t *socket;
[46d4d9f]134 packet_t *next_packet;
[aadf01e]135 size_t total_length;
136 uint32_t checksum;
137 int fragments;
[46d4d9f]138 packet_t *tmp_packet;
[aadf01e]139 icmp_type_t type;
140 icmp_code_t code;
[14f1db0]141 void *ip_header;
[7282582]142 struct sockaddr *src;
143 struct sockaddr *dest;
[f772bc55]144 packet_dimension_t *packet_dimension;
[46ae62c]145 int rc;
[aadf01e]146
[457a6f5]147 switch (error) {
148 case SERVICE_NONE:
149 break;
150 case SERVICE_ICMP:
[fb04cba8]151 /* Ignore error */
[457a6f5]152 // length = icmp_client_header_length(packet);
[fb04cba8]153
154 /* Process error */
[457a6f5]155 result = icmp_client_process_packet(packet, &type,
156 &code, NULL, NULL);
157 if (result < 0)
158 return udp_release_and_return(packet, result);
159 length = (size_t) result;
[46ae62c]160 rc = packet_trim(packet, length, 0);
161 if (rc != EOK)
162 return udp_release_and_return(packet, rc);
[457a6f5]163 break;
164 default:
165 return udp_release_and_return(packet, ENOTSUP);
[21580dd]166 }
[7282582]167
[fb04cba8]168 /* TODO process received ipopts? */
[aadf01e]169 result = ip_client_process_packet(packet, NULL, NULL, NULL, NULL, NULL);
[7282582]170 if (result < 0)
[aadf01e]171 return udp_release_and_return(packet, result);
172 offset = (size_t) result;
[21580dd]173
[aadf01e]174 length = packet_get_data_length(packet);
[7282582]175 if (length <= 0)
[aadf01e]176 return udp_release_and_return(packet, EINVAL);
[7282582]177 if (length < UDP_HEADER_SIZE + offset)
[aadf01e]178 return udp_release_and_return(packet, NO_DATA);
[21580dd]179
[fb04cba8]180 /* Trim all but UDP header */
[46ae62c]181 rc = packet_trim(packet, offset, 0);
182 if (rc != EOK)
183 return udp_release_and_return(packet, rc);
[21580dd]184
[fb04cba8]185 /* Get UDP header */
[4e5c7ba]186 header = (udp_header_t *) packet_get_data(packet);
[7282582]187 if (!header)
[aadf01e]188 return udp_release_and_return(packet, NO_DATA);
[7282582]189
[fb04cba8]190 /* Find the destination socket */
[7282582]191 socket = socket_port_find(&udp_globals.sockets,
[61bfc370]192 ntohs(header->destination_port), (uint8_t *) SOCKET_MAP_KEY_LISTENING, 0);
[7282582]193 if (!socket) {
[6b82009]194 if (tl_prepare_icmp_packet(udp_globals.net_sess,
195 udp_globals.icmp_sess, packet, error) == EOK) {
196 icmp_destination_unreachable_msg(udp_globals.icmp_sess,
[7282582]197 ICMP_PORT_UNREACH, 0, packet);
[21580dd]198 }
199 return EADDRNOTAVAIL;
200 }
201
[fb04cba8]202 /* Count the received packet fragments */
[21580dd]203 next_packet = packet;
204 fragments = 0;
[aadf01e]205 total_length = ntohs(header->total_length);
[7282582]206
[fb04cba8]207 /* Compute header checksum if set */
[457a6f5]208 if (header->checksum && !error) {
[7282582]209 result = packet_get_addr(packet, (uint8_t **) &src,
210 (uint8_t **) &dest);
[457a6f5]211 if (result <= 0)
[aadf01e]212 return udp_release_and_return(packet, result);
[46ae62c]213
214 rc = ip_client_get_pseudo_header(IPPROTO_UDP, src, result, dest,
215 result, total_length, &ip_header, &length);
216 if (rc != EOK) {
217 return udp_release_and_return(packet, rc);
[7282582]218 } else {
[aadf01e]219 checksum = compute_checksum(0, ip_header, length);
[fb04cba8]220 /*
221 * The udp header checksum will be added with the first
222 * fragment later.
223 */
[aadf01e]224 free(ip_header);
[21580dd]225 }
[7282582]226 } else {
[21580dd]227 header->checksum = 0;
228 checksum = 0;
229 }
230
[7282582]231 do {
[457a6f5]232 fragments++;
[aadf01e]233 length = packet_get_data_length(next_packet);
[7282582]234 if (length <= 0)
[aadf01e]235 return udp_release_and_return(packet, NO_DATA);
[7282582]236
237 if (total_length < length) {
[46ae62c]238 rc = packet_trim(next_packet, 0, length - total_length);
239 if (rc != EOK)
240 return udp_release_and_return(packet, rc);
[7282582]241
[fb04cba8]242 /* Add partial checksum if set */
[7282582]243 if (header->checksum) {
244 checksum = compute_checksum(checksum,
245 packet_get_data(packet),
246 packet_get_data_length(packet));
[21580dd]247 }
[7282582]248
[fb04cba8]249 /* Relese the rest of the packet fragments */
[aadf01e]250 tmp_packet = pq_next(next_packet);
[7282582]251 while (tmp_packet) {
[aadf01e]252 next_packet = pq_detach(tmp_packet);
[6b82009]253 pq_release_remote(udp_globals.net_sess,
[7282582]254 packet_get_id(tmp_packet));
[21580dd]255 tmp_packet = next_packet;
256 }
[7282582]257
[fb04cba8]258 /* Exit the loop */
[21580dd]259 break;
260 }
261 total_length -= length;
[7282582]262
[fb04cba8]263 /* Add partial checksum if set */
[7282582]264 if (header->checksum) {
265 checksum = compute_checksum(checksum,
266 packet_get_data(packet),
267 packet_get_data_length(packet));
[21580dd]268 }
[7282582]269
270 } while ((next_packet = pq_next(next_packet)) && (total_length > 0));
[21580dd]271
[fb04cba8]272 /* Verify checksum */
[7282582]273 if (header->checksum) {
274 if (flip_checksum(compact_checksum(checksum)) !=
275 IP_CHECKSUM_ZERO) {
[6b82009]276 if (tl_prepare_icmp_packet(udp_globals.net_sess,
277 udp_globals.icmp_sess, packet, error) == EOK) {
[fb04cba8]278 /* Checksum error ICMP */
[7282582]279 icmp_parameter_problem_msg(
[6b82009]280 udp_globals.icmp_sess, ICMP_PARAM_POINTER,
[7282582]281 ((size_t) ((void *) &header->checksum)) -
282 ((size_t) ((void *) header)), packet);
[21580dd]283 }
284 return EINVAL;
285 }
286 }
287
[fb04cba8]288 /* Queue the received packet */
[46ae62c]289 rc = dyn_fifo_push(&socket->received, packet_get_id(packet),
290 SOCKET_MAX_RECEIVED_SIZE);
291 if (rc != EOK)
292 return udp_release_and_return(packet, rc);
293
[6b82009]294 rc = tl_get_ip_packet_dimension(udp_globals.ip_sess,
[46ae62c]295 &udp_globals.dimensions, device_id, &packet_dimension);
296 if (rc != EOK)
297 return udp_release_and_return(packet, rc);
[21580dd]298
[fb04cba8]299 /* Notify the destination socket */
[aadf01e]300 fibril_rwlock_write_unlock(&udp_globals.lock);
[6b82009]301
302 async_exch_t *exch = async_exchange_begin(socket->sess);
303 async_msg_5(exch, NET_SOCKET_RECEIVED, (sysarg_t) socket->socket_id,
304 packet_dimension->content, 0, 0, (sysarg_t) fragments);
305 async_exchange_end(exch);
[7282582]306
[2e99277]307 return EOK;
[21580dd]308}
309
[457a6f5]310/** Processes the received UDP packet queue.
311 *
312 * Is used as an entry point from the underlying IP module.
313 * Locks the global lock and calls udp_process_packet() function.
314 *
315 * @param[in] device_id The receiving device identifier.
316 * @param[in,out] packet The received packet queue.
317 * @param receiver The target service. Ignored parameter.
318 * @param[in] error The packet error reporting service. Prefixes the
319 * received packet.
[1bfd3d3]320 * @return EOK on success.
321 * @return Other error codes as defined for the
[457a6f5]322 * udp_process_packet() function.
323 */
[46d4d9f]324static int udp_received_msg(device_id_t device_id, packet_t *packet,
[fb04cba8]325 services_t receiver, services_t error)
[7282582]326{
[457a6f5]327 int result;
[a8a13d0]328
[457a6f5]329 fibril_rwlock_write_lock(&udp_globals.lock);
330 result = udp_process_packet(device_id, packet, error);
331 if (result != EOK)
332 fibril_rwlock_write_unlock(&udp_globals.lock);
[21580dd]333
[457a6f5]334 return result;
[21580dd]335}
336
[014dd57b]337/** Process IPC messages from the IP module
338 *
339 * @param[in] iid Message identifier.
340 * @param[in,out] icall Message parameters.
[9934f7d]341 * @param[in] arg Local argument.
[6b82009]342 *
[014dd57b]343 */
[9934f7d]344static void udp_receiver(ipc_callid_t iid, ipc_call_t *icall, void *arg)
[014dd57b]345{
346 packet_t *packet;
347 int rc;
348
349 while (true) {
350 switch (IPC_GET_IMETHOD(*icall)) {
351 case NET_TL_RECEIVED:
[6b82009]352 rc = packet_translate_remote(udp_globals.net_sess, &packet,
[014dd57b]353 IPC_GET_PACKET(*icall));
354 if (rc == EOK)
355 rc = udp_received_msg(IPC_GET_DEVICE(*icall), packet,
356 SERVICE_UDP, IPC_GET_ERROR(*icall));
357
[ffa2c8ef]358 async_answer_0(iid, (sysarg_t) rc);
[014dd57b]359 break;
360 default:
[ffa2c8ef]361 async_answer_0(iid, (sysarg_t) ENOTSUP);
[014dd57b]362 }
363
364 iid = async_get_call(icall);
365 }
366}
367
368/** Initialize the UDP module.
369 *
[6b82009]370 * @param[in] sess Network module session.
[014dd57b]371 *
372 * @return EOK on success.
373 * @return ENOMEM if there is not enough memory left.
374 *
375 */
[6b82009]376int tl_initialize(async_sess_t *sess)
[014dd57b]377{
378 measured_string_t names[] = {
379 {
380 (uint8_t *) "UDP_CHECKSUM_COMPUTING",
381 22
382 },
383 {
384 (uint8_t *) "UDP_AUTOBINDING",
385 15
386 }
387 };
388 measured_string_t *configuration;
389 size_t count = sizeof(names) / sizeof(measured_string_t);
390 uint8_t *data;
391
392 fibril_rwlock_initialize(&udp_globals.lock);
393 fibril_rwlock_write_lock(&udp_globals.lock);
394
[6b82009]395 udp_globals.net_sess = sess;
396 udp_globals.icmp_sess = icmp_connect_module();
[014dd57b]397
[6b82009]398 udp_globals.ip_sess = ip_bind_service(SERVICE_IP, IPPROTO_UDP,
399 SERVICE_UDP, udp_receiver);
400 if (udp_globals.ip_sess == NULL) {
401 fibril_rwlock_write_unlock(&udp_globals.lock);
402 return ENOENT;
[014dd57b]403 }
404
405 /* Read default packet dimensions */
[6b82009]406 int rc = ip_packet_size_req(udp_globals.ip_sess, -1,
[014dd57b]407 &udp_globals.packet_dimension);
408 if (rc != EOK) {
409 fibril_rwlock_write_unlock(&udp_globals.lock);
410 return rc;
411 }
412
413 rc = socket_ports_initialize(&udp_globals.sockets);
414 if (rc != EOK) {
415 fibril_rwlock_write_unlock(&udp_globals.lock);
416 return rc;
417 }
418
419 rc = packet_dimensions_initialize(&udp_globals.dimensions);
420 if (rc != EOK) {
[5fe7692]421 socket_ports_destroy(&udp_globals.sockets, free);
[014dd57b]422 fibril_rwlock_write_unlock(&udp_globals.lock);
423 return rc;
424 }
425
426 udp_globals.packet_dimension.prefix += sizeof(udp_header_t);
427 udp_globals.packet_dimension.content -= sizeof(udp_header_t);
428 udp_globals.last_used_port = UDP_FREE_PORTS_START - 1;
429
430 udp_globals.checksum_computing = NET_DEFAULT_UDP_CHECKSUM_COMPUTING;
431 udp_globals.autobinding = NET_DEFAULT_UDP_AUTOBINDING;
432
433 /* Get configuration */
434 configuration = &names[0];
[6b82009]435 rc = net_get_conf_req(udp_globals.net_sess, &configuration, count,
[014dd57b]436 &data);
437 if (rc != EOK) {
[5fe7692]438 socket_ports_destroy(&udp_globals.sockets, free);
[014dd57b]439 fibril_rwlock_write_unlock(&udp_globals.lock);
440 return rc;
441 }
442
443 if (configuration) {
444 if (configuration[0].value)
445 udp_globals.checksum_computing =
446 (configuration[0].value[0] == 'y');
447
448 if (configuration[1].value)
449 udp_globals.autobinding =
450 (configuration[1].value[0] == 'y');
451
452 net_free_settings(configuration, data);
453 }
454
455 fibril_rwlock_write_unlock(&udp_globals.lock);
456 return EOK;
457}
458
[457a6f5]459/** Sends data from the socket to the remote address.
460 *
461 * Binds the socket to a free port if not already connected/bound.
462 * Handles the NET_SOCKET_SENDTO message.
463 * Supports AF_INET and AF_INET6 address families.
464 *
465 * @param[in,out] local_sockets The application local sockets.
466 * @param[in] socket_id Socket identifier.
467 * @param[in] addr The destination address.
468 * @param[in] addrlen The address length.
469 * @param[in] fragments The number of data fragments.
470 * @param[out] data_fragment_size The data fragment size in bytes.
471 * @param[in] flags Various send flags.
[1bfd3d3]472 * @return EOK on success.
473 * @return EAFNOTSUPPORT if the address family is not supported.
474 * @return ENOTSOCK if the socket is not found.
475 * @return EINVAL if the address is invalid.
476 * @return ENOTCONN if the sending socket is not and cannot be
[457a6f5]477 * bound.
[1bfd3d3]478 * @return ENOMEM if there is not enough memory left.
479 * @return Other error codes as defined for the
[457a6f5]480 * socket_read_packet_data() function.
[1bfd3d3]481 * @return Other error codes as defined for the
[457a6f5]482 * ip_client_prepare_packet() function.
[1bfd3d3]483 * @return Other error codes as defined for the ip_send_msg()
[457a6f5]484 * function.
485 */
[aaa3f33a]486static int udp_sendto_message(socket_cores_t *local_sockets, int socket_id,
[7282582]487 const struct sockaddr *addr, socklen_t addrlen, int fragments,
488 size_t *data_fragment_size, int flags)
489{
[88a1bb9]490 socket_core_t *socket;
[46d4d9f]491 packet_t *packet;
492 packet_t *next_packet;
[4e5c7ba]493 udp_header_t *header;
[aadf01e]494 int index;
495 size_t total_length;
496 int result;
497 uint16_t dest_port;
498 uint32_t checksum;
[14f1db0]499 void *ip_header;
[aadf01e]500 size_t headerlen;
501 device_id_t device_id;
[f772bc55]502 packet_dimension_t *packet_dimension;
[348c589]503 size_t size;
[46ae62c]504 int rc;
[348c589]505
506 /* In case of error, do not update the data fragment size. */
507 *data_fragment_size = 0;
[46ae62c]508
509 rc = tl_get_address_port(addr, addrlen, &dest_port);
510 if (rc != EOK)
511 return rc;
[aadf01e]512
513 socket = socket_cores_find(local_sockets, socket_id);
[7282582]514 if (!socket)
[aadf01e]515 return ENOTSOCK;
516
[7282582]517 if ((socket->port <= 0) && udp_globals.autobinding) {
[fb04cba8]518 /* Bind the socket to a random free port if not bound */
[a873201]519 rc = socket_bind_free_port(&udp_globals.sockets, socket,
520 UDP_FREE_PORTS_START, UDP_FREE_PORTS_END,
521 udp_globals.last_used_port);
522 if (rc != EOK)
523 return rc;
[fb04cba8]524 /* Set the next port as the search starting port number */
[a873201]525 udp_globals.last_used_port = socket->port;
[21580dd]526 }
527
[7282582]528 if (udp_globals.checksum_computing) {
[6b82009]529 rc = ip_get_route_req(udp_globals.ip_sess, IPPROTO_UDP, addr,
[46ae62c]530 addrlen, &device_id, &ip_header, &headerlen);
531 if (rc != EOK)
[d94f309]532 return rc;
[fb04cba8]533 /* Get the device packet dimension */
[6b82009]534// rc = tl_get_ip_packet_dimension(udp_globals.ip_sess,
[46ae62c]535// &udp_globals.dimensions, device_id, &packet_dimension);
536// if (rc != EOK)
537// return rc;
[21580dd]538 }
[7282582]539// } else {
[fb04cba8]540 /* Do not ask all the time */
[6b82009]541 rc = ip_packet_size_req(udp_globals.ip_sess, -1,
[46ae62c]542 &udp_globals.packet_dimension);
543 if (rc != EOK)
544 return rc;
[aadf01e]545 packet_dimension = &udp_globals.packet_dimension;
[21580dd]546// }
547
[348c589]548 /*
549 * Update the data fragment size based on what the lower layers can
550 * handle without fragmentation, but not more than the maximum allowed
551 * for UDP.
552 */
553 size = MAX_UDP_FRAGMENT_SIZE;
554 if (packet_dimension->content < size)
555 size = packet_dimension->content;
556 *data_fragment_size = size;
557
[fb04cba8]558 /* Read the first packet fragment */
[6b82009]559 result = tl_socket_read_packet_data(udp_globals.net_sess, &packet,
[7282582]560 UDP_HEADER_SIZE, packet_dimension, addr, addrlen);
561 if (result < 0)
[aadf01e]562 return result;
[7282582]563
[aadf01e]564 total_length = (size_t) result;
[7282582]565 if (udp_globals.checksum_computing)
566 checksum = compute_checksum(0, packet_get_data(packet),
567 packet_get_data_length(packet));
568 else
[21580dd]569 checksum = 0;
[7282582]570
[fb04cba8]571 /* Prefix the UDP header */
[aadf01e]572 header = PACKET_PREFIX(packet, udp_header_t);
[457a6f5]573 if (!header)
[aadf01e]574 return udp_release_and_return(packet, ENOMEM);
[7282582]575
[aadf01e]576 bzero(header, sizeof(*header));
[fb04cba8]577
578 /* Read the rest of the packet fragments */
[457a6f5]579 for (index = 1; index < fragments; index++) {
[6b82009]580 result = tl_socket_read_packet_data(udp_globals.net_sess,
[7282582]581 &next_packet, 0, packet_dimension, addr, addrlen);
582 if (result < 0)
[aadf01e]583 return udp_release_and_return(packet, result);
[7282582]584
[46ae62c]585 rc = pq_add(&packet, next_packet, index, 0);
586 if (rc != EOK)
587 return udp_release_and_return(packet, rc);
[7282582]588
[aadf01e]589 total_length += (size_t) result;
[7282582]590 if (udp_globals.checksum_computing) {
591 checksum = compute_checksum(checksum,
592 packet_get_data(next_packet),
593 packet_get_data_length(next_packet));
[21580dd]594 }
595 }
[7282582]596
[fb04cba8]597 /* Set the UDP header */
[aadf01e]598 header->source_port = htons((socket->port > 0) ? socket->port : 0);
599 header->destination_port = htons(dest_port);
600 header->total_length = htons(total_length + sizeof(*header));
[21580dd]601 header->checksum = 0;
[fb04cba8]602
[7282582]603 if (udp_globals.checksum_computing) {
[fb04cba8]604 /* Update the pseudo header */
[46ae62c]605 rc = ip_client_set_pseudo_header_data_length(ip_header,
606 headerlen, total_length + UDP_HEADER_SIZE);
607 if (rc != EOK) {
[aadf01e]608 free(ip_header);
[46ae62c]609 return udp_release_and_return(packet, rc);
[21580dd]610 }
[7282582]611
[fb04cba8]612 /* Finish the checksum computation */
[aadf01e]613 checksum = compute_checksum(checksum, ip_header, headerlen);
[7282582]614 checksum = compute_checksum(checksum, (uint8_t *) header,
615 sizeof(*header));
616 header->checksum =
617 htons(flip_checksum(compact_checksum(checksum)));
[aadf01e]618 free(ip_header);
[7282582]619 } else {
[ede63e4]620 device_id = DEVICE_INVALID_ID;
[21580dd]621 }
[7282582]622
[fb04cba8]623 /* Prepare the first packet fragment */
[46ae62c]624 rc = ip_client_prepare_packet(packet, IPPROTO_UDP, 0, 0, 0, 0);
625 if (rc != EOK)
626 return udp_release_and_return(packet, rc);
[7282582]627
[f3cb50e]628 /* Release the UDP global lock on success. */
[aadf01e]629 fibril_rwlock_write_unlock(&udp_globals.lock);
[f3cb50e]630
[fb04cba8]631 /* Send the packet */
[6b82009]632 ip_send_msg(udp_globals.ip_sess, device_id, packet, SERVICE_UDP, 0);
[7282582]633
[21580dd]634 return EOK;
635}
636
[457a6f5]637/** Receives data to the socket.
638 *
639 * Handles the NET_SOCKET_RECVFROM message.
640 * Replies the source address as well.
641 *
642 * @param[in] local_sockets The application local sockets.
643 * @param[in] socket_id Socket identifier.
644 * @param[in] flags Various receive flags.
645 * @param[out] addrlen The source address length.
[1bfd3d3]646 * @return The number of bytes received.
647 * @return ENOTSOCK if the socket is not found.
648 * @return NO_DATA if there are no received packets or data.
649 * @return ENOMEM if there is not enough memory left.
650 * @return EINVAL if the received address is not an IP address.
651 * @return Other error codes as defined for the packet_translate()
[457a6f5]652 * function.
[1bfd3d3]653 * @return Other error codes as defined for the data_reply()
[457a6f5]654 * function.
655 */
[aaa3f33a]656static int udp_recvfrom_message(socket_cores_t *local_sockets, int socket_id,
[fb04cba8]657 int flags, size_t *addrlen)
[7282582]658{
[88a1bb9]659 socket_core_t *socket;
[aadf01e]660 int packet_id;
[46d4d9f]661 packet_t *packet;
[4e5c7ba]662 udp_header_t *header;
[7282582]663 struct sockaddr *addr;
[aadf01e]664 size_t length;
[7282582]665 uint8_t *data;
[aadf01e]666 int result;
[46ae62c]667 int rc;
[21580dd]668
[fb04cba8]669 /* Find the socket */
[aadf01e]670 socket = socket_cores_find(local_sockets, socket_id);
[7282582]671 if (!socket)
[aadf01e]672 return ENOTSOCK;
[7282582]673
[fb04cba8]674 /* Get the next received packet */
[08042bd]675 packet_id = dyn_fifo_value(&socket->received);
[7282582]676 if (packet_id < 0)
[aadf01e]677 return NO_DATA;
[46ae62c]678
[6b82009]679 rc = packet_translate_remote(udp_globals.net_sess, &packet, packet_id);
[08042bd]680 if (rc != EOK) {
681 (void) dyn_fifo_pop(&socket->received);
[46ae62c]682 return rc;
[08042bd]683 }
[7282582]684
[fb04cba8]685 /* Get UDP header */
[aadf01e]686 data = packet_get_data(packet);
[08042bd]687 if (!data) {
688 (void) dyn_fifo_pop(&socket->received);
[de229f8e]689 return udp_release_and_return(packet, NO_DATA);
[08042bd]690 }
[4e5c7ba]691 header = (udp_header_t *) data;
[21580dd]692
[fb04cba8]693 /* Set the source address port */
[aadf01e]694 result = packet_get_addr(packet, (uint8_t **) &addr, NULL);
[46ae62c]695 rc = tl_set_address_port(addr, result, ntohs(header->source_port));
[08042bd]696 if (rc != EOK) {
697 (void) dyn_fifo_pop(&socket->received);
[de229f8e]698 return udp_release_and_return(packet, rc);
[08042bd]699 }
[aadf01e]700 *addrlen = (size_t) result;
[7282582]701
[fb04cba8]702 /* Send the source address */
[46ae62c]703 rc = data_reply(addr, *addrlen);
[08042bd]704 switch (rc) {
705 case EOK:
706 break;
707 case EOVERFLOW:
708 return rc;
709 default:
710 (void) dyn_fifo_pop(&socket->received);
[a63ff7d]711 return udp_release_and_return(packet, rc);
[08042bd]712 }
[21580dd]713
[fb04cba8]714 /* Trim the header */
[46ae62c]715 rc = packet_trim(packet, UDP_HEADER_SIZE, 0);
[08042bd]716 if (rc != EOK) {
717 (void) dyn_fifo_pop(&socket->received);
[a63ff7d]718 return udp_release_and_return(packet, rc);
[08042bd]719 }
[21580dd]720
[fb04cba8]721 /* Reply the packets */
[46ae62c]722 rc = socket_reply_packets(packet, &length);
[08042bd]723 switch (rc) {
724 case EOK:
725 break;
726 case EOVERFLOW:
727 return rc;
728 default:
729 (void) dyn_fifo_pop(&socket->received);
[a63ff7d]730 return udp_release_and_return(packet, rc);
[08042bd]731 }
732
733 (void) dyn_fifo_pop(&socket->received);
[7282582]734
[fb04cba8]735 /* Release the packet and return the total length */
[de229f8e]736 return udp_release_and_return(packet, (int) length);
[21580dd]737}
738
[6b82009]739/** Process the socket client messages.
[457a6f5]740 *
[6b82009]741 * Run until the client module disconnects.
[457a6f5]742 *
[6b82009]743 * @see socket.h
744 *
745 * @param[in] sess Callback session.
746 * @param[in] callid Message identifier.
747 * @param[in] call Message parameters.
748 *
749 * @return EOK on success.
[457a6f5]750 *
751 */
[6b82009]752static int udp_process_client_messages(async_sess_t *sess, ipc_callid_t callid,
753 ipc_call_t call)
[7282582]754{
[457a6f5]755 int res;
756 socket_cores_t local_sockets;
757 struct sockaddr *addr;
758 int socket_id;
759 size_t addrlen;
[348c589]760 size_t size;
[457a6f5]761 ipc_call_t answer;
[774e6d1a]762 size_t answer_count;
[f772bc55]763 packet_dimension_t *packet_dimension;
[457a6f5]764
765 /*
766 * Accept the connection
767 * - Answer the first IPC_M_CONNECT_TO_ME call.
768 */
769 res = EOK;
770 answer_count = 0;
771
[fb04cba8]772 /*
773 * The client connection is only in one fibril and therefore no
774 * additional locks are needed.
775 */
[457a6f5]776
777 socket_cores_initialize(&local_sockets);
778
[79ae36dd]779 while (true) {
[457a6f5]780
[fb04cba8]781 /* Answer the call */
[457a6f5]782 answer_call(callid, res, &answer, answer_count);
783
[fb04cba8]784 /* Refresh data */
[457a6f5]785 refresh_answer(&answer, &answer_count);
786
[fb04cba8]787 /* Get the next call */
[457a6f5]788 callid = async_get_call(&call);
[6b82009]789
790 /* Process the call */
[79ae36dd]791 if (!IPC_GET_IMETHOD(call)) {
[457a6f5]792 res = EHANGUP;
793 break;
[79ae36dd]794 }
[6b82009]795
[79ae36dd]796 switch (IPC_GET_IMETHOD(call)) {
[457a6f5]797 case NET_SOCKET:
798 socket_id = SOCKET_GET_SOCKET_ID(call);
[6b82009]799 res = socket_create(&local_sockets, sess, NULL,
[457a6f5]800 &socket_id);
801 SOCKET_SET_SOCKET_ID(answer, socket_id);
802
803 if (res != EOK)
804 break;
805
[348c589]806 size = MAX_UDP_FRAGMENT_SIZE;
[6b82009]807 if (tl_get_ip_packet_dimension(udp_globals.ip_sess,
[457a6f5]808 &udp_globals.dimensions, DEVICE_INVALID_ID,
809 &packet_dimension) == EOK) {
[348c589]810 if (packet_dimension->content < size)
811 size = packet_dimension->content;
[457a6f5]812 }
[348c589]813 SOCKET_SET_DATA_FRAGMENT_SIZE(answer, size);
[457a6f5]814 SOCKET_SET_HEADER_SIZE(answer, UDP_HEADER_SIZE);
815 answer_count = 3;
816 break;
817
818 case NET_SOCKET_BIND:
[7880d58]819 res = async_data_write_accept((void **) &addr, false,
820 0, 0, 0, &addrlen);
[457a6f5]821 if (res != EOK)
822 break;
823 fibril_rwlock_write_lock(&udp_globals.lock);
824 res = socket_bind(&local_sockets, &udp_globals.sockets,
825 SOCKET_GET_SOCKET_ID(call), addr, addrlen,
826 UDP_FREE_PORTS_START, UDP_FREE_PORTS_END,
827 udp_globals.last_used_port);
828 fibril_rwlock_write_unlock(&udp_globals.lock);
829 free(addr);
830 break;
831
832 case NET_SOCKET_SENDTO:
[7880d58]833 res = async_data_write_accept((void **) &addr, false,
834 0, 0, 0, &addrlen);
[457a6f5]835 if (res != EOK)
836 break;
837
838 fibril_rwlock_write_lock(&udp_globals.lock);
839 res = udp_sendto_message(&local_sockets,
840 SOCKET_GET_SOCKET_ID(call), addr, addrlen,
841 SOCKET_GET_DATA_FRAGMENTS(call), &size,
842 SOCKET_GET_FLAGS(call));
843 SOCKET_SET_DATA_FRAGMENT_SIZE(answer, size);
844
845 if (res != EOK)
846 fibril_rwlock_write_unlock(&udp_globals.lock);
847 else
848 answer_count = 2;
849
850 free(addr);
851 break;
852
853 case NET_SOCKET_RECVFROM:
854 fibril_rwlock_write_lock(&udp_globals.lock);
855 res = udp_recvfrom_message(&local_sockets,
856 SOCKET_GET_SOCKET_ID(call), SOCKET_GET_FLAGS(call),
857 &addrlen);
858 fibril_rwlock_write_unlock(&udp_globals.lock);
859
860 if (res <= 0)
861 break;
862
863 SOCKET_SET_READ_DATA_LENGTH(answer, res);
864 SOCKET_SET_ADDRESS_LENGTH(answer, addrlen);
865 answer_count = 3;
866 res = EOK;
867 break;
868
869 case NET_SOCKET_CLOSE:
870 fibril_rwlock_write_lock(&udp_globals.lock);
[6b82009]871 res = socket_destroy(udp_globals.net_sess,
[457a6f5]872 SOCKET_GET_SOCKET_ID(call), &local_sockets,
873 &udp_globals.sockets, NULL);
874 fibril_rwlock_write_unlock(&udp_globals.lock);
875 break;
876
877 case NET_SOCKET_GETSOCKOPT:
878 case NET_SOCKET_SETSOCKOPT:
879 default:
880 res = ENOTSUP;
881 break;
882 }
883 }
884
[6b82009]885 /* Release the application session */
886 async_hangup(sess);
[457a6f5]887
[fb04cba8]888 /* Release all local sockets */
[6b82009]889 socket_cores_release(udp_globals.net_sess, &local_sockets,
[457a6f5]890 &udp_globals.sockets, NULL);
891
892 return res;
893}
894
[f1938c6]895/** Per-connection initialization
896 *
897 */
898void tl_connection(void)
899{
900}
901
[457a6f5]902/** Processes the UDP message.
903 *
904 * @param[in] callid The message identifier.
905 * @param[in] call The message parameters.
906 * @param[out] answer The message answer parameters.
907 * @param[out] answer_count The last parameter for the actual answer in the
908 * answer parameter.
[1bfd3d3]909 * @return EOK on success.
910 * @return ENOTSUP if the message is not known.
[457a6f5]911 *
912 * @see udp_interface.h
913 * @see IS_NET_UDP_MESSAGE()
914 */
[f1938c6]915int tl_message(ipc_callid_t callid, ipc_call_t *call,
[774e6d1a]916 ipc_call_t *answer, size_t *answer_count)
[457a6f5]917{
918 *answer_count = 0;
[6b82009]919
920 async_sess_t *callback =
921 async_callback_receive_start(EXCHANGE_SERIALIZE, call);
922 if (callback)
923 return udp_process_client_messages(callback, callid, *call);
924
[457a6f5]925 return ENOTSUP;
[21580dd]926}
927
[849ed54]928int main(int argc, char *argv[])
929{
930 /* Start the module */
[014dd57b]931 return tl_module_start(SERVICE_UDP);
[849ed54]932}
933
[21580dd]934/** @}
935 */
Note: See TracBrowser for help on using the repository browser.