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

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since fab2746 was 78192cc7, checked in by Jiri Svoboda <jiri@…>, 11 years ago

Fibril timer locking improvements.

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