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