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