source: mainline/uspace/srv/net/tcp/conn.c@ dba3e2c

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

improve debugging output

  • Property mode set to 100644
File size: 32.4 KB
RevLine 
[c5808b41]1/*
2 * Copyright (c) 2011 Jiri Svoboda
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
30 * @{
31 */
32
33/**
[032bbe7]34 * @file TCP connection processing and state machine
[c5808b41]35 */
36
37#include <adt/list.h>
[3e6a98c5]38#include <stdbool.h>
[c5808b41]39#include <errno.h>
40#include <io/log.h>
[0093ab6]41#include <macros.h>
[c5808b41]42#include <stdlib.h>
43#include "conn.h"
44#include "iqueue.h"
45#include "segment.h"
46#include "seq_no.h"
47#include "tcp_type.h"
48#include "tqueue.h"
[762b48a]49#include "ucall.h"
[c5808b41]50
[a52de0e]51#define RCV_BUF_SIZE 4096/*2*/
[32105348]52#define SND_BUF_SIZE 4096
[0093ab6]53
[2a3214e]54#define MAX_SEGMENT_LIFETIME (15*1000*1000) //(2*60*1000*1000)
55#define TIME_WAIT_TIMEOUT (2*MAX_SEGMENT_LIFETIME)
56
[c5808b41]57LIST_INITIALIZE(conn_list);
[0edaf0f6]58FIBRIL_MUTEX_INITIALIZE(conn_list_lock);
[c5808b41]59
60static void tcp_conn_seg_process(tcp_conn_t *conn, tcp_segment_t *seg);
[2a3214e]61static void tcp_conn_tw_timer_set(tcp_conn_t *conn);
[704586fb]62static void tcp_conn_tw_timer_clear(tcp_conn_t *conn);
[c5808b41]63
[7a8c1c4e]64/** Create new connection structure.
[032bbe7]65 *
66 * @param lsock Local socket (will be deeply copied)
67 * @param fsock Foreign socket (will be deeply copied)
[7a8c1c4e]68 * @return New connection or NULL
[032bbe7]69 */
[c5808b41]70tcp_conn_t *tcp_conn_new(tcp_sock_t *lsock, tcp_sock_t *fsock)
71{
[2a3214e]72 tcp_conn_t *conn = NULL;
[7cf7ded]73 bool tqueue_inited = false;
[c5808b41]74
[0093ab6]75 /* Allocate connection structure */
[c5808b41]76 conn = calloc(1, sizeof(tcp_conn_t));
77 if (conn == NULL)
[2a3214e]78 goto error;
79
80 conn->tw_timer = fibril_timer_create();
81 if (conn->tw_timer == NULL)
82 goto error;
[c5808b41]83
[0edaf0f6]84 fibril_mutex_initialize(&conn->lock);
85
86 /* One for the user, one for not being in closed state */
87 atomic_set(&conn->refcnt, 2);
88
[0093ab6]89 /* Allocate receive buffer */
[d9ce049]90 fibril_condvar_initialize(&conn->rcv_buf_cv);
[0093ab6]91 conn->rcv_buf_size = RCV_BUF_SIZE;
92 conn->rcv_buf_used = 0;
[8c7a054]93 conn->rcv_buf_fin = false;
[d9ce049]94
[0093ab6]95 conn->rcv_buf = calloc(1, conn->rcv_buf_size);
[2a3214e]96 if (conn->rcv_buf == NULL)
97 goto error;
[0093ab6]98
[32105348]99 /** Allocate send buffer */
[bbf159a]100 fibril_condvar_initialize(&conn->snd_buf_cv);
[32105348]101 conn->snd_buf_size = SND_BUF_SIZE;
102 conn->snd_buf_used = 0;
[8c7a054]103 conn->snd_buf_fin = false;
[32105348]104 conn->snd_buf = calloc(1, conn->snd_buf_size);
[2a3214e]105 if (conn->snd_buf == NULL)
106 goto error;
[32105348]107
[0093ab6]108 /* Set up receive window. */
109 conn->rcv_wnd = conn->rcv_buf_size;
110
111 /* Initialize incoming segment queue */
[c5808b41]112 tcp_iqueue_init(&conn->incoming, conn);
113
[6df418c4]114 /* Initialize retransmission queue */
[7cf7ded]115 if (tcp_tqueue_init(&conn->retransmit, conn) != EOK)
116 goto error;
117
118 tqueue_inited = true;
[6df418c4]119
[004a5fe]120 /* Connection state change signalling */
121 fibril_condvar_initialize(&conn->cstate_cv);
122
[ae481e0]123 conn->cstate_cb = NULL;
124
[c5808b41]125 conn->cstate = st_listen;
[d9e14fa4]126 conn->reset = false;
[7a8c1c4e]127 conn->deleted = false;
[d9e14fa4]128 conn->ap = ap_passive;
[6df418c4]129 conn->fin_is_acked = false;
[c5808b41]130 conn->ident.local = *lsock;
131 if (fsock != NULL)
132 conn->ident.foreign = *fsock;
133
134 return conn;
[2a3214e]135
136error:
[7cf7ded]137 if (tqueue_inited)
138 tcp_tqueue_fini(&conn->retransmit);
[2a3214e]139 if (conn != NULL && conn->rcv_buf != NULL)
140 free(conn->rcv_buf);
141 if (conn != NULL && conn->snd_buf != NULL)
142 free(conn->snd_buf);
143 if (conn != NULL && conn->tw_timer != NULL)
144 fibril_timer_destroy(conn->tw_timer);
145 if (conn != NULL)
146 free(conn);
147
148 return NULL;
[c5808b41]149}
150
[7a8c1c4e]151/** Destroy connection structure.
152 *
[0edaf0f6]153 * Connection structure should be destroyed when the folowing condtitions
154 * are met:
155 * (1) user has deleted the connection
156 * (2) the connection has entered closed state
157 * (3) nobody is holding references to the connection
158 *
159 * This happens when @a conn->refcnt is zero as we count (1) and (2)
160 * as special references.
[7a8c1c4e]161 *
162 * @param conn Connection
163 */
164static void tcp_conn_free(tcp_conn_t *conn)
165{
[a1a101d]166 log_msg(LOG_DEFAULT, LVL_DEBUG, "%s: tcp_conn_free(%p)", conn->name, conn);
[7a8c1c4e]167 tcp_tqueue_fini(&conn->retransmit);
168
169 if (conn->rcv_buf != NULL)
170 free(conn->rcv_buf);
171 if (conn->snd_buf != NULL)
172 free(conn->snd_buf);
173 if (conn->tw_timer != NULL)
174 fibril_timer_destroy(conn->tw_timer);
175 free(conn);
176}
177
[0edaf0f6]178/** Add reference to connection.
179 *
180 * Increase connection reference count by one.
181 *
182 * @param conn Connection
183 */
184void tcp_conn_addref(tcp_conn_t *conn)
185{
[a1a101d]186 log_msg(LOG_DEFAULT, LVL_DEBUG2, "%s: tcp_conn_addref(%p)", conn->name, conn);
[0edaf0f6]187 atomic_inc(&conn->refcnt);
188}
189
190/** Remove reference from connection.
191 *
192 * Decrease connection reference count by one.
193 *
194 * @param conn Connection
195 */
196void tcp_conn_delref(tcp_conn_t *conn)
197{
[a1a101d]198 log_msg(LOG_DEFAULT, LVL_DEBUG2, "%s: tcp_conn_delref(%p)", conn->name, conn);
[0edaf0f6]199
200 if (atomic_predec(&conn->refcnt) == 0)
201 tcp_conn_free(conn);
202}
203
[7a8c1c4e]204/** Delete connection.
205 *
206 * The caller promises not make no further references to @a conn.
207 * TCP will free @a conn eventually.
208 *
209 * @param conn Connection
210 */
211void tcp_conn_delete(tcp_conn_t *conn)
212{
[a1a101d]213 log_msg(LOG_DEFAULT, LVL_DEBUG, "%s: tcp_conn_delete(%p)", conn->name, conn);
[7a8c1c4e]214
[0edaf0f6]215 assert(conn->deleted == false);
216 tcp_conn_delref(conn);
[7a8c1c4e]217}
218
[032bbe7]219/** Enlist connection.
220 *
221 * Add connection to the connection map.
222 */
[c5808b41]223void tcp_conn_add(tcp_conn_t *conn)
224{
[e1c6dde9]225 tcp_conn_addref(conn);
[0edaf0f6]226 fibril_mutex_lock(&conn_list_lock);
[c5808b41]227 list_append(&conn->link, &conn_list);
[0edaf0f6]228 fibril_mutex_unlock(&conn_list_lock);
[c5808b41]229}
230
[6e88fea]231/** Delist connection.
232 *
233 * Remove connection from the connection map.
234 */
235void tcp_conn_remove(tcp_conn_t *conn)
236{
[0edaf0f6]237 fibril_mutex_lock(&conn_list_lock);
[6e88fea]238 list_remove(&conn->link);
[0edaf0f6]239 fibril_mutex_unlock(&conn_list_lock);
[e1c6dde9]240 tcp_conn_delref(conn);
[6e88fea]241}
242
[004a5fe]243static void tcp_conn_state_set(tcp_conn_t *conn, tcp_cstate_t nstate)
244{
[0edaf0f6]245 tcp_cstate_t old_state;
246
[a1a101d]247 log_msg(LOG_DEFAULT, LVL_DEBUG, "tcp_conn_state_set(%p)", conn);
[ae481e0]248
[0edaf0f6]249 old_state = conn->cstate;
[004a5fe]250 conn->cstate = nstate;
251 fibril_condvar_broadcast(&conn->cstate_cv);
[7a8c1c4e]252
[ae481e0]253 /* Run user callback function */
254 if (conn->cstate_cb != NULL) {
[a1a101d]255 log_msg(LOG_DEFAULT, LVL_DEBUG, "tcp_conn_state_set() - run user CB");
[ae481e0]256 conn->cstate_cb(conn, conn->cstate_cb_arg);
257 } else {
[a1a101d]258 log_msg(LOG_DEFAULT, LVL_DEBUG, "tcp_conn_state_set() - no user CB");
[ae481e0]259 }
260
[0edaf0f6]261 assert(old_state != st_closed);
262 if (nstate == st_closed) {
263 /* Drop one reference for now being in closed state */
264 tcp_conn_delref(conn);
[7a8c1c4e]265 }
[004a5fe]266}
267
[032bbe7]268/** Synchronize connection.
269 *
270 * This is the first step of an active connection attempt,
271 * sends out SYN and sets up ISS and SND.xxx.
272 */
[c5808b41]273void tcp_conn_sync(tcp_conn_t *conn)
274{
275 /* XXX select ISS */
276 conn->iss = 1;
277 conn->snd_nxt = conn->iss;
278 conn->snd_una = conn->iss;
[d9e14fa4]279 conn->ap = ap_active;
[c5808b41]280
281 tcp_tqueue_ctrl_seg(conn, CTL_SYN);
[004a5fe]282 tcp_conn_state_set(conn, st_syn_sent);
[c5808b41]283}
284
[f343a16]285/** FIN has been sent.
286 *
287 * This function should be called when FIN is sent over the connection,
288 * as a result the connection state is changed appropriately.
289 */
290void tcp_conn_fin_sent(tcp_conn_t *conn)
291{
292 switch (conn->cstate) {
293 case st_syn_received:
294 case st_established:
[a1a101d]295 log_msg(LOG_DEFAULT, LVL_DEBUG, "%s: FIN sent -> Fin-Wait-1", conn->name);
[004a5fe]296 tcp_conn_state_set(conn, st_fin_wait_1);
[f343a16]297 break;
298 case st_close_wait:
[a1a101d]299 log_msg(LOG_DEFAULT, LVL_DEBUG, "%s: FIN sent -> Last-Ack", conn->name);
[004a5fe]300 tcp_conn_state_set(conn, st_last_ack);
[f343a16]301 break;
302 default:
[a1a101d]303 log_msg(LOG_DEFAULT, LVL_ERROR, "%s: Connection state %d", conn->name,
[6896409c]304 conn->cstate);
[f343a16]305 assert(false);
306 }
[6df418c4]307
308 conn->fin_is_acked = false;
[f343a16]309}
310
[92b42442]311/** Match socket with pattern. */
[3aa2642a]312static bool tcp_socket_match(tcp_sock_t *sock, tcp_sock_t *patt)
[c5808b41]313{
[a1a101d]314 log_msg(LOG_DEFAULT, LVL_DEBUG2, "tcp_socket_match(sock=(%x,%u), pat=(%x,%u))",
[3aa2642a]315 sock->addr.ipv4, sock->port, patt->addr.ipv4, patt->port);
[c5808b41]316
[3aa2642a]317 if (patt->addr.ipv4 != TCP_IPV4_ANY &&
318 patt->addr.ipv4 != sock->addr.ipv4)
[c5808b41]319 return false;
320
[3aa2642a]321 if (patt->port != TCP_PORT_ANY &&
322 patt->port != sock->port)
[c5808b41]323 return false;
324
[a1a101d]325 log_msg(LOG_DEFAULT, LVL_DEBUG2, " -> match");
[c5808b41]326
327 return true;
328}
329
[92b42442]330/** Match socket pair with pattern. */
[c5808b41]331static bool tcp_sockpair_match(tcp_sockpair_t *sp, tcp_sockpair_t *pattern)
332{
[a1a101d]333 log_msg(LOG_DEFAULT, LVL_DEBUG2, "tcp_sockpair_match(%p, %p)", sp, pattern);
[c5808b41]334
[3aa2642a]335 if (!tcp_socket_match(&sp->local, &pattern->local))
[c5808b41]336 return false;
337
[3aa2642a]338 if (!tcp_socket_match(&sp->foreign, &pattern->foreign))
[c5808b41]339 return false;
340
341 return true;
342}
343
[032bbe7]344/** Find connection structure for specified socket pair.
345 *
346 * A connection is uniquely identified by a socket pair. Look up our
347 * connection map and return connection structure based on socket pair.
[0edaf0f6]348 * The connection reference count is bumped by one.
[032bbe7]349 *
350 * @param sp Socket pair
351 * @return Connection structure or NULL if not found.
352 */
[0edaf0f6]353tcp_conn_t *tcp_conn_find_ref(tcp_sockpair_t *sp)
[c5808b41]354{
[a1a101d]355 log_msg(LOG_DEFAULT, LVL_DEBUG, "tcp_conn_find_ref(%p)", sp);
[f597bc4]356
357 log_msg(LOG_DEFAULT, LVL_DEBUG2, "compare conn (f:(%x,%u), l:(%x,%u))",
358 sp->foreign.addr.ipv4, sp->foreign.port,
359 sp->local.addr.ipv4, sp->local.port);
360
[0edaf0f6]361 fibril_mutex_lock(&conn_list_lock);
[f597bc4]362
[c5808b41]363 list_foreach(conn_list, link) {
364 tcp_conn_t *conn = list_get_instance(link, tcp_conn_t, link);
[704586fb]365 tcp_sockpair_t *csp = &conn->ident;
[f597bc4]366
367 log_msg(LOG_DEFAULT, LVL_DEBUG2, " - with (f:(%x,%u), l:(%x,%u))",
[704586fb]368 csp->foreign.addr.ipv4, csp->foreign.port,
369 csp->local.addr.ipv4, csp->local.port);
[f597bc4]370
[704586fb]371 if (tcp_sockpair_match(sp, csp)) {
[0edaf0f6]372 tcp_conn_addref(conn);
373 fibril_mutex_unlock(&conn_list_lock);
[c5808b41]374 return conn;
375 }
376 }
[f597bc4]377
[0edaf0f6]378 fibril_mutex_unlock(&conn_list_lock);
[c5808b41]379 return NULL;
380}
381
[704586fb]382/** Reset connection.
383 *
384 * @param conn Connection
385 */
386static void tcp_conn_reset(tcp_conn_t *conn)
387{
[a1a101d]388 log_msg(LOG_DEFAULT, LVL_DEBUG, "%s: tcp_conn_reset()", conn->name);
[704586fb]389 tcp_conn_state_set(conn, st_closed);
[d9e14fa4]390 conn->reset = true;
391
[704586fb]392 tcp_conn_tw_timer_clear(conn);
393 tcp_tqueue_clear(&conn->retransmit);
[d9e14fa4]394
395 fibril_condvar_broadcast(&conn->rcv_buf_cv);
[bbf159a]396 fibril_condvar_broadcast(&conn->snd_buf_cv);
[d9e14fa4]397}
398
399/** Signal to the user that connection has been reset.
400 *
401 * Send an out-of-band signal to the user.
402 */
403static void tcp_reset_signal(tcp_conn_t *conn)
404{
405 /* TODO */
[a1a101d]406 log_msg(LOG_DEFAULT, LVL_DEBUG, "%s: tcp_reset_signal()", conn->name);
[704586fb]407}
408
[32105348]409/** Determine if SYN has been received.
410 *
411 * @param conn Connection
412 * @return @c true if SYN has been received, @c false otherwise.
413 */
414bool tcp_conn_got_syn(tcp_conn_t *conn)
415{
416 switch (conn->cstate) {
417 case st_listen:
418 case st_syn_sent:
419 return false;
420 case st_syn_received:
421 case st_established:
422 case st_fin_wait_1:
423 case st_fin_wait_2:
424 case st_close_wait:
425 case st_closing:
426 case st_last_ack:
427 case st_time_wait:
428 return true;
429 case st_closed:
[a1a101d]430 log_msg(LOG_DEFAULT, LVL_WARN, "state=%d", (int) conn->cstate);
[32105348]431 assert(false);
432 }
433
434 assert(false);
435}
436
[032bbe7]437/** Segment arrived in Listen state.
438 *
439 * @param conn Connection
440 * @param seg Segment
441 */
[c5808b41]442static void tcp_conn_sa_listen(tcp_conn_t *conn, tcp_segment_t *seg)
443{
[a1a101d]444 log_msg(LOG_DEFAULT, LVL_DEBUG, "tcp_conn_sa_listen(%p, %p)", conn, seg);
[c5808b41]445
446 if ((seg->ctrl & CTL_RST) != 0) {
[a1a101d]447 log_msg(LOG_DEFAULT, LVL_DEBUG, "Ignoring incoming RST.");
[c5808b41]448 return;
449 }
450
451 if ((seg->ctrl & CTL_ACK) != 0) {
[a1a101d]452 log_msg(LOG_DEFAULT, LVL_DEBUG, "Incoming ACK, send acceptable RST.");
[c5808b41]453 tcp_reply_rst(&conn->ident, seg);
454 return;
455 }
456
457 if ((seg->ctrl & CTL_SYN) == 0) {
[a1a101d]458 log_msg(LOG_DEFAULT, LVL_DEBUG, "SYN not present. Ignoring segment.");
[c5808b41]459 return;
460 }
461
[a1a101d]462 log_msg(LOG_DEFAULT, LVL_DEBUG, "Got SYN, sending SYN, ACK.");
[c5808b41]463
464 conn->rcv_nxt = seg->seq + 1;
465 conn->irs = seg->seq;
466
[4c55a64]467
[a1a101d]468 log_msg(LOG_DEFAULT, LVL_DEBUG, "rcv_nxt=%u", conn->rcv_nxt);
[c5808b41]469
470 if (seg->len > 1)
[a1a101d]471 log_msg(LOG_DEFAULT, LVL_WARN, "SYN combined with data, ignoring data.");
[c5808b41]472
473 /* XXX select ISS */
474 conn->iss = 1;
475 conn->snd_nxt = conn->iss;
476 conn->snd_una = conn->iss;
477
[4c55a64]478 /*
479 * Surprisingly the spec does not deal with initial window setting.
480 * Set SND.WND = SEG.WND and set SND.WL1 so that next segment
481 * will always be accepted as new window setting.
482 */
483 conn->snd_wnd = seg->wnd;
484 conn->snd_wl1 = seg->seq;
485 conn->snd_wl2 = seg->seq;
486
[004a5fe]487 tcp_conn_state_set(conn, st_syn_received);
[c5808b41]488
489 tcp_tqueue_ctrl_seg(conn, CTL_SYN | CTL_ACK /* XXX */);
[0093ab6]490
491 tcp_segment_delete(seg);
[c5808b41]492}
493
[032bbe7]494/** Segment arrived in Syn-Sent state.
495 *
496 * @param conn Connection
497 * @param seg Segment
498 */
[c5808b41]499static void tcp_conn_sa_syn_sent(tcp_conn_t *conn, tcp_segment_t *seg)
500{
[a1a101d]501 log_msg(LOG_DEFAULT, LVL_DEBUG, "tcp_conn_sa_syn_sent(%p, %p)", conn, seg);
[c5808b41]502
503 if ((seg->ctrl & CTL_ACK) != 0) {
[a1a101d]504 log_msg(LOG_DEFAULT, LVL_DEBUG, "snd_una=%u, seg.ack=%u, snd_nxt=%u",
[c5808b41]505 conn->snd_una, seg->ack, conn->snd_nxt);
506 if (!seq_no_ack_acceptable(conn, seg->ack)) {
[2f0dd2a]507 if ((seg->ctrl & CTL_RST) == 0) {
[a1a101d]508 log_msg(LOG_DEFAULT, LVL_WARN, "ACK not acceptable, send RST");
[2f0dd2a]509 tcp_reply_rst(&conn->ident, seg);
510 } else {
[a1a101d]511 log_msg(LOG_DEFAULT, LVL_WARN, "RST,ACK not acceptable, drop");
[2f0dd2a]512 }
[c5808b41]513 return;
514 }
515 }
516
517 if ((seg->ctrl & CTL_RST) != 0) {
[2f0dd2a]518 /* If we get here, we have either an acceptable ACK or no ACK */
519 if ((seg->ctrl & CTL_ACK) != 0) {
[a1a101d]520 log_msg(LOG_DEFAULT, LVL_DEBUG, "%s: Connection reset. -> Closed",
[2f0dd2a]521 conn->name);
522 /* Reset connection */
523 tcp_conn_reset(conn);
524 return;
525 } else {
[a1a101d]526 log_msg(LOG_DEFAULT, LVL_DEBUG, "%s: RST without ACK, drop",
[2f0dd2a]527 conn->name);
528 return;
529 }
[c5808b41]530 }
531
532 /* XXX precedence */
533
534 if ((seg->ctrl & CTL_SYN) == 0) {
[a1a101d]535 log_msg(LOG_DEFAULT, LVL_DEBUG, "No SYN bit, ignoring segment.");
[c5808b41]536 return;
537 }
538
539 conn->rcv_nxt = seg->seq + 1;
540 conn->irs = seg->seq;
541
542 if ((seg->ctrl & CTL_ACK) != 0) {
543 conn->snd_una = seg->ack;
[d9ce049]544
545 /*
546 * Prune acked segments from retransmission queue and
547 * possibly transmit more data.
548 */
549 tcp_tqueue_ack_received(conn);
[c5808b41]550 }
551
[a1a101d]552 log_msg(LOG_DEFAULT, LVL_DEBUG, "Sent SYN, got SYN.");
[c5808b41]553
[32105348]554 /*
555 * Surprisingly the spec does not deal with initial window setting.
556 * Set SND.WND = SEG.WND and set SND.WL1 so that next segment
557 * will always be accepted as new window setting.
558 */
[a1a101d]559 log_msg(LOG_DEFAULT, LVL_DEBUG, "SND.WND := %" PRIu32 ", SND.WL1 := %" PRIu32 ", "
[32105348]560 "SND.WL2 = %" PRIu32, seg->wnd, seg->seq, seg->seq);
561 conn->snd_wnd = seg->wnd;
562 conn->snd_wl1 = seg->seq;
563 conn->snd_wl2 = seg->seq;
564
[c5808b41]565 if (seq_no_syn_acked(conn)) {
[a1a101d]566 log_msg(LOG_DEFAULT, LVL_DEBUG, "%s: syn acked -> Established", conn->name);
[004a5fe]567 tcp_conn_state_set(conn, st_established);
[c5808b41]568 tcp_tqueue_ctrl_seg(conn, CTL_ACK /* XXX */);
569 } else {
[a1a101d]570 log_msg(LOG_DEFAULT, LVL_DEBUG, "%s: syn not acked -> Syn-Received",
[6896409c]571 conn->name);
[004a5fe]572 tcp_conn_state_set(conn, st_syn_received);
[c5808b41]573 tcp_tqueue_ctrl_seg(conn, CTL_SYN | CTL_ACK /* XXX */);
574 }
[0093ab6]575
576 tcp_segment_delete(seg);
[c5808b41]577}
578
[032bbe7]579/** Segment arrived in state where segments are processed in sequence order.
580 *
581 * Queue segment in incoming segments queue for processing.
582 *
583 * @param conn Connection
584 * @param seg Segment
585 */
[c5808b41]586static void tcp_conn_sa_queue(tcp_conn_t *conn, tcp_segment_t *seg)
587{
588 tcp_segment_t *pseg;
589
[a1a101d]590 log_msg(LOG_DEFAULT, LVL_DEBUG, "tcp_conn_sa_seq(%p, %p)", conn, seg);
[c5808b41]591
[7cf7ded]592 /* Discard unacceptable segments ("old duplicates") */
593 if (!seq_no_segment_acceptable(conn, seg)) {
[a1a101d]594 log_msg(LOG_DEFAULT, LVL_DEBUG, "Replying ACK to unacceptable segment.");
[7cf7ded]595 tcp_tqueue_ctrl_seg(conn, CTL_ACK);
596 tcp_segment_delete(seg);
597 return;
598 }
[c5808b41]599
600 /* Queue for processing */
601 tcp_iqueue_insert_seg(&conn->incoming, seg);
602
603 /*
604 * Process all segments from incoming queue that are ready.
605 * Unacceptable segments are discarded by tcp_iqueue_get_ready_seg().
606 *
607 * XXX Need to return ACK for unacceptable segments
608 */
609 while (tcp_iqueue_get_ready_seg(&conn->incoming, &pseg) == EOK)
610 tcp_conn_seg_process(conn, pseg);
611}
612
[032bbe7]613/** Process segment RST field.
614 *
615 * @param conn Connection
616 * @param seg Segment
617 * @return cp_done if we are done with this segment, cp_continue
618 * if not
619 */
[c5808b41]620static cproc_t tcp_conn_seg_proc_rst(tcp_conn_t *conn, tcp_segment_t *seg)
621{
[03be171]622 if ((seg->ctrl & CTL_RST) == 0)
623 return cp_continue;
624
[d9e14fa4]625 switch (conn->cstate) {
626 case st_syn_received:
627 /* XXX In case of passive open, revert to Listen state */
628 if (conn->ap == ap_passive) {
629 tcp_conn_state_set(conn, st_listen);
630 /* XXX Revert conn->ident */
631 tcp_conn_tw_timer_clear(conn);
632 tcp_tqueue_clear(&conn->retransmit);
633 } else {
634 tcp_conn_reset(conn);
635 }
636 break;
637 case st_established:
638 case st_fin_wait_1:
639 case st_fin_wait_2:
640 case st_close_wait:
641 /* General "connection reset" signal */
642 tcp_reset_signal(conn);
643 tcp_conn_reset(conn);
644 break;
645 case st_closing:
646 case st_last_ack:
647 case st_time_wait:
648 tcp_conn_reset(conn);
649 break;
650 case st_listen:
651 case st_syn_sent:
652 case st_closed:
653 assert(false);
654 }
655
656 return cp_done;
[c5808b41]657}
658
[032bbe7]659/** Process segment security and precedence fields.
660 *
661 * @param conn Connection
662 * @param seg Segment
663 * @return cp_done if we are done with this segment, cp_continue
664 * if not
665 */
[c5808b41]666static cproc_t tcp_conn_seg_proc_sp(tcp_conn_t *conn, tcp_segment_t *seg)
667{
[4c55a64]668 /* TODO */
[c5808b41]669 return cp_continue;
670}
671
[032bbe7]672/** Process segment SYN field.
673 *
674 * @param conn Connection
675 * @param seg Segment
676 * @return cp_done if we are done with this segment, cp_continue
677 * if not
678 */
[c5808b41]679static cproc_t tcp_conn_seg_proc_syn(tcp_conn_t *conn, tcp_segment_t *seg)
680{
[7cf7ded]681 if ((seg->ctrl & CTL_SYN) == 0)
682 return cp_continue;
683
684 /*
685 * Assert SYN is in receive window, otherwise this step should not
686 * be reached.
687 */
688 assert(seq_no_in_rcv_wnd(conn, seg->seq));
689
[a1a101d]690 log_msg(LOG_DEFAULT, LVL_WARN, "SYN is in receive window, should send reset. XXX");
[7cf7ded]691
692 /*
693 * TODO
694 *
695 * Send a reset, resond "reset" to all outstanding RECEIVEs and SEND,
696 * flush segment queues. Send unsolicited "connection reset" signal
697 * to user, connection -> closed state, delete TCB, return.
698 */
699 return cp_done;
[c5808b41]700}
701
[032bbe7]702/** Process segment ACK field in Syn-Received state.
703 *
704 * @param conn Connection
705 * @param seg Segment
706 * @return cp_done if we are done with this segment, cp_continue
707 * if not
708 */
[c5808b41]709static cproc_t tcp_conn_seg_proc_ack_sr(tcp_conn_t *conn, tcp_segment_t *seg)
710{
711 if (!seq_no_ack_acceptable(conn, seg->ack)) {
712 /* ACK is not acceptable, send RST. */
[a1a101d]713 log_msg(LOG_DEFAULT, LVL_WARN, "Segment ACK not acceptable, sending RST.");
[c5808b41]714 tcp_reply_rst(&conn->ident, seg);
[4c55a64]715 tcp_segment_delete(seg);
716 return cp_done;
[c5808b41]717 }
718
[a1a101d]719 log_msg(LOG_DEFAULT, LVL_DEBUG, "%s: SYN ACKed -> Established", conn->name);
[c5808b41]720
[004a5fe]721 tcp_conn_state_set(conn, st_established);
[c5808b41]722
723 /* XXX Not mentioned in spec?! */
724 conn->snd_una = seg->ack;
725
726 return cp_continue;
727}
728
[032bbe7]729/** Process segment ACK field in Established state.
730 *
731 * @param conn Connection
732 * @param seg Segment
733 * @return cp_done if we are done with this segment, cp_continue
734 * if not
735 */
[c5808b41]736static cproc_t tcp_conn_seg_proc_ack_est(tcp_conn_t *conn, tcp_segment_t *seg)
737{
[a1a101d]738 log_msg(LOG_DEFAULT, LVL_DEBUG, "tcp_conn_seg_proc_ack_est(%p, %p)", conn, seg);
[0093ab6]739
[a1a101d]740 log_msg(LOG_DEFAULT, LVL_DEBUG, "SEG.ACK=%u, SND.UNA=%u, SND.NXT=%u",
[0093ab6]741 (unsigned)seg->ack, (unsigned)conn->snd_una,
742 (unsigned)conn->snd_nxt);
743
[4c55a64]744 if (!seq_no_ack_acceptable(conn, seg->ack)) {
[a1a101d]745 log_msg(LOG_DEFAULT, LVL_DEBUG, "ACK not acceptable.");
[4c55a64]746 if (!seq_no_ack_duplicate(conn, seg->ack)) {
[a1a101d]747 log_msg(LOG_DEFAULT, LVL_WARN, "Not acceptable, not duplicate. "
[0093ab6]748 "Send ACK and drop.");
[4c55a64]749 /* Not acceptable, not duplicate. Send ACK and drop. */
750 tcp_tqueue_ctrl_seg(conn, CTL_ACK);
751 tcp_segment_delete(seg);
752 return cp_done;
[0093ab6]753 } else {
[a1a101d]754 log_msg(LOG_DEFAULT, LVL_DEBUG, "Ignoring duplicate ACK.");
[4c55a64]755 }
756 } else {
757 /* Update SND.UNA */
758 conn->snd_una = seg->ack;
759 }
760
761 if (seq_no_new_wnd_update(conn, seg)) {
762 conn->snd_wnd = seg->wnd;
763 conn->snd_wl1 = seg->seq;
764 conn->snd_wl2 = seg->ack;
[d9ce049]765
[a1a101d]766 log_msg(LOG_DEFAULT, LVL_DEBUG, "Updating send window, SND.WND=%" PRIu32
[d9ce049]767 ", SND.WL1=%" PRIu32 ", SND.WL2=%" PRIu32,
768 conn->snd_wnd, conn->snd_wl1, conn->snd_wl2);
[4c55a64]769 }
770
[d9ce049]771 /*
772 * Prune acked segments from retransmission queue and
773 * possibly transmit more data.
774 */
775 tcp_tqueue_ack_received(conn);
776
[c5808b41]777 return cp_continue;
778}
779
[032bbe7]780/** Process segment ACK field in Fin-Wait-1 state.
781 *
782 * @param conn Connection
783 * @param seg Segment
784 * @return cp_done if we are done with this segment, cp_continue
785 * if not
786 */
[c5808b41]787static cproc_t tcp_conn_seg_proc_ack_fw1(tcp_conn_t *conn, tcp_segment_t *seg)
788{
[4c55a64]789 if (tcp_conn_seg_proc_ack_est(conn, seg) == cp_done)
790 return cp_done;
791
[6df418c4]792 if (conn->fin_is_acked) {
[a1a101d]793 log_msg(LOG_DEFAULT, LVL_DEBUG, "%s: FIN acked -> Fin-Wait-2", conn->name);
[004a5fe]794 tcp_conn_state_set(conn, st_fin_wait_2);
[6df418c4]795 }
796
[c5808b41]797 return cp_continue;
798}
799
[032bbe7]800/** Process segment ACK field in Fin-Wait-2 state.
801 *
802 * @param conn Connection
803 * @param seg Segment
804 * @return cp_done if we are done with this segment, cp_continue
805 * if not
806 */
[c5808b41]807static cproc_t tcp_conn_seg_proc_ack_fw2(tcp_conn_t *conn, tcp_segment_t *seg)
808{
[4c55a64]809 if (tcp_conn_seg_proc_ack_est(conn, seg) == cp_done)
810 return cp_done;
811
812 /* TODO */
[c5808b41]813 return cp_continue;
814}
815
[032bbe7]816/** Process segment ACK field in Close-Wait state.
817 *
818 * @param conn Connection
819 * @param seg Segment
820 * @return cp_done if we are done with this segment, cp_continue
821 * if not
822 */
[c5808b41]823static cproc_t tcp_conn_seg_proc_ack_cw(tcp_conn_t *conn, tcp_segment_t *seg)
824{
[4c55a64]825 /* The same processing as in Established state */
826 return tcp_conn_seg_proc_ack_est(conn, seg);
[c5808b41]827}
828
[032bbe7]829/** Process segment ACK field in Closing state.
830 *
831 * @param conn Connection
832 * @param seg Segment
833 * @return cp_done if we are done with this segment, cp_continue
834 * if not
835 */
[c5808b41]836static cproc_t tcp_conn_seg_proc_ack_cls(tcp_conn_t *conn, tcp_segment_t *seg)
837{
[4c55a64]838 if (tcp_conn_seg_proc_ack_est(conn, seg) == cp_done)
839 return cp_done;
840
841 /* TODO */
[c5808b41]842 return cp_continue;
843}
844
[032bbe7]845/** Process segment ACK field in Last-Ack state.
846 *
847 * @param conn Connection
848 * @param seg Segment
849 * @return cp_done if we are done with this segment, cp_continue
850 * if not
851 */
[c5808b41]852static cproc_t tcp_conn_seg_proc_ack_la(tcp_conn_t *conn, tcp_segment_t *seg)
853{
[4c55a64]854 if (tcp_conn_seg_proc_ack_est(conn, seg) == cp_done)
855 return cp_done;
856
[6e88fea]857 if (conn->fin_is_acked) {
[a1a101d]858 log_msg(LOG_DEFAULT, LVL_DEBUG, "%s: FIN acked -> Closed", conn->name);
[6e88fea]859 tcp_conn_remove(conn);
[004a5fe]860 tcp_conn_state_set(conn, st_closed);
[6e88fea]861 return cp_done;
862 }
863
[c5808b41]864 return cp_continue;
865}
866
[032bbe7]867/** Process segment ACK field in Time-Wait state.
868 *
869 * @param conn Connection
870 * @param seg Segment
871 * @return cp_done if we are done with this segment, cp_continue
872 * if not
873 */
[c5808b41]874static cproc_t tcp_conn_seg_proc_ack_tw(tcp_conn_t *conn, tcp_segment_t *seg)
875{
[4c55a64]876 /* Nothing to do */
[c5808b41]877 return cp_continue;
878}
879
[032bbe7]880/** Process segment ACK field.
881 *
882 * @param conn Connection
883 * @param seg Segment
884 * @return cp_done if we are done with this segment, cp_continue
885 * if not
886 */
[c5808b41]887static cproc_t tcp_conn_seg_proc_ack(tcp_conn_t *conn, tcp_segment_t *seg)
888{
[a1a101d]889 log_msg(LOG_DEFAULT, LVL_DEBUG, "%s: tcp_conn_seg_proc_ack(%p, %p)",
[23fe06c]890 conn->name, conn, seg);
[c5808b41]891
892 if ((seg->ctrl & CTL_ACK) == 0) {
[a1a101d]893 log_msg(LOG_DEFAULT, LVL_WARN, "Segment has no ACK. Dropping.");
[4c55a64]894 tcp_segment_delete(seg);
[c5808b41]895 return cp_done;
896 }
897
898 switch (conn->cstate) {
899 case st_syn_received:
900 return tcp_conn_seg_proc_ack_sr(conn, seg);
901 case st_established:
902 return tcp_conn_seg_proc_ack_est(conn, seg);
903 case st_fin_wait_1:
904 return tcp_conn_seg_proc_ack_fw1(conn, seg);
905 case st_fin_wait_2:
906 return tcp_conn_seg_proc_ack_fw2(conn, seg);
907 case st_close_wait:
908 return tcp_conn_seg_proc_ack_cw(conn, seg);
909 case st_closing:
910 return tcp_conn_seg_proc_ack_cls(conn, seg);
911 case st_last_ack:
912 return tcp_conn_seg_proc_ack_la(conn, seg);
913 case st_time_wait:
914 return tcp_conn_seg_proc_ack_tw(conn, seg);
915 case st_listen:
916 case st_syn_sent:
917 case st_closed:
918 assert(false);
919 }
920
921 assert(false);
922}
923
[032bbe7]924/** Process segment URG field.
925 *
926 * @param conn Connection
927 * @param seg Segment
928 * @return cp_done if we are done with this segment, cp_continue
929 * if not
930 */
[c5808b41]931static cproc_t tcp_conn_seg_proc_urg(tcp_conn_t *conn, tcp_segment_t *seg)
932{
933 return cp_continue;
934}
935
[032bbe7]936/** Process segment text.
937 *
938 * @param conn Connection
939 * @param seg Segment
940 * @return cp_done if we are done with this segment, cp_continue
941 * if not
942 */
[c5808b41]943static cproc_t tcp_conn_seg_proc_text(tcp_conn_t *conn, tcp_segment_t *seg)
944{
[0093ab6]945 size_t text_size;
946 size_t xfer_size;
947
[a1a101d]948 log_msg(LOG_DEFAULT, LVL_DEBUG, "%s: tcp_conn_seg_proc_text(%p, %p)",
[23fe06c]949 conn->name, conn, seg);
[0093ab6]950
[4c55a64]951 switch (conn->cstate) {
952 case st_established:
953 case st_fin_wait_1:
954 case st_fin_wait_2:
955 /* OK */
956 break;
957 case st_close_wait:
958 case st_closing:
959 case st_last_ack:
960 case st_time_wait:
961 /* Invalid since FIN has been received. Ignore text. */
962 return cp_continue;
963 case st_listen:
964 case st_syn_sent:
965 case st_syn_received:
966 case st_closed:
967 assert(false);
968 }
969
[0093ab6]970 /*
971 * Process segment text
972 */
973 assert(seq_no_segment_ready(conn, seg));
974
975 /* Trim anything outside our receive window */
976 tcp_conn_trim_seg_to_wnd(conn, seg);
977
978 /* Determine how many bytes to copy */
979 text_size = tcp_segment_text_size(seg);
980 xfer_size = min(text_size, conn->rcv_buf_size - conn->rcv_buf_used);
981
982 /* Copy data to receive buffer */
[d9ce049]983 tcp_segment_text_copy(seg, conn->rcv_buf + conn->rcv_buf_used,
984 xfer_size);
985 conn->rcv_buf_used += xfer_size;
986
987 /* Signal to the receive function that new data has arrived */
988 fibril_condvar_broadcast(&conn->rcv_buf_cv);
[0093ab6]989
[a1a101d]990 log_msg(LOG_DEFAULT, LVL_DEBUG, "Received %zu bytes of data.", xfer_size);
[32105348]991
[0093ab6]992 /* Advance RCV.NXT */
993 conn->rcv_nxt += xfer_size;
994
995 /* Update receive window. XXX Not an efficient strategy. */
996 conn->rcv_wnd -= xfer_size;
997
998 /* Send ACK */
999 if (xfer_size > 0)
1000 tcp_tqueue_ctrl_seg(conn, CTL_ACK);
1001
1002 if (xfer_size < seg->len) {
[8c7a054]1003 /* Trim part of segment which we just received */
[0093ab6]1004 tcp_conn_trim_seg_to_wnd(conn, seg);
[8c7a054]1005 } else {
[a1a101d]1006 log_msg(LOG_DEFAULT, LVL_DEBUG, "%s: Nothing left in segment, dropping "
[f0a2720]1007 "(xfer_size=%zu, SEG.LEN=%" PRIu32 ", seg->ctrl=%u)",
1008 conn->name, xfer_size, seg->len, (unsigned int) seg->ctrl);
[8c7a054]1009 /* Nothing left in segment */
1010 tcp_segment_delete(seg);
[0093ab6]1011 return cp_done;
1012 }
1013
[c5808b41]1014 return cp_continue;
1015}
1016
[032bbe7]1017/** Process segment FIN field.
1018 *
1019 * @param conn Connection
1020 * @param seg Segment
1021 * @return cp_done if we are done with this segment, cp_continue
1022 * if not
1023 */
[c5808b41]1024static cproc_t tcp_conn_seg_proc_fin(tcp_conn_t *conn, tcp_segment_t *seg)
1025{
[a1a101d]1026 log_msg(LOG_DEFAULT, LVL_DEBUG, "%s: tcp_conn_seg_proc_fin(%p, %p)",
[23fe06c]1027 conn->name, conn, seg);
[a1a101d]1028 log_msg(LOG_DEFAULT, LVL_DEBUG, " seg->len=%zu, seg->ctl=%u", (size_t) seg->len,
[7cf7ded]1029 (unsigned) seg->ctrl);
[8c7a054]1030
1031 /* Only process FIN if no text is left in segment. */
1032 if (tcp_segment_text_size(seg) == 0 && (seg->ctrl & CTL_FIN) != 0) {
[a1a101d]1033 log_msg(LOG_DEFAULT, LVL_DEBUG, " - FIN found in segment.");
[8c7a054]1034
[6e88fea]1035 /* Send ACK */
1036 tcp_tqueue_ctrl_seg(conn, CTL_ACK);
1037
[8c7a054]1038 conn->rcv_nxt++;
1039 conn->rcv_wnd--;
1040
[f343a16]1041 /* Change connection state */
1042 switch (conn->cstate) {
1043 case st_listen:
1044 case st_syn_sent:
1045 case st_closed:
1046 /* Connection not synchronized */
1047 assert(false);
1048 case st_syn_received:
1049 case st_established:
[a1a101d]1050 log_msg(LOG_DEFAULT, LVL_DEBUG, "%s: FIN received -> Close-Wait",
[6896409c]1051 conn->name);
[004a5fe]1052 tcp_conn_state_set(conn, st_close_wait);
[f343a16]1053 break;
1054 case st_fin_wait_1:
[a1a101d]1055 log_msg(LOG_DEFAULT, LVL_DEBUG, "%s: FIN received -> Closing",
[6896409c]1056 conn->name);
[004a5fe]1057 tcp_conn_state_set(conn, st_closing);
[f343a16]1058 break;
1059 case st_fin_wait_2:
[a1a101d]1060 log_msg(LOG_DEFAULT, LVL_DEBUG, "%s: FIN received -> Time-Wait",
[6896409c]1061 conn->name);
[004a5fe]1062 tcp_conn_state_set(conn, st_time_wait);
[2a3214e]1063 /* Start the Time-Wait timer */
1064 tcp_conn_tw_timer_set(conn);
[f343a16]1065 break;
1066 case st_close_wait:
1067 case st_closing:
1068 case st_last_ack:
1069 /* Do nothing */
1070 break;
1071 case st_time_wait:
[2a3214e]1072 /* Restart the Time-Wait timer */
1073 tcp_conn_tw_timer_set(conn);
[f343a16]1074 break;
1075 }
[8c7a054]1076
1077 /* Add FIN to the receive buffer */
1078 conn->rcv_buf_fin = true;
1079 fibril_condvar_broadcast(&conn->rcv_buf_cv);
1080
1081 tcp_segment_delete(seg);
1082 return cp_done;
1083 }
1084
[c5808b41]1085 return cp_continue;
1086}
1087
1088/** Process incoming segment.
1089 *
1090 * We are in connection state where segments are processed in order
1091 * of sequence number. This processes one segment taken from the
1092 * connection incoming segments queue.
[032bbe7]1093 *
1094 * @param conn Connection
1095 * @param seg Segment
[c5808b41]1096 */
1097static void tcp_conn_seg_process(tcp_conn_t *conn, tcp_segment_t *seg)
1098{
[a1a101d]1099 log_msg(LOG_DEFAULT, LVL_DEBUG, "tcp_conn_seg_process(%p, %p)", conn, seg);
[6896409c]1100 tcp_segment_dump(seg);
[c5808b41]1101
1102 /* Check whether segment is acceptable */
1103 /* XXX Permit valid ACKs, URGs and RSTs */
1104/* if (!seq_no_segment_acceptable(conn, seg)) {
[a1a101d]1105 log_msg(LOG_DEFAULT, LVL_WARN, "Segment not acceptable, dropping.");
[c5808b41]1106 if ((seg->ctrl & CTL_RST) == 0) {
1107 tcp_tqueue_ctrl_seg(conn, CTL_ACK);
1108 }
1109 return;
1110 }
1111*/
1112
1113 if (tcp_conn_seg_proc_rst(conn, seg) == cp_done)
1114 return;
1115
1116 if (tcp_conn_seg_proc_sp(conn, seg) == cp_done)
1117 return;
1118
1119 if (tcp_conn_seg_proc_syn(conn, seg) == cp_done)
1120 return;
1121
1122 if (tcp_conn_seg_proc_ack(conn, seg) == cp_done)
1123 return;
1124
1125 if (tcp_conn_seg_proc_urg(conn, seg) == cp_done)
1126 return;
1127
1128 if (tcp_conn_seg_proc_text(conn, seg) == cp_done)
1129 return;
1130
1131 if (tcp_conn_seg_proc_fin(conn, seg) == cp_done)
1132 return;
[0093ab6]1133
[8c7a054]1134 /*
1135 * If anything is left from the segment, insert it back into the
1136 * incoming segments queue.
1137 */
[7cf7ded]1138 if (seg->len > 0) {
[a1a101d]1139 log_msg(LOG_DEFAULT, LVL_DEBUG, "Re-insert segment %p. seg->len=%zu",
[7cf7ded]1140 seg, (size_t) seg->len);
[8c7a054]1141 tcp_iqueue_insert_seg(&conn->incoming, seg);
[7cf7ded]1142 } else {
[8c7a054]1143 tcp_segment_delete(seg);
[7cf7ded]1144 }
[c5808b41]1145}
1146
[032bbe7]1147/** Segment arrived on a connection.
1148 *
1149 * @param conn Connection
1150 * @param seg Segment
1151 */
[c5808b41]1152void tcp_conn_segment_arrived(tcp_conn_t *conn, tcp_segment_t *seg)
1153{
[70253688]1154 log_msg(LOG_DEFAULT, LVL_DEBUG, "%s: tcp_conn_segment_arrived(%p)",
[23fe06c]1155 conn->name, seg);
[c5808b41]1156
1157 switch (conn->cstate) {
1158 case st_listen:
1159 tcp_conn_sa_listen(conn, seg); break;
1160 case st_syn_sent:
1161 tcp_conn_sa_syn_sent(conn, seg); break;
1162 case st_syn_received:
1163 case st_established:
1164 case st_fin_wait_1:
1165 case st_fin_wait_2:
1166 case st_close_wait:
1167 case st_closing:
1168 case st_last_ack:
1169 case st_time_wait:
1170 /* Process segments in order of sequence number */
1171 tcp_conn_sa_queue(conn, seg); break;
1172 case st_closed:
[a1a101d]1173 log_msg(LOG_DEFAULT, LVL_DEBUG, "state=%d", (int) conn->cstate);
[c5808b41]1174 assert(false);
1175 }
1176}
1177
[2a3214e]1178/** Time-Wait timeout handler.
1179 *
1180 * @param arg Connection
1181 */
1182static void tw_timeout_func(void *arg)
1183{
1184 tcp_conn_t *conn = (tcp_conn_t *) arg;
1185
[a1a101d]1186 log_msg(LOG_DEFAULT, LVL_DEBUG, "tw_timeout_func(%p)", conn);
[2a3214e]1187
[0edaf0f6]1188 fibril_mutex_lock(&conn->lock);
1189
[704586fb]1190 if (conn->cstate == st_closed) {
[a1a101d]1191 log_msg(LOG_DEFAULT, LVL_DEBUG, "Connection already closed.");
[0edaf0f6]1192 fibril_mutex_unlock(&conn->lock);
1193 tcp_conn_delref(conn);
[704586fb]1194 return;
1195 }
1196
[a1a101d]1197 log_msg(LOG_DEFAULT, LVL_DEBUG, "%s: TW Timeout -> Closed", conn->name);
[2a3214e]1198 tcp_conn_remove(conn);
[004a5fe]1199 tcp_conn_state_set(conn, st_closed);
[0edaf0f6]1200
1201 fibril_mutex_unlock(&conn->lock);
1202 tcp_conn_delref(conn);
[2a3214e]1203}
1204
1205/** Start or restart the Time-Wait timeout.
1206 *
1207 * @param conn Connection
1208 */
1209void tcp_conn_tw_timer_set(tcp_conn_t *conn)
1210{
[0edaf0f6]1211 tcp_conn_addref(conn);
[2a3214e]1212 fibril_timer_set(conn->tw_timer, TIME_WAIT_TIMEOUT, tw_timeout_func,
1213 (void *)conn);
1214}
1215
[704586fb]1216/** Clear the Time-Wait timeout.
1217 *
1218 * @param conn Connection
1219 */
1220void tcp_conn_tw_timer_clear(tcp_conn_t *conn)
1221{
[0edaf0f6]1222 if (fibril_timer_clear(conn->tw_timer) == fts_active)
1223 tcp_conn_delref(conn);
[704586fb]1224}
1225
[032bbe7]1226/** Trim segment to the receive window.
1227 *
1228 * @param conn Connection
1229 * @param seg Segment
1230 */
[0093ab6]1231void tcp_conn_trim_seg_to_wnd(tcp_conn_t *conn, tcp_segment_t *seg)
1232{
1233 uint32_t left, right;
1234
1235 seq_no_seg_trim_calc(conn, seg, &left, &right);
1236 tcp_segment_trim(seg, left, right);
1237}
1238
[032bbe7]1239/** Handle unexpected segment received on a socket pair.
1240 *
1241 * We reply with an RST unless the received segment has RST.
1242 *
1243 * @param sp Socket pair which received the segment
1244 * @param seg Unexpected segment
1245 */
[c5808b41]1246void tcp_unexpected_segment(tcp_sockpair_t *sp, tcp_segment_t *seg)
1247{
[a1a101d]1248 log_msg(LOG_DEFAULT, LVL_DEBUG, "tcp_unexpected_segment(%p, %p)", sp, seg);
[c5808b41]1249
1250 if ((seg->ctrl & CTL_RST) == 0)
1251 tcp_reply_rst(sp, seg);
1252}
1253
[032bbe7]1254/** Compute flipped socket pair for response.
1255 *
[32105348]1256 * Flipped socket pair has local and foreign sockets exchanged.
[032bbe7]1257 *
1258 * @param sp Socket pair
1259 * @param fsp Place to store flipped socket pair
1260 */
[c5808b41]1261void tcp_sockpair_flipped(tcp_sockpair_t *sp, tcp_sockpair_t *fsp)
1262{
1263 fsp->local = sp->foreign;
1264 fsp->foreign = sp->local;
1265}
1266
[032bbe7]1267/** Send RST in response to an incoming segment.
1268 *
1269 * @param sp Socket pair which received the segment
1270 * @param seg Incoming segment
1271 */
[c5808b41]1272void tcp_reply_rst(tcp_sockpair_t *sp, tcp_segment_t *seg)
1273{
1274 tcp_segment_t *rseg;
1275
[a1a101d]1276 log_msg(LOG_DEFAULT, LVL_DEBUG, "tcp_reply_rst(%p, %p)", sp, seg);
[c5808b41]1277
1278 rseg = tcp_segment_make_rst(seg);
[704586fb]1279 tcp_transmit_segment(sp, rseg);
[c5808b41]1280}
1281
1282/**
1283 * @}
1284 */
Note: See TracBrowser for help on using the repository browser.