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