source: mainline/uspace/srv/net/tl/tcp/tcp.c@ 80099c19

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 80099c19 was 49bd793b, checked in by Martin Decky <martin@…>, 14 years ago

networking fixes

  • use sysarg_t for packet_id_t to avoid potential overflow
  • fix the confusion in the order of arguments for nil_received_msg(), this fixes the strange ping timeout on 127.0.0.1
  • Property mode set to 100644
File size: 69.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 tcp
[89e57cee]30 * @{
[21580dd]31 */
32
33/** @file
[89e57cee]34 * TCP module implementation.
35 * @see tcp.h
[21580dd]36 */
37
38#include <assert.h>
39#include <async.h>
40#include <fibril_synch.h>
41#include <malloc.h>
[fb04cba8]42/* TODO remove stdio */
[21580dd]43#include <stdio.h>
[e98b1d5]44#include <errno.h>
[21580dd]45
46#include <ipc/services.h>
[514ee46]47#include <ipc/net.h>
[8e3a65c]48#include <ipc/tl.h>
[88e127ee]49#include <ipc/socket.h>
[21580dd]50
[058edb6]51#include <net/socket_codes.h>
[fe5d3c1b]52#include <net/ip_protocols.h>
[e4554d4]53#include <net/in.h>
54#include <net/in6.h>
55#include <net/inet.h>
[c7a8442]56#include <net/modules.h>
[058edb6]57
[849ed54]58#include <adt/dynamic_fifo.h>
[0a866eeb]59#include <packet_client.h>
[14f1db0]60#include <packet_remote.h>
[849ed54]61#include <net_checksum.h>
62#include <ip_client.h>
63#include <ip_interface.h>
64#include <icmp_client.h>
[f1938c6]65#include <icmp_remote.h>
[849ed54]66#include <net_interface.h>
67#include <socket_core.h>
68#include <tl_common.h>
[014dd57b]69#include <tl_remote.h>
70#include <tl_skel.h>
71
72#include "tcp.h"
73#include "tcp_header.h"
[21580dd]74
[7c8267b]75/** TCP module name. */
[014dd57b]76#define NAME "tcp"
[849ed54]77
[7c8267b]78/** The TCP window default value. */
79#define NET_DEFAULT_TCP_WINDOW 10240
[21580dd]80
[7c8267b]81/** Initial timeout for new connections. */
[21580dd]82#define NET_DEFAULT_TCP_INITIAL_TIMEOUT 3000000L
83
[7c8267b]84/** Default timeout for closing. */
85#define NET_DEFAULT_TCP_TIME_WAIT_TIMEOUT 2000L
[21580dd]86
[7c8267b]87/** The initial outgoing sequence number. */
88#define TCP_INITIAL_SEQUENCE_NUMBER 2999
[21580dd]89
[7c8267b]90/** Maximum TCP fragment size. */
91#define MAX_TCP_FRAGMENT_SIZE 65535
[21580dd]92
[7c8267b]93/** Free ports pool start. */
94#define TCP_FREE_PORTS_START 1025
[21580dd]95
[7c8267b]96/** Free ports pool end. */
[21580dd]97#define TCP_FREE_PORTS_END 65535
98
[7c8267b]99/** Timeout for connection initialization, SYN sent. */
100#define TCP_SYN_SENT_TIMEOUT 1000000L
[21580dd]101
[7c8267b]102/** The maximum number of timeouts in a row before singaling connection lost. */
[21580dd]103#define TCP_MAX_TIMEOUTS 8
104
[7c8267b]105/** The number of acknowledgements before retransmit. */
[21580dd]106#define TCP_FAST_RETRANSMIT_COUNT 3
107
[7c8267b]108/** Returns a value indicating whether the value is in the interval respecting
[89e57cee]109 * the possible overflow.
110 *
111 * The high end and/or the value may overflow, be lower than the low value.
[7c8267b]112 *
[89e57cee]113 * @param[in] lower The last value before the interval.
114 * @param[in] value The value to be checked.
115 * @param[in] higher_equal The last value in the interval.
[21580dd]116 */
[7c8267b]117#define IS_IN_INTERVAL_OVERFLOW(lower, value, higher_equal) \
118 ((((lower) < (value)) && (((value) <= (higher_equal)) || \
119 ((higher_equal) < (lower)))) || (((value) <= (higher_equal)) && \
120 ((higher_equal) < (lower))))
[21580dd]121
122/** Type definition of the TCP timeout.
123 * @see tcp_timeout
124 */
[7c8267b]125typedef struct tcp_timeout tcp_timeout_t;
[21580dd]126
127/** TCP reply timeout data.
128 * Used as a timeouting fibril argument.
129 * @see tcp_timeout()
130 */
[7c8267b]131struct tcp_timeout {
132 /** TCP global data are going to be read only. */
[aadf01e]133 int globals_read_only;
[7c8267b]134
135 /** Socket port. */
[aadf01e]136 int port;
[7c8267b]137
138 /** Local sockets. */
[aaa3f33a]139 socket_cores_t *local_sockets;
[7c8267b]140
141 /** Socket identifier. */
[aadf01e]142 int socket_id;
[7c8267b]143
144 /** Socket state. */
[aadf01e]145 tcp_socket_state_t state;
[7c8267b]146
147 /** Sent packet sequence number. */
[aadf01e]148 int sequence_number;
[7c8267b]149
150 /** Timeout in microseconds. */
[aadf01e]151 suseconds_t timeout;
[7c8267b]152
153 /** Port map key. */
[61bfc370]154 uint8_t *key;
[7c8267b]155
156 /** Port map key length. */
[aadf01e]157 size_t key_length;
[21580dd]158};
159
[46d4d9f]160static int tcp_release_and_return(packet_t *, int);
[88a1bb9]161static void tcp_prepare_operation_header(socket_core_t *, tcp_socket_data_t *,
[4e5c7ba]162 tcp_header_t *, int synchronize, int);
[88a1bb9]163static int tcp_prepare_timeout(int (*)(void *), socket_core_t *,
[4e5c7ba]164 tcp_socket_data_t *, size_t, tcp_socket_state_t, suseconds_t, int);
[88a1bb9]165static void tcp_free_socket_data(socket_core_t *);
[89e57cee]166
167static int tcp_timeout(void *);
168
169static int tcp_release_after_timeout(void *);
170
[609243f4]171static int tcp_process_packet(nic_device_id_t, packet_t *, services_t);
[aaa3f33a]172static int tcp_connect_core(socket_core_t *, socket_cores_t *,
[89e57cee]173 struct sockaddr *, socklen_t);
[88a1bb9]174static int tcp_queue_prepare_packet(socket_core_t *, tcp_socket_data_t *,
[46d4d9f]175 packet_t *, size_t);
176static int tcp_queue_packet(socket_core_t *, tcp_socket_data_t *, packet_t *,
[89e57cee]177 size_t);
[46d4d9f]178static packet_t *tcp_get_packets_to_send(socket_core_t *, tcp_socket_data_t *);
[609243f4]179static void tcp_send_packets(nic_device_id_t, packet_t *);
[89e57cee]180
[88a1bb9]181static void tcp_process_acknowledgement(socket_core_t *, tcp_socket_data_t *,
[4e5c7ba]182 tcp_header_t *);
[46d4d9f]183static packet_t *tcp_send_prepare_packet(socket_core_t *, tcp_socket_data_t *,
184 packet_t *, size_t, size_t);
185static packet_t *tcp_prepare_copy(socket_core_t *, tcp_socket_data_t *,
186 packet_t *, size_t, size_t);
[88a1bb9]187/* static */ void tcp_retransmit_packet(socket_core_t *, tcp_socket_data_t *,
[89e57cee]188 size_t);
[46d4d9f]189static int tcp_create_notification_packet(packet_t **, socket_core_t *,
[4e5c7ba]190 tcp_socket_data_t *, int, int);
191static void tcp_refresh_socket_data(tcp_socket_data_t *);
192
193static void tcp_initialize_socket_data(tcp_socket_data_t *);
194
[88a1bb9]195static int tcp_process_listen(socket_core_t *, tcp_socket_data_t *,
[46d4d9f]196 tcp_header_t *, packet_t *, struct sockaddr *, struct sockaddr *, size_t);
[88a1bb9]197static int tcp_process_syn_sent(socket_core_t *, tcp_socket_data_t *,
[46d4d9f]198 tcp_header_t *, packet_t *);
[88a1bb9]199static int tcp_process_syn_received(socket_core_t *, tcp_socket_data_t *,
[46d4d9f]200 tcp_header_t *, packet_t *);
[88a1bb9]201static int tcp_process_established(socket_core_t *, tcp_socket_data_t *,
[46d4d9f]202 tcp_header_t *, packet_t *, int, size_t);
[88a1bb9]203static int tcp_queue_received_packet(socket_core_t *, tcp_socket_data_t *,
[46d4d9f]204 packet_t *, int, size_t);
[d493830e]205static void tcp_queue_received_end_of_data(socket_core_t *socket);
[89e57cee]206
[609243f4]207static int tcp_received_msg(nic_device_id_t, packet_t *, services_t, services_t);
[6b82009]208static int tcp_process_client_messages(async_sess_t *, ipc_callid_t,
209 ipc_call_t);
[89e57cee]210
[aaa3f33a]211static int tcp_listen_message(socket_cores_t *, int, int);
212static int tcp_connect_message(socket_cores_t *, int, struct sockaddr *,
[89e57cee]213 socklen_t);
[aaa3f33a]214static int tcp_recvfrom_message(socket_cores_t *, int, int, size_t *);
215static int tcp_send_message(socket_cores_t *, int, int, size_t *, int);
216static int tcp_accept_message(socket_cores_t *, int, int, size_t *, size_t *);
217static int tcp_close_message(socket_cores_t *, int);
[21580dd]218
[7c8267b]219/** TCP global data. */
220tcp_globals_t tcp_globals;
[21580dd]221
[609243f4]222int tcp_received_msg(nic_device_id_t device_id, packet_t *packet,
[fb04cba8]223 services_t receiver, services_t error)
[7c8267b]224{
[0578271]225 int rc;
[21580dd]226
[7c8267b]227 if (receiver != SERVICE_TCP)
[aadf01e]228 return EREFUSED;
[7c8267b]229
[aadf01e]230 fibril_rwlock_write_lock(&tcp_globals.lock);
[0578271]231 rc = tcp_process_packet(device_id, packet, error);
232 if (rc != EOK)
[aadf01e]233 fibril_rwlock_write_unlock(&tcp_globals.lock);
[7c8267b]234
[0578271]235 printf("receive %d \n", rc);
[21580dd]236
[0578271]237 return rc;
[21580dd]238}
239
[609243f4]240int tcp_process_packet(nic_device_id_t device_id, packet_t *packet, services_t error)
[7c8267b]241{
[aadf01e]242 size_t length;
243 size_t offset;
244 int result;
[4e5c7ba]245 tcp_header_t *header;
[88a1bb9]246 socket_core_t *socket;
[4e5c7ba]247 tcp_socket_data_t *socket_data;
[46d4d9f]248 packet_t *next_packet;
[aadf01e]249 size_t total_length;
250 uint32_t checksum;
251 int fragments;
252 icmp_type_t type;
253 icmp_code_t code;
[7c8267b]254 struct sockaddr *src;
255 struct sockaddr *dest;
[aadf01e]256 size_t addrlen;
[0578271]257 int rc;
[aadf01e]258
[89e57cee]259 switch (error) {
260 case SERVICE_NONE:
261 break;
262 case SERVICE_ICMP:
[fb04cba8]263 /* Process error */
[89e57cee]264 result = icmp_client_process_packet(packet, &type, &code, NULL,
265 NULL);
266 if (result < 0)
267 return tcp_release_and_return(packet, result);
[7c8267b]268
[89e57cee]269 length = (size_t) result;
[0578271]270 rc = packet_trim(packet, length, 0);
271 if (rc != EOK)
272 return tcp_release_and_return(packet, rc);
[89e57cee]273 break;
274 default:
275 return tcp_release_and_return(packet, ENOTSUP);
[21580dd]276 }
277
[fb04cba8]278 /* TODO process received ipopts? */
[aadf01e]279 result = ip_client_process_packet(packet, NULL, NULL, NULL, NULL, NULL);
[7c8267b]280 if (result < 0)
[aadf01e]281 return tcp_release_and_return(packet, result);
[7c8267b]282
[aadf01e]283 offset = (size_t) result;
[21580dd]284
[aadf01e]285 length = packet_get_data_length(packet);
[7c8267b]286 if (length <= 0)
[aadf01e]287 return tcp_release_and_return(packet, EINVAL);
[7c8267b]288
289 if (length < TCP_HEADER_SIZE + offset)
[aadf01e]290 return tcp_release_and_return(packet, NO_DATA);
[21580dd]291
[fb04cba8]292 /* Trim all but TCP header */
[0578271]293 rc = packet_trim(packet, offset, 0);
294 if (rc != EOK)
295 return tcp_release_and_return(packet, rc);
[21580dd]296
[fb04cba8]297 /* Get tcp header */
[4e5c7ba]298 header = (tcp_header_t *) packet_get_data(packet);
[7c8267b]299 if (!header)
[aadf01e]300 return tcp_release_and_return(packet, NO_DATA);
[7c8267b]301
[28a3e74]302#if 0
303 printf("header len %d, port %d \n", TCP_HEADER_LENGTH(header),
304 ntohs(header->destination_port));
305#endif
[aadf01e]306 result = packet_get_addr(packet, (uint8_t **) &src, (uint8_t **) &dest);
[7c8267b]307 if (result <= 0)
[aadf01e]308 return tcp_release_and_return(packet, result);
[7c8267b]309
[aadf01e]310 addrlen = (size_t) result;
[21580dd]311
[0578271]312 rc = tl_set_address_port(src, addrlen, ntohs(header->source_port));
313 if (rc != EOK)
314 return tcp_release_and_return(packet, rc);
[7c8267b]315
[fb04cba8]316 /* Find the destination socket */
[7c8267b]317 socket = socket_port_find(&tcp_globals.sockets,
[61bfc370]318 ntohs(header->destination_port), (uint8_t *) src, addrlen);
[7c8267b]319 if (!socket) {
[fb04cba8]320 /* Find the listening destination socket */
[7c8267b]321 socket = socket_port_find(&tcp_globals.sockets,
[61bfc370]322 ntohs(header->destination_port),
323 (uint8_t *) SOCKET_MAP_KEY_LISTENING, 0);
[89e57cee]324 }
[fb04cba8]325
[89e57cee]326 if (!socket) {
[6b82009]327 if (tl_prepare_icmp_packet(tcp_globals.net_sess,
328 tcp_globals.icmp_sess, packet, error) == EOK) {
329 icmp_destination_unreachable_msg(tcp_globals.icmp_sess,
[89e57cee]330 ICMP_PORT_UNREACH, 0, packet);
[21580dd]331 }
[89e57cee]332 return EADDRNOTAVAIL;
[21580dd]333 }
[89e57cee]334
[aadf01e]335 printf("socket id %d\n", socket->socket_id);
[4e5c7ba]336 socket_data = (tcp_socket_data_t *) socket->specific_data;
[aadf01e]337 assert(socket_data);
[21580dd]338
[fb04cba8]339 /* Some data received, clear the timeout counter */
[21580dd]340 socket_data->timeout_count = 0;
341
[fb04cba8]342 /* Count the received packet fragments */
[21580dd]343 next_packet = packet;
344 fragments = 0;
345 checksum = 0;
346 total_length = 0;
[7c8267b]347 do {
[89e57cee]348 fragments++;
[aadf01e]349 length = packet_get_data_length(next_packet);
[7c8267b]350 if (length <= 0)
[aadf01e]351 return tcp_release_and_return(packet, NO_DATA);
[7c8267b]352
[21580dd]353 total_length += length;
[7c8267b]354
[fb04cba8]355 /* Add partial checksum if set */
[7c8267b]356 if (!error) {
357 checksum = compute_checksum(checksum,
358 packet_get_data(packet),
359 packet_get_data_length(packet));
[21580dd]360 }
361
[7c8267b]362 } while ((next_packet = pq_next(next_packet)));
363
[aadf01e]364 fibril_rwlock_write_lock(socket_data->local_lock);
[7c8267b]365
366 if (error)
[89e57cee]367 goto has_error_service;
[7c8267b]368
369 if (socket_data->state == TCP_SOCKET_LISTEN) {
370 if (socket_data->pseudo_header) {
371 free(socket_data->pseudo_header);
372 socket_data->pseudo_header = NULL;
373 socket_data->headerlen = 0;
374 }
375
[0578271]376 rc = ip_client_get_pseudo_header(IPPROTO_TCP, src, addrlen,
377 dest, addrlen, total_length, &socket_data->pseudo_header,
378 &socket_data->headerlen);
379 if (rc != EOK) {
[aadf01e]380 fibril_rwlock_write_unlock(socket_data->local_lock);
[0578271]381 return tcp_release_and_return(packet, rc);
382 }
383 } else {
384 rc = ip_client_set_pseudo_header_data_length(
385 socket_data->pseudo_header, socket_data->headerlen,
386 total_length);
387 if (rc != EOK) {
388 fibril_rwlock_write_unlock(socket_data->local_lock);
389 return tcp_release_and_return(packet, rc);
[21580dd]390 }
[7c8267b]391 }
392
393 checksum = compute_checksum(checksum, socket_data->pseudo_header,
394 socket_data->headerlen);
395 if (flip_checksum(compact_checksum(checksum)) != IP_CHECKSUM_ZERO) {
396 printf("checksum err %x -> %x\n", header->checksum,
397 flip_checksum(compact_checksum(checksum)));
398 fibril_rwlock_write_unlock(socket_data->local_lock);
399
[6b82009]400 rc = tl_prepare_icmp_packet(tcp_globals.net_sess,
401 tcp_globals.icmp_sess, packet, error);
[0578271]402 if (rc == EOK) {
[fb04cba8]403 /* Checksum error ICMP */
[6b82009]404 icmp_parameter_problem_msg(tcp_globals.icmp_sess,
[7c8267b]405 ICMP_PARAM_POINTER,
406 ((size_t) ((void *) &header->checksum)) -
407 ((size_t) ((void *) header)), packet);
[21580dd]408 }
[7c8267b]409
410 return EINVAL;
[21580dd]411 }
412
[89e57cee]413has_error_service:
[092e4f1]414 fibril_rwlock_write_unlock(&tcp_globals.lock);
[21580dd]415
[fb04cba8]416 /* TODO error reporting/handling */
[7c8267b]417 switch (socket_data->state) {
418 case TCP_SOCKET_LISTEN:
[0578271]419 rc = tcp_process_listen(socket, socket_data, header, packet,
420 src, dest, addrlen);
[7c8267b]421 break;
422 case TCP_SOCKET_SYN_RECEIVED:
[0578271]423 rc = tcp_process_syn_received(socket, socket_data, header,
424 packet);
[7c8267b]425 break;
426 case TCP_SOCKET_SYN_SENT:
[0578271]427 rc = tcp_process_syn_sent(socket, socket_data, header, packet);
[7c8267b]428 break;
429 case TCP_SOCKET_FIN_WAIT_1:
[fb04cba8]430 /* ack changing the state to FIN_WAIT_2 gets processed later */
[7c8267b]431 case TCP_SOCKET_FIN_WAIT_2:
[fb04cba8]432 /* fin changing state to LAST_ACK gets processed later */
[7c8267b]433 case TCP_SOCKET_LAST_ACK:
[fb04cba8]434 /* ack releasing the socket get processed later */
[7c8267b]435 case TCP_SOCKET_CLOSING:
[fb04cba8]436 /* ack releasing the socket gets processed later */
[7c8267b]437 case TCP_SOCKET_ESTABLISHED:
[0578271]438 rc = tcp_process_established(socket, socket_data, header,
439 packet, fragments, total_length);
[7c8267b]440 break;
441 default:
[6b82009]442 pq_release_remote(tcp_globals.net_sess, packet_get_id(packet));
[21580dd]443 }
444
[0578271]445 if (rc != EOK) {
[aadf01e]446 fibril_rwlock_write_unlock(socket_data->local_lock);
[0578271]447 printf("process %d\n", rc);
[21580dd]448 }
[7c8267b]449
[21580dd]450 return EOK;
451}
452
[88a1bb9]453int tcp_process_established(socket_core_t *socket, tcp_socket_data_t *
[46d4d9f]454 socket_data, tcp_header_t *header, packet_t *packet, int fragments,
[fb04cba8]455 size_t total_length)
[7c8267b]456{
[46d4d9f]457 packet_t *next_packet;
458 packet_t *tmp_packet;
[aadf01e]459 uint32_t old_incoming;
460 size_t order;
461 uint32_t sequence_number;
462 size_t length;
463 size_t offset;
464 uint32_t new_sequence_number;
[d493830e]465 bool forced_ack;
[0578271]466 int rc;
[aadf01e]467
468 assert(socket);
469 assert(socket_data);
470 assert(socket->specific_data == socket_data);
471 assert(header);
472 assert(packet);
473
[d493830e]474 forced_ack = false;
475
[aadf01e]476 new_sequence_number = ntohl(header->sequence_number);
[21580dd]477 old_incoming = socket_data->next_incoming;
478
[0743493a]479 if (GET_TCP_HEADER_FINALIZE(header)) {
[d493830e]480 socket_data->fin_incoming = new_sequence_number +
481 total_length - TCP_HEADER_LENGTH(header);
482 }
[21580dd]483
[fb04cba8]484 /* Trim begining if containing expected data */
[7c8267b]485 if (IS_IN_INTERVAL_OVERFLOW(new_sequence_number,
486 socket_data->next_incoming, new_sequence_number + total_length)) {
487
[fb04cba8]488 /* Get the acknowledged offset */
[7c8267b]489 if (socket_data->next_incoming < new_sequence_number) {
490 offset = new_sequence_number -
491 socket_data->next_incoming;
492 } else {
493 offset = socket_data->next_incoming -
494 new_sequence_number;
[21580dd]495 }
[7c8267b]496
[21580dd]497 new_sequence_number += offset;
498 total_length -= offset;
[aadf01e]499 length = packet_get_data_length(packet);
[fb04cba8]500
501 /* Trim the acknowledged data */
[7c8267b]502 while (length <= offset) {
[fb04cba8]503 /* Release the acknowledged packets */
[aadf01e]504 next_packet = pq_next(packet);
[6b82009]505 pq_release_remote(tcp_globals.net_sess,
[7c8267b]506 packet_get_id(packet));
[21580dd]507 packet = next_packet;
508 offset -= length;
[aadf01e]509 length = packet_get_data_length(packet);
[21580dd]510 }
[7c8267b]511
[0578271]512 if (offset > 0) {
513 rc = packet_trim(packet, offset, 0);
514 if (rc != EOK)
515 return tcp_release_and_return(packet, rc);
516 }
[7c8267b]517
[aadf01e]518 assert(new_sequence_number == socket_data->next_incoming);
[21580dd]519 }
520
[fb04cba8]521 /* Release if overflowing the window */
[21580dd]522/*
[7c8267b]523 if (IS_IN_INTERVAL_OVERFLOW(socket_data->next_incoming +
524 socket_data->window, new_sequence_number, new_sequence_number +
525 total_length)) {
526 return tcp_release_and_return(packet, EOVERFLOW);
527 }
528
[21580dd]529 // trim end if overflowing the window
[7c8267b]530 if (IS_IN_INTERVAL_OVERFLOW(new_sequence_number,
531 socket_data->next_incoming + socket_data->window,
532 new_sequence_number + total_length)) {
[21580dd]533 // get the allowed data length
[7c8267b]534 if (socket_data->next_incoming + socket_data->window <
535 new_sequence_number) {
536 offset = new_sequence_number -
537 socket_data->next_incoming + socket_data->window;
538 } else {
539 offset = socket_data->next_incoming +
540 socket_data->window - new_sequence_number;
[21580dd]541 }
542 next_packet = packet;
543 // trim the overflowing data
[7c8267b]544 while (next_packet && (offset > 0)) {
[aadf01e]545 length = packet_get_data_length(packet);
[7c8267b]546 if (length <= offset)
[aadf01e]547 next_packet = pq_next(next_packet);
[0578271]548 else {
549 rc = packet_trim(next_packet, 0,
550 length - offset));
551 if (rc != EOK)
552 return tcp_release_and_return(packet,
553 rc);
554 }
[21580dd]555 offset -= length;
556 total_length -= length - offset;
557 }
558 // release the overflowing packets
[aadf01e]559 next_packet = pq_next(next_packet);
[7c8267b]560 if (next_packet) {
[21580dd]561 tmp_packet = next_packet;
[aadf01e]562 next_packet = pq_next(next_packet);
563 pq_insert_after(tmp_packet, next_packet);
[6b82009]564 pq_release_remote(tcp_globals.net_sess,
[7c8267b]565 packet_get_id(tmp_packet));
[21580dd]566 }
[7c8267b]567 assert(new_sequence_number + total_length ==
568 socket_data->next_incoming + socket_data->window);
[21580dd]569 }
570*/
[fb04cba8]571 /* The expected one arrived? */
[7c8267b]572 if (new_sequence_number == socket_data->next_incoming) {
[21580dd]573 printf("expected\n");
[fb04cba8]574 /* Process acknowledgement */
[aadf01e]575 tcp_process_acknowledgement(socket, socket_data, header);
[21580dd]576
[fb04cba8]577 /* Remove the header */
[aadf01e]578 total_length -= TCP_HEADER_LENGTH(header);
[0578271]579 rc = packet_trim(packet, TCP_HEADER_LENGTH(header), 0);
580 if (rc != EOK)
581 return tcp_release_and_return(packet, rc);
[21580dd]582
[7c8267b]583 if (total_length) {
[0578271]584 rc = tcp_queue_received_packet(socket, socket_data,
585 packet, fragments, total_length);
586 if (rc != EOK)
587 return rc;
[7c8267b]588 } else {
[21580dd]589 total_length = 1;
590 }
[7c8267b]591
[21580dd]592 socket_data->next_incoming = old_incoming + total_length;
593 packet = socket_data->incoming;
[7c8267b]594 while (packet) {
[0578271]595 rc = pq_get_order(socket_data->incoming, &order, NULL);
596 if (rc != EOK) {
[fb04cba8]597 /* Remove the corrupted packet */
[aadf01e]598 next_packet = pq_detach(packet);
[7c8267b]599 if (packet == socket_data->incoming)
[21580dd]600 socket_data->incoming = next_packet;
[6b82009]601 pq_release_remote(tcp_globals.net_sess,
[7c8267b]602 packet_get_id(packet));
[21580dd]603 packet = next_packet;
604 continue;
605 }
[7c8267b]606
[aadf01e]607 sequence_number = (uint32_t) order;
[7c8267b]608 if (IS_IN_INTERVAL_OVERFLOW(sequence_number,
609 old_incoming, socket_data->next_incoming)) {
[fb04cba8]610 /* Move to the next */
[aadf01e]611 packet = pq_next(packet);
[fb04cba8]612 /* Coninual data? */
[7c8267b]613 } else if (IS_IN_INTERVAL_OVERFLOW(old_incoming,
614 sequence_number, socket_data->next_incoming)) {
[fb04cba8]615 /* Detach the packet */
[aadf01e]616 next_packet = pq_detach(packet);
[7c8267b]617 if (packet == socket_data->incoming)
[21580dd]618 socket_data->incoming = next_packet;
[fb04cba8]619 /* Get data length */
[aadf01e]620 length = packet_get_data_length(packet);
[21580dd]621 new_sequence_number = sequence_number + length;
[7c8267b]622 if (length <= 0) {
[fb04cba8]623 /* Remove the empty packet */
[6b82009]624 pq_release_remote(tcp_globals.net_sess,
[7c8267b]625 packet_get_id(packet));
[21580dd]626 packet = next_packet;
627 continue;
628 }
[fb04cba8]629 /* Exactly following */
[7c8267b]630 if (sequence_number ==
631 socket_data->next_incoming) {
[fb04cba8]632 /* Queue received data */
[0578271]633 rc = tcp_queue_received_packet(socket,
[7c8267b]634 socket_data, packet, 1,
[0578271]635 packet_get_data_length(packet));
636 if (rc != EOK)
637 return rc;
[7c8267b]638 socket_data->next_incoming =
639 new_sequence_number;
[21580dd]640 packet = next_packet;
641 continue;
[fb04cba8]642 /* At least partly following data? */
[89e57cee]643 }
644 if (IS_IN_INTERVAL_OVERFLOW(sequence_number,
645 socket_data->next_incoming, new_sequence_number)) {
[7c8267b]646 if (socket_data->next_incoming <
647 new_sequence_number) {
648 length = new_sequence_number -
649 socket_data->next_incoming;
650 } else {
651 length =
652 socket_data->next_incoming -
653 new_sequence_number;
[21580dd]654 }
[0578271]655 rc = packet_trim(packet,length, 0);
656 if (rc == EOK) {
[fb04cba8]657 /* Queue received data */
[0578271]658 rc = tcp_queue_received_packet(
[7c8267b]659 socket, socket_data, packet,
660 1, packet_get_data_length(
[0578271]661 packet));
662 if (rc != EOK)
663 return rc;
[7c8267b]664 socket_data->next_incoming =
665 new_sequence_number;
[21580dd]666 packet = next_packet;
667 continue;
668 }
669 }
[fb04cba8]670 /* Remove the duplicit or corrupted packet */
[6b82009]671 pq_release_remote(tcp_globals.net_sess,
[7c8267b]672 packet_get_id(packet));
[21580dd]673 packet = next_packet;
674 continue;
[7c8267b]675 } else {
[21580dd]676 break;
677 }
678 }
[7c8267b]679 } else if (IS_IN_INTERVAL(socket_data->next_incoming,
680 new_sequence_number,
681 socket_data->next_incoming + socket_data->window)) {
[21580dd]682 printf("in window\n");
[fb04cba8]683 /* Process acknowledgement */
[aadf01e]684 tcp_process_acknowledgement(socket, socket_data, header);
[21580dd]685
[fb04cba8]686 /* Remove the header */
[aadf01e]687 total_length -= TCP_HEADER_LENGTH(header);
[0578271]688 rc = packet_trim(packet, TCP_HEADER_LENGTH(header), 0);
689 if (rc != EOK)
690 return tcp_release_and_return(packet, rc);
[21580dd]691
[aadf01e]692 next_packet = pq_detach(packet);
693 length = packet_get_data_length(packet);
[0578271]694 rc = pq_add(&socket_data->incoming, packet, new_sequence_number,
695 length);
696 if (rc != EOK) {
[fb04cba8]697 /* Remove the corrupted packets */
[6b82009]698 pq_release_remote(tcp_globals.net_sess,
[7c8267b]699 packet_get_id(packet));
[6b82009]700 pq_release_remote(tcp_globals.net_sess,
[7c8267b]701 packet_get_id(next_packet));
702 } else {
703 while (next_packet) {
[21580dd]704 new_sequence_number += length;
[aadf01e]705 tmp_packet = pq_detach(next_packet);
706 length = packet_get_data_length(next_packet);
[0578271]707
708 rc = pq_set_order(next_packet,
709 new_sequence_number, length);
710 if (rc != EOK) {
[6b82009]711 pq_release_remote(tcp_globals.net_sess,
[0578271]712 packet_get_id(next_packet));
713 }
714 rc = pq_insert_after(packet, next_packet);
715 if (rc != EOK) {
[6b82009]716 pq_release_remote(tcp_globals.net_sess,
[7c8267b]717 packet_get_id(next_packet));
[21580dd]718 }
719 next_packet = tmp_packet;
720 }
721 }
[7c8267b]722 } else {
[21580dd]723 printf("unexpected\n");
[fb04cba8]724 /* Release duplicite or restricted */
[6b82009]725 pq_release_remote(tcp_globals.net_sess, packet_get_id(packet));
[d493830e]726 forced_ack = true;
[21580dd]727 }
728
[d493830e]729 /* If next in sequence is an incoming FIN */
730 if (socket_data->next_incoming == socket_data->fin_incoming) {
731 /* Advance sequence number */
732 socket_data->next_incoming += 1;
733
734 /* Handle FIN */
[7c8267b]735 switch (socket_data->state) {
736 case TCP_SOCKET_FIN_WAIT_1:
737 case TCP_SOCKET_FIN_WAIT_2:
738 case TCP_SOCKET_CLOSING:
739 socket_data->state = TCP_SOCKET_CLOSING;
740 break;
[d493830e]741 case TCP_SOCKET_ESTABLISHED:
742 /* Queue end-of-data marker on the socket. */
743 tcp_queue_received_end_of_data(socket);
744 socket_data->state = TCP_SOCKET_CLOSE_WAIT;
745 break;
[7c8267b]746 default:
747 socket_data->state = TCP_SOCKET_CLOSE_WAIT;
748 break;
[21580dd]749 }
750 }
751
[aadf01e]752 packet = tcp_get_packets_to_send(socket, socket_data);
[d493830e]753 if (!packet && (socket_data->next_incoming != old_incoming || forced_ack)) {
[fb04cba8]754 /* Create the notification packet */
[0578271]755 rc = tcp_create_notification_packet(&packet, socket,
756 socket_data, 0, 0);
757 if (rc != EOK)
758 return rc;
759 rc = tcp_queue_prepare_packet(socket, socket_data, packet, 1);
760 if (rc != EOK)
761 return rc;
[7c8267b]762 packet = tcp_send_prepare_packet(socket, socket_data, packet, 1,
763 socket_data->last_outgoing + 1);
[21580dd]764 }
[7c8267b]765
[aadf01e]766 fibril_rwlock_write_unlock(socket_data->local_lock);
[7c8267b]767
[fb04cba8]768 /* Send the packet */
[aadf01e]769 tcp_send_packets(socket_data->device_id, packet);
[7c8267b]770
[21580dd]771 return EOK;
772}
773
[88a1bb9]774int tcp_queue_received_packet(socket_core_t *socket,
[46d4d9f]775 tcp_socket_data_t *socket_data, packet_t *packet, int fragments,
[7c8267b]776 size_t total_length)
777{
[f772bc55]778 packet_dimension_t *packet_dimension;
[0578271]779 int rc;
[ede63e4]780
[aadf01e]781 assert(socket);
782 assert(socket_data);
783 assert(socket->specific_data == socket_data);
784 assert(packet);
785 assert(fragments >= 1);
786 assert(socket_data->window > total_length);
[21580dd]787
[fb04cba8]788 /* Queue the received packet */
[0578271]789 rc = dyn_fifo_push(&socket->received, packet_get_id(packet),
790 SOCKET_MAX_RECEIVED_SIZE);
791 if (rc != EOK)
792 return tcp_release_and_return(packet, rc);
[6b82009]793 rc = tl_get_ip_packet_dimension(tcp_globals.ip_sess,
[0578271]794 &tcp_globals.dimensions, socket_data->device_id, &packet_dimension);
795 if (rc != EOK)
796 return tcp_release_and_return(packet, rc);
[21580dd]797
[fb04cba8]798 /* Decrease the window size */
[21580dd]799 socket_data->window -= total_length;
800
[fb04cba8]801 /* Notify the destination socket */
[6b82009]802 async_exch_t *exch = async_exchange_begin(socket->sess);
803 async_msg_5(exch, NET_SOCKET_RECEIVED, (sysarg_t) socket->socket_id,
[7c8267b]804 ((packet_dimension->content < socket_data->data_fragment_size) ?
805 packet_dimension->content : socket_data->data_fragment_size), 0, 0,
[96b02eb9]806 (sysarg_t) fragments);
[6b82009]807 async_exchange_end(exch);
[7c8267b]808
[21580dd]809 return EOK;
810}
811
[d493830e]812/** Queue end-of-data marker on the socket.
813 *
814 * Next element in the sequence space is FIN. Queue end-of-data marker
815 * on the socket.
816 *
817 * @param socket Socket
818 */
819static void tcp_queue_received_end_of_data(socket_core_t *socket)
820{
821 assert(socket != NULL);
822
823 /* Notify the destination socket */
[6b82009]824 async_exch_t *exch = async_exchange_begin(socket->sess);
825 async_msg_5(exch, NET_SOCKET_RECEIVED, (sysarg_t) socket->socket_id,
826 0, 0, 0, (sysarg_t) 0 /* 0 fragments == no more data */);
827 async_exchange_end(exch);
[d493830e]828}
829
[88a1bb9]830int tcp_process_syn_sent(socket_core_t *socket, tcp_socket_data_t *
[46d4d9f]831 socket_data, tcp_header_t *header, packet_t *packet)
[7c8267b]832{
[46d4d9f]833 packet_t *next_packet;
[0578271]834 int rc;
[21580dd]835
[aadf01e]836 assert(socket);
837 assert(socket_data);
838 assert(socket->specific_data == socket_data);
839 assert(header);
840 assert(packet);
[21580dd]841
[0743493a]842 if (!GET_TCP_HEADER_SYNCHRONIZE(header))
[7c8267b]843 return tcp_release_and_return(packet, EINVAL);
844
[fb04cba8]845 /* Process acknowledgement */
[7c8267b]846 tcp_process_acknowledgement(socket, socket_data, header);
847
848 socket_data->next_incoming = ntohl(header->sequence_number) + 1;
[fb04cba8]849
850 /* Release additional packets */
[7c8267b]851 next_packet = pq_detach(packet);
852 if (next_packet) {
[6b82009]853 pq_release_remote(tcp_globals.net_sess,
[7c8267b]854 packet_get_id(next_packet));
855 }
[fb04cba8]856
857 /* Trim if longer than the header */
[0578271]858 if (packet_get_data_length(packet) > sizeof(*header)) {
859 rc = packet_trim(packet, 0,
860 packet_get_data_length(packet) - sizeof(*header));
861 if (rc != EOK)
862 return tcp_release_and_return(packet, rc);
[7c8267b]863 }
[fb04cba8]864
[7c8267b]865 tcp_prepare_operation_header(socket, socket_data, header, 0, 0);
866 fibril_mutex_lock(&socket_data->operation.mutex);
867 socket_data->operation.result = tcp_queue_packet(socket, socket_data,
868 packet, 1);
[fb04cba8]869
[7c8267b]870 if (socket_data->operation.result == EOK) {
871 socket_data->state = TCP_SOCKET_ESTABLISHED;
872 packet = tcp_get_packets_to_send(socket, socket_data);
873 if (packet) {
874 fibril_rwlock_write_unlock( socket_data->local_lock);
[fb04cba8]875 /* Send the packet */
[7c8267b]876 tcp_send_packets(socket_data->device_id, packet);
[fb04cba8]877 /* Signal the result */
[7c8267b]878 fibril_condvar_signal( &socket_data->operation.condvar);
879 fibril_mutex_unlock( &socket_data->operation.mutex);
880 return EOK;
[21580dd]881 }
882 }
[fb04cba8]883
[7c8267b]884 fibril_mutex_unlock(&socket_data->operation.mutex);
[aadf01e]885 return tcp_release_and_return(packet, EINVAL);
[21580dd]886}
887
[88a1bb9]888int tcp_process_listen(socket_core_t *listening_socket,
[4e5c7ba]889 tcp_socket_data_t *listening_socket_data, tcp_header_t *header,
[46d4d9f]890 packet_t *packet, struct sockaddr *src, struct sockaddr *dest,
[7c8267b]891 size_t addrlen)
892{
[46d4d9f]893 packet_t *next_packet;
[88a1bb9]894 socket_core_t *socket;
[4e5c7ba]895 tcp_socket_data_t *socket_data;
[aadf01e]896 int socket_id;
897 int listening_socket_id = listening_socket->socket_id;
898 int listening_port = listening_socket->port;
[0578271]899 int rc;
[aadf01e]900
901 assert(listening_socket);
902 assert(listening_socket_data);
903 assert(listening_socket->specific_data == listening_socket_data);
904 assert(header);
905 assert(packet);
906
[0743493a]907 if (!GET_TCP_HEADER_SYNCHRONIZE(header))
[7c8267b]908 return tcp_release_and_return(packet, EINVAL);
[21580dd]909
[4e5c7ba]910 socket_data = (tcp_socket_data_t *) malloc(sizeof(*socket_data));
[7c8267b]911 if (!socket_data)
912 return tcp_release_and_return(packet, ENOMEM);
913
914 tcp_initialize_socket_data(socket_data);
915 socket_data->local_lock = listening_socket_data->local_lock;
916 socket_data->local_sockets = listening_socket_data->local_sockets;
917 socket_data->listening_socket_id = listening_socket->socket_id;
918 socket_data->next_incoming = ntohl(header->sequence_number);
919 socket_data->treshold = socket_data->next_incoming +
920 ntohs(header->window);
921 socket_data->addrlen = addrlen;
922 socket_data->addr = malloc(socket_data->addrlen);
923 if (!socket_data->addr) {
924 free(socket_data);
925 return tcp_release_and_return(packet, ENOMEM);
926 }
[fb04cba8]927
[7c8267b]928 memcpy(socket_data->addr, src, socket_data->addrlen);
929 socket_data->dest_port = ntohs(header->source_port);
[0578271]930 rc = tl_set_address_port(socket_data->addr, socket_data->addrlen,
931 socket_data->dest_port);
932 if (rc != EOK) {
[7c8267b]933 free(socket_data->addr);
934 free(socket_data);
[0578271]935 return tcp_release_and_return(packet, rc);
[7c8267b]936 }
[21580dd]937
[fb04cba8]938 /* Create a socket */
[7c8267b]939 socket_id = -1;
[6b82009]940 rc = socket_create(socket_data->local_sockets, listening_socket->sess,
[0578271]941 socket_data, &socket_id);
942 if (rc != EOK) {
[7c8267b]943 free(socket_data->addr);
944 free(socket_data);
[0578271]945 return tcp_release_and_return(packet, rc);
[7c8267b]946 }
[21580dd]947
[7c8267b]948 printf("new_sock %d\n", socket_id);
949 socket_data->pseudo_header = listening_socket_data->pseudo_header;
950 socket_data->headerlen = listening_socket_data->headerlen;
951 listening_socket_data->pseudo_header = NULL;
952 listening_socket_data->headerlen = 0;
[21580dd]953
[7c8267b]954 fibril_rwlock_write_unlock(socket_data->local_lock);
955 fibril_rwlock_write_lock(&tcp_globals.lock);
[21580dd]956
[fb04cba8]957 /* Find the destination socket */
[7c8267b]958 listening_socket = socket_port_find(&tcp_globals.sockets,
[61bfc370]959 listening_port, (uint8_t *) SOCKET_MAP_KEY_LISTENING, 0);
[89e57cee]960 if (!listening_socket ||
[7c8267b]961 (listening_socket->socket_id != listening_socket_id)) {
962 fibril_rwlock_write_unlock(&tcp_globals.lock);
[fb04cba8]963 /* A shadow may remain until app hangs up */
[7c8267b]964 return tcp_release_and_return(packet, EOK /*ENOTSOCK*/);
965 }
966 listening_socket_data =
[4e5c7ba]967 (tcp_socket_data_t *) listening_socket->specific_data;
[7c8267b]968 assert(listening_socket_data);
[21580dd]969
[7c8267b]970 fibril_rwlock_write_lock(listening_socket_data->local_lock);
[21580dd]971
[7c8267b]972 socket = socket_cores_find(listening_socket_data->local_sockets,
973 socket_id);
974 if (!socket) {
[fb04cba8]975 /* Where is the socket?!? */
[7c8267b]976 fibril_rwlock_write_unlock(&tcp_globals.lock);
977 return ENOTSOCK;
978 }
[4e5c7ba]979 socket_data = (tcp_socket_data_t *) socket->specific_data;
[7c8267b]980 assert(socket_data);
[21580dd]981
[0578271]982 rc = socket_port_add(&tcp_globals.sockets, listening_port, socket,
[61bfc370]983 (uint8_t *) socket_data->addr, socket_data->addrlen);
[7c8267b]984 assert(socket == socket_port_find(&tcp_globals.sockets, listening_port,
[61bfc370]985 (uint8_t *) socket_data->addr, socket_data->addrlen));
[21580dd]986
[0578271]987// rc = socket_bind_free_port(&tcp_globals.sockets, socket,
[7c8267b]988// TCP_FREE_PORTS_START, TCP_FREE_PORTS_END,
989// tcp_globals.last_used_port);
990// tcp_globals.last_used_port = socket->port;
991 fibril_rwlock_write_unlock(&tcp_globals.lock);
[0578271]992 if (rc != EOK) {
[6b82009]993 socket_destroy(tcp_globals.net_sess, socket->socket_id,
[7c8267b]994 socket_data->local_sockets, &tcp_globals.sockets,
995 tcp_free_socket_data);
[0578271]996 return tcp_release_and_return(packet, rc);
[7c8267b]997 }
[21580dd]998
[7c8267b]999 socket_data->state = TCP_SOCKET_LISTEN;
1000 socket_data->next_incoming = ntohl(header->sequence_number) + 1;
[21580dd]1001
[fb04cba8]1002 /* Release additional packets */
[7c8267b]1003 next_packet = pq_detach(packet);
1004 if (next_packet) {
[6b82009]1005 pq_release_remote(tcp_globals.net_sess,
[7c8267b]1006 packet_get_id(next_packet));
[21580dd]1007 }
[7c8267b]1008
[fb04cba8]1009 /* Trim if longer than the header */
[0578271]1010 if (packet_get_data_length(packet) > sizeof(*header)) {
1011 rc = packet_trim(packet, 0,
1012 packet_get_data_length(packet) - sizeof(*header));
1013 if (rc != EOK) {
[6b82009]1014 socket_destroy(tcp_globals.net_sess, socket->socket_id,
[0578271]1015 socket_data->local_sockets, &tcp_globals.sockets,
1016 tcp_free_socket_data);
1017 return tcp_release_and_return(packet, rc);
1018 }
[7c8267b]1019 }
1020
1021 tcp_prepare_operation_header(socket, socket_data, header, 1, 0);
1022
[0578271]1023 rc = tcp_queue_packet(socket, socket_data, packet, 1);
1024 if (rc != EOK) {
[6b82009]1025 socket_destroy(tcp_globals.net_sess, socket->socket_id,
[7c8267b]1026 socket_data->local_sockets, &tcp_globals.sockets,
1027 tcp_free_socket_data);
[0578271]1028 return rc;
[7c8267b]1029 }
1030
1031 packet = tcp_get_packets_to_send(socket, socket_data);
1032 if (!packet) {
[6b82009]1033 socket_destroy(tcp_globals.net_sess, socket->socket_id,
[7c8267b]1034 socket_data->local_sockets, &tcp_globals.sockets,
1035 tcp_free_socket_data);
1036 return EINVAL;
1037 }
1038
1039 socket_data->state = TCP_SOCKET_SYN_RECEIVED;
1040 fibril_rwlock_write_unlock(socket_data->local_lock);
1041
[fb04cba8]1042 /* Send the packet */
[7c8267b]1043 tcp_send_packets(socket_data->device_id, packet);
1044
1045 return EOK;
[21580dd]1046}
1047
[88a1bb9]1048int tcp_process_syn_received(socket_core_t *socket,
[46d4d9f]1049 tcp_socket_data_t *socket_data, tcp_header_t *header, packet_t *packet)
[7c8267b]1050{
[88a1bb9]1051 socket_core_t *listening_socket;
[4e5c7ba]1052 tcp_socket_data_t *listening_socket_data;
[0578271]1053 int rc;
[21580dd]1054
[aadf01e]1055 assert(socket);
1056 assert(socket_data);
1057 assert(socket->specific_data == socket_data);
1058 assert(header);
1059 assert(packet);
[21580dd]1060
[0743493a]1061 if (!GET_TCP_HEADER_ACKNOWLEDGE(header))
[7c8267b]1062 return tcp_release_and_return(packet, EINVAL);
[21580dd]1063
[fb04cba8]1064 /* Process acknowledgement */
[7c8267b]1065 tcp_process_acknowledgement(socket, socket_data, header);
1066
[28a3e74]1067 socket_data->next_incoming = ntohl(header->sequence_number); /* + 1; */
[6b82009]1068 pq_release_remote(tcp_globals.net_sess, packet_get_id(packet));
[7c8267b]1069 socket_data->state = TCP_SOCKET_ESTABLISHED;
1070 listening_socket = socket_cores_find(socket_data->local_sockets,
1071 socket_data->listening_socket_id);
1072 if (listening_socket) {
1073 listening_socket_data =
[4e5c7ba]1074 (tcp_socket_data_t *) listening_socket->specific_data;
[7c8267b]1075 assert(listening_socket_data);
1076
[fb04cba8]1077 /* Queue the received packet */
[0578271]1078 rc = dyn_fifo_push(&listening_socket->accepted,
1079 (-1 * socket->socket_id), listening_socket_data->backlog);
1080 if (rc == EOK) {
[fb04cba8]1081 /* Notify the destination socket */
[6b82009]1082 async_exch_t *exch = async_exchange_begin(socket->sess);
1083 async_msg_5(exch, NET_SOCKET_ACCEPTED,
[96b02eb9]1084 (sysarg_t) listening_socket->socket_id,
[7c8267b]1085 socket_data->data_fragment_size, TCP_HEADER_SIZE,
[96b02eb9]1086 0, (sysarg_t) socket->socket_id);
[6b82009]1087 async_exchange_end(exch);
[7c8267b]1088
1089 fibril_rwlock_write_unlock(socket_data->local_lock);
1090 return EOK;
[21580dd]1091 }
[7c8267b]1092 }
[fb04cba8]1093 /* Send FIN */
[7c8267b]1094 socket_data->state = TCP_SOCKET_FIN_WAIT_1;
[21580dd]1095
[fb04cba8]1096 /* Create the notification packet */
[0578271]1097 rc = tcp_create_notification_packet(&packet, socket, socket_data, 0, 1);
1098 if (rc != EOK)
1099 return rc;
[21580dd]1100
[fb04cba8]1101 /* Send the packet */
[0578271]1102 rc = tcp_queue_packet(socket, socket_data, packet, 1);
1103 if (rc != EOK)
1104 return rc;
[21580dd]1105
[fb04cba8]1106 /* Flush packets */
[7c8267b]1107 packet = tcp_get_packets_to_send(socket, socket_data);
1108 fibril_rwlock_write_unlock(socket_data->local_lock);
1109 if (packet) {
[fb04cba8]1110 /* Send the packet */
[7c8267b]1111 tcp_send_packets(socket_data->device_id, packet);
[21580dd]1112 }
[7c8267b]1113
1114 return EOK;
[21580dd]1115}
1116
[88a1bb9]1117void tcp_process_acknowledgement(socket_core_t *socket,
[4e5c7ba]1118 tcp_socket_data_t *socket_data, tcp_header_t *header)
[7c8267b]1119{
[aadf01e]1120 size_t number;
1121 size_t length;
[46d4d9f]1122 packet_t *packet;
1123 packet_t *next;
1124 packet_t *acknowledged = NULL;
[aadf01e]1125 uint32_t old;
1126
1127 assert(socket);
1128 assert(socket_data);
1129 assert(socket->specific_data == socket_data);
1130 assert(header);
1131
[0743493a]1132 if (!GET_TCP_HEADER_ACKNOWLEDGE(header))
[7c8267b]1133 return;
1134
1135 number = ntohl(header->acknowledgement_number);
[fb04cba8]1136
1137 /* If more data acknowledged */
[7c8267b]1138 if (number != socket_data->expected) {
1139 old = socket_data->expected;
1140 if (IS_IN_INTERVAL_OVERFLOW(old, socket_data->fin_outgoing,
1141 number)) {
1142 switch (socket_data->state) {
1143 case TCP_SOCKET_FIN_WAIT_1:
1144 socket_data->state = TCP_SOCKET_FIN_WAIT_2;
1145 break;
1146 case TCP_SOCKET_LAST_ACK:
1147 case TCP_SOCKET_CLOSING:
[fb04cba8]1148 /*
1149 * FIN acknowledged - release the socket in
1150 * another fibril.
1151 */
[7c8267b]1152 tcp_prepare_timeout(tcp_release_after_timeout,
1153 socket, socket_data, 0,
1154 TCP_SOCKET_TIME_WAIT,
1155 NET_DEFAULT_TCP_TIME_WAIT_TIMEOUT, true);
1156 break;
1157 default:
1158 break;
[21580dd]1159 }
[7c8267b]1160 }
[fb04cba8]1161
1162 /* Update the treshold if higher than set */
[7c8267b]1163 if (number + ntohs(header->window) >
1164 socket_data->expected + socket_data->treshold) {
1165 socket_data->treshold = number + ntohs(header->window) -
1166 socket_data->expected;
1167 }
[fb04cba8]1168
1169 /* Set new expected sequence number */
[7c8267b]1170 socket_data->expected = number;
1171 socket_data->expected_count = 1;
1172 packet = socket_data->outgoing;
1173 while (pq_get_order(packet, &number, &length) == EOK) {
1174 if (IS_IN_INTERVAL_OVERFLOW((uint32_t) old,
1175 (uint32_t) (number + length),
1176 (uint32_t) socket_data->expected)) {
1177 next = pq_detach(packet);
1178 if (packet == socket_data->outgoing)
1179 socket_data->outgoing = next;
1180
[fb04cba8]1181 /* Add to acknowledged or release */
[7c8267b]1182 if (pq_add(&acknowledged, packet, 0, 0) != EOK)
[6b82009]1183 pq_release_remote(tcp_globals.net_sess,
[7c8267b]1184 packet_get_id(packet));
1185 packet = next;
1186 } else if (old < socket_data->expected)
1187 break;
1188 }
[fb04cba8]1189
1190 /* Release acknowledged */
[7c8267b]1191 if (acknowledged) {
[6b82009]1192 pq_release_remote(tcp_globals.net_sess,
[7c8267b]1193 packet_get_id(acknowledged));
1194 }
1195 return;
[fb04cba8]1196 /* If the same as the previous time */
[7c8267b]1197 }
[fb04cba8]1198
[7c8267b]1199 if (number == socket_data->expected) {
[fb04cba8]1200 /* Increase the counter */
[89e57cee]1201 socket_data->expected_count++;
[7c8267b]1202 if (socket_data->expected_count == TCP_FAST_RETRANSMIT_COUNT) {
1203 socket_data->expected_count = 1;
[fb04cba8]1204 /* TODO retransmit lock */
[7c8267b]1205 //tcp_retransmit_packet(socket, socket_data, number);
[21580dd]1206 }
1207 }
1208}
1209
[f1938c6]1210/** Per-connection initialization
1211 *
1212 */
1213void tl_connection(void)
1214{
1215}
1216
[89e57cee]1217/** Processes the TCP message.
1218 *
1219 * @param[in] callid The message identifier.
1220 * @param[in] call The message parameters.
1221 * @param[out] answer The message answer parameters.
1222 * @param[out] answer_count The last parameter for the actual answer in the
1223 * answer parameter.
[1bfd3d3]1224 * @return EOK on success.
1225 * @return ENOTSUP if the message is not known.
[89e57cee]1226 *
1227 * @see tcp_interface.h
1228 * @see IS_NET_TCP_MESSAGE()
1229 */
[f1938c6]1230int tl_message(ipc_callid_t callid, ipc_call_t *call,
[774e6d1a]1231 ipc_call_t *answer, size_t *answer_count)
[7c8267b]1232{
[aadf01e]1233 assert(call);
1234 assert(answer);
1235 assert(answer_count);
[6b82009]1236
[aadf01e]1237 *answer_count = 0;
[6b82009]1238
1239 async_sess_t *callback =
1240 async_callback_receive_start(EXCHANGE_SERIALIZE, call);
1241 if (callback)
1242 return tcp_process_client_messages(callback, callid, *call);
1243
[21580dd]1244 return ENOTSUP;
1245}
1246
[4e5c7ba]1247void tcp_refresh_socket_data(tcp_socket_data_t *socket_data)
[7c8267b]1248{
[aadf01e]1249 assert(socket_data);
[21580dd]1250
[aadf01e]1251 bzero(socket_data, sizeof(*socket_data));
[21580dd]1252 socket_data->state = TCP_SOCKET_INITIAL;
[609243f4]1253 socket_data->device_id = NIC_DEVICE_INVALID_ID;
[21580dd]1254 socket_data->window = NET_DEFAULT_TCP_WINDOW;
1255 socket_data->treshold = socket_data->window;
1256 socket_data->last_outgoing = TCP_INITIAL_SEQUENCE_NUMBER;
1257 socket_data->timeout = NET_DEFAULT_TCP_INITIAL_TIMEOUT;
1258 socket_data->acknowledged = socket_data->last_outgoing;
1259 socket_data->next_outgoing = socket_data->last_outgoing + 1;
1260 socket_data->expected = socket_data->next_outgoing;
1261}
1262
[4e5c7ba]1263void tcp_initialize_socket_data(tcp_socket_data_t *socket_data)
[7c8267b]1264{
[aadf01e]1265 assert(socket_data);
[21580dd]1266
[aadf01e]1267 tcp_refresh_socket_data(socket_data);
1268 fibril_mutex_initialize(&socket_data->operation.mutex);
1269 fibril_condvar_initialize(&socket_data->operation.condvar);
[ede63e4]1270 socket_data->data_fragment_size = MAX_TCP_FRAGMENT_SIZE;
[21580dd]1271}
1272
[6b82009]1273int tcp_process_client_messages(async_sess_t *sess, ipc_callid_t callid,
1274 ipc_call_t call)
[7c8267b]1275{
[aadf01e]1276 int res;
1277 socket_cores_t local_sockets;
[7c8267b]1278 struct sockaddr *addr;
[6092b56e]1279 int socket_id;
[aadf01e]1280 size_t addrlen;
[e417b96]1281 size_t size;
[aadf01e]1282 fibril_rwlock_t lock;
1283 ipc_call_t answer;
[774e6d1a]1284 size_t answer_count;
[4e5c7ba]1285 tcp_socket_data_t *socket_data;
[88a1bb9]1286 socket_core_t *socket;
[f772bc55]1287 packet_dimension_t *packet_dimension;
[21580dd]1288
1289 /*
1290 * Accept the connection
1291 * - Answer the first IPC_M_CONNECT_ME_TO call.
1292 */
[2e99277]1293 res = EOK;
1294 answer_count = 0;
[21580dd]1295
[aadf01e]1296 socket_cores_initialize(&local_sockets);
1297 fibril_rwlock_initialize(&lock);
[21580dd]1298
[79ae36dd]1299 while (true) {
[2e99277]1300
[fb04cba8]1301 /* Answer the call */
[aadf01e]1302 answer_call(callid, res, &answer, answer_count);
[fb04cba8]1303 /* Refresh data */
[aadf01e]1304 refresh_answer(&answer, &answer_count);
[fb04cba8]1305 /* Get the next call */
[aadf01e]1306 callid = async_get_call(&call);
[79ae36dd]1307
1308 if (!IPC_GET_IMETHOD(call)) {
[7c8267b]1309 res = EHANGUP;
1310 break;
[79ae36dd]1311 }
[7c8267b]1312
[79ae36dd]1313 /* Process the call */
1314 switch (IPC_GET_IMETHOD(call)) {
[7c8267b]1315 case NET_SOCKET:
1316 socket_data =
[4e5c7ba]1317 (tcp_socket_data_t *) malloc(sizeof(*socket_data));
[7c8267b]1318 if (!socket_data) {
1319 res = ENOMEM;
[21580dd]1320 break;
[7c8267b]1321 }
1322
1323 tcp_initialize_socket_data(socket_data);
1324 socket_data->local_lock = &lock;
1325 socket_data->local_sockets = &local_sockets;
1326 fibril_rwlock_write_lock(&lock);
1327 socket_id = SOCKET_GET_SOCKET_ID(call);
[6b82009]1328 res = socket_create(&local_sockets, sess,
[7c8267b]1329 socket_data, &socket_id);
1330 SOCKET_SET_SOCKET_ID(answer, socket_id);
1331 fibril_rwlock_write_unlock(&lock);
1332 if (res != EOK) {
1333 free(socket_data);
[21580dd]1334 break;
[7c8267b]1335 }
[6b82009]1336 if (tl_get_ip_packet_dimension(tcp_globals.ip_sess,
[609243f4]1337 &tcp_globals.dimensions, NIC_DEVICE_INVALID_ID,
[7c8267b]1338 &packet_dimension) == EOK) {
1339 SOCKET_SET_DATA_FRAGMENT_SIZE(answer,
1340 ((packet_dimension->content <
1341 socket_data->data_fragment_size) ?
1342 packet_dimension->content :
1343 socket_data->data_fragment_size));
1344 }
1345// SOCKET_SET_DATA_FRAGMENT_SIZE(answer, MAX_TCP_FRAGMENT_SIZE);
1346 SOCKET_SET_HEADER_SIZE(answer, TCP_HEADER_SIZE);
1347 answer_count = 3;
1348 break;
1349
1350 case NET_SOCKET_BIND:
[7880d58]1351 res = async_data_write_accept((void **) &addr, false,
1352 0, 0, 0, &addrlen);
[7c8267b]1353 if (res != EOK)
[21580dd]1354 break;
[7c8267b]1355 fibril_rwlock_write_lock(&tcp_globals.lock);
1356 fibril_rwlock_write_lock(&lock);
1357 res = socket_bind(&local_sockets, &tcp_globals.sockets,
1358 SOCKET_GET_SOCKET_ID(call), addr, addrlen,
1359 TCP_FREE_PORTS_START, TCP_FREE_PORTS_END,
1360 tcp_globals.last_used_port);
1361 if (res == EOK) {
1362 socket = socket_cores_find(&local_sockets,
1363 SOCKET_GET_SOCKET_ID(call));
1364 if (socket) {
[4e5c7ba]1365 socket_data = (tcp_socket_data_t *)
[7c8267b]1366 socket->specific_data;
1367 assert(socket_data);
1368 socket_data->state = TCP_SOCKET_LISTEN;
[21580dd]1369 }
[7c8267b]1370 }
1371 fibril_rwlock_write_unlock(&lock);
1372 fibril_rwlock_write_unlock(&tcp_globals.lock);
1373 free(addr);
1374 break;
1375
1376 case NET_SOCKET_LISTEN:
1377 fibril_rwlock_read_lock(&tcp_globals.lock);
1378// fibril_rwlock_write_lock(&tcp_globals.lock);
1379 fibril_rwlock_write_lock(&lock);
1380 res = tcp_listen_message(&local_sockets,
1381 SOCKET_GET_SOCKET_ID(call),
1382 SOCKET_GET_BACKLOG(call));
1383 fibril_rwlock_write_unlock(&lock);
1384// fibril_rwlock_write_unlock(&tcp_globals.lock);
1385 fibril_rwlock_read_unlock(&tcp_globals.lock);
1386 break;
1387
1388 case NET_SOCKET_CONNECT:
[7880d58]1389 res = async_data_write_accept((void **) &addr, false,
1390 0, 0, 0, &addrlen);
[7c8267b]1391 if (res != EOK)
[21580dd]1392 break;
[fb04cba8]1393 /*
1394 * The global lock may be released in the
1395 * tcp_connect_message() function.
1396 */
[7c8267b]1397 fibril_rwlock_write_lock(&tcp_globals.lock);
1398 fibril_rwlock_write_lock(&lock);
1399 res = tcp_connect_message(&local_sockets,
1400 SOCKET_GET_SOCKET_ID(call), addr, addrlen);
1401 if (res != EOK) {
[aadf01e]1402 fibril_rwlock_write_unlock(&lock);
[7c8267b]1403 fibril_rwlock_write_unlock(&tcp_globals.lock);
1404 free(addr);
1405 }
1406 break;
1407
1408 case NET_SOCKET_ACCEPT:
1409 fibril_rwlock_read_lock(&tcp_globals.lock);
1410 fibril_rwlock_write_lock(&lock);
1411 res = tcp_accept_message(&local_sockets,
1412 SOCKET_GET_SOCKET_ID(call),
1413 SOCKET_GET_NEW_SOCKET_ID(call), &size, &addrlen);
1414 SOCKET_SET_DATA_FRAGMENT_SIZE(answer, size);
1415 fibril_rwlock_write_unlock(&lock);
1416 fibril_rwlock_read_unlock(&tcp_globals.lock);
1417 if (res > 0) {
1418 SOCKET_SET_SOCKET_ID(answer, res);
1419 SOCKET_SET_ADDRESS_LENGTH(answer, addrlen);
1420 answer_count = 3;
1421 }
1422 break;
1423
1424 case NET_SOCKET_SEND:
1425 fibril_rwlock_read_lock(&tcp_globals.lock);
1426 fibril_rwlock_write_lock(&lock);
1427 res = tcp_send_message(&local_sockets,
1428 SOCKET_GET_SOCKET_ID(call),
1429 SOCKET_GET_DATA_FRAGMENTS(call), &size,
1430 SOCKET_GET_FLAGS(call));
1431 SOCKET_SET_DATA_FRAGMENT_SIZE(answer, size);
1432 if (res != EOK) {
[aadf01e]1433 fibril_rwlock_write_unlock(&lock);
1434 fibril_rwlock_read_unlock(&tcp_globals.lock);
[7c8267b]1435 } else {
1436 answer_count = 2;
1437 }
1438 break;
1439
1440 case NET_SOCKET_SENDTO:
[7880d58]1441 res = async_data_write_accept((void **) &addr, false,
1442 0, 0, 0, &addrlen);
[7c8267b]1443 if (res != EOK)
[21580dd]1444 break;
[7c8267b]1445 fibril_rwlock_read_lock(&tcp_globals.lock);
1446 fibril_rwlock_write_lock(&lock);
1447 res = tcp_send_message(&local_sockets,
1448 SOCKET_GET_SOCKET_ID(call),
1449 SOCKET_GET_DATA_FRAGMENTS(call), &size,
1450 SOCKET_GET_FLAGS(call));
1451 SOCKET_SET_DATA_FRAGMENT_SIZE(answer, size);
1452 if (res != EOK) {
[aadf01e]1453 fibril_rwlock_write_unlock(&lock);
1454 fibril_rwlock_read_unlock(&tcp_globals.lock);
[7c8267b]1455 } else {
1456 answer_count = 2;
1457 }
1458 free(addr);
1459 break;
1460
1461 case NET_SOCKET_RECV:
1462 fibril_rwlock_read_lock(&tcp_globals.lock);
1463 fibril_rwlock_write_lock(&lock);
1464 res = tcp_recvfrom_message(&local_sockets,
1465 SOCKET_GET_SOCKET_ID(call), SOCKET_GET_FLAGS(call),
1466 NULL);
1467 fibril_rwlock_write_unlock(&lock);
1468 fibril_rwlock_read_unlock(&tcp_globals.lock);
1469 if (res > 0) {
1470 SOCKET_SET_READ_DATA_LENGTH(answer, res);
1471 answer_count = 1;
1472 res = EOK;
1473 }
1474 break;
1475
1476 case NET_SOCKET_RECVFROM:
1477 fibril_rwlock_read_lock(&tcp_globals.lock);
1478 fibril_rwlock_write_lock(&lock);
1479 res = tcp_recvfrom_message(&local_sockets,
1480 SOCKET_GET_SOCKET_ID(call), SOCKET_GET_FLAGS(call),
1481 &addrlen);
1482 fibril_rwlock_write_unlock(&lock);
1483 fibril_rwlock_read_unlock(&tcp_globals.lock);
1484 if (res > 0) {
1485 SOCKET_SET_READ_DATA_LENGTH(answer, res);
1486 SOCKET_SET_ADDRESS_LENGTH(answer, addrlen);
1487 answer_count = 3;
1488 res = EOK;
1489 }
1490 break;
1491
1492 case NET_SOCKET_CLOSE:
1493 fibril_rwlock_write_lock(&tcp_globals.lock);
1494 fibril_rwlock_write_lock(&lock);
1495 res = tcp_close_message(&local_sockets,
1496 SOCKET_GET_SOCKET_ID(call));
1497 if (res != EOK) {
1498 fibril_rwlock_write_unlock(&lock);
1499 fibril_rwlock_write_unlock(&tcp_globals.lock);
1500 }
1501 break;
1502
1503 case NET_SOCKET_GETSOCKOPT:
1504 case NET_SOCKET_SETSOCKOPT:
1505 default:
1506 res = ENOTSUP;
1507 break;
[21580dd]1508 }
1509 }
1510
[6b82009]1511 /* Release the application session */
1512 async_hangup(sess);
[a8a13d0]1513
[21580dd]1514 printf("release\n");
[fb04cba8]1515 /* Release all local sockets */
[6b82009]1516 socket_cores_release(tcp_globals.net_sess, &local_sockets,
[7c8267b]1517 &tcp_globals.sockets, tcp_free_socket_data);
[21580dd]1518
1519 return EOK;
1520}
1521
[7c8267b]1522int tcp_timeout(void *data)
1523{
[f772bc55]1524 tcp_timeout_t *timeout = data;
[aadf01e]1525 int keep_write_lock = false;
[88a1bb9]1526 socket_core_t *socket;
[4e5c7ba]1527 tcp_socket_data_t *socket_data;
[21580dd]1528
[aadf01e]1529 assert(timeout);
[21580dd]1530
[fb04cba8]1531 /* Sleep the given timeout */
[aadf01e]1532 async_usleep(timeout->timeout);
[fb04cba8]1533 /* Lock the globals */
[7c8267b]1534 if (timeout->globals_read_only)
[aadf01e]1535 fibril_rwlock_read_lock(&tcp_globals.lock);
[7c8267b]1536 else
[aadf01e]1537 fibril_rwlock_write_lock(&tcp_globals.lock);
[7c8267b]1538
[fb04cba8]1539 /* Find the pending operation socket */
[7c8267b]1540 socket = socket_port_find(&tcp_globals.sockets, timeout->port,
1541 timeout->key, timeout->key_length);
[89e57cee]1542 if (!socket || (socket->socket_id != timeout->socket_id))
[7c8267b]1543 goto out;
1544
[4e5c7ba]1545 socket_data = (tcp_socket_data_t *) socket->specific_data;
[7c8267b]1546 assert(socket_data);
1547 if (socket_data->local_sockets != timeout->local_sockets)
1548 goto out;
1549
1550 fibril_rwlock_write_lock(socket_data->local_lock);
1551 if (timeout->sequence_number) {
[fb04cba8]1552 /* Increase the timeout counter */
[89e57cee]1553 socket_data->timeout_count++;
[7c8267b]1554 if (socket_data->timeout_count == TCP_MAX_TIMEOUTS) {
[fb04cba8]1555 /* TODO release as connection lost */
[7c8267b]1556 //tcp_refresh_socket_data(socket_data);
1557 fibril_rwlock_write_unlock(socket_data->local_lock);
1558 } else {
[fb04cba8]1559 /* Retransmit */
[7c8267b]1560// tcp_retransmit_packet(socket,
1561// socket_data, timeout->sequence_number);
1562 fibril_rwlock_write_unlock(socket_data->local_lock);
[21580dd]1563 }
[7c8267b]1564 } else {
1565 fibril_mutex_lock(&socket_data->operation.mutex);
[fb04cba8]1566 /* Set the timeout operation result if state not changed */
[7c8267b]1567 if (socket_data->state == timeout->state) {
1568 socket_data->operation.result = ETIMEOUT;
[fb04cba8]1569
1570 /* Notify the main fibril */
[7c8267b]1571 fibril_condvar_signal(&socket_data->operation.condvar);
[fb04cba8]1572
1573 /* Keep the global write lock */
[7c8267b]1574 keep_write_lock = true;
1575 } else {
[fb04cba8]1576 /*
1577 * Operation is ok, do nothing.
1578 * Unlocking from now on, so the unlocking
1579 * order does not matter.
1580 */
[7c8267b]1581 fibril_rwlock_write_unlock(socket_data->local_lock);
1582 }
1583 fibril_mutex_unlock(&socket_data->operation.mutex);
[21580dd]1584 }
[7c8267b]1585
1586out:
[fb04cba8]1587 /* Unlock only if no socket */
[7c8267b]1588 if (timeout->globals_read_only)
[aadf01e]1589 fibril_rwlock_read_unlock(&tcp_globals.lock);
[7c8267b]1590 else if (!keep_write_lock)
[fb04cba8]1591 /* Release if not desired */
[aadf01e]1592 fibril_rwlock_write_unlock(&tcp_globals.lock);
[7c8267b]1593
[fb04cba8]1594 /* Release the timeout structure */
[aadf01e]1595 free(timeout);
[21580dd]1596 return EOK;
1597}
1598
[7c8267b]1599int tcp_release_after_timeout(void *data)
1600{
[f772bc55]1601 tcp_timeout_t *timeout = data;
[88a1bb9]1602 socket_core_t *socket;
[4e5c7ba]1603 tcp_socket_data_t *socket_data;
[7c8267b]1604 fibril_rwlock_t *local_lock;
[21580dd]1605
[aadf01e]1606 assert(timeout);
[21580dd]1607
[fb04cba8]1608 /* Sleep the given timeout */
[aadf01e]1609 async_usleep(timeout->timeout);
[fb04cba8]1610
1611 /* Lock the globals */
[aadf01e]1612 fibril_rwlock_write_lock(&tcp_globals.lock);
[fb04cba8]1613
1614 /* Find the pending operation socket */
[7c8267b]1615 socket = socket_port_find(&tcp_globals.sockets, timeout->port,
1616 timeout->key, timeout->key_length);
[fb04cba8]1617
[7c8267b]1618 if (socket && (socket->socket_id == timeout->socket_id)) {
[4e5c7ba]1619 socket_data = (tcp_socket_data_t *) socket->specific_data;
[aadf01e]1620 assert(socket_data);
[7c8267b]1621 if (socket_data->local_sockets == timeout->local_sockets) {
[21580dd]1622 local_lock = socket_data->local_lock;
[aadf01e]1623 fibril_rwlock_write_lock(local_lock);
[6b82009]1624 socket_destroy(tcp_globals.net_sess,
[7c8267b]1625 timeout->socket_id, timeout->local_sockets,
1626 &tcp_globals.sockets, tcp_free_socket_data);
[aadf01e]1627 fibril_rwlock_write_unlock(local_lock);
[21580dd]1628 }
1629 }
[fb04cba8]1630
1631 /* Unlock the globals */
[aadf01e]1632 fibril_rwlock_write_unlock(&tcp_globals.lock);
[fb04cba8]1633
1634 /* Release the timeout structure */
[aadf01e]1635 free(timeout);
[fb04cba8]1636
[21580dd]1637 return EOK;
1638}
1639
[88a1bb9]1640void tcp_retransmit_packet(socket_core_t *socket, tcp_socket_data_t *
[fb04cba8]1641 socket_data, size_t sequence_number)
[7c8267b]1642{
[46d4d9f]1643 packet_t *packet;
1644 packet_t *copy;
[aadf01e]1645 size_t data_length;
[21580dd]1646
[aadf01e]1647 assert(socket);
1648 assert(socket_data);
1649 assert(socket->specific_data == socket_data);
[21580dd]1650
[fb04cba8]1651 /* Sent packet? */
[aadf01e]1652 packet = pq_find(socket_data->outgoing, sequence_number);
[7c8267b]1653 if (packet) {
[aadf01e]1654 pq_get_order(packet, NULL, &data_length);
[7c8267b]1655 copy = tcp_prepare_copy(socket, socket_data, packet,
1656 data_length, sequence_number);
[aadf01e]1657 fibril_rwlock_write_unlock(socket_data->local_lock);
[7c8267b]1658// printf("r send %d\n", packet_get_id(packet));
1659 if (copy)
[aadf01e]1660 tcp_send_packets(socket_data->device_id, copy);
[7c8267b]1661 } else {
[aadf01e]1662 fibril_rwlock_write_unlock(socket_data->local_lock);
[21580dd]1663 }
1664}
1665
[aaa3f33a]1666int tcp_listen_message(socket_cores_t *local_sockets, int socket_id,
[fb04cba8]1667 int backlog)
[7c8267b]1668{
[88a1bb9]1669 socket_core_t *socket;
[4e5c7ba]1670 tcp_socket_data_t *socket_data;
[21580dd]1671
[aadf01e]1672 assert(local_sockets);
[21580dd]1673
[7c8267b]1674 if (backlog < 0)
[aadf01e]1675 return EINVAL;
[7c8267b]1676
[fb04cba8]1677 /* Find the socket */
[aadf01e]1678 socket = socket_cores_find(local_sockets, socket_id);
[7c8267b]1679 if (!socket)
[aadf01e]1680 return ENOTSOCK;
[7c8267b]1681
[fb04cba8]1682 /* Get the socket specific data */
[4e5c7ba]1683 socket_data = (tcp_socket_data_t *) socket->specific_data;
[aadf01e]1684 assert(socket_data);
[fb04cba8]1685
1686 /* Set the backlog */
[21580dd]1687 socket_data->backlog = backlog;
[7c8267b]1688
[21580dd]1689 return EOK;
1690}
1691
[aaa3f33a]1692int tcp_connect_message(socket_cores_t *local_sockets, int socket_id,
[7c8267b]1693 struct sockaddr *addr, socklen_t addrlen)
1694{
[88a1bb9]1695 socket_core_t *socket;
[0578271]1696 int rc;
[21580dd]1697
[aadf01e]1698 assert(local_sockets);
1699 assert(addr);
1700 assert(addrlen > 0);
[21580dd]1701
[fb04cba8]1702 /* Find the socket */
[aadf01e]1703 socket = socket_cores_find(local_sockets, socket_id);
[7c8267b]1704 if (!socket)
[aadf01e]1705 return ENOTSOCK;
[7c8267b]1706
[0578271]1707 rc = tcp_connect_core(socket, local_sockets, addr, addrlen);
1708 if (rc != EOK) {
[aadf01e]1709 tcp_free_socket_data(socket);
[fb04cba8]1710 /* Unbind if bound */
[7c8267b]1711 if (socket->port > 0) {
1712 socket_ports_exclude(&tcp_globals.sockets,
[5fe7692]1713 socket->port, free);
[21580dd]1714 socket->port = 0;
1715 }
1716 }
[0578271]1717 return rc;
[21580dd]1718}
1719
[aaa3f33a]1720int tcp_connect_core(socket_core_t *socket, socket_cores_t *local_sockets,
[7c8267b]1721 struct sockaddr *addr, socklen_t addrlen)
1722{
[4e5c7ba]1723 tcp_socket_data_t *socket_data;
[46d4d9f]1724 packet_t *packet;
[0578271]1725 int rc;
[21580dd]1726
[aadf01e]1727 assert(socket);
1728 assert(addr);
1729 assert(addrlen > 0);
[21580dd]1730
[fb04cba8]1731 /* Get the socket specific data */
[4e5c7ba]1732 socket_data = (tcp_socket_data_t *) socket->specific_data;
[aadf01e]1733 assert(socket_data);
1734 assert(socket->specific_data == socket_data);
[7c8267b]1735 if ((socket_data->state != TCP_SOCKET_INITIAL) &&
1736 ((socket_data->state != TCP_SOCKET_LISTEN) ||
1737 (socket->port <= 0)))
[21580dd]1738 return EINVAL;
[7c8267b]1739
[fb04cba8]1740 /* Get the destination port */
[0578271]1741 rc = tl_get_address_port(addr, addrlen, &socket_data->dest_port);
1742 if (rc != EOK)
1743 return rc;
1744
[7c8267b]1745 if (socket->port <= 0) {
[fb04cba8]1746 /* Try to find a free port */
[0578271]1747 rc = socket_bind_free_port(&tcp_globals.sockets, socket,
1748 TCP_FREE_PORTS_START, TCP_FREE_PORTS_END,
1749 tcp_globals.last_used_port);
1750 if (rc != EOK)
1751 return rc;
[fb04cba8]1752 /* Set the next port as the search starting port number */
[21580dd]1753 tcp_globals.last_used_port = socket->port;
1754 }
[7c8267b]1755
[6b82009]1756 rc = ip_get_route_req(tcp_globals.ip_sess, IPPROTO_TCP,
[7c8267b]1757 addr, addrlen, &socket_data->device_id,
[0578271]1758 &socket_data->pseudo_header, &socket_data->headerlen);
1759 if (rc != EOK)
1760 return rc;
[21580dd]1761
[fb04cba8]1762 /* Create the notification packet */
[0578271]1763 rc = tcp_create_notification_packet(&packet, socket, socket_data, 1, 0);
1764 if (rc != EOK)
1765 return rc;
[21580dd]1766
[fb04cba8]1767 /* Unlock the globals and wait for an operation */
[aadf01e]1768 fibril_rwlock_write_unlock(&tcp_globals.lock);
[21580dd]1769
1770 socket_data->addr = addr;
1771 socket_data->addrlen = addrlen;
[fb04cba8]1772
1773 /* Send the packet */
[0578271]1774
1775 if (((rc = tcp_queue_packet(socket, socket_data, packet, 1)) != EOK) ||
1776 ((rc = tcp_prepare_timeout(tcp_timeout, socket, socket_data, 0,
1777 TCP_SOCKET_INITIAL, NET_DEFAULT_TCP_INITIAL_TIMEOUT, false)) !=
1778 EOK)) {
[21580dd]1779 socket_data->addr = NULL;
1780 socket_data->addrlen = 0;
[aadf01e]1781 fibril_rwlock_write_lock(&tcp_globals.lock);
[7c8267b]1782 } else {
[aadf01e]1783 packet = tcp_get_packets_to_send(socket, socket_data);
[7c8267b]1784 if (packet) {
[aadf01e]1785 fibril_mutex_lock(&socket_data->operation.mutex);
1786 fibril_rwlock_write_unlock(socket_data->local_lock);
[fb04cba8]1787
[3ac66f69]1788 socket_data->state = TCP_SOCKET_SYN_SENT;
1789
[fb04cba8]1790 /* Send the packet */
[aadf01e]1791 tcp_send_packets(socket_data->device_id, packet);
[7c8267b]1792
[fb04cba8]1793 /* Wait for a reply */
[7c8267b]1794 fibril_condvar_wait(&socket_data->operation.condvar,
1795 &socket_data->operation.mutex);
[0578271]1796 rc = socket_data->operation.result;
1797 if (rc != EOK) {
[21580dd]1798 socket_data->addr = NULL;
1799 socket_data->addrlen = 0;
1800 }
[7c8267b]1801 } else {
[21580dd]1802 socket_data->addr = NULL;
1803 socket_data->addrlen = 0;
[0578271]1804 rc = EINTR;
[21580dd]1805 }
1806 }
1807
[aadf01e]1808 fibril_mutex_unlock(&socket_data->operation.mutex);
[0578271]1809 return rc;
[21580dd]1810}
1811
[88a1bb9]1812int tcp_queue_prepare_packet(socket_core_t *socket,
[46d4d9f]1813 tcp_socket_data_t *socket_data, packet_t *packet, size_t data_length)
[7c8267b]1814{
[4e5c7ba]1815 tcp_header_t *header;
[0578271]1816 int rc;
[21580dd]1817
[aadf01e]1818 assert(socket);
1819 assert(socket_data);
1820 assert(socket->specific_data == socket_data);
[21580dd]1821
[fb04cba8]1822 /* Get TCP header */
[4e5c7ba]1823 header = (tcp_header_t *) packet_get_data(packet);
[7c8267b]1824 if (!header)
[aadf01e]1825 return NO_DATA;
[7c8267b]1826
[aadf01e]1827 header->destination_port = htons(socket_data->dest_port);
1828 header->source_port = htons(socket->port);
1829 header->sequence_number = htonl(socket_data->next_outgoing);
[7c8267b]1830
[0578271]1831 rc = packet_set_addr(packet, NULL, (uint8_t *) socket_data->addr,
1832 socket_data->addrlen);
1833 if (rc != EOK)
[aadf01e]1834 return tcp_release_and_return(packet, EINVAL);
[7c8267b]1835
[fb04cba8]1836 /* Remember the outgoing FIN */
[0743493a]1837 if (GET_TCP_HEADER_FINALIZE(header))
[21580dd]1838 socket_data->fin_outgoing = socket_data->next_outgoing;
[7c8267b]1839
[21580dd]1840 return EOK;
1841}
1842
[88a1bb9]1843int tcp_queue_packet(socket_core_t *socket, tcp_socket_data_t *socket_data,
[46d4d9f]1844 packet_t *packet, size_t data_length)
[7c8267b]1845{
[0578271]1846 int rc;
[21580dd]1847
[aadf01e]1848 assert(socket);
1849 assert(socket_data);
1850 assert(socket->specific_data == socket_data);
[21580dd]1851
[0578271]1852 rc = tcp_queue_prepare_packet(socket, socket_data, packet, data_length);
1853 if (rc != EOK)
1854 return rc;
[21580dd]1855
[0578271]1856 rc = pq_add(&socket_data->outgoing, packet, socket_data->next_outgoing,
1857 data_length);
1858 if (rc != EOK)
1859 return tcp_release_and_return(packet, rc);
[7c8267b]1860
[21580dd]1861 socket_data->next_outgoing += data_length;
1862 return EOK;
1863}
1864
[46d4d9f]1865packet_t *tcp_get_packets_to_send(socket_core_t *socket, tcp_socket_data_t *
[fb04cba8]1866 socket_data)
[7c8267b]1867{
[46d4d9f]1868 packet_t *packet;
1869 packet_t *copy;
1870 packet_t *sending = NULL;
1871 packet_t *previous = NULL;
[aadf01e]1872 size_t data_length;
[0578271]1873 int rc;
[21580dd]1874
[aadf01e]1875 assert(socket);
1876 assert(socket_data);
1877 assert(socket->specific_data == socket_data);
[21580dd]1878
[aadf01e]1879 packet = pq_find(socket_data->outgoing, socket_data->last_outgoing + 1);
[7c8267b]1880 while (packet) {
[aadf01e]1881 pq_get_order(packet, NULL, &data_length);
[7c8267b]1882
[fb04cba8]1883 /*
1884 * Send only if fits into the window, respecting the possible
1885 * overflow.
1886 */
[7c8267b]1887 if (!IS_IN_INTERVAL_OVERFLOW(
1888 (uint32_t) socket_data->last_outgoing,
1889 (uint32_t) (socket_data->last_outgoing + data_length),
1890 (uint32_t) (socket_data->expected + socket_data->treshold)))
[21580dd]1891 break;
[7c8267b]1892
1893 copy = tcp_prepare_copy(socket, socket_data, packet,
1894 data_length, socket_data->last_outgoing + 1);
1895 if (!copy)
1896 return sending;
1897
1898 if (!sending) {
1899 sending = copy;
[0578271]1900 } else {
1901 rc = pq_insert_after(previous, copy);
1902 if (rc != EOK) {
[6b82009]1903 pq_release_remote(tcp_globals.net_sess,
[0578271]1904 packet_get_id(copy));
1905 return sending;
1906 }
[7c8267b]1907 }
1908
1909 previous = copy;
1910 packet = pq_next(packet);
[fb04cba8]1911
1912 /* Overflow occurred? */
[89e57cee]1913 if (!packet &&
[7c8267b]1914 (socket_data->last_outgoing > socket_data->next_outgoing)) {
1915 printf("gpts overflow\n");
[fb04cba8]1916 /* Continue from the beginning */
[7c8267b]1917 packet = socket_data->outgoing;
[21580dd]1918 }
[7c8267b]1919 socket_data->last_outgoing += data_length;
[21580dd]1920 }
[7c8267b]1921
[21580dd]1922 return sending;
1923}
1924
[46d4d9f]1925packet_t *tcp_send_prepare_packet(socket_core_t *socket, tcp_socket_data_t *
1926 socket_data, packet_t *packet, size_t data_length, size_t sequence_number)
[7c8267b]1927{
[4e5c7ba]1928 tcp_header_t *header;
[aadf01e]1929 uint32_t checksum;
[0578271]1930 int rc;
[21580dd]1931
[aadf01e]1932 assert(socket);
1933 assert(socket_data);
1934 assert(socket->specific_data == socket_data);
[21580dd]1935
[fb04cba8]1936 /* Adjust the pseudo header */
[0578271]1937 rc = ip_client_set_pseudo_header_data_length(socket_data->pseudo_header,
1938 socket_data->headerlen, packet_get_data_length(packet));
1939 if (rc != EOK) {
[6b82009]1940 pq_release_remote(tcp_globals.net_sess, packet_get_id(packet));
[21580dd]1941 return NULL;
1942 }
1943
[fb04cba8]1944 /* Get the header */
[4e5c7ba]1945 header = (tcp_header_t *) packet_get_data(packet);
[7c8267b]1946 if (!header) {
[6b82009]1947 pq_release_remote(tcp_globals.net_sess, packet_get_id(packet));
[21580dd]1948 return NULL;
1949 }
[aadf01e]1950 assert(ntohl(header->sequence_number) == sequence_number);
[21580dd]1951
[fb04cba8]1952 /* Adjust the header */
[7c8267b]1953 if (socket_data->next_incoming) {
1954 header->acknowledgement_number =
1955 htonl(socket_data->next_incoming);
[0743493a]1956 SET_TCP_HEADER_ACKNOWLEDGE(header, 1);
[21580dd]1957 }
[aadf01e]1958 header->window = htons(socket_data->window);
[21580dd]1959
[fb04cba8]1960 /* Checksum */
[21580dd]1961 header->checksum = 0;
[7c8267b]1962 checksum = compute_checksum(0, socket_data->pseudo_header,
1963 socket_data->headerlen);
[0578271]1964 checksum = compute_checksum(checksum,
1965 (uint8_t *) packet_get_data(packet),
[7c8267b]1966 packet_get_data_length(packet));
[aadf01e]1967 header->checksum = htons(flip_checksum(compact_checksum(checksum)));
[7c8267b]1968
[fb04cba8]1969 /* Prepare the packet */
[0578271]1970 rc = ip_client_prepare_packet(packet, IPPROTO_TCP, 0, 0, 0, 0);
1971 if (rc != EOK) {
[6b82009]1972 pq_release_remote(tcp_globals.net_sess, packet_get_id(packet));
[0578271]1973 return NULL;
1974 }
[fb04cba8]1975
[0578271]1976 rc = tcp_prepare_timeout(tcp_timeout, socket, socket_data,
1977 sequence_number, socket_data->state, socket_data->timeout, true);
1978 if (rc != EOK) {
[6b82009]1979 pq_release_remote(tcp_globals.net_sess, packet_get_id(packet));
[21580dd]1980 return NULL;
1981 }
[7c8267b]1982
[21580dd]1983 return packet;
1984}
1985
[46d4d9f]1986packet_t *tcp_prepare_copy(socket_core_t *socket, tcp_socket_data_t *
1987 socket_data, packet_t *packet, size_t data_length, size_t sequence_number)
[7c8267b]1988{
[46d4d9f]1989 packet_t *copy;
[21580dd]1990
[aadf01e]1991 assert(socket);
1992 assert(socket_data);
1993 assert(socket->specific_data == socket_data);
[21580dd]1994
[fb04cba8]1995 /* Make a copy of the packet */
[6b82009]1996 copy = packet_get_copy(tcp_globals.net_sess, packet);
[7c8267b]1997 if (!copy)
[aadf01e]1998 return NULL;
[21580dd]1999
[7c8267b]2000 return tcp_send_prepare_packet(socket, socket_data, copy, data_length,
2001 sequence_number);
[21580dd]2002}
2003
[609243f4]2004void tcp_send_packets(nic_device_id_t device_id, packet_t *packet)
[7c8267b]2005{
[46d4d9f]2006 packet_t *next;
[21580dd]2007
[7c8267b]2008 while (packet) {
[aadf01e]2009 next = pq_detach(packet);
[6b82009]2010 ip_send_msg(tcp_globals.ip_sess, device_id, packet,
[7c8267b]2011 SERVICE_TCP, 0);
[21580dd]2012 packet = next;
2013 }
2014}
2015
[88a1bb9]2016void tcp_prepare_operation_header(socket_core_t *socket,
[4e5c7ba]2017 tcp_socket_data_t *socket_data, tcp_header_t *header, int synchronize,
[7c8267b]2018 int finalize)
2019{
[aadf01e]2020 assert(socket);
2021 assert(socket_data);
2022 assert(socket->specific_data == socket_data);
2023 assert(header);
[21580dd]2024
[aadf01e]2025 bzero(header, sizeof(*header));
2026 header->source_port = htons(socket->port);
2027 header->source_port = htons(socket_data->dest_port);
[0743493a]2028 SET_TCP_HEADER_LENGTH(header,
2029 TCP_COMPUTE_HEADER_LENGTH(sizeof(*header)));
2030 SET_TCP_HEADER_SYNCHRONIZE(header, synchronize);
2031 SET_TCP_HEADER_FINALIZE(header, finalize);
[21580dd]2032}
2033
[fb04cba8]2034int tcp_prepare_timeout(int (*timeout_function)(void *tcp_timeout_t),
[88a1bb9]2035 socket_core_t *socket, tcp_socket_data_t *socket_data,
[7c8267b]2036 size_t sequence_number, tcp_socket_state_t state, suseconds_t timeout,
2037 int globals_read_only)
2038{
[f772bc55]2039 tcp_timeout_t *operation_timeout;
[aadf01e]2040 fid_t fibril;
[21580dd]2041
[aadf01e]2042 assert(socket);
2043 assert(socket_data);
2044 assert(socket->specific_data == socket_data);
[21580dd]2045
[fb04cba8]2046 /* Prepare the timeout with key bundle structure */
[7c8267b]2047 operation_timeout = malloc(sizeof(*operation_timeout) +
2048 socket->key_length + 1);
2049 if (!operation_timeout)
[aadf01e]2050 return ENOMEM;
[7c8267b]2051
[aadf01e]2052 bzero(operation_timeout, sizeof(*operation_timeout));
[21580dd]2053 operation_timeout->globals_read_only = globals_read_only;
2054 operation_timeout->port = socket->port;
2055 operation_timeout->local_sockets = socket_data->local_sockets;
2056 operation_timeout->socket_id = socket->socket_id;
2057 operation_timeout->timeout = timeout;
2058 operation_timeout->sequence_number = sequence_number;
2059 operation_timeout->state = state;
2060
[fb04cba8]2061 /* Copy the key */
[61bfc370]2062 operation_timeout->key = ((uint8_t *) operation_timeout) +
[7c8267b]2063 sizeof(*operation_timeout);
[21580dd]2064 operation_timeout->key_length = socket->key_length;
[aadf01e]2065 memcpy(operation_timeout->key, socket->key, socket->key_length);
2066 operation_timeout->key[operation_timeout->key_length] = '\0';
[21580dd]2067
[fb04cba8]2068 /* Prepare the timeouting thread */
[aadf01e]2069 fibril = fibril_create(timeout_function, operation_timeout);
[7c8267b]2070 if (!fibril) {
[aadf01e]2071 free(operation_timeout);
[be942bc]2072 return ENOMEM;
[21580dd]2073 }
[be942bc]2074
[7c8267b]2075// fibril_mutex_lock(&socket_data->operation.mutex);
[fb04cba8]2076 /* Start the timeout fibril */
[aadf01e]2077 fibril_add_ready(fibril);
[21580dd]2078 //socket_data->state = state;
2079 return EOK;
2080}
2081
[aaa3f33a]2082int tcp_recvfrom_message(socket_cores_t *local_sockets, int socket_id,
[fb04cba8]2083 int flags, size_t *addrlen)
[7c8267b]2084{
[88a1bb9]2085 socket_core_t *socket;
[4e5c7ba]2086 tcp_socket_data_t *socket_data;
[aadf01e]2087 int packet_id;
[46d4d9f]2088 packet_t *packet;
[aadf01e]2089 size_t length;
[0578271]2090 int rc;
[21580dd]2091
[aadf01e]2092 assert(local_sockets);
[21580dd]2093
[fb04cba8]2094 /* Find the socket */
[aadf01e]2095 socket = socket_cores_find(local_sockets, socket_id);
[7c8267b]2096 if (!socket)
[aadf01e]2097 return ENOTSOCK;
[7c8267b]2098
[fb04cba8]2099 /* Get the socket specific data */
[7c8267b]2100 if (!socket->specific_data)
[aadf01e]2101 return NO_DATA;
[7c8267b]2102
[4e5c7ba]2103 socket_data = (tcp_socket_data_t *) socket->specific_data;
[21580dd]2104
[fb04cba8]2105 /* Check state */
[7c8267b]2106 if ((socket_data->state != TCP_SOCKET_ESTABLISHED) &&
2107 (socket_data->state != TCP_SOCKET_CLOSE_WAIT))
[21580dd]2108 return ENOTCONN;
2109
[fb04cba8]2110 /* Send the source address if desired */
[7c8267b]2111 if (addrlen) {
[0578271]2112 rc = data_reply(socket_data->addr, socket_data->addrlen);
2113 if (rc != EOK)
2114 return rc;
[aadf01e]2115 *addrlen = socket_data->addrlen;
[21580dd]2116 }
2117
[fb04cba8]2118 /* Get the next received packet */
[aadf01e]2119 packet_id = dyn_fifo_value(&socket->received);
[7c8267b]2120 if (packet_id < 0)
[aadf01e]2121 return NO_DATA;
[7c8267b]2122
[6b82009]2123 rc = packet_translate_remote(tcp_globals.net_sess, &packet, packet_id);
[0578271]2124 if (rc != EOK)
2125 return rc;
[21580dd]2126
[fb04cba8]2127 /* Reply the packets */
[0578271]2128 rc = socket_reply_packets(packet, &length);
2129 if (rc != EOK)
2130 return rc;
[21580dd]2131
[fb04cba8]2132 /* Release the packet */
[aadf01e]2133 dyn_fifo_pop(&socket->received);
[6b82009]2134 pq_release_remote(tcp_globals.net_sess, packet_get_id(packet));
[fb04cba8]2135
2136 /* Return the total length */
[aadf01e]2137 return (int) length;
[21580dd]2138}
2139
[aaa3f33a]2140int tcp_send_message(socket_cores_t *local_sockets, int socket_id,
[fb04cba8]2141 int fragments, size_t *data_fragment_size, int flags)
[7c8267b]2142{
[88a1bb9]2143 socket_core_t *socket;
[4e5c7ba]2144 tcp_socket_data_t *socket_data;
[f772bc55]2145 packet_dimension_t *packet_dimension;
[46d4d9f]2146 packet_t *packet;
[aadf01e]2147 size_t total_length;
[4e5c7ba]2148 tcp_header_t *header;
[aadf01e]2149 int index;
2150 int result;
[0578271]2151 int rc;
[21580dd]2152
[aadf01e]2153 assert(local_sockets);
2154 assert(data_fragment_size);
[21580dd]2155
[fb04cba8]2156 /* Find the socket */
[aadf01e]2157 socket = socket_cores_find(local_sockets, socket_id);
[7c8267b]2158 if (!socket)
[aadf01e]2159 return ENOTSOCK;
[7c8267b]2160
[fb04cba8]2161 /* Get the socket specific data */
[7c8267b]2162 if (!socket->specific_data)
[aadf01e]2163 return NO_DATA;
[7c8267b]2164
[4e5c7ba]2165 socket_data = (tcp_socket_data_t *) socket->specific_data;
[21580dd]2166
[fb04cba8]2167 /* Check state */
[7c8267b]2168 if ((socket_data->state != TCP_SOCKET_ESTABLISHED) &&
2169 (socket_data->state != TCP_SOCKET_CLOSE_WAIT))
[21580dd]2170 return ENOTCONN;
2171
[6b82009]2172 rc = tl_get_ip_packet_dimension(tcp_globals.ip_sess,
[0578271]2173 &tcp_globals.dimensions, socket_data->device_id, &packet_dimension);
2174 if (rc != EOK)
2175 return rc;
[21580dd]2176
[7c8267b]2177 *data_fragment_size =
2178 ((packet_dimension->content < socket_data->data_fragment_size) ?
2179 packet_dimension->content : socket_data->data_fragment_size);
[21580dd]2180
[89e57cee]2181 for (index = 0; index < fragments; index++) {
[fb04cba8]2182 /* Read the data fragment */
[6b82009]2183 result = tl_socket_read_packet_data(tcp_globals.net_sess,
[7c8267b]2184 &packet, TCP_HEADER_SIZE, packet_dimension,
2185 socket_data->addr, socket_data->addrlen);
2186 if (result < 0)
[aadf01e]2187 return result;
[7c8267b]2188
[aadf01e]2189 total_length = (size_t) result;
[fb04cba8]2190
2191 /* Prefix the TCP header */
[aadf01e]2192 header = PACKET_PREFIX(packet, tcp_header_t);
[7c8267b]2193 if (!header)
[aadf01e]2194 return tcp_release_and_return(packet, ENOMEM);
[7c8267b]2195
[aadf01e]2196 tcp_prepare_operation_header(socket, socket_data, header, 0, 0);
[86f6121]2197 rc = tcp_queue_packet(socket, socket_data, packet, total_length);
[0578271]2198 if (rc != EOK)
2199 return rc;
[21580dd]2200 }
2201
[fb04cba8]2202 /* Flush packets */
[aadf01e]2203 packet = tcp_get_packets_to_send(socket, socket_data);
2204 fibril_rwlock_write_unlock(socket_data->local_lock);
2205 fibril_rwlock_read_unlock(&tcp_globals.lock);
[7c8267b]2206
2207 if (packet) {
[fb04cba8]2208 /* Send the packet */
[aadf01e]2209 tcp_send_packets(socket_data->device_id, packet);
[21580dd]2210 }
2211
2212 return EOK;
2213}
2214
[7c8267b]2215int
[aaa3f33a]2216tcp_close_message(socket_cores_t *local_sockets, int socket_id)
[7c8267b]2217{
[88a1bb9]2218 socket_core_t *socket;
[4e5c7ba]2219 tcp_socket_data_t *socket_data;
[46d4d9f]2220 packet_t *packet;
[0578271]2221 int rc;
[21580dd]2222
[fb04cba8]2223 /* Find the socket */
[aadf01e]2224 socket = socket_cores_find(local_sockets, socket_id);
[7c8267b]2225 if (!socket)
[aadf01e]2226 return ENOTSOCK;
[7c8267b]2227
[fb04cba8]2228 /* Get the socket specific data */
[4e5c7ba]2229 socket_data = (tcp_socket_data_t *) socket->specific_data;
[aadf01e]2230 assert(socket_data);
[21580dd]2231
[fb04cba8]2232 /* Check state */
[7c8267b]2233 switch (socket_data->state) {
2234 case TCP_SOCKET_ESTABLISHED:
2235 socket_data->state = TCP_SOCKET_FIN_WAIT_1;
2236 break;
2237
2238 case TCP_SOCKET_CLOSE_WAIT:
2239 socket_data->state = TCP_SOCKET_LAST_ACK;
2240 break;
2241
2242// case TCP_SOCKET_LISTEN:
2243
2244 default:
[fb04cba8]2245 /* Just destroy */
[6b82009]2246 rc = socket_destroy(tcp_globals.net_sess, socket_id,
[7c8267b]2247 local_sockets, &tcp_globals.sockets,
[0578271]2248 tcp_free_socket_data);
2249 if (rc == EOK) {
[7c8267b]2250 fibril_rwlock_write_unlock(socket_data->local_lock);
2251 fibril_rwlock_write_unlock(&tcp_globals.lock);
2252 }
[0578271]2253 return rc;
[21580dd]2254 }
[7c8267b]2255
[fb04cba8]2256 /*
2257 * Send FIN.
2258 * TODO should I wait to complete?
2259 */
[21580dd]2260
[fb04cba8]2261 /* Create the notification packet */
[0578271]2262 rc = tcp_create_notification_packet(&packet, socket, socket_data, 0, 1);
2263 if (rc != EOK)
2264 return rc;
[21580dd]2265
[fb04cba8]2266 /* Send the packet */
[0578271]2267 rc = tcp_queue_packet(socket, socket_data, packet, 1);
2268 if (rc != EOK)
2269 return rc;
[21580dd]2270
[fb04cba8]2271 /* Flush packets */
[aadf01e]2272 packet = tcp_get_packets_to_send(socket, socket_data);
2273 fibril_rwlock_write_unlock(socket_data->local_lock);
2274 fibril_rwlock_write_unlock(&tcp_globals.lock);
[7c8267b]2275
2276 if (packet) {
[fb04cba8]2277 /* Send the packet */
[aadf01e]2278 tcp_send_packets(socket_data->device_id, packet);
[21580dd]2279 }
[7c8267b]2280
[21580dd]2281 return EOK;
2282}
2283
[46d4d9f]2284int tcp_create_notification_packet(packet_t **packet, socket_core_t *socket,
[4e5c7ba]2285 tcp_socket_data_t *socket_data, int synchronize, int finalize)
[7c8267b]2286{
[f772bc55]2287 packet_dimension_t *packet_dimension;
[4e5c7ba]2288 tcp_header_t *header;
[0578271]2289 int rc;
[21580dd]2290
[aadf01e]2291 assert(packet);
[21580dd]2292
[fb04cba8]2293 /* Get the device packet dimension */
[6b82009]2294 rc = tl_get_ip_packet_dimension(tcp_globals.ip_sess,
[0578271]2295 &tcp_globals.dimensions, socket_data->device_id, &packet_dimension);
2296 if (rc != EOK)
2297 return rc;
[7c8267b]2298
[fb04cba8]2299 /* Get a new packet */
[6b82009]2300 *packet = packet_get_4_remote(tcp_globals.net_sess, TCP_HEADER_SIZE,
[7c8267b]2301 packet_dimension->addr_len, packet_dimension->prefix,
2302 packet_dimension->suffix);
2303
2304 if (!*packet)
[aadf01e]2305 return ENOMEM;
[7c8267b]2306
[fb04cba8]2307 /* Allocate space in the packet */
[aadf01e]2308 header = PACKET_SUFFIX(*packet, tcp_header_t);
[7c8267b]2309 if (!header)
[aadf01e]2310 tcp_release_and_return(*packet, ENOMEM);
[21580dd]2311
[7c8267b]2312 tcp_prepare_operation_header(socket, socket_data, header, synchronize,
2313 finalize);
2314
[21580dd]2315 return EOK;
2316}
2317
[aaa3f33a]2318int tcp_accept_message(socket_cores_t *local_sockets, int socket_id,
[89e57cee]2319 int new_socket_id, size_t *data_fragment_size, size_t *addrlen)
[7c8267b]2320{
[88a1bb9]2321 socket_core_t *accepted;
2322 socket_core_t *socket;
[4e5c7ba]2323 tcp_socket_data_t *socket_data;
[f772bc55]2324 packet_dimension_t *packet_dimension;
[0578271]2325 int rc;
[21580dd]2326
[aadf01e]2327 assert(local_sockets);
2328 assert(data_fragment_size);
2329 assert(addrlen);
[21580dd]2330
[fb04cba8]2331 /* Find the socket */
[aadf01e]2332 socket = socket_cores_find(local_sockets, socket_id);
[7c8267b]2333 if (!socket)
[aadf01e]2334 return ENOTSOCK;
[7c8267b]2335
[fb04cba8]2336 /* Get the socket specific data */
[4e5c7ba]2337 socket_data = (tcp_socket_data_t *) socket->specific_data;
[aadf01e]2338 assert(socket_data);
[21580dd]2339
[fb04cba8]2340 /* Check state */
[7c8267b]2341 if (socket_data->state != TCP_SOCKET_LISTEN)
[21580dd]2342 return EINVAL;
2343
[7c8267b]2344 do {
[aadf01e]2345 socket_id = dyn_fifo_value(&socket->accepted);
[7c8267b]2346 if (socket_id < 0)
[aadf01e]2347 return ENOTSOCK;
[d510c0fe]2348 socket_id *= -1;
[21580dd]2349
[aadf01e]2350 accepted = socket_cores_find(local_sockets, socket_id);
[7c8267b]2351 if (!accepted)
[aadf01e]2352 return ENOTSOCK;
[7c8267b]2353
[fb04cba8]2354 /* Get the socket specific data */
[4e5c7ba]2355 socket_data = (tcp_socket_data_t *) accepted->specific_data;
[aadf01e]2356 assert(socket_data);
[fb04cba8]2357 /* TODO can it be in another state? */
[7c8267b]2358 if (socket_data->state == TCP_SOCKET_ESTABLISHED) {
[0578271]2359 rc = data_reply(socket_data->addr,
2360 socket_data->addrlen);
2361 if (rc != EOK)
2362 return rc;
[6b82009]2363 rc = tl_get_ip_packet_dimension(tcp_globals.ip_sess,
[0578271]2364 &tcp_globals.dimensions, socket_data->device_id,
2365 &packet_dimension);
2366 if (rc != EOK)
2367 return rc;
[aadf01e]2368 *addrlen = socket_data->addrlen;
[7c8267b]2369
2370 *data_fragment_size =
2371 ((packet_dimension->content <
2372 socket_data->data_fragment_size) ?
2373 packet_dimension->content :
2374 socket_data->data_fragment_size);
2375
2376 if (new_socket_id > 0) {
[0578271]2377 rc = socket_cores_update(local_sockets,
2378 accepted->socket_id, new_socket_id);
2379 if (rc != EOK)
2380 return rc;
[ede63e4]2381 accepted->socket_id = new_socket_id;
2382 }
[21580dd]2383 }
[aadf01e]2384 dyn_fifo_pop(&socket->accepted);
[7c8267b]2385 } while (socket_data->state != TCP_SOCKET_ESTABLISHED);
2386
[aadf01e]2387 printf("ret accept %d\n", accepted->socket_id);
[21580dd]2388 return accepted->socket_id;
2389}
2390
[88a1bb9]2391void tcp_free_socket_data(socket_core_t *socket)
[7c8267b]2392{
[4e5c7ba]2393 tcp_socket_data_t *socket_data;
[21580dd]2394
[aadf01e]2395 assert(socket);
[21580dd]2396
[aadf01e]2397 printf("destroy_socket %d\n", socket->socket_id);
[21580dd]2398
[fb04cba8]2399 /* Get the socket specific data */
[4e5c7ba]2400 socket_data = (tcp_socket_data_t *) socket->specific_data;
[aadf01e]2401 assert(socket_data);
[fb04cba8]2402
2403 /* Free the pseudo header */
[7c8267b]2404 if (socket_data->pseudo_header) {
2405 if (socket_data->headerlen) {
[21580dd]2406 printf("d pseudo\n");
[aadf01e]2407 free(socket_data->pseudo_header);
[21580dd]2408 socket_data->headerlen = 0;
2409 }
2410 socket_data->pseudo_header = NULL;
2411 }
[fb04cba8]2412
[21580dd]2413 socket_data->headerlen = 0;
[fb04cba8]2414
2415 /* Free the address */
[7c8267b]2416 if (socket_data->addr) {
2417 if (socket_data->addrlen) {
[21580dd]2418 printf("d addr\n");
[aadf01e]2419 free(socket_data->addr);
[21580dd]2420 socket_data->addrlen = 0;
2421 }
2422 socket_data->addr = NULL;
2423 }
2424 socket_data->addrlen = 0;
2425}
2426
[89e57cee]2427/** Releases the packet and returns the result.
2428 *
2429 * @param[in] packet The packet queue to be released.
2430 * @param[in] result The result to be returned.
2431 * @return The result parameter.
2432 */
[46d4d9f]2433int tcp_release_and_return(packet_t *packet, int result)
[7c8267b]2434{
[6b82009]2435 pq_release_remote(tcp_globals.net_sess, packet_get_id(packet));
[21580dd]2436 return result;
2437}
2438
[014dd57b]2439/** Process IPC messages from the IP module
[849ed54]2440 *
[014dd57b]2441 * @param[in] iid Message identifier.
2442 * @param[in,out] icall Message parameters.
[9934f7d]2443 * @param[in] arg Local argument.
[6b82009]2444 *
[849ed54]2445 */
[9934f7d]2446static void tcp_receiver(ipc_callid_t iid, ipc_call_t *icall, void *arg)
[849ed54]2447{
[014dd57b]2448 packet_t *packet;
2449 int rc;
2450
[7c8267b]2451 while (true) {
[014dd57b]2452 switch (IPC_GET_IMETHOD(*icall)) {
2453 case NET_TL_RECEIVED:
[6b82009]2454 rc = packet_translate_remote(tcp_globals.net_sess, &packet,
[014dd57b]2455 IPC_GET_PACKET(*icall));
2456 if (rc == EOK)
2457 rc = tcp_received_msg(IPC_GET_DEVICE(*icall), packet,
2458 SERVICE_TCP, IPC_GET_ERROR(*icall));
2459
[ffa2c8ef]2460 async_answer_0(iid, (sysarg_t) rc);
[014dd57b]2461 break;
2462 default:
[ffa2c8ef]2463 async_answer_0(iid, (sysarg_t) ENOTSUP);
[014dd57b]2464 }
2465
2466 iid = async_get_call(icall);
[849ed54]2467 }
2468}
2469
[014dd57b]2470/** Initialize the TCP module.
2471 *
[6b82009]2472 * @param[in] sess Network module session.
[014dd57b]2473 *
2474 * @return EOK on success.
2475 * @return ENOMEM if there is not enough memory left.
[849ed54]2476 *
2477 */
[6b82009]2478int tl_initialize(async_sess_t *sess)
[849ed54]2479{
[014dd57b]2480 fibril_rwlock_initialize(&tcp_globals.lock);
2481 fibril_rwlock_write_lock(&tcp_globals.lock);
2482
[6b82009]2483 tcp_globals.net_sess = sess;
[014dd57b]2484
[6b82009]2485 tcp_globals.icmp_sess = icmp_connect_module();
2486 tcp_globals.ip_sess = ip_bind_service(SERVICE_IP, IPPROTO_TCP,
[014dd57b]2487 SERVICE_TCP, tcp_receiver);
[6b82009]2488 if (tcp_globals.ip_sess == NULL) {
[014dd57b]2489 fibril_rwlock_write_unlock(&tcp_globals.lock);
[6b82009]2490 return ENOENT;
[014dd57b]2491 }
2492
2493 int rc = socket_ports_initialize(&tcp_globals.sockets);
2494 if (rc != EOK)
2495 goto out;
[7c8267b]2496
[014dd57b]2497 rc = packet_dimensions_initialize(&tcp_globals.dimensions);
2498 if (rc != EOK) {
[5fe7692]2499 socket_ports_destroy(&tcp_globals.sockets, free);
[014dd57b]2500 goto out;
2501 }
2502
2503 tcp_globals.last_used_port = TCP_FREE_PORTS_START - 1;
2504
2505out:
2506 fibril_rwlock_write_unlock(&tcp_globals.lock);
[0578271]2507 return rc;
[849ed54]2508}
2509
[014dd57b]2510int main(int argc, char *argv[])
2511{
2512 return tl_module_start(SERVICE_TCP);
2513}
2514
[21580dd]2515/** @}
2516 */
Note: See TracBrowser for help on using the repository browser.