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

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

Add unit tests for TCP tqueue. Fix tqueue possibly being finalized without freeing pending segments.

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