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

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 5d00e40 was 5d00e40, checked in by Vojtech Horky <vojtechhorky@…>, 14 years ago

Merge mainline changes

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