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