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

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

cherrypick trivial changes from lp:~as-s/helenos/ipv6

  • Property mode set to 100644
File size: 32.2 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{
[c0f3460]314 log_msg(LOG_DEFAULT, LVL_DEBUG2,
315 "tcp_socket_match(sock=(%u), pat=(%u))", sock->port, patt->port);
316
[a2e3ee6]317 if ((!inet_addr_is_any(&patt->addr)) &&
318 (!inet_addr_compare(&patt->addr, &sock->addr)))
[c5808b41]319 return false;
320
[a2e3ee6]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
[c0f3460]357 log_msg(LOG_DEFAULT, LVL_DEBUG2, "compare conn (f:(%u), l:(%u))",
358 sp->foreign.port, sp->local.port);
359
[0edaf0f6]360 fibril_mutex_lock(&conn_list_lock);
[f597bc4]361
[c5808b41]362 list_foreach(conn_list, link) {
363 tcp_conn_t *conn = list_get_instance(link, tcp_conn_t, link);
[704586fb]364 tcp_sockpair_t *csp = &conn->ident;
[f597bc4]365
[c0f3460]366 log_msg(LOG_DEFAULT, LVL_DEBUG2, " - with (f:(%u), l:(%u))",
367 csp->foreign.port, csp->local.port);
368
[704586fb]369 if (tcp_sockpair_match(sp, csp)) {
[0edaf0f6]370 tcp_conn_addref(conn);
371 fibril_mutex_unlock(&conn_list_lock);
[c5808b41]372 return conn;
373 }
374 }
[f597bc4]375
[0edaf0f6]376 fibril_mutex_unlock(&conn_list_lock);
[c5808b41]377 return NULL;
378}
379
[704586fb]380/** Reset connection.
381 *
382 * @param conn Connection
383 */
384static void tcp_conn_reset(tcp_conn_t *conn)
385{
[a1a101d]386 log_msg(LOG_DEFAULT, LVL_DEBUG, "%s: tcp_conn_reset()", conn->name);
[704586fb]387 tcp_conn_state_set(conn, st_closed);
[d9e14fa4]388 conn->reset = true;
389
[704586fb]390 tcp_conn_tw_timer_clear(conn);
391 tcp_tqueue_clear(&conn->retransmit);
[d9e14fa4]392
393 fibril_condvar_broadcast(&conn->rcv_buf_cv);
[bbf159a]394 fibril_condvar_broadcast(&conn->snd_buf_cv);
[d9e14fa4]395}
396
397/** Signal to the user that connection has been reset.
398 *
399 * Send an out-of-band signal to the user.
400 */
401static void tcp_reset_signal(tcp_conn_t *conn)
402{
403 /* TODO */
[a1a101d]404 log_msg(LOG_DEFAULT, LVL_DEBUG, "%s: tcp_reset_signal()", conn->name);
[704586fb]405}
406
[32105348]407/** Determine if SYN has been received.
408 *
409 * @param conn Connection
410 * @return @c true if SYN has been received, @c false otherwise.
411 */
412bool tcp_conn_got_syn(tcp_conn_t *conn)
413{
414 switch (conn->cstate) {
415 case st_listen:
416 case st_syn_sent:
417 return false;
418 case st_syn_received:
419 case st_established:
420 case st_fin_wait_1:
421 case st_fin_wait_2:
422 case st_close_wait:
423 case st_closing:
424 case st_last_ack:
425 case st_time_wait:
426 return true;
427 case st_closed:
[a1a101d]428 log_msg(LOG_DEFAULT, LVL_WARN, "state=%d", (int) conn->cstate);
[32105348]429 assert(false);
430 }
431
432 assert(false);
433}
434
[032bbe7]435/** Segment arrived in Listen state.
436 *
437 * @param conn Connection
438 * @param seg Segment
439 */
[c5808b41]440static void tcp_conn_sa_listen(tcp_conn_t *conn, tcp_segment_t *seg)
441{
[a1a101d]442 log_msg(LOG_DEFAULT, LVL_DEBUG, "tcp_conn_sa_listen(%p, %p)", conn, seg);
[c5808b41]443
444 if ((seg->ctrl & CTL_RST) != 0) {
[a1a101d]445 log_msg(LOG_DEFAULT, LVL_DEBUG, "Ignoring incoming RST.");
[c5808b41]446 return;
447 }
448
449 if ((seg->ctrl & CTL_ACK) != 0) {
[a1a101d]450 log_msg(LOG_DEFAULT, LVL_DEBUG, "Incoming ACK, send acceptable RST.");
[c5808b41]451 tcp_reply_rst(&conn->ident, seg);
452 return;
453 }
454
455 if ((seg->ctrl & CTL_SYN) == 0) {
[a1a101d]456 log_msg(LOG_DEFAULT, LVL_DEBUG, "SYN not present. Ignoring segment.");
[c5808b41]457 return;
458 }
459
[a1a101d]460 log_msg(LOG_DEFAULT, LVL_DEBUG, "Got SYN, sending SYN, ACK.");
[c5808b41]461
462 conn->rcv_nxt = seg->seq + 1;
463 conn->irs = seg->seq;
464
[4c55a64]465
[a1a101d]466 log_msg(LOG_DEFAULT, LVL_DEBUG, "rcv_nxt=%u", conn->rcv_nxt);
[c5808b41]467
468 if (seg->len > 1)
[a1a101d]469 log_msg(LOG_DEFAULT, LVL_WARN, "SYN combined with data, ignoring data.");
[c5808b41]470
471 /* XXX select ISS */
472 conn->iss = 1;
473 conn->snd_nxt = conn->iss;
474 conn->snd_una = conn->iss;
475
[4c55a64]476 /*
477 * Surprisingly the spec does not deal with initial window setting.
478 * Set SND.WND = SEG.WND and set SND.WL1 so that next segment
479 * will always be accepted as new window setting.
480 */
481 conn->snd_wnd = seg->wnd;
482 conn->snd_wl1 = seg->seq;
483 conn->snd_wl2 = seg->seq;
484
[004a5fe]485 tcp_conn_state_set(conn, st_syn_received);
[c5808b41]486
487 tcp_tqueue_ctrl_seg(conn, CTL_SYN | CTL_ACK /* XXX */);
[0093ab6]488
489 tcp_segment_delete(seg);
[c5808b41]490}
491
[032bbe7]492/** Segment arrived in Syn-Sent state.
493 *
494 * @param conn Connection
495 * @param seg Segment
496 */
[c5808b41]497static void tcp_conn_sa_syn_sent(tcp_conn_t *conn, tcp_segment_t *seg)
498{
[a1a101d]499 log_msg(LOG_DEFAULT, LVL_DEBUG, "tcp_conn_sa_syn_sent(%p, %p)", conn, seg);
[c5808b41]500
501 if ((seg->ctrl & CTL_ACK) != 0) {
[a1a101d]502 log_msg(LOG_DEFAULT, LVL_DEBUG, "snd_una=%u, seg.ack=%u, snd_nxt=%u",
[c5808b41]503 conn->snd_una, seg->ack, conn->snd_nxt);
504 if (!seq_no_ack_acceptable(conn, seg->ack)) {
[2f0dd2a]505 if ((seg->ctrl & CTL_RST) == 0) {
[a1a101d]506 log_msg(LOG_DEFAULT, LVL_WARN, "ACK not acceptable, send RST");
[2f0dd2a]507 tcp_reply_rst(&conn->ident, seg);
508 } else {
[a1a101d]509 log_msg(LOG_DEFAULT, LVL_WARN, "RST,ACK not acceptable, drop");
[2f0dd2a]510 }
[c5808b41]511 return;
512 }
513 }
514
515 if ((seg->ctrl & CTL_RST) != 0) {
[2f0dd2a]516 /* If we get here, we have either an acceptable ACK or no ACK */
517 if ((seg->ctrl & CTL_ACK) != 0) {
[a1a101d]518 log_msg(LOG_DEFAULT, LVL_DEBUG, "%s: Connection reset. -> Closed",
[2f0dd2a]519 conn->name);
520 /* Reset connection */
521 tcp_conn_reset(conn);
522 return;
523 } else {
[a1a101d]524 log_msg(LOG_DEFAULT, LVL_DEBUG, "%s: RST without ACK, drop",
[2f0dd2a]525 conn->name);
526 return;
527 }
[c5808b41]528 }
529
530 /* XXX precedence */
531
532 if ((seg->ctrl & CTL_SYN) == 0) {
[a1a101d]533 log_msg(LOG_DEFAULT, LVL_DEBUG, "No SYN bit, ignoring segment.");
[c5808b41]534 return;
535 }
536
537 conn->rcv_nxt = seg->seq + 1;
538 conn->irs = seg->seq;
539
540 if ((seg->ctrl & CTL_ACK) != 0) {
541 conn->snd_una = seg->ack;
[d9ce049]542
543 /*
544 * Prune acked segments from retransmission queue and
545 * possibly transmit more data.
546 */
547 tcp_tqueue_ack_received(conn);
[c5808b41]548 }
549
[a1a101d]550 log_msg(LOG_DEFAULT, LVL_DEBUG, "Sent SYN, got SYN.");
[c5808b41]551
[32105348]552 /*
553 * Surprisingly the spec does not deal with initial window setting.
554 * Set SND.WND = SEG.WND and set SND.WL1 so that next segment
555 * will always be accepted as new window setting.
556 */
[a1a101d]557 log_msg(LOG_DEFAULT, LVL_DEBUG, "SND.WND := %" PRIu32 ", SND.WL1 := %" PRIu32 ", "
[32105348]558 "SND.WL2 = %" PRIu32, seg->wnd, seg->seq, seg->seq);
559 conn->snd_wnd = seg->wnd;
560 conn->snd_wl1 = seg->seq;
561 conn->snd_wl2 = seg->seq;
562
[c5808b41]563 if (seq_no_syn_acked(conn)) {
[a1a101d]564 log_msg(LOG_DEFAULT, LVL_DEBUG, "%s: syn acked -> Established", conn->name);
[004a5fe]565 tcp_conn_state_set(conn, st_established);
[c5808b41]566 tcp_tqueue_ctrl_seg(conn, CTL_ACK /* XXX */);
567 } else {
[a1a101d]568 log_msg(LOG_DEFAULT, LVL_DEBUG, "%s: syn not acked -> Syn-Received",
[6896409c]569 conn->name);
[004a5fe]570 tcp_conn_state_set(conn, st_syn_received);
[c5808b41]571 tcp_tqueue_ctrl_seg(conn, CTL_SYN | CTL_ACK /* XXX */);
572 }
[0093ab6]573
574 tcp_segment_delete(seg);
[c5808b41]575}
576
[032bbe7]577/** Segment arrived in state where segments are processed in sequence order.
578 *
579 * Queue segment in incoming segments queue for processing.
580 *
581 * @param conn Connection
582 * @param seg Segment
583 */
[c5808b41]584static void tcp_conn_sa_queue(tcp_conn_t *conn, tcp_segment_t *seg)
585{
586 tcp_segment_t *pseg;
587
[a1a101d]588 log_msg(LOG_DEFAULT, LVL_DEBUG, "tcp_conn_sa_seq(%p, %p)", conn, seg);
[c5808b41]589
[7cf7ded]590 /* Discard unacceptable segments ("old duplicates") */
591 if (!seq_no_segment_acceptable(conn, seg)) {
[a1a101d]592 log_msg(LOG_DEFAULT, LVL_DEBUG, "Replying ACK to unacceptable segment.");
[7cf7ded]593 tcp_tqueue_ctrl_seg(conn, CTL_ACK);
594 tcp_segment_delete(seg);
595 return;
596 }
[c5808b41]597
598 /* Queue for processing */
599 tcp_iqueue_insert_seg(&conn->incoming, seg);
600
601 /*
602 * Process all segments from incoming queue that are ready.
603 * Unacceptable segments are discarded by tcp_iqueue_get_ready_seg().
604 *
605 * XXX Need to return ACK for unacceptable segments
606 */
607 while (tcp_iqueue_get_ready_seg(&conn->incoming, &pseg) == EOK)
608 tcp_conn_seg_process(conn, pseg);
609}
610
[032bbe7]611/** Process segment RST field.
612 *
613 * @param conn Connection
614 * @param seg Segment
615 * @return cp_done if we are done with this segment, cp_continue
616 * if not
617 */
[c5808b41]618static cproc_t tcp_conn_seg_proc_rst(tcp_conn_t *conn, tcp_segment_t *seg)
619{
[03be171]620 if ((seg->ctrl & CTL_RST) == 0)
621 return cp_continue;
622
[d9e14fa4]623 switch (conn->cstate) {
624 case st_syn_received:
625 /* XXX In case of passive open, revert to Listen state */
626 if (conn->ap == ap_passive) {
627 tcp_conn_state_set(conn, st_listen);
628 /* XXX Revert conn->ident */
629 tcp_conn_tw_timer_clear(conn);
630 tcp_tqueue_clear(&conn->retransmit);
631 } else {
632 tcp_conn_reset(conn);
633 }
634 break;
635 case st_established:
636 case st_fin_wait_1:
637 case st_fin_wait_2:
638 case st_close_wait:
639 /* General "connection reset" signal */
640 tcp_reset_signal(conn);
641 tcp_conn_reset(conn);
642 break;
643 case st_closing:
644 case st_last_ack:
645 case st_time_wait:
646 tcp_conn_reset(conn);
647 break;
648 case st_listen:
649 case st_syn_sent:
650 case st_closed:
651 assert(false);
652 }
653
654 return cp_done;
[c5808b41]655}
656
[032bbe7]657/** Process segment security and precedence fields.
658 *
659 * @param conn Connection
660 * @param seg Segment
661 * @return cp_done if we are done with this segment, cp_continue
662 * if not
663 */
[c5808b41]664static cproc_t tcp_conn_seg_proc_sp(tcp_conn_t *conn, tcp_segment_t *seg)
665{
[4c55a64]666 /* TODO */
[c5808b41]667 return cp_continue;
668}
669
[032bbe7]670/** Process segment SYN field.
671 *
672 * @param conn Connection
673 * @param seg Segment
674 * @return cp_done if we are done with this segment, cp_continue
675 * if not
676 */
[c5808b41]677static cproc_t tcp_conn_seg_proc_syn(tcp_conn_t *conn, tcp_segment_t *seg)
678{
[7cf7ded]679 if ((seg->ctrl & CTL_SYN) == 0)
680 return cp_continue;
681
682 /*
683 * Assert SYN is in receive window, otherwise this step should not
684 * be reached.
685 */
686 assert(seq_no_in_rcv_wnd(conn, seg->seq));
687
[a1a101d]688 log_msg(LOG_DEFAULT, LVL_WARN, "SYN is in receive window, should send reset. XXX");
[7cf7ded]689
690 /*
691 * TODO
692 *
693 * Send a reset, resond "reset" to all outstanding RECEIVEs and SEND,
694 * flush segment queues. Send unsolicited "connection reset" signal
695 * to user, connection -> closed state, delete TCB, return.
696 */
697 return cp_done;
[c5808b41]698}
699
[032bbe7]700/** Process segment ACK field in Syn-Received state.
701 *
702 * @param conn Connection
703 * @param seg Segment
704 * @return cp_done if we are done with this segment, cp_continue
705 * if not
706 */
[c5808b41]707static cproc_t tcp_conn_seg_proc_ack_sr(tcp_conn_t *conn, tcp_segment_t *seg)
708{
709 if (!seq_no_ack_acceptable(conn, seg->ack)) {
710 /* ACK is not acceptable, send RST. */
[a1a101d]711 log_msg(LOG_DEFAULT, LVL_WARN, "Segment ACK not acceptable, sending RST.");
[c5808b41]712 tcp_reply_rst(&conn->ident, seg);
[4c55a64]713 tcp_segment_delete(seg);
714 return cp_done;
[c5808b41]715 }
716
[a1a101d]717 log_msg(LOG_DEFAULT, LVL_DEBUG, "%s: SYN ACKed -> Established", conn->name);
[c5808b41]718
[004a5fe]719 tcp_conn_state_set(conn, st_established);
[c5808b41]720
721 /* XXX Not mentioned in spec?! */
722 conn->snd_una = seg->ack;
723
724 return cp_continue;
725}
726
[032bbe7]727/** Process segment ACK field in Established state.
728 *
729 * @param conn Connection
730 * @param seg Segment
731 * @return cp_done if we are done with this segment, cp_continue
732 * if not
733 */
[c5808b41]734static cproc_t tcp_conn_seg_proc_ack_est(tcp_conn_t *conn, tcp_segment_t *seg)
735{
[a1a101d]736 log_msg(LOG_DEFAULT, LVL_DEBUG, "tcp_conn_seg_proc_ack_est(%p, %p)", conn, seg);
[0093ab6]737
[a1a101d]738 log_msg(LOG_DEFAULT, LVL_DEBUG, "SEG.ACK=%u, SND.UNA=%u, SND.NXT=%u",
[0093ab6]739 (unsigned)seg->ack, (unsigned)conn->snd_una,
740 (unsigned)conn->snd_nxt);
741
[4c55a64]742 if (!seq_no_ack_acceptable(conn, seg->ack)) {
[a1a101d]743 log_msg(LOG_DEFAULT, LVL_DEBUG, "ACK not acceptable.");
[4c55a64]744 if (!seq_no_ack_duplicate(conn, seg->ack)) {
[a1a101d]745 log_msg(LOG_DEFAULT, LVL_WARN, "Not acceptable, not duplicate. "
[0093ab6]746 "Send ACK and drop.");
[4c55a64]747 /* Not acceptable, not duplicate. Send ACK and drop. */
748 tcp_tqueue_ctrl_seg(conn, CTL_ACK);
749 tcp_segment_delete(seg);
750 return cp_done;
[0093ab6]751 } else {
[a1a101d]752 log_msg(LOG_DEFAULT, LVL_DEBUG, "Ignoring duplicate ACK.");
[4c55a64]753 }
754 } else {
755 /* Update SND.UNA */
756 conn->snd_una = seg->ack;
757 }
758
759 if (seq_no_new_wnd_update(conn, seg)) {
760 conn->snd_wnd = seg->wnd;
761 conn->snd_wl1 = seg->seq;
762 conn->snd_wl2 = seg->ack;
[d9ce049]763
[a1a101d]764 log_msg(LOG_DEFAULT, LVL_DEBUG, "Updating send window, SND.WND=%" PRIu32
[d9ce049]765 ", SND.WL1=%" PRIu32 ", SND.WL2=%" PRIu32,
766 conn->snd_wnd, conn->snd_wl1, conn->snd_wl2);
[4c55a64]767 }
768
[d9ce049]769 /*
770 * Prune acked segments from retransmission queue and
771 * possibly transmit more data.
772 */
773 tcp_tqueue_ack_received(conn);
774
[c5808b41]775 return cp_continue;
776}
777
[032bbe7]778/** Process segment ACK field in Fin-Wait-1 state.
779 *
780 * @param conn Connection
781 * @param seg Segment
782 * @return cp_done if we are done with this segment, cp_continue
783 * if not
784 */
[c5808b41]785static cproc_t tcp_conn_seg_proc_ack_fw1(tcp_conn_t *conn, tcp_segment_t *seg)
786{
[4c55a64]787 if (tcp_conn_seg_proc_ack_est(conn, seg) == cp_done)
788 return cp_done;
789
[6df418c4]790 if (conn->fin_is_acked) {
[a1a101d]791 log_msg(LOG_DEFAULT, LVL_DEBUG, "%s: FIN acked -> Fin-Wait-2", conn->name);
[004a5fe]792 tcp_conn_state_set(conn, st_fin_wait_2);
[6df418c4]793 }
794
[c5808b41]795 return cp_continue;
796}
797
[032bbe7]798/** Process segment ACK field in Fin-Wait-2 state.
799 *
800 * @param conn Connection
801 * @param seg Segment
802 * @return cp_done if we are done with this segment, cp_continue
803 * if not
804 */
[c5808b41]805static cproc_t tcp_conn_seg_proc_ack_fw2(tcp_conn_t *conn, tcp_segment_t *seg)
806{
[4c55a64]807 if (tcp_conn_seg_proc_ack_est(conn, seg) == cp_done)
808 return cp_done;
809
810 /* TODO */
[c5808b41]811 return cp_continue;
812}
813
[032bbe7]814/** Process segment ACK field in Close-Wait state.
815 *
816 * @param conn Connection
817 * @param seg Segment
818 * @return cp_done if we are done with this segment, cp_continue
819 * if not
820 */
[c5808b41]821static cproc_t tcp_conn_seg_proc_ack_cw(tcp_conn_t *conn, tcp_segment_t *seg)
822{
[4c55a64]823 /* The same processing as in Established state */
824 return tcp_conn_seg_proc_ack_est(conn, seg);
[c5808b41]825}
826
[032bbe7]827/** Process segment ACK field in Closing state.
828 *
829 * @param conn Connection
830 * @param seg Segment
831 * @return cp_done if we are done with this segment, cp_continue
832 * if not
833 */
[c5808b41]834static cproc_t tcp_conn_seg_proc_ack_cls(tcp_conn_t *conn, tcp_segment_t *seg)
835{
[4c55a64]836 if (tcp_conn_seg_proc_ack_est(conn, seg) == cp_done)
837 return cp_done;
838
839 /* TODO */
[c5808b41]840 return cp_continue;
841}
842
[032bbe7]843/** Process segment ACK field in Last-Ack state.
844 *
845 * @param conn Connection
846 * @param seg Segment
847 * @return cp_done if we are done with this segment, cp_continue
848 * if not
849 */
[c5808b41]850static cproc_t tcp_conn_seg_proc_ack_la(tcp_conn_t *conn, tcp_segment_t *seg)
851{
[4c55a64]852 if (tcp_conn_seg_proc_ack_est(conn, seg) == cp_done)
853 return cp_done;
854
[6e88fea]855 if (conn->fin_is_acked) {
[a1a101d]856 log_msg(LOG_DEFAULT, LVL_DEBUG, "%s: FIN acked -> Closed", conn->name);
[6e88fea]857 tcp_conn_remove(conn);
[004a5fe]858 tcp_conn_state_set(conn, st_closed);
[6e88fea]859 return cp_done;
860 }
861
[c5808b41]862 return cp_continue;
863}
864
[032bbe7]865/** Process segment ACK field in Time-Wait state.
866 *
867 * @param conn Connection
868 * @param seg Segment
869 * @return cp_done if we are done with this segment, cp_continue
870 * if not
871 */
[c5808b41]872static cproc_t tcp_conn_seg_proc_ack_tw(tcp_conn_t *conn, tcp_segment_t *seg)
873{
[4c55a64]874 /* Nothing to do */
[c5808b41]875 return cp_continue;
876}
877
[032bbe7]878/** Process segment ACK field.
879 *
880 * @param conn Connection
881 * @param seg Segment
882 * @return cp_done if we are done with this segment, cp_continue
883 * if not
884 */
[c5808b41]885static cproc_t tcp_conn_seg_proc_ack(tcp_conn_t *conn, tcp_segment_t *seg)
886{
[a1a101d]887 log_msg(LOG_DEFAULT, LVL_DEBUG, "%s: tcp_conn_seg_proc_ack(%p, %p)",
[23fe06c]888 conn->name, conn, seg);
[c5808b41]889
890 if ((seg->ctrl & CTL_ACK) == 0) {
[a1a101d]891 log_msg(LOG_DEFAULT, LVL_WARN, "Segment has no ACK. Dropping.");
[4c55a64]892 tcp_segment_delete(seg);
[c5808b41]893 return cp_done;
894 }
895
896 switch (conn->cstate) {
897 case st_syn_received:
898 return tcp_conn_seg_proc_ack_sr(conn, seg);
899 case st_established:
900 return tcp_conn_seg_proc_ack_est(conn, seg);
901 case st_fin_wait_1:
902 return tcp_conn_seg_proc_ack_fw1(conn, seg);
903 case st_fin_wait_2:
904 return tcp_conn_seg_proc_ack_fw2(conn, seg);
905 case st_close_wait:
906 return tcp_conn_seg_proc_ack_cw(conn, seg);
907 case st_closing:
908 return tcp_conn_seg_proc_ack_cls(conn, seg);
909 case st_last_ack:
910 return tcp_conn_seg_proc_ack_la(conn, seg);
911 case st_time_wait:
912 return tcp_conn_seg_proc_ack_tw(conn, seg);
913 case st_listen:
914 case st_syn_sent:
915 case st_closed:
916 assert(false);
917 }
918
919 assert(false);
920}
921
[032bbe7]922/** Process segment URG field.
923 *
924 * @param conn Connection
925 * @param seg Segment
926 * @return cp_done if we are done with this segment, cp_continue
927 * if not
928 */
[c5808b41]929static cproc_t tcp_conn_seg_proc_urg(tcp_conn_t *conn, tcp_segment_t *seg)
930{
931 return cp_continue;
932}
933
[032bbe7]934/** Process segment text.
935 *
936 * @param conn Connection
937 * @param seg Segment
938 * @return cp_done if we are done with this segment, cp_continue
939 * if not
940 */
[c5808b41]941static cproc_t tcp_conn_seg_proc_text(tcp_conn_t *conn, tcp_segment_t *seg)
942{
[0093ab6]943 size_t text_size;
944 size_t xfer_size;
945
[a1a101d]946 log_msg(LOG_DEFAULT, LVL_DEBUG, "%s: tcp_conn_seg_proc_text(%p, %p)",
[23fe06c]947 conn->name, conn, seg);
[0093ab6]948
[4c55a64]949 switch (conn->cstate) {
950 case st_established:
951 case st_fin_wait_1:
952 case st_fin_wait_2:
953 /* OK */
954 break;
955 case st_close_wait:
956 case st_closing:
957 case st_last_ack:
958 case st_time_wait:
959 /* Invalid since FIN has been received. Ignore text. */
960 return cp_continue;
961 case st_listen:
962 case st_syn_sent:
963 case st_syn_received:
964 case st_closed:
965 assert(false);
966 }
967
[0093ab6]968 /*
969 * Process segment text
970 */
971 assert(seq_no_segment_ready(conn, seg));
972
973 /* Trim anything outside our receive window */
974 tcp_conn_trim_seg_to_wnd(conn, seg);
975
976 /* Determine how many bytes to copy */
977 text_size = tcp_segment_text_size(seg);
978 xfer_size = min(text_size, conn->rcv_buf_size - conn->rcv_buf_used);
979
980 /* Copy data to receive buffer */
[d9ce049]981 tcp_segment_text_copy(seg, conn->rcv_buf + conn->rcv_buf_used,
982 xfer_size);
983 conn->rcv_buf_used += xfer_size;
984
985 /* Signal to the receive function that new data has arrived */
986 fibril_condvar_broadcast(&conn->rcv_buf_cv);
[0093ab6]987
[a1a101d]988 log_msg(LOG_DEFAULT, LVL_DEBUG, "Received %zu bytes of data.", xfer_size);
[32105348]989
[0093ab6]990 /* Advance RCV.NXT */
991 conn->rcv_nxt += xfer_size;
992
993 /* Update receive window. XXX Not an efficient strategy. */
994 conn->rcv_wnd -= xfer_size;
995
996 /* Send ACK */
997 if (xfer_size > 0)
998 tcp_tqueue_ctrl_seg(conn, CTL_ACK);
999
1000 if (xfer_size < seg->len) {
[8c7a054]1001 /* Trim part of segment which we just received */
[0093ab6]1002 tcp_conn_trim_seg_to_wnd(conn, seg);
[8c7a054]1003 } else {
[a1a101d]1004 log_msg(LOG_DEFAULT, LVL_DEBUG, "%s: Nothing left in segment, dropping "
[f0a2720]1005 "(xfer_size=%zu, SEG.LEN=%" PRIu32 ", seg->ctrl=%u)",
1006 conn->name, xfer_size, seg->len, (unsigned int) seg->ctrl);
[8c7a054]1007 /* Nothing left in segment */
1008 tcp_segment_delete(seg);
[0093ab6]1009 return cp_done;
1010 }
1011
[c5808b41]1012 return cp_continue;
1013}
1014
[032bbe7]1015/** Process segment FIN field.
1016 *
1017 * @param conn Connection
1018 * @param seg Segment
1019 * @return cp_done if we are done with this segment, cp_continue
1020 * if not
1021 */
[c5808b41]1022static cproc_t tcp_conn_seg_proc_fin(tcp_conn_t *conn, tcp_segment_t *seg)
1023{
[a1a101d]1024 log_msg(LOG_DEFAULT, LVL_DEBUG, "%s: tcp_conn_seg_proc_fin(%p, %p)",
[23fe06c]1025 conn->name, conn, seg);
[a1a101d]1026 log_msg(LOG_DEFAULT, LVL_DEBUG, " seg->len=%zu, seg->ctl=%u", (size_t) seg->len,
[7cf7ded]1027 (unsigned) seg->ctrl);
[8c7a054]1028
1029 /* Only process FIN if no text is left in segment. */
1030 if (tcp_segment_text_size(seg) == 0 && (seg->ctrl & CTL_FIN) != 0) {
[a1a101d]1031 log_msg(LOG_DEFAULT, LVL_DEBUG, " - FIN found in segment.");
[8c7a054]1032
[6e88fea]1033 /* Send ACK */
1034 tcp_tqueue_ctrl_seg(conn, CTL_ACK);
1035
[8c7a054]1036 conn->rcv_nxt++;
1037 conn->rcv_wnd--;
1038
[f343a16]1039 /* Change connection state */
1040 switch (conn->cstate) {
1041 case st_listen:
1042 case st_syn_sent:
1043 case st_closed:
1044 /* Connection not synchronized */
1045 assert(false);
1046 case st_syn_received:
1047 case st_established:
[a1a101d]1048 log_msg(LOG_DEFAULT, LVL_DEBUG, "%s: FIN received -> Close-Wait",
[6896409c]1049 conn->name);
[004a5fe]1050 tcp_conn_state_set(conn, st_close_wait);
[f343a16]1051 break;
1052 case st_fin_wait_1:
[a1a101d]1053 log_msg(LOG_DEFAULT, LVL_DEBUG, "%s: FIN received -> Closing",
[6896409c]1054 conn->name);
[004a5fe]1055 tcp_conn_state_set(conn, st_closing);
[f343a16]1056 break;
1057 case st_fin_wait_2:
[a1a101d]1058 log_msg(LOG_DEFAULT, LVL_DEBUG, "%s: FIN received -> Time-Wait",
[6896409c]1059 conn->name);
[004a5fe]1060 tcp_conn_state_set(conn, st_time_wait);
[2a3214e]1061 /* Start the Time-Wait timer */
1062 tcp_conn_tw_timer_set(conn);
[f343a16]1063 break;
1064 case st_close_wait:
1065 case st_closing:
1066 case st_last_ack:
1067 /* Do nothing */
1068 break;
1069 case st_time_wait:
[2a3214e]1070 /* Restart the Time-Wait timer */
1071 tcp_conn_tw_timer_set(conn);
[f343a16]1072 break;
1073 }
[8c7a054]1074
1075 /* Add FIN to the receive buffer */
1076 conn->rcv_buf_fin = true;
1077 fibril_condvar_broadcast(&conn->rcv_buf_cv);
1078
1079 tcp_segment_delete(seg);
1080 return cp_done;
1081 }
1082
[c5808b41]1083 return cp_continue;
1084}
1085
1086/** Process incoming segment.
1087 *
1088 * We are in connection state where segments are processed in order
1089 * of sequence number. This processes one segment taken from the
1090 * connection incoming segments queue.
[032bbe7]1091 *
1092 * @param conn Connection
1093 * @param seg Segment
[c5808b41]1094 */
1095static void tcp_conn_seg_process(tcp_conn_t *conn, tcp_segment_t *seg)
1096{
[a1a101d]1097 log_msg(LOG_DEFAULT, LVL_DEBUG, "tcp_conn_seg_process(%p, %p)", conn, seg);
[6896409c]1098 tcp_segment_dump(seg);
[c5808b41]1099
1100 /* Check whether segment is acceptable */
1101 /* XXX Permit valid ACKs, URGs and RSTs */
1102/* if (!seq_no_segment_acceptable(conn, seg)) {
[a1a101d]1103 log_msg(LOG_DEFAULT, LVL_WARN, "Segment not acceptable, dropping.");
[c5808b41]1104 if ((seg->ctrl & CTL_RST) == 0) {
1105 tcp_tqueue_ctrl_seg(conn, CTL_ACK);
1106 }
1107 return;
1108 }
1109*/
1110
1111 if (tcp_conn_seg_proc_rst(conn, seg) == cp_done)
1112 return;
1113
1114 if (tcp_conn_seg_proc_sp(conn, seg) == cp_done)
1115 return;
1116
1117 if (tcp_conn_seg_proc_syn(conn, seg) == cp_done)
1118 return;
1119
1120 if (tcp_conn_seg_proc_ack(conn, seg) == cp_done)
1121 return;
1122
1123 if (tcp_conn_seg_proc_urg(conn, seg) == cp_done)
1124 return;
1125
1126 if (tcp_conn_seg_proc_text(conn, seg) == cp_done)
1127 return;
1128
1129 if (tcp_conn_seg_proc_fin(conn, seg) == cp_done)
1130 return;
[0093ab6]1131
[8c7a054]1132 /*
1133 * If anything is left from the segment, insert it back into the
1134 * incoming segments queue.
1135 */
[7cf7ded]1136 if (seg->len > 0) {
[a1a101d]1137 log_msg(LOG_DEFAULT, LVL_DEBUG, "Re-insert segment %p. seg->len=%zu",
[7cf7ded]1138 seg, (size_t) seg->len);
[8c7a054]1139 tcp_iqueue_insert_seg(&conn->incoming, seg);
[7cf7ded]1140 } else {
[8c7a054]1141 tcp_segment_delete(seg);
[7cf7ded]1142 }
[c5808b41]1143}
1144
[032bbe7]1145/** Segment arrived on a connection.
1146 *
1147 * @param conn Connection
1148 * @param seg Segment
1149 */
[c5808b41]1150void tcp_conn_segment_arrived(tcp_conn_t *conn, tcp_segment_t *seg)
1151{
[70253688]1152 log_msg(LOG_DEFAULT, LVL_DEBUG, "%s: tcp_conn_segment_arrived(%p)",
[23fe06c]1153 conn->name, seg);
[c5808b41]1154
1155 switch (conn->cstate) {
1156 case st_listen:
1157 tcp_conn_sa_listen(conn, seg); break;
1158 case st_syn_sent:
1159 tcp_conn_sa_syn_sent(conn, seg); break;
1160 case st_syn_received:
1161 case st_established:
1162 case st_fin_wait_1:
1163 case st_fin_wait_2:
1164 case st_close_wait:
1165 case st_closing:
1166 case st_last_ack:
1167 case st_time_wait:
1168 /* Process segments in order of sequence number */
1169 tcp_conn_sa_queue(conn, seg); break;
1170 case st_closed:
[a1a101d]1171 log_msg(LOG_DEFAULT, LVL_DEBUG, "state=%d", (int) conn->cstate);
[c5808b41]1172 assert(false);
1173 }
1174}
1175
[2a3214e]1176/** Time-Wait timeout handler.
1177 *
1178 * @param arg Connection
1179 */
1180static void tw_timeout_func(void *arg)
1181{
1182 tcp_conn_t *conn = (tcp_conn_t *) arg;
1183
[a1a101d]1184 log_msg(LOG_DEFAULT, LVL_DEBUG, "tw_timeout_func(%p)", conn);
[2a3214e]1185
[0edaf0f6]1186 fibril_mutex_lock(&conn->lock);
1187
[704586fb]1188 if (conn->cstate == st_closed) {
[a1a101d]1189 log_msg(LOG_DEFAULT, LVL_DEBUG, "Connection already closed.");
[0edaf0f6]1190 fibril_mutex_unlock(&conn->lock);
1191 tcp_conn_delref(conn);
[704586fb]1192 return;
1193 }
1194
[a1a101d]1195 log_msg(LOG_DEFAULT, LVL_DEBUG, "%s: TW Timeout -> Closed", conn->name);
[2a3214e]1196 tcp_conn_remove(conn);
[004a5fe]1197 tcp_conn_state_set(conn, st_closed);
[0edaf0f6]1198
1199 fibril_mutex_unlock(&conn->lock);
1200 tcp_conn_delref(conn);
[2a3214e]1201}
1202
1203/** Start or restart the Time-Wait timeout.
1204 *
1205 * @param conn Connection
1206 */
1207void tcp_conn_tw_timer_set(tcp_conn_t *conn)
1208{
[0edaf0f6]1209 tcp_conn_addref(conn);
[2a3214e]1210 fibril_timer_set(conn->tw_timer, TIME_WAIT_TIMEOUT, tw_timeout_func,
1211 (void *)conn);
1212}
1213
[704586fb]1214/** Clear the Time-Wait timeout.
1215 *
1216 * @param conn Connection
1217 */
1218void tcp_conn_tw_timer_clear(tcp_conn_t *conn)
1219{
[0edaf0f6]1220 if (fibril_timer_clear(conn->tw_timer) == fts_active)
1221 tcp_conn_delref(conn);
[704586fb]1222}
1223
[032bbe7]1224/** Trim segment to the receive window.
1225 *
1226 * @param conn Connection
1227 * @param seg Segment
1228 */
[0093ab6]1229void tcp_conn_trim_seg_to_wnd(tcp_conn_t *conn, tcp_segment_t *seg)
1230{
1231 uint32_t left, right;
1232
1233 seq_no_seg_trim_calc(conn, seg, &left, &right);
1234 tcp_segment_trim(seg, left, right);
1235}
1236
[032bbe7]1237/** Handle unexpected segment received on a socket pair.
1238 *
1239 * We reply with an RST unless the received segment has RST.
1240 *
1241 * @param sp Socket pair which received the segment
1242 * @param seg Unexpected segment
1243 */
[c5808b41]1244void tcp_unexpected_segment(tcp_sockpair_t *sp, tcp_segment_t *seg)
1245{
[a1a101d]1246 log_msg(LOG_DEFAULT, LVL_DEBUG, "tcp_unexpected_segment(%p, %p)", sp, seg);
[c5808b41]1247
1248 if ((seg->ctrl & CTL_RST) == 0)
1249 tcp_reply_rst(sp, seg);
1250}
1251
[032bbe7]1252/** Compute flipped socket pair for response.
1253 *
[32105348]1254 * Flipped socket pair has local and foreign sockets exchanged.
[032bbe7]1255 *
1256 * @param sp Socket pair
1257 * @param fsp Place to store flipped socket pair
1258 */
[c5808b41]1259void tcp_sockpair_flipped(tcp_sockpair_t *sp, tcp_sockpair_t *fsp)
1260{
1261 fsp->local = sp->foreign;
1262 fsp->foreign = sp->local;
1263}
1264
[032bbe7]1265/** Send RST in response to an incoming segment.
1266 *
1267 * @param sp Socket pair which received the segment
1268 * @param seg Incoming segment
1269 */
[c5808b41]1270void tcp_reply_rst(tcp_sockpair_t *sp, tcp_segment_t *seg)
1271{
1272 tcp_segment_t *rseg;
1273
[a1a101d]1274 log_msg(LOG_DEFAULT, LVL_DEBUG, "tcp_reply_rst(%p, %p)", sp, seg);
[c5808b41]1275
1276 rseg = tcp_segment_make_rst(seg);
[704586fb]1277 tcp_transmit_segment(sp, rseg);
[c5808b41]1278}
1279
1280/**
1281 * @}
1282 */
Note: See TracBrowser for help on using the repository browser.