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

Last change on this file since ca48672 was 09ab0a9a, checked in by Jiri Svoboda <jiri@…>, 7 years ago

Fix vertical spacing with new Ccheck revision.

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