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