blob: e9c52c5e27f8c61942ededa9ba994d54a86302c2 [file] [log] [blame]
Dave Barach68b0fb02017-02-28 15:15:56 -05001/*
2 * Copyright (c) 2016 Cisco and/or its affiliates.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at:
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16#include <vppinfra/sparse_vec.h>
17#include <vnet/tcp/tcp_packet.h>
18#include <vnet/tcp/tcp.h>
19#include <vnet/session/session.h>
20#include <math.h>
21
22static char *tcp_error_strings[] = {
23#define tcp_error(n,s) s,
24#include <vnet/tcp/tcp_error.def>
25#undef tcp_error
26};
27
28/* All TCP nodes have the same outgoing arcs */
29#define foreach_tcp_state_next \
30 _ (DROP, "error-drop") \
31 _ (TCP4_OUTPUT, "tcp4-output") \
32 _ (TCP6_OUTPUT, "tcp6-output")
33
34typedef enum _tcp_established_next
35{
36#define _(s,n) TCP_ESTABLISHED_NEXT_##s,
37 foreach_tcp_state_next
38#undef _
39 TCP_ESTABLISHED_N_NEXT,
40} tcp_established_next_t;
41
42typedef enum _tcp_rcv_process_next
43{
44#define _(s,n) TCP_RCV_PROCESS_NEXT_##s,
45 foreach_tcp_state_next
46#undef _
47 TCP_RCV_PROCESS_N_NEXT,
48} tcp_rcv_process_next_t;
49
50typedef enum _tcp_syn_sent_next
51{
52#define _(s,n) TCP_SYN_SENT_NEXT_##s,
53 foreach_tcp_state_next
54#undef _
55 TCP_SYN_SENT_N_NEXT,
56} tcp_syn_sent_next_t;
57
58typedef enum _tcp_listen_next
59{
60#define _(s,n) TCP_LISTEN_NEXT_##s,
61 foreach_tcp_state_next
62#undef _
63 TCP_LISTEN_N_NEXT,
64} tcp_listen_next_t;
65
66/* Generic, state independent indices */
67typedef enum _tcp_state_next
68{
69#define _(s,n) TCP_NEXT_##s,
70 foreach_tcp_state_next
71#undef _
72 TCP_STATE_N_NEXT,
73} tcp_state_next_t;
74
75#define tcp_next_output(is_ip4) (is_ip4 ? TCP_NEXT_TCP4_OUTPUT \
76 : TCP_NEXT_TCP6_OUTPUT)
77
78vlib_node_registration_t tcp4_established_node;
79vlib_node_registration_t tcp6_established_node;
80
81/**
82 * Validate segment sequence number. As per RFC793:
83 *
84 * Segment Receive Test
85 * Length Window
86 * ------- ------- -------------------------------------------
87 * 0 0 SEG.SEQ = RCV.NXT
88 * 0 >0 RCV.NXT =< SEG.SEQ < RCV.NXT+RCV.WND
89 * >0 0 not acceptable
90 * >0 >0 RCV.NXT =< SEG.SEQ < RCV.NXT+RCV.WND
91 * or RCV.NXT =< SEG.SEQ+SEG.LEN-1 < RCV.NXT+RCV.WND
92 *
93 * This ultimately consists in checking if segment falls within the window.
94 * The one important difference compared to RFC793 is that we use rcv_las,
95 * or the rcv_nxt at last ack sent instead of rcv_nxt since that's the
96 * peer's reference when computing our receive window.
97 *
Florin Coras6792ec02017-03-13 03:49:51 -070098 * This:
99 * seq_leq (end_seq, tc->rcv_las + tc->rcv_wnd) && seq_geq (seq, tc->rcv_las)
100 * however, is too strict when we have retransmits. Instead we just check that
101 * the seq is not beyond the right edge and that the end of the segment is not
102 * less than the left edge.
103 *
104 * N.B. rcv_nxt and rcv_wnd are both updated in this node if acks are sent, so
105 * use rcv_nxt in the right edge window test instead of rcv_las.
106 *
Dave Barach68b0fb02017-02-28 15:15:56 -0500107 */
108always_inline u8
109tcp_segment_in_rcv_wnd (tcp_connection_t * tc, u32 seq, u32 end_seq)
110{
Florin Coras6792ec02017-03-13 03:49:51 -0700111 return (seq_geq (end_seq, tc->rcv_las)
112 && seq_leq (seq, tc->rcv_nxt + tc->rcv_wnd));
Dave Barach68b0fb02017-02-28 15:15:56 -0500113}
114
115void
116tcp_options_parse (tcp_header_t * th, tcp_options_t * to)
117{
118 const u8 *data;
119 u8 opt_len, opts_len, kind;
120 int j;
121 sack_block_t b;
122
123 opts_len = (tcp_doff (th) << 2) - sizeof (tcp_header_t);
124 data = (const u8 *) (th + 1);
125
126 /* Zero out all flags but those set in SYN */
127 to->flags &= (TCP_OPTS_FLAG_SACK_PERMITTED | TCP_OPTS_FLAG_WSCALE);
128
129 for (; opts_len > 0; opts_len -= opt_len, data += opt_len)
130 {
131 kind = data[0];
132
133 /* Get options length */
134 if (kind == TCP_OPTION_EOL)
135 break;
136 else if (kind == TCP_OPTION_NOOP)
137 opt_len = 1;
138 else
139 {
140 /* broken options */
141 if (opts_len < 2)
142 break;
143 opt_len = data[1];
144
145 /* weird option length */
146 if (opt_len < 2 || opt_len > opts_len)
147 break;
148 }
149
150 /* Parse options */
151 switch (kind)
152 {
153 case TCP_OPTION_MSS:
154 if ((opt_len == TCP_OPTION_LEN_MSS) && tcp_syn (th))
155 {
156 to->flags |= TCP_OPTS_FLAG_MSS;
157 to->mss = clib_net_to_host_u16 (*(u16 *) (data + 2));
158 }
159 break;
160 case TCP_OPTION_WINDOW_SCALE:
161 if ((opt_len == TCP_OPTION_LEN_WINDOW_SCALE) && tcp_syn (th))
162 {
163 to->flags |= TCP_OPTS_FLAG_WSCALE;
164 to->wscale = data[2];
165 if (to->wscale > TCP_MAX_WND_SCALE)
166 {
167 clib_warning ("Illegal window scaling value: %d",
168 to->wscale);
169 to->wscale = TCP_MAX_WND_SCALE;
170 }
171 }
172 break;
173 case TCP_OPTION_TIMESTAMP:
174 if (opt_len == TCP_OPTION_LEN_TIMESTAMP)
175 {
176 to->flags |= TCP_OPTS_FLAG_TSTAMP;
177 to->tsval = clib_net_to_host_u32 (*(u32 *) (data + 2));
178 to->tsecr = clib_net_to_host_u32 (*(u32 *) (data + 6));
179 }
180 break;
181 case TCP_OPTION_SACK_PERMITTED:
182 if (opt_len == TCP_OPTION_LEN_SACK_PERMITTED && tcp_syn (th))
183 to->flags |= TCP_OPTS_FLAG_SACK_PERMITTED;
184 break;
185 case TCP_OPTION_SACK_BLOCK:
186 /* If SACK permitted was not advertised or a SYN, break */
187 if ((to->flags & TCP_OPTS_FLAG_SACK_PERMITTED) == 0 || tcp_syn (th))
188 break;
189
190 /* If too short or not correctly formatted, break */
191 if (opt_len < 10 || ((opt_len - 2) % TCP_OPTION_LEN_SACK_BLOCK))
192 break;
193
194 to->flags |= TCP_OPTS_FLAG_SACK;
195 to->n_sack_blocks = (opt_len - 2) / TCP_OPTION_LEN_SACK_BLOCK;
196 vec_reset_length (to->sacks);
197 for (j = 0; j < to->n_sack_blocks; j++)
198 {
199 b.start = clib_net_to_host_u32 (*(u32 *) (data + 2 + 4 * j));
200 b.end = clib_net_to_host_u32 (*(u32 *) (data + 6 + 4 * j));
201 vec_add1 (to->sacks, b);
202 }
203 break;
204 default:
205 /* Nothing to see here */
206 continue;
207 }
208 }
209}
210
Florin Corasc28764f2017-04-26 00:08:42 -0700211/**
212 * RFC1323: Check against wrapped sequence numbers (PAWS). If we have
213 * timestamp to echo and it's less than tsval_recent, drop segment
214 * but still send an ACK in order to retain TCP's mechanism for detecting
215 * and recovering from half-open connections
216 *
217 * Or at least that's what the theory says. It seems that this might not work
218 * very well with packet reordering and fast retransmit. XXX
219 */
Dave Barach68b0fb02017-02-28 15:15:56 -0500220always_inline int
221tcp_segment_check_paws (tcp_connection_t * tc)
222{
Dave Barach68b0fb02017-02-28 15:15:56 -0500223 return tcp_opts_tstamp (&tc->opt) && tc->tsval_recent
224 && timestamp_lt (tc->opt.tsval, tc->tsval_recent);
225}
226
227/**
Florin Corasc28764f2017-04-26 00:08:42 -0700228 * Update tsval recent
229 */
230always_inline void
231tcp_update_timestamp (tcp_connection_t * tc, u32 seq, u32 seq_end)
232{
233 /*
234 * RFC1323: If Last.ACK.sent falls within the range of sequence numbers
235 * of an incoming segment:
236 * SEG.SEQ <= Last.ACK.sent < SEG.SEQ + SEG.LEN
237 * then the TSval from the segment is copied to TS.Recent;
238 * otherwise, the TSval is ignored.
239 */
240 if (tcp_opts_tstamp (&tc->opt) && tc->tsval_recent
241 && seq_leq (seq, tc->rcv_las) && seq_leq (tc->rcv_las, seq_end))
242 {
243 tc->tsval_recent = tc->opt.tsval;
244 tc->tsval_recent_age = tcp_time_now ();
245 }
246}
247
248/**
Dave Barach68b0fb02017-02-28 15:15:56 -0500249 * Validate incoming segment as per RFC793 p. 69 and RFC1323 p. 19
250 *
251 * It first verifies if segment has a wrapped sequence number (PAWS) and then
252 * does the processing associated to the first four steps (ignoring security
253 * and precedence): sequence number, rst bit and syn bit checks.
254 *
255 * @return 0 if segments passes validation.
256 */
257static int
258tcp_segment_validate (vlib_main_t * vm, tcp_connection_t * tc0,
259 vlib_buffer_t * b0, tcp_header_t * th0, u32 * next0)
260{
Dave Barach68b0fb02017-02-28 15:15:56 -0500261 if (PREDICT_FALSE (!tcp_ack (th0) && !tcp_rst (th0) && !tcp_syn (th0)))
262 return -1;
263
264 tcp_options_parse (th0, &tc0->opt);
265
Florin Corasc28764f2017-04-26 00:08:42 -0700266 if (tcp_segment_check_paws (tc0))
Dave Barach68b0fb02017-02-28 15:15:56 -0500267 {
268 clib_warning ("paws failed");
Florin Corasc28764f2017-04-26 00:08:42 -0700269 TCP_EVT_DBG (TCP_EVT_PAWS_FAIL, tc0, vnet_buffer (b0)->tcp.seq_number,
270 vnet_buffer (b0)->tcp.seq_end);
Dave Barach68b0fb02017-02-28 15:15:56 -0500271
272 /* If it just so happens that a segment updates tsval_recent for a
273 * segment over 24 days old, invalidate tsval_recent. */
274 if (timestamp_lt (tc0->tsval_recent_age + TCP_PAWS_IDLE,
275 tcp_time_now ()))
276 {
277 /* Age isn't reset until we get a valid tsval (bsd inspired) */
278 tc0->tsval_recent = 0;
Florin Corasc28764f2017-04-26 00:08:42 -0700279 clib_warning ("paws failed - really old segment. REALLY?");
Dave Barach68b0fb02017-02-28 15:15:56 -0500280 }
281 else
282 {
283 /* Drop after ack if not rst */
284 if (!tcp_rst (th0))
285 {
286 tcp_make_ack (tc0, b0);
287 *next0 = tcp_next_output (tc0->c_is_ip4);
Florin Coras6792ec02017-03-13 03:49:51 -0700288 TCP_EVT_DBG (TCP_EVT_DUPACK_SENT, tc0);
Dave Barach68b0fb02017-02-28 15:15:56 -0500289 return -1;
290 }
291 }
292 }
293
294 /* 1st: check sequence number */
295 if (!tcp_segment_in_rcv_wnd (tc0, vnet_buffer (b0)->tcp.seq_number,
296 vnet_buffer (b0)->tcp.seq_end))
297 {
Florin Coras6792ec02017-03-13 03:49:51 -0700298 /* If our window is 0 and the packet is in sequence, let it pass
299 * through for ack processing. It should be dropped later.*/
300 if (tc0->rcv_wnd == 0
301 && tc0->rcv_nxt == vnet_buffer (b0)->tcp.seq_number)
Dave Barach68b0fb02017-02-28 15:15:56 -0500302 {
Florin Coras3e350af2017-03-30 02:54:28 -0700303 /* TODO Should segment be tagged? */
Dave Barach68b0fb02017-02-28 15:15:56 -0500304 }
Florin Coras6792ec02017-03-13 03:49:51 -0700305 else
306 {
307 /* If not RST, send dup ack */
308 if (!tcp_rst (th0))
309 {
310 tcp_make_ack (tc0, b0);
311 *next0 = tcp_next_output (tc0->c_is_ip4);
312 TCP_EVT_DBG (TCP_EVT_DUPACK_SENT, tc0);
313 }
314 return -1;
315 }
Dave Barach68b0fb02017-02-28 15:15:56 -0500316 }
317
318 /* 2nd: check the RST bit */
319 if (tcp_rst (th0))
320 {
Florin Corasd79b41e2017-03-04 05:37:52 -0800321 tcp_connection_reset (tc0);
Dave Barach68b0fb02017-02-28 15:15:56 -0500322 return -1;
323 }
324
325 /* 3rd: check security and precedence (skip) */
326
327 /* 4th: check the SYN bit */
328 if (tcp_syn (th0))
329 {
330 tcp_send_reset (b0, tc0->c_is_ip4);
331 return -1;
332 }
333
Florin Corasc28764f2017-04-26 00:08:42 -0700334 /* If segment in window, save timestamp */
335 tcp_update_timestamp (tc0, vnet_buffer (b0)->tcp.seq_number,
336 vnet_buffer (b0)->tcp.seq_end);
Dave Barach68b0fb02017-02-28 15:15:56 -0500337
338 return 0;
339}
340
341always_inline int
342tcp_rcv_ack_is_acceptable (tcp_connection_t * tc0, vlib_buffer_t * tb0)
343{
344 /* SND.UNA =< SEG.ACK =< SND.NXT */
345 return (seq_leq (tc0->snd_una, vnet_buffer (tb0)->tcp.ack_number)
346 && seq_leq (vnet_buffer (tb0)->tcp.ack_number, tc0->snd_nxt));
347}
348
349/**
350 * Compute smoothed RTT as per VJ's '88 SIGCOMM and RFC6298
351 *
352 * Note that although the original article, srtt and rttvar are scaled
353 * to minimize round-off errors, here we don't. Instead, we rely on
354 * better precision time measurements.
355 *
356 * TODO support us rtt resolution
357 */
358static void
359tcp_estimate_rtt (tcp_connection_t * tc, u32 mrtt)
360{
361 int err;
362
363 if (tc->srtt != 0)
364 {
365 err = mrtt - tc->srtt;
366 tc->srtt += err >> 3;
367
368 /* XXX Drop in RTT results in RTTVAR increase and bigger RTO.
369 * The increase should be bound */
Florin Coras6792ec02017-03-13 03:49:51 -0700370 tc->rttvar += ((int) clib_abs (err) - (int) tc->rttvar) >> 2;
Dave Barach68b0fb02017-02-28 15:15:56 -0500371 }
372 else
373 {
374 /* First measurement. */
375 tc->srtt = mrtt;
Florin Coras6792ec02017-03-13 03:49:51 -0700376 tc->rttvar = mrtt >> 1;
Dave Barach68b0fb02017-02-28 15:15:56 -0500377 }
378}
379
380/** Update RTT estimate and RTO timer
381 *
382 * Measure RTT: We have two sources of RTT measurements: TSOPT and ACK
383 * timing. Middle boxes are known to fiddle with TCP options so we
384 * should give higher priority to ACK timing.
385 *
386 * return 1 if valid rtt 0 otherwise
387 */
388static int
389tcp_update_rtt (tcp_connection_t * tc, u32 ack)
390{
391 u32 mrtt = 0;
392
393 /* Karn's rule, part 1. Don't use retransmitted segments to estimate
394 * RTT because they're ambiguous. */
Florin Coras3af90fc2017-05-03 21:09:42 -0700395 if (tc->rtt_ts && seq_geq (ack, tc->rtt_seq) && !tc->rto_boff)
Dave Barach68b0fb02017-02-28 15:15:56 -0500396 {
397 mrtt = tcp_time_now () - tc->rtt_ts;
Dave Barach68b0fb02017-02-28 15:15:56 -0500398 }
Dave Barach68b0fb02017-02-28 15:15:56 -0500399 /* As per RFC7323 TSecr can be used for RTTM only if the segment advances
400 * snd_una, i.e., the left side of the send window:
401 * seq_lt (tc->snd_una, ack). Note: last condition could be dropped, we don't
402 * try to update rtt for dupacks */
403 else if (tcp_opts_tstamp (&tc->opt) && tc->opt.tsecr && tc->bytes_acked)
404 {
405 mrtt = tcp_time_now () - tc->opt.tsecr;
406 }
407
Florin Coras3af90fc2017-05-03 21:09:42 -0700408 /* Allow measuring of a new RTT */
409 tc->rtt_ts = 0;
410
411 /* If ACK moves left side of the wnd make sure boff is 0, even if mrtt is
412 * not valid */
413 if (tc->bytes_acked)
414 tc->rto_boff = 0;
415
Dave Barach68b0fb02017-02-28 15:15:56 -0500416 /* Ignore dubious measurements */
417 if (mrtt == 0 || mrtt > TCP_RTT_MAX)
418 return 0;
419
420 tcp_estimate_rtt (tc, mrtt);
Dave Barach68b0fb02017-02-28 15:15:56 -0500421 tc->rto = clib_min (tc->srtt + (tc->rttvar << 2), TCP_RTO_MAX);
422
Florin Coras3af90fc2017-05-03 21:09:42 -0700423 return 0;
Dave Barach68b0fb02017-02-28 15:15:56 -0500424}
425
426/**
427 * Dequeue bytes that have been acked and while at it update RTT estimates.
428 */
429static void
430tcp_dequeue_acked (tcp_connection_t * tc, u32 ack)
431{
432 /* Dequeue the newly ACKed bytes */
433 stream_session_dequeue_drop (&tc->connection, tc->bytes_acked);
434
435 /* Update rtt and rto */
Florin Coras3e350af2017-03-30 02:54:28 -0700436 tcp_update_rtt (tc, ack);
Dave Barach68b0fb02017-02-28 15:15:56 -0500437}
438
Florin Coras6792ec02017-03-13 03:49:51 -0700439/**
440 * Check if dupack as per RFC5681 Sec. 2
441 *
442 * This works only if called before updating snd_wnd.
443 * */
Dave Barach68b0fb02017-02-28 15:15:56 -0500444always_inline u8
445tcp_ack_is_dupack (tcp_connection_t * tc, vlib_buffer_t * b, u32 new_snd_wnd)
446{
447 return ((vnet_buffer (b)->tcp.ack_number == tc->snd_una)
448 && seq_gt (tc->snd_una_max, tc->snd_una)
449 && (vnet_buffer (b)->tcp.seq_end == vnet_buffer (b)->tcp.seq_number)
450 && (new_snd_wnd == tc->snd_wnd));
451}
452
453void
454scoreboard_remove_hole (sack_scoreboard_t * sb, sack_scoreboard_hole_t * hole)
455{
456 sack_scoreboard_hole_t *next, *prev;
457
458 if (hole->next != TCP_INVALID_SACK_HOLE_INDEX)
459 {
460 next = pool_elt_at_index (sb->holes, hole->next);
461 next->prev = hole->prev;
462 }
463
464 if (hole->prev != TCP_INVALID_SACK_HOLE_INDEX)
465 {
466 prev = pool_elt_at_index (sb->holes, hole->prev);
467 prev->next = hole->next;
468 }
469 else
470 {
471 sb->head = hole->next;
472 }
473
474 pool_put (sb->holes, hole);
475}
476
477sack_scoreboard_hole_t *
Florin Coras6792ec02017-03-13 03:49:51 -0700478scoreboard_insert_hole (sack_scoreboard_t * sb, u32 prev_index,
Dave Barach68b0fb02017-02-28 15:15:56 -0500479 u32 start, u32 end)
480{
Florin Coras6792ec02017-03-13 03:49:51 -0700481 sack_scoreboard_hole_t *hole, *next, *prev;
Dave Barach68b0fb02017-02-28 15:15:56 -0500482 u32 hole_index;
483
484 pool_get (sb->holes, hole);
485 memset (hole, 0, sizeof (*hole));
486
487 hole->start = start;
488 hole->end = end;
489 hole_index = hole - sb->holes;
490
Florin Coras6792ec02017-03-13 03:49:51 -0700491 prev = scoreboard_get_hole (sb, prev_index);
Dave Barach68b0fb02017-02-28 15:15:56 -0500492 if (prev)
493 {
494 hole->prev = prev - sb->holes;
495 hole->next = prev->next;
496
497 if ((next = scoreboard_next_hole (sb, hole)))
498 next->prev = hole_index;
499
500 prev->next = hole_index;
501 }
502 else
503 {
504 sb->head = hole_index;
505 hole->prev = TCP_INVALID_SACK_HOLE_INDEX;
506 hole->next = TCP_INVALID_SACK_HOLE_INDEX;
507 }
508
509 return hole;
510}
511
Florin Coras6792ec02017-03-13 03:49:51 -0700512void
Dave Barach68b0fb02017-02-28 15:15:56 -0500513tcp_rcv_sacks (tcp_connection_t * tc, u32 ack)
514{
515 sack_scoreboard_t *sb = &tc->sack_sb;
516 sack_block_t *blk, tmp;
Florin Coras6792ec02017-03-13 03:49:51 -0700517 sack_scoreboard_hole_t *hole, *next_hole, *last_hole, *new_hole;
518 u32 blk_index = 0, old_sacked_bytes, hole_index;
Dave Barach68b0fb02017-02-28 15:15:56 -0500519 int i, j;
520
Florin Coras6792ec02017-03-13 03:49:51 -0700521 sb->last_sacked_bytes = 0;
522 sb->snd_una_adv = 0;
523 old_sacked_bytes = sb->sacked_bytes;
524
525 if (!tcp_opts_sack (&tc->opt) && sb->head == TCP_INVALID_SACK_HOLE_INDEX)
Dave Barach68b0fb02017-02-28 15:15:56 -0500526 return;
527
528 /* Remove invalid blocks */
Florin Coras6792ec02017-03-13 03:49:51 -0700529 blk = tc->opt.sacks;
530 while (blk < vec_end (tc->opt.sacks))
531 {
532 if (seq_lt (blk->start, blk->end)
533 && seq_gt (blk->start, tc->snd_una)
534 && seq_gt (blk->start, ack) && seq_leq (blk->end, tc->snd_nxt))
535 {
536 blk++;
537 continue;
538 }
539 vec_del1 (tc->opt.sacks, blk - tc->opt.sacks);
540 }
Dave Barach68b0fb02017-02-28 15:15:56 -0500541
542 /* Add block for cumulative ack */
543 if (seq_gt (ack, tc->snd_una))
544 {
545 tmp.start = tc->snd_una;
546 tmp.end = ack;
547 vec_add1 (tc->opt.sacks, tmp);
548 }
549
550 if (vec_len (tc->opt.sacks) == 0)
551 return;
552
553 /* Make sure blocks are ordered */
554 for (i = 0; i < vec_len (tc->opt.sacks); i++)
Florin Coras6792ec02017-03-13 03:49:51 -0700555 for (j = i + 1; j < vec_len (tc->opt.sacks); j++)
Dave Barach68b0fb02017-02-28 15:15:56 -0500556 if (seq_lt (tc->opt.sacks[j].start, tc->opt.sacks[i].start))
557 {
558 tmp = tc->opt.sacks[i];
559 tc->opt.sacks[i] = tc->opt.sacks[j];
560 tc->opt.sacks[j] = tmp;
561 }
562
Dave Barach68b0fb02017-02-28 15:15:56 -0500563 if (sb->head == TCP_INVALID_SACK_HOLE_INDEX)
564 {
Florin Coras6792ec02017-03-13 03:49:51 -0700565 /* If no holes, insert the first that covers all outstanding bytes */
566 last_hole = scoreboard_insert_hole (sb, TCP_INVALID_SACK_HOLE_INDEX,
567 tc->snd_una, tc->snd_una_max);
568 sb->tail = scoreboard_hole_index (sb, last_hole);
569 }
570 else
571 {
572 /* If we have holes but snd_una_max is beyond the last hole, update
573 * last hole end */
574 tmp = tc->opt.sacks[vec_len (tc->opt.sacks) - 1];
575 last_hole = scoreboard_last_hole (sb);
576 if (seq_gt (tc->snd_una_max, sb->max_byte_sacked)
577 && seq_gt (tc->snd_una_max, last_hole->end))
578 last_hole->end = tc->snd_una_max;
Dave Barach68b0fb02017-02-28 15:15:56 -0500579 }
580
581 /* Walk the holes with the SACK blocks */
582 hole = pool_elt_at_index (sb->holes, sb->head);
583 while (hole && blk_index < vec_len (tc->opt.sacks))
584 {
585 blk = &tc->opt.sacks[blk_index];
586
587 if (seq_leq (blk->start, hole->start))
588 {
589 /* Block covers hole. Remove hole */
590 if (seq_geq (blk->end, hole->end))
591 {
592 next_hole = scoreboard_next_hole (sb, hole);
593
594 /* Byte accounting */
Florin Coras6792ec02017-03-13 03:49:51 -0700595 if (seq_leq (hole->end, ack))
Dave Barach68b0fb02017-02-28 15:15:56 -0500596 {
Florin Coras6792ec02017-03-13 03:49:51 -0700597 /* Bytes lost because snd_wnd left edge advances */
598 if (next_hole && seq_leq (next_hole->start, ack))
Dave Barach68b0fb02017-02-28 15:15:56 -0500599 sb->sacked_bytes -= next_hole->start - hole->end;
600 else
601 sb->sacked_bytes -= ack - hole->end;
602 }
603 else
604 {
605 sb->sacked_bytes += scoreboard_hole_bytes (hole);
606 }
607
Florin Coras6792ec02017-03-13 03:49:51 -0700608 /* snd_una needs to be advanced */
609 if (seq_geq (ack, hole->end))
610 {
611 if (next_hole && seq_lt (ack, next_hole->start))
612 sb->snd_una_adv = next_hole->start - ack;
613 else
614 sb->snd_una_adv = sb->max_byte_sacked - ack;
615
616 /* all these can be delivered */
617 sb->sacked_bytes -= sb->snd_una_adv;
618 }
619
620 /* About to remove last hole */
621 if (hole == last_hole)
622 {
623 sb->tail = hole->prev;
624 last_hole = scoreboard_last_hole (sb);
625 /* keep track of max byte sacked in case the last hole
626 * is acked */
627 if (seq_gt (hole->end, sb->max_byte_sacked))
628 sb->max_byte_sacked = hole->end;
629 }
Dave Barach68b0fb02017-02-28 15:15:56 -0500630 scoreboard_remove_hole (sb, hole);
631 hole = next_hole;
632 }
Florin Coras6792ec02017-03-13 03:49:51 -0700633 /* Partial 'head' overlap */
Dave Barach68b0fb02017-02-28 15:15:56 -0500634 else
635 {
Florin Coras6792ec02017-03-13 03:49:51 -0700636 if (seq_gt (blk->end, hole->start))
637 {
638 sb->sacked_bytes += blk->end - hole->start;
639 hole->start = blk->end;
640 }
Dave Barach68b0fb02017-02-28 15:15:56 -0500641 blk_index++;
642 }
643 }
644 else
645 {
646 /* Hole must be split */
Florin Coras6792ec02017-03-13 03:49:51 -0700647 if (seq_lt (blk->end, hole->end))
Dave Barach68b0fb02017-02-28 15:15:56 -0500648 {
649 sb->sacked_bytes += blk->end - blk->start;
Florin Coras6792ec02017-03-13 03:49:51 -0700650 hole_index = scoreboard_hole_index (sb, hole);
651 new_hole = scoreboard_insert_hole (sb, hole_index, blk->end,
652 hole->end);
653
654 /* Pool might've moved */
655 hole = scoreboard_get_hole (sb, hole_index);
656 hole->end = blk->start;
657
658 /* New or split of tail */
659 if ((last_hole->end == new_hole->end)
660 || seq_lt (last_hole->end, new_hole->start))
661 {
662 last_hole = new_hole;
663 sb->tail = scoreboard_hole_index (sb, new_hole);
664 }
665
Dave Barach68b0fb02017-02-28 15:15:56 -0500666 blk_index++;
Florin Coras6792ec02017-03-13 03:49:51 -0700667 hole = scoreboard_next_hole (sb, hole);
Dave Barach68b0fb02017-02-28 15:15:56 -0500668 }
669 else
670 {
Florin Coras6792ec02017-03-13 03:49:51 -0700671 sb->sacked_bytes += hole->end - blk->start;
672 hole->end = blk->start;
Dave Barach68b0fb02017-02-28 15:15:56 -0500673 hole = scoreboard_next_hole (sb, hole);
674 }
675 }
676 }
Florin Coras6792ec02017-03-13 03:49:51 -0700677
678 sb->last_sacked_bytes = sb->sacked_bytes + sb->snd_una_adv
679 - old_sacked_bytes;
Dave Barach68b0fb02017-02-28 15:15:56 -0500680}
681
682/** Update snd_wnd
683 *
684 * If (SND.WL1 < SEG.SEQ or (SND.WL1 = SEG.SEQ and SND.WL2 =< SEG.ACK)), set
685 * SND.WND <- SEG.WND, set SND.WL1 <- SEG.SEQ, and set SND.WL2 <- SEG.ACK */
686static void
687tcp_update_snd_wnd (tcp_connection_t * tc, u32 seq, u32 ack, u32 snd_wnd)
688{
Florin Coras6792ec02017-03-13 03:49:51 -0700689 if (seq_lt (tc->snd_wl1, seq)
690 || (tc->snd_wl1 == seq && seq_leq (tc->snd_wl2, ack)))
Dave Barach68b0fb02017-02-28 15:15:56 -0500691 {
692 tc->snd_wnd = snd_wnd;
693 tc->snd_wl1 = seq;
694 tc->snd_wl2 = ack;
Florin Coras6792ec02017-03-13 03:49:51 -0700695 TCP_EVT_DBG (TCP_EVT_SND_WND, tc);
Florin Coras3e350af2017-03-30 02:54:28 -0700696
697 /* Set probe timer if we just got 0 wnd */
698 if (tc->snd_wnd < tc->snd_mss
699 && !tcp_timer_is_active (tc, TCP_TIMER_PERSIST))
700 tcp_persist_timer_set (tc);
701 else
702 tcp_persist_timer_reset (tc);
Dave Barach68b0fb02017-02-28 15:15:56 -0500703 }
704}
705
Florin Coras6792ec02017-03-13 03:49:51 -0700706void
Dave Barach68b0fb02017-02-28 15:15:56 -0500707tcp_cc_congestion (tcp_connection_t * tc)
708{
Florin Coras6792ec02017-03-13 03:49:51 -0700709 tc->snd_congestion = tc->snd_nxt;
Dave Barach68b0fb02017-02-28 15:15:56 -0500710 tc->cc_algo->congestion (tc);
Florin Coras6792ec02017-03-13 03:49:51 -0700711 TCP_EVT_DBG (TCP_EVT_CC_EVT, tc, 4);
Dave Barach68b0fb02017-02-28 15:15:56 -0500712}
713
Florin Coras6792ec02017-03-13 03:49:51 -0700714void
Dave Barach68b0fb02017-02-28 15:15:56 -0500715tcp_cc_recover (tcp_connection_t * tc)
716{
Florin Coras3e350af2017-03-30 02:54:28 -0700717 /* TODO: check if time to recover was small. It might be that RTO popped
718 * too soon.
719 */
720
Florin Coras6792ec02017-03-13 03:49:51 -0700721 tc->cc_algo->recovered (tc);
722
723 tc->rtx_bytes = 0;
724 tc->rcv_dupacks = 0;
725 tc->snd_nxt = tc->snd_una;
726
727 tc->cc_algo->rcv_ack (tc);
728 tc->tsecr_last_ack = tc->opt.tsecr;
729
Florin Coras3e350af2017-03-30 02:54:28 -0700730 tcp_cong_recovery_off (tc);
Florin Coras6792ec02017-03-13 03:49:51 -0700731
732 TCP_EVT_DBG (TCP_EVT_CC_EVT, tc, 3);
Dave Barach68b0fb02017-02-28 15:15:56 -0500733}
734
735static void
Florin Coras6792ec02017-03-13 03:49:51 -0700736tcp_cc_rcv_ack (tcp_connection_t * tc, vlib_buffer_t * b)
Dave Barach68b0fb02017-02-28 15:15:56 -0500737{
738 u8 partial_ack;
739
Florin Coras3af90fc2017-05-03 21:09:42 -0700740 if (tcp_in_fastrecovery (tc))
Dave Barach68b0fb02017-02-28 15:15:56 -0500741 {
Florin Coras6792ec02017-03-13 03:49:51 -0700742 partial_ack = seq_lt (tc->snd_una, tc->snd_congestion);
Dave Barach68b0fb02017-02-28 15:15:56 -0500743 if (!partial_ack)
744 {
745 /* Clear retransmitted bytes. */
Dave Barach68b0fb02017-02-28 15:15:56 -0500746 tcp_cc_recover (tc);
747 }
748 else
749 {
Florin Coras6792ec02017-03-13 03:49:51 -0700750 TCP_EVT_DBG (TCP_EVT_CC_PACK, tc);
751
Dave Barach68b0fb02017-02-28 15:15:56 -0500752 /* Clear retransmitted bytes. XXX should we clear all? */
753 tc->rtx_bytes = 0;
Florin Coras3af90fc2017-05-03 21:09:42 -0700754
Dave Barach68b0fb02017-02-28 15:15:56 -0500755 tc->cc_algo->rcv_cong_ack (tc, TCP_CC_PARTIALACK);
756
Florin Coras6792ec02017-03-13 03:49:51 -0700757 /* In case snd_nxt is still in the past and output tries to
758 * shove some new bytes */
Florin Coras3e350af2017-03-30 02:54:28 -0700759 tc->snd_nxt = tc->snd_una_max;
Florin Coras6792ec02017-03-13 03:49:51 -0700760
761 /* XXX need proper RFC6675 support */
Florin Coras3e350af2017-03-30 02:54:28 -0700762 if (tc->sack_sb.last_sacked_bytes && !tcp_in_recovery (tc))
Florin Coras6792ec02017-03-13 03:49:51 -0700763 {
764 tcp_fast_retransmit (tc);
765 }
766 else
767 {
768 /* Retransmit first unacked segment */
769 tcp_retransmit_first_unacked (tc);
Florin Coras6792ec02017-03-13 03:49:51 -0700770 }
Dave Barach68b0fb02017-02-28 15:15:56 -0500771 }
772 }
773 else
774 {
775 tc->cc_algo->rcv_ack (tc);
Florin Coras6792ec02017-03-13 03:49:51 -0700776 tc->tsecr_last_ack = tc->opt.tsecr;
777 tc->rcv_dupacks = 0;
Florin Coras3af90fc2017-05-03 21:09:42 -0700778 if (tcp_in_recovery (tc))
779 {
780 tc->rtx_bytes -= clib_min (tc->bytes_acked, tc->rtx_bytes);
781 tc->rto = clib_min (tc->srtt + (tc->rttvar << 2), TCP_RTO_MAX);
782 if (seq_geq (tc->snd_una, tc->snd_congestion))
783 tcp_recovery_off (tc);
784 }
Dave Barach68b0fb02017-02-28 15:15:56 -0500785 }
Dave Barach68b0fb02017-02-28 15:15:56 -0500786}
787
788static void
789tcp_cc_rcv_dupack (tcp_connection_t * tc, u32 ack)
790{
Florin Coras6792ec02017-03-13 03:49:51 -0700791// ASSERT (seq_geq(tc->snd_una, ack));
Dave Barach68b0fb02017-02-28 15:15:56 -0500792
793 tc->rcv_dupacks++;
794 if (tc->rcv_dupacks == TCP_DUPACK_THRESHOLD)
795 {
796 /* RFC6582 NewReno heuristic to avoid multiple fast retransmits */
797 if (tc->opt.tsecr != tc->tsecr_last_ack)
798 {
799 tc->rcv_dupacks = 0;
800 return;
801 }
802
803 tcp_fastrecovery_on (tc);
804
805 /* Handle congestion and dupack */
806 tcp_cc_congestion (tc);
807 tc->cc_algo->rcv_cong_ack (tc, TCP_CC_DUPACK);
808
809 tcp_fast_retransmit (tc);
810
811 /* Post retransmit update cwnd to ssthresh and account for the
812 * three segments that have left the network and should've been
813 * buffered at the receiver */
814 tc->cwnd = tc->ssthresh + TCP_DUPACK_THRESHOLD * tc->snd_mss;
815 }
816 else if (tc->rcv_dupacks > TCP_DUPACK_THRESHOLD)
817 {
818 ASSERT (tcp_in_fastrecovery (tc));
819
820 tc->cc_algo->rcv_cong_ack (tc, TCP_CC_DUPACK);
821 }
822}
823
824void
825tcp_cc_init (tcp_connection_t * tc)
826{
827 tc->cc_algo = tcp_cc_algo_get (TCP_CC_NEWRENO);
828 tc->cc_algo->init (tc);
829}
830
831static int
832tcp_rcv_ack (tcp_connection_t * tc, vlib_buffer_t * b,
833 tcp_header_t * th, u32 * next, u32 * error)
834{
835 u32 new_snd_wnd;
836
Florin Coras6792ec02017-03-13 03:49:51 -0700837 /* If the ACK acks something not yet sent (SEG.ACK > SND.NXT) */
Dave Barach68b0fb02017-02-28 15:15:56 -0500838 if (seq_gt (vnet_buffer (b)->tcp.ack_number, tc->snd_nxt))
839 {
Florin Coras6792ec02017-03-13 03:49:51 -0700840 /* If we have outstanding data and this is within the window, accept it,
841 * probably retransmit has timed out. Otherwise ACK segment and then
842 * drop it */
843 if (seq_gt (vnet_buffer (b)->tcp.ack_number, tc->snd_una_max))
844 {
845 tcp_make_ack (tc, b);
846 *next = tcp_next_output (tc->c_is_ip4);
847 *error = TCP_ERROR_ACK_INVALID;
848 TCP_EVT_DBG (TCP_EVT_ACK_RCV_ERR, tc, 0,
849 vnet_buffer (b)->tcp.ack_number);
850 return -1;
851 }
852
Florin Coras6792ec02017-03-13 03:49:51 -0700853 TCP_EVT_DBG (TCP_EVT_ACK_RCV_ERR, tc, 2,
854 vnet_buffer (b)->tcp.ack_number);
Florin Coras3e350af2017-03-30 02:54:28 -0700855
856 tc->snd_nxt = vnet_buffer (b)->tcp.ack_number;
857 *error = TCP_ERROR_ACK_FUTURE;
Dave Barach68b0fb02017-02-28 15:15:56 -0500858 }
859
Florin Coras6792ec02017-03-13 03:49:51 -0700860 /* If old ACK, probably it's an old dupack */
Dave Barach68b0fb02017-02-28 15:15:56 -0500861 if (seq_lt (vnet_buffer (b)->tcp.ack_number, tc->snd_una))
862 {
863 *error = TCP_ERROR_ACK_OLD;
Florin Coras6792ec02017-03-13 03:49:51 -0700864 TCP_EVT_DBG (TCP_EVT_ACK_RCV_ERR, tc, 1,
865 vnet_buffer (b)->tcp.ack_number);
866 if (tcp_in_fastrecovery (tc) && tc->rcv_dupacks == TCP_DUPACK_THRESHOLD)
867 {
868 TCP_EVT_DBG (TCP_EVT_DUPACK_RCVD, tc);
869 tcp_cc_rcv_dupack (tc, vnet_buffer (b)->tcp.ack_number);
870 }
Florin Corasc28764f2017-04-26 00:08:42 -0700871 /* Don't drop yet */
872 return 0;
Dave Barach68b0fb02017-02-28 15:15:56 -0500873 }
874
875 if (tcp_opts_sack_permitted (&tc->opt))
876 tcp_rcv_sacks (tc, vnet_buffer (b)->tcp.ack_number);
877
Florin Corase04c2992017-03-01 08:17:34 -0800878 new_snd_wnd = clib_net_to_host_u16 (th->window) << tc->snd_wscale;
Dave Barach68b0fb02017-02-28 15:15:56 -0500879
880 if (tcp_ack_is_dupack (tc, b, new_snd_wnd))
881 {
Florin Coras6792ec02017-03-13 03:49:51 -0700882 TCP_EVT_DBG (TCP_EVT_DUPACK_RCVD, tc, 1);
Dave Barach68b0fb02017-02-28 15:15:56 -0500883 tcp_cc_rcv_dupack (tc, vnet_buffer (b)->tcp.ack_number);
884 *error = TCP_ERROR_ACK_DUP;
885 return -1;
886 }
887
Florin Coras6792ec02017-03-13 03:49:51 -0700888 /*
889 * Valid ACK
890 */
891
Dave Barach68b0fb02017-02-28 15:15:56 -0500892 tc->bytes_acked = vnet_buffer (b)->tcp.ack_number - tc->snd_una;
Florin Coras6792ec02017-03-13 03:49:51 -0700893 tc->snd_una = vnet_buffer (b)->tcp.ack_number + tc->sack_sb.snd_una_adv;
Dave Barach68b0fb02017-02-28 15:15:56 -0500894
Florin Coras6792ec02017-03-13 03:49:51 -0700895 /* Dequeue ACKed data and update RTT */
Dave Barach68b0fb02017-02-28 15:15:56 -0500896 tcp_dequeue_acked (tc, vnet_buffer (b)->tcp.ack_number);
Dave Barach68b0fb02017-02-28 15:15:56 -0500897 tcp_update_snd_wnd (tc, vnet_buffer (b)->tcp.seq_number,
898 vnet_buffer (b)->tcp.ack_number, new_snd_wnd);
899
Florin Coras6792ec02017-03-13 03:49:51 -0700900 /* If some of our sent bytes have been acked, update cc and retransmit
901 * timer. */
902 if (tc->bytes_acked)
903 {
Florin Coras3e350af2017-03-30 02:54:28 -0700904 TCP_EVT_DBG (TCP_EVT_ACK_RCVD, tc);
Dave Barach68b0fb02017-02-28 15:15:56 -0500905
Florin Coras6792ec02017-03-13 03:49:51 -0700906 /* Updates congestion control (slow start/congestion avoidance) */
907 tcp_cc_rcv_ack (tc, b);
Florin Corase69f4952017-03-07 10:06:24 -0800908
Florin Coras6792ec02017-03-13 03:49:51 -0700909 /* If everything has been acked, stop retransmit timer
Florin Coras3af90fc2017-05-03 21:09:42 -0700910 * otherwise update. */
Florin Coras6792ec02017-03-13 03:49:51 -0700911 if (tc->snd_una == tc->snd_una_max)
912 tcp_retransmit_timer_reset (tc);
913 else
914 tcp_retransmit_timer_update (tc);
915 }
Dave Barach68b0fb02017-02-28 15:15:56 -0500916
917 return 0;
918}
919
920/**
921 * Build SACK list as per RFC2018.
922 *
923 * Makes sure the first block contains the segment that generated the current
924 * ACK and the following ones are the ones most recently reported in SACK
925 * blocks.
926 *
927 * @param tc TCP connection for which the SACK list is updated
928 * @param start Start sequence number of the newest SACK block
929 * @param end End sequence of the newest SACK block
930 */
Florin Coras45d34962017-04-25 00:05:27 -0700931void
Dave Barach68b0fb02017-02-28 15:15:56 -0500932tcp_update_sack_list (tcp_connection_t * tc, u32 start, u32 end)
933{
Florin Coras45d34962017-04-25 00:05:27 -0700934 sack_block_t *new_list = 0, *block = 0;
Dave Barach68b0fb02017-02-28 15:15:56 -0500935 int i;
Dave Barach68b0fb02017-02-28 15:15:56 -0500936
937 /* If the first segment is ooo add it to the list. Last write might've moved
938 * rcv_nxt over the first segment. */
939 if (seq_lt (tc->rcv_nxt, start))
940 {
Florin Coras45d34962017-04-25 00:05:27 -0700941 vec_add2 (new_list, block, 1);
942 block->start = start;
943 block->end = end;
Dave Barach68b0fb02017-02-28 15:15:56 -0500944 }
945
946 /* Find the blocks still worth keeping. */
947 for (i = 0; i < vec_len (tc->snd_sacks); i++)
948 {
Florin Coras45d34962017-04-25 00:05:27 -0700949 /* Discard if rcv_nxt advanced beyond current block */
950 if (seq_leq (tc->snd_sacks[i].start, tc->rcv_nxt))
Dave Barach68b0fb02017-02-28 15:15:56 -0500951 continue;
952
Florin Coras45d34962017-04-25 00:05:27 -0700953 /* Merge or drop if segment overlapped by the new segment */
954 if (block && (seq_geq (tc->snd_sacks[i].end, new_list[0].start)
955 && seq_leq (tc->snd_sacks[i].start, new_list[0].end)))
956 {
957 if (seq_lt (tc->snd_sacks[i].start, new_list[0].start))
958 new_list[0].start = tc->snd_sacks[i].start;
959 if (seq_lt (new_list[0].end, tc->snd_sacks[i].end))
960 new_list[0].end = tc->snd_sacks[i].end;
961 continue;
962 }
963
964 /* Save to new SACK list if we have space. */
965 if (vec_len (new_list) < TCP_MAX_SACK_BLOCKS)
966 {
967 vec_add1 (new_list, tc->snd_sacks[i]);
968 }
Dave Barach68b0fb02017-02-28 15:15:56 -0500969 }
970
Florin Coras45d34962017-04-25 00:05:27 -0700971 ASSERT (vec_len (new_list) <= TCP_MAX_SACK_BLOCKS);
Florin Coras6792ec02017-03-13 03:49:51 -0700972
Dave Barach68b0fb02017-02-28 15:15:56 -0500973 /* Replace old vector with new one */
974 vec_free (tc->snd_sacks);
975 tc->snd_sacks = new_list;
976}
977
978/** Enqueue data for delivery to application */
Florin Coras6792ec02017-03-13 03:49:51 -0700979always_inline int
Dave Barach68b0fb02017-02-28 15:15:56 -0500980tcp_session_enqueue_data (tcp_connection_t * tc, vlib_buffer_t * b,
981 u16 data_len)
982{
983 int written;
984
985 /* Pure ACK. Update rcv_nxt and be done. */
986 if (PREDICT_FALSE (data_len == 0))
987 {
988 tc->rcv_nxt = vnet_buffer (b)->tcp.seq_end;
989 return TCP_ERROR_PURE_ACK;
990 }
991
992 written = stream_session_enqueue_data (&tc->connection,
993 vlib_buffer_get_current (b),
994 data_len, 1 /* queue event */ );
995
Florin Coras6792ec02017-03-13 03:49:51 -0700996 TCP_EVT_DBG (TCP_EVT_INPUT, tc, 0, data_len, written);
997
Dave Barach68b0fb02017-02-28 15:15:56 -0500998 /* Update rcv_nxt */
999 if (PREDICT_TRUE (written == data_len))
1000 {
1001 tc->rcv_nxt = vnet_buffer (b)->tcp.seq_end;
1002 }
1003 /* If more data written than expected, account for out-of-order bytes. */
1004 else if (written > data_len)
1005 {
1006 tc->rcv_nxt = vnet_buffer (b)->tcp.seq_end + written - data_len;
1007
1008 /* Send ACK confirming the update */
1009 tc->flags |= TCP_CONN_SNDACK;
Florin Coras6792ec02017-03-13 03:49:51 -07001010 }
1011 else if (written > 0)
1012 {
1013 /* We've written something but FIFO is probably full now */
1014 tc->rcv_nxt += written;
Dave Barach68b0fb02017-02-28 15:15:56 -05001015
Florin Coras6792ec02017-03-13 03:49:51 -07001016 /* Depending on how fast the app is, all remaining buffers in burst will
Florin Coras3e350af2017-03-30 02:54:28 -07001017 * not be enqueued. Inform peer */
1018 tc->flags |= TCP_CONN_SNDACK;
1019
Florin Coras6792ec02017-03-13 03:49:51 -07001020 return TCP_ERROR_PARTIALLY_ENQUEUED;
Dave Barach68b0fb02017-02-28 15:15:56 -05001021 }
1022 else
1023 {
Florin Coras3e350af2017-03-30 02:54:28 -07001024 tc->flags |= TCP_CONN_SNDACK;
Dave Barach68b0fb02017-02-28 15:15:56 -05001025 return TCP_ERROR_FIFO_FULL;
1026 }
1027
Florin Coras6792ec02017-03-13 03:49:51 -07001028 /* Update SACK list if need be */
1029 if (tcp_opts_sack_permitted (&tc->opt))
1030 {
1031 /* Remove SACK blocks that have been delivered */
1032 tcp_update_sack_list (tc, tc->rcv_nxt, tc->rcv_nxt);
1033 }
1034
Dave Barach68b0fb02017-02-28 15:15:56 -05001035 return TCP_ERROR_ENQUEUED;
1036}
1037
1038/** Enqueue out-of-order data */
Florin Coras6792ec02017-03-13 03:49:51 -07001039always_inline int
Dave Barach68b0fb02017-02-28 15:15:56 -05001040tcp_session_enqueue_ooo (tcp_connection_t * tc, vlib_buffer_t * b,
1041 u16 data_len)
1042{
1043 stream_session_t *s0;
Florin Coras6792ec02017-03-13 03:49:51 -07001044 int rv;
1045
1046 /* Pure ACK. Do nothing */
1047 if (PREDICT_FALSE (data_len == 0))
1048 {
1049 return TCP_ERROR_PURE_ACK;
1050 }
Dave Barach68b0fb02017-02-28 15:15:56 -05001051
1052 s0 = stream_session_get (tc->c_s_index, tc->c_thread_index);
Dave Barach1f75cfd2017-04-14 16:46:44 -04001053
Florin Corasc28764f2017-04-26 00:08:42 -07001054 /* Enqueue out-of-order data with absolute offset */
1055 rv = svm_fifo_enqueue_with_offset (s0->server_rx_fifo,
1056 vnet_buffer (b)->tcp.seq_number,
1057 data_len, vlib_buffer_get_current (b));
Florin Coras6792ec02017-03-13 03:49:51 -07001058
1059 /* Nothing written */
1060 if (rv)
1061 {
1062 TCP_EVT_DBG (TCP_EVT_INPUT, tc, 1, data_len, 0);
1063 return TCP_ERROR_FIFO_FULL;
1064 }
1065
1066 TCP_EVT_DBG (TCP_EVT_INPUT, tc, 1, data_len, data_len);
Dave Barach68b0fb02017-02-28 15:15:56 -05001067
1068 /* Update SACK list if in use */
1069 if (tcp_opts_sack_permitted (&tc->opt))
1070 {
1071 ooo_segment_t *newest;
1072 u32 start, end;
1073
1074 /* Get the newest segment from the fifo */
1075 newest = svm_fifo_newest_ooo_segment (s0->server_rx_fifo);
Dave Barach1f75cfd2017-04-14 16:46:44 -04001076 start = ooo_segment_offset (s0->server_rx_fifo, newest);
1077 end = ooo_segment_end_offset (s0->server_rx_fifo, newest);
Dave Barach68b0fb02017-02-28 15:15:56 -05001078
1079 tcp_update_sack_list (tc, start, end);
1080 }
1081
1082 return TCP_ERROR_ENQUEUED;
1083}
1084
1085/**
Florin Coras6792ec02017-03-13 03:49:51 -07001086 * Check if ACK could be delayed. If ack can be delayed, it should return
1087 * true for a full frame. If we're always acking return 0.
Dave Barach68b0fb02017-02-28 15:15:56 -05001088 */
1089always_inline int
1090tcp_can_delack (tcp_connection_t * tc)
1091{
Florin Coras6792ec02017-03-13 03:49:51 -07001092 /* Send ack if ... */
1093 if (TCP_ALWAYS_ACK
1094 /* just sent a rcv wnd 0 */
1095 || (tc->flags & TCP_CONN_SENT_RCV_WND0) != 0
1096 /* constrained to send ack */
1097 || (tc->flags & TCP_CONN_SNDACK) != 0
1098 /* we're almost out of tx wnd */
1099 || tcp_available_snd_space (tc) < 2 * tc->snd_mss)
1100 return 0;
Dave Barach68b0fb02017-02-28 15:15:56 -05001101
Florin Coras6792ec02017-03-13 03:49:51 -07001102 return 1;
Dave Barach68b0fb02017-02-28 15:15:56 -05001103}
1104
1105static int
1106tcp_segment_rcv (tcp_main_t * tm, tcp_connection_t * tc, vlib_buffer_t * b,
1107 u16 n_data_bytes, u32 * next0)
1108{
1109 u32 error = 0;
1110
1111 /* Handle out-of-order data */
1112 if (PREDICT_FALSE (vnet_buffer (b)->tcp.seq_number != tc->rcv_nxt))
1113 {
Florin Coras6792ec02017-03-13 03:49:51 -07001114 /* Old sequence numbers allowed through because they overlapped
1115 * the rx window */
Dave Barach1f75cfd2017-04-14 16:46:44 -04001116
Florin Coras6792ec02017-03-13 03:49:51 -07001117 if (seq_lt (vnet_buffer (b)->tcp.seq_number, tc->rcv_nxt))
Dave Barach68b0fb02017-02-28 15:15:56 -05001118 {
Florin Coras6792ec02017-03-13 03:49:51 -07001119 error = TCP_ERROR_SEGMENT_OLD;
1120 *next0 = TCP_NEXT_DROP;
1121 goto done;
Dave Barach68b0fb02017-02-28 15:15:56 -05001122 }
1123
Florin Coras6792ec02017-03-13 03:49:51 -07001124 error = tcp_session_enqueue_ooo (tc, b, n_data_bytes);
1125
1126 /* N.B. Should not filter burst of dupacks. Two issues 1) dupacks open
1127 * cwnd on remote peer when congested 2) acks leaving should have the
1128 * latest rcv_wnd since the burst may eaten up all of it, so only the
1129 * old ones could be filtered.
1130 */
1131
1132 /* RFC2581: Send DUPACK for fast retransmit */
1133 tcp_make_ack (tc, b);
1134 *next0 = tcp_next_output (tc->c_is_ip4);
1135
1136 /* Mark as DUPACK. We may filter these in output if
1137 * the burst fills the holes. */
1138 if (n_data_bytes)
1139 vnet_buffer (b)->tcp.flags = TCP_BUF_FLAG_DUPACK;
1140
1141 TCP_EVT_DBG (TCP_EVT_DUPACK_SENT, tc);
Dave Barach68b0fb02017-02-28 15:15:56 -05001142 goto done;
1143 }
1144
1145 /* In order data, enqueue. Fifo figures out by itself if any out-of-order
1146 * segments can be enqueued after fifo tail offset changes. */
1147 error = tcp_session_enqueue_data (tc, b, n_data_bytes);
1148
Florin Coras6792ec02017-03-13 03:49:51 -07001149 if (n_data_bytes == 0)
1150 {
1151 *next0 = TCP_NEXT_DROP;
1152 goto done;
1153 }
1154
Dave Barach68b0fb02017-02-28 15:15:56 -05001155 /* Check if ACK can be delayed */
Florin Coras3e350af2017-03-30 02:54:28 -07001156 if (tcp_can_delack (tc))
Dave Barach68b0fb02017-02-28 15:15:56 -05001157 {
Florin Coras6792ec02017-03-13 03:49:51 -07001158 if (!tcp_timer_is_active (tc, TCP_TIMER_DELACK))
1159 tcp_timer_set (tc, TCP_TIMER_DELACK, TCP_DELACK_TIME);
Florin Coras3e350af2017-03-30 02:54:28 -07001160 goto done;
Dave Barach68b0fb02017-02-28 15:15:56 -05001161 }
1162
Florin Coras3e350af2017-03-30 02:54:28 -07001163 *next0 = tcp_next_output (tc->c_is_ip4);
1164 tcp_make_ack (tc, b);
1165
Dave Barach68b0fb02017-02-28 15:15:56 -05001166done:
1167 return error;
1168}
1169
Clement Durand6cf260c2017-04-13 13:27:04 +02001170typedef struct
1171{
1172 tcp_header_t tcp_header;
1173 tcp_connection_t tcp_connection;
1174} tcp_rx_trace_t;
1175
1176u8 *
1177format_tcp_rx_trace (u8 * s, va_list * args)
1178{
1179 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
1180 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
1181 tcp_rx_trace_t *t = va_arg (*args, tcp_rx_trace_t *);
1182 uword indent = format_get_indent (s);
1183
1184 s = format (s, "%U\n%U%U",
1185 format_tcp_header, &t->tcp_header, 128,
1186 format_white_space, indent,
1187 format_tcp_connection_verbose, &t->tcp_connection);
1188
1189 return s;
1190}
1191
1192u8 *
1193format_tcp_rx_trace_short (u8 * s, va_list * args)
1194{
1195 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
1196 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
1197 tcp_rx_trace_t *t = va_arg (*args, tcp_rx_trace_t *);
1198
1199 s = format (s, "%d -> %d (%U)",
1200 clib_net_to_host_u16 (t->tcp_header.src_port),
1201 clib_net_to_host_u16 (t->tcp_header.dst_port), format_tcp_state,
1202 &t->tcp_connection.state);
1203
1204 return s;
1205}
1206
Florin Coras82b13a82017-04-25 11:58:06 -07001207void
1208tcp_set_rx_trace_data (tcp_rx_trace_t * t0, tcp_connection_t * tc0,
1209 tcp_header_t * th0, vlib_buffer_t * b0, u8 is_ip4)
1210{
1211 if (tc0)
1212 {
1213 clib_memcpy (&t0->tcp_connection, tc0, sizeof (t0->tcp_connection));
1214 }
1215 else
1216 {
1217 th0 = tcp_buffer_hdr (b0);
1218 }
1219 clib_memcpy (&t0->tcp_header, th0, sizeof (t0->tcp_header));
1220}
1221
Florin Coras6792ec02017-03-13 03:49:51 -07001222always_inline void
1223tcp_established_inc_counter (vlib_main_t * vm, u8 is_ip4, u8 evt, u8 val)
Dave Barach68b0fb02017-02-28 15:15:56 -05001224{
Florin Coras6792ec02017-03-13 03:49:51 -07001225 if (PREDICT_TRUE (!val))
1226 return;
Dave Barach68b0fb02017-02-28 15:15:56 -05001227
Florin Coras6792ec02017-03-13 03:49:51 -07001228 if (is_ip4)
1229 vlib_node_increment_counter (vm, tcp4_established_node.index, evt, val);
1230 else
1231 vlib_node_increment_counter (vm, tcp6_established_node.index, evt, val);
Dave Barach68b0fb02017-02-28 15:15:56 -05001232}
1233
1234always_inline uword
1235tcp46_established_inline (vlib_main_t * vm, vlib_node_runtime_t * node,
1236 vlib_frame_t * from_frame, int is_ip4)
1237{
1238 u32 n_left_from, next_index, *from, *to_next;
Damjan Marion586afd72017-04-05 19:18:20 +02001239 u32 my_thread_index = vm->thread_index, errors = 0;
Dave Barach68b0fb02017-02-28 15:15:56 -05001240 tcp_main_t *tm = vnet_get_tcp_main ();
Dave Barach1f75cfd2017-04-14 16:46:44 -04001241 u8 is_fin = 0;
Dave Barach68b0fb02017-02-28 15:15:56 -05001242
1243 from = vlib_frame_vector_args (from_frame);
1244 n_left_from = from_frame->n_vectors;
1245
1246 next_index = node->cached_next_index;
1247
1248 while (n_left_from > 0)
1249 {
1250 u32 n_left_to_next;
1251
1252 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
1253
1254 while (n_left_from > 0 && n_left_to_next > 0)
1255 {
1256 u32 bi0;
1257 vlib_buffer_t *b0;
1258 tcp_header_t *th0 = 0;
1259 tcp_connection_t *tc0;
Dave Barach68b0fb02017-02-28 15:15:56 -05001260 u32 next0 = TCP_ESTABLISHED_NEXT_DROP, error0 = TCP_ERROR_ENQUEUED;
1261
1262 bi0 = from[0];
1263 to_next[0] = bi0;
1264 from += 1;
1265 to_next += 1;
1266 n_left_from -= 1;
1267 n_left_to_next -= 1;
1268
1269 b0 = vlib_get_buffer (vm, bi0);
1270 tc0 = tcp_connection_get (vnet_buffer (b0)->tcp.connection_index,
1271 my_thread_index);
1272
Florin Corasd79b41e2017-03-04 05:37:52 -08001273 if (PREDICT_FALSE (tc0 == 0))
1274 {
1275 error0 = TCP_ERROR_INVALID_CONNECTION;
Florin Coras6792ec02017-03-13 03:49:51 -07001276 goto done;
Florin Corasd79b41e2017-03-04 05:37:52 -08001277 }
1278
Florin Coras82b13a82017-04-25 11:58:06 -07001279 th0 = tcp_buffer_hdr (b0);
Dave Barach68b0fb02017-02-28 15:15:56 -05001280
Dave Barach1f75cfd2017-04-14 16:46:44 -04001281 is_fin = (th0->flags & TCP_FLAG_FIN) != 0;
1282
Dave Barach68b0fb02017-02-28 15:15:56 -05001283 /* SYNs, FINs and data consume sequence numbers */
1284 vnet_buffer (b0)->tcp.seq_end = vnet_buffer (b0)->tcp.seq_number
Florin Coras82b13a82017-04-25 11:58:06 -07001285 + tcp_is_syn (th0) + is_fin + vnet_buffer (b0)->tcp.data_len;
Dave Barach68b0fb02017-02-28 15:15:56 -05001286
1287 /* TODO header prediction fast path */
1288
1289 /* 1-4: check SEQ, RST, SYN */
1290 if (PREDICT_FALSE (tcp_segment_validate (vm, tc0, b0, th0, &next0)))
1291 {
1292 error0 = TCP_ERROR_SEGMENT_INVALID;
Florin Coras6792ec02017-03-13 03:49:51 -07001293 TCP_EVT_DBG (TCP_EVT_SEG_INVALID, tc0,
1294 vnet_buffer (b0)->tcp.seq_number,
1295 vnet_buffer (b0)->tcp.seq_end);
1296 goto done;
Dave Barach68b0fb02017-02-28 15:15:56 -05001297 }
1298
1299 /* 5: check the ACK field */
1300 if (tcp_rcv_ack (tc0, b0, th0, &next0, &error0))
1301 {
Florin Coras6792ec02017-03-13 03:49:51 -07001302 goto done;
Dave Barach68b0fb02017-02-28 15:15:56 -05001303 }
1304
1305 /* 6: check the URG bit TODO */
1306
1307 /* 7: process the segment text */
Florin Coras6792ec02017-03-13 03:49:51 -07001308
Florin Coras82b13a82017-04-25 11:58:06 -07001309 vlib_buffer_advance (b0, vnet_buffer (b0)->tcp.data_offset);
1310 error0 = tcp_segment_rcv (tm, tc0, b0,
1311 vnet_buffer (b0)->tcp.data_len, &next0);
Dave Barach68b0fb02017-02-28 15:15:56 -05001312
Dave Barach1f75cfd2017-04-14 16:46:44 -04001313 /* N.B. buffer is rewritten if segment is ooo. Thus, th0 becomes a
1314 * dangling reference. */
1315
Dave Barach68b0fb02017-02-28 15:15:56 -05001316 /* 8: check the FIN bit */
Dave Barach1f75cfd2017-04-14 16:46:44 -04001317 if (is_fin)
Dave Barach68b0fb02017-02-28 15:15:56 -05001318 {
Florin Corasd79b41e2017-03-04 05:37:52 -08001319 /* Enter CLOSE-WAIT and notify session. Don't send ACK, instead
1320 * wait for session to call close. To avoid lingering
1321 * in CLOSE-WAIT, set timer (reuse WAITCLOSE). */
Dave Barach68b0fb02017-02-28 15:15:56 -05001322 tc0->state = TCP_STATE_CLOSE_WAIT;
Florin Corase69f4952017-03-07 10:06:24 -08001323 TCP_EVT_DBG (TCP_EVT_FIN_RCVD, tc0);
Dave Barach68b0fb02017-02-28 15:15:56 -05001324 stream_session_disconnect_notify (&tc0->connection);
Florin Corasd79b41e2017-03-04 05:37:52 -08001325 tcp_timer_set (tc0, TCP_TIMER_WAITCLOSE, TCP_CLOSEWAIT_TIME);
Dave Barach68b0fb02017-02-28 15:15:56 -05001326 }
1327
Florin Coras6792ec02017-03-13 03:49:51 -07001328 done:
Dave Barach68b0fb02017-02-28 15:15:56 -05001329 b0->error = node->errors[error0];
1330 if (PREDICT_FALSE (b0->flags & VLIB_BUFFER_IS_TRACED))
1331 {
Florin Coras82b13a82017-04-25 11:58:06 -07001332 tcp_rx_trace_t *t0 =
1333 vlib_add_trace (vm, node, b0, sizeof (*t0));
1334 tcp_set_rx_trace_data (t0, tc0, th0, b0, is_ip4);
Dave Barach68b0fb02017-02-28 15:15:56 -05001335 }
1336
1337 vlib_validate_buffer_enqueue_x1 (vm, node, next_index, to_next,
1338 n_left_to_next, bi0, next0);
1339 }
1340
1341 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
1342 }
1343
1344 errors = session_manager_flush_enqueue_events (my_thread_index);
Florin Coras6792ec02017-03-13 03:49:51 -07001345 tcp_established_inc_counter (vm, is_ip4, TCP_ERROR_EVENT_FIFO_FULL, errors);
Dave Barach68b0fb02017-02-28 15:15:56 -05001346
1347 return from_frame->n_vectors;
1348}
1349
1350static uword
1351tcp4_established (vlib_main_t * vm, vlib_node_runtime_t * node,
1352 vlib_frame_t * from_frame)
1353{
1354 return tcp46_established_inline (vm, node, from_frame, 1 /* is_ip4 */ );
1355}
1356
1357static uword
1358tcp6_established (vlib_main_t * vm, vlib_node_runtime_t * node,
1359 vlib_frame_t * from_frame)
1360{
1361 return tcp46_established_inline (vm, node, from_frame, 0 /* is_ip4 */ );
1362}
1363
1364/* *INDENT-OFF* */
1365VLIB_REGISTER_NODE (tcp4_established_node) =
1366{
1367 .function = tcp4_established,
1368 .name = "tcp4-established",
1369 /* Takes a vector of packets. */
1370 .vector_size = sizeof (u32),
Florin Corase69f4952017-03-07 10:06:24 -08001371 .n_errors = TCP_N_ERROR,
1372 .error_strings = tcp_error_strings,
Dave Barach68b0fb02017-02-28 15:15:56 -05001373 .n_next_nodes = TCP_ESTABLISHED_N_NEXT,
1374 .next_nodes =
1375 {
1376#define _(s,n) [TCP_ESTABLISHED_NEXT_##s] = n,
1377 foreach_tcp_state_next
1378#undef _
1379 },
Clement Durand6cf260c2017-04-13 13:27:04 +02001380 .format_trace = format_tcp_rx_trace_short,
Dave Barach68b0fb02017-02-28 15:15:56 -05001381};
1382/* *INDENT-ON* */
1383
1384VLIB_NODE_FUNCTION_MULTIARCH (tcp4_established_node, tcp4_established);
1385
1386/* *INDENT-OFF* */
1387VLIB_REGISTER_NODE (tcp6_established_node) =
1388{
1389 .function = tcp6_established,
1390 .name = "tcp6-established",
1391 /* Takes a vector of packets. */
1392 .vector_size = sizeof (u32),
1393 .n_errors = TCP_N_ERROR,
1394 .error_strings = tcp_error_strings,
1395 .n_next_nodes = TCP_ESTABLISHED_N_NEXT,
1396 .next_nodes =
1397 {
1398#define _(s,n) [TCP_ESTABLISHED_NEXT_##s] = n,
1399 foreach_tcp_state_next
1400#undef _
1401 },
Clement Durand6cf260c2017-04-13 13:27:04 +02001402 .format_trace = format_tcp_rx_trace_short,
Dave Barach68b0fb02017-02-28 15:15:56 -05001403};
1404/* *INDENT-ON* */
1405
1406
1407VLIB_NODE_FUNCTION_MULTIARCH (tcp6_established_node, tcp6_established);
1408
1409vlib_node_registration_t tcp4_syn_sent_node;
1410vlib_node_registration_t tcp6_syn_sent_node;
1411
1412always_inline uword
1413tcp46_syn_sent_inline (vlib_main_t * vm, vlib_node_runtime_t * node,
1414 vlib_frame_t * from_frame, int is_ip4)
1415{
1416 tcp_main_t *tm = vnet_get_tcp_main ();
1417 u32 n_left_from, next_index, *from, *to_next;
Damjan Marion586afd72017-04-05 19:18:20 +02001418 u32 my_thread_index = vm->thread_index, errors = 0;
Dave Barach68b0fb02017-02-28 15:15:56 -05001419 u8 sst = is_ip4 ? SESSION_TYPE_IP4_TCP : SESSION_TYPE_IP6_TCP;
1420
1421 from = vlib_frame_vector_args (from_frame);
1422 n_left_from = from_frame->n_vectors;
1423
1424 next_index = node->cached_next_index;
1425
1426 while (n_left_from > 0)
1427 {
1428 u32 n_left_to_next;
1429
1430 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
1431
1432 while (n_left_from > 0 && n_left_to_next > 0)
1433 {
1434 u32 bi0, ack0, seq0;
1435 vlib_buffer_t *b0;
Clement Durand6cf260c2017-04-13 13:27:04 +02001436 tcp_rx_trace_t *t0;
Dave Barach68b0fb02017-02-28 15:15:56 -05001437 tcp_header_t *tcp0 = 0;
1438 tcp_connection_t *tc0;
Dave Barach68b0fb02017-02-28 15:15:56 -05001439 tcp_connection_t *new_tc0;
1440 u32 next0 = TCP_SYN_SENT_NEXT_DROP, error0 = TCP_ERROR_ENQUEUED;
1441
1442 bi0 = from[0];
1443 to_next[0] = bi0;
1444 from += 1;
1445 to_next += 1;
1446 n_left_from -= 1;
1447 n_left_to_next -= 1;
1448
1449 b0 = vlib_get_buffer (vm, bi0);
1450 tc0 =
1451 tcp_half_open_connection_get (vnet_buffer (b0)->
1452 tcp.connection_index);
1453
1454 ack0 = vnet_buffer (b0)->tcp.ack_number;
1455 seq0 = vnet_buffer (b0)->tcp.seq_number;
Florin Coras82b13a82017-04-25 11:58:06 -07001456 tcp0 = tcp_buffer_hdr (b0);
Dave Barach68b0fb02017-02-28 15:15:56 -05001457
1458 if (PREDICT_FALSE
1459 (!tcp_ack (tcp0) && !tcp_rst (tcp0) && !tcp_syn (tcp0)))
1460 goto drop;
1461
1462 /* SYNs, FINs and data consume sequence numbers */
1463 vnet_buffer (b0)->tcp.seq_end = seq0 + tcp_is_syn (tcp0)
Florin Coras82b13a82017-04-25 11:58:06 -07001464 + tcp_is_fin (tcp0) + vnet_buffer (b0)->tcp.data_len;
Dave Barach68b0fb02017-02-28 15:15:56 -05001465
1466 /*
1467 * 1. check the ACK bit
1468 */
1469
1470 /*
1471 * If the ACK bit is set
1472 * If SEG.ACK =< ISS, or SEG.ACK > SND.NXT, send a reset (unless
1473 * the RST bit is set, if so drop the segment and return)
1474 * <SEQ=SEG.ACK><CTL=RST>
1475 * and discard the segment. Return.
1476 * If SND.UNA =< SEG.ACK =< SND.NXT then the ACK is acceptable.
1477 */
1478 if (tcp_ack (tcp0))
1479 {
1480 if (ack0 <= tc0->iss || ack0 > tc0->snd_nxt)
1481 {
1482 if (!tcp_rst (tcp0))
1483 tcp_send_reset (b0, is_ip4);
1484
1485 goto drop;
1486 }
1487
1488 /* Make sure ACK is valid */
1489 if (tc0->snd_una > ack0)
1490 goto drop;
1491 }
1492
1493 /*
1494 * 2. check the RST bit
1495 */
1496
1497 if (tcp_rst (tcp0))
1498 {
1499 /* If ACK is acceptable, signal client that peer is not
1500 * willing to accept connection and drop connection*/
1501 if (tcp_ack (tcp0))
1502 {
1503 stream_session_connect_notify (&tc0->connection, sst,
1504 1 /* fail */ );
1505 tcp_connection_cleanup (tc0);
1506 }
1507 goto drop;
1508 }
1509
1510 /*
1511 * 3. check the security and precedence (skipped)
1512 */
1513
1514 /*
1515 * 4. check the SYN bit
1516 */
1517
1518 /* No SYN flag. Drop. */
1519 if (!tcp_syn (tcp0))
1520 goto drop;
1521
1522 /* Stop connection establishment and retransmit timers */
1523 tcp_timer_reset (tc0, TCP_TIMER_ESTABLISH);
1524 tcp_timer_reset (tc0, TCP_TIMER_RETRANSMIT_SYN);
1525
1526 /* Valid SYN or SYN-ACK. Move connection from half-open pool to
1527 * current thread pool. */
1528 pool_get (tm->connections[my_thread_index], new_tc0);
1529 clib_memcpy (new_tc0, tc0, sizeof (*new_tc0));
1530
1531 new_tc0->c_thread_index = my_thread_index;
1532
1533 /* Cleanup half-open connection XXX lock */
1534 pool_put (tm->half_open_connections, tc0);
1535
1536 new_tc0->rcv_nxt = vnet_buffer (b0)->tcp.seq_end;
1537 new_tc0->irs = seq0;
1538
1539 /* Parse options */
1540 tcp_options_parse (tcp0, &new_tc0->opt);
Dave Barach68b0fb02017-02-28 15:15:56 -05001541
1542 if (tcp_opts_tstamp (&new_tc0->opt))
1543 {
1544 new_tc0->tsval_recent = new_tc0->opt.tsval;
1545 new_tc0->tsval_recent_age = tcp_time_now ();
1546 }
1547
1548 if (tcp_opts_wscale (&new_tc0->opt))
1549 new_tc0->snd_wscale = new_tc0->opt.wscale;
1550
Florin Corase04c2992017-03-01 08:17:34 -08001551 /* No scaling */
1552 new_tc0->snd_wnd = clib_net_to_host_u16 (tcp0->window);
Dave Barach68b0fb02017-02-28 15:15:56 -05001553 new_tc0->snd_wl1 = seq0;
1554 new_tc0->snd_wl2 = ack0;
1555
Florin Corase04c2992017-03-01 08:17:34 -08001556 tcp_connection_init_vars (new_tc0);
1557
Dave Barach68b0fb02017-02-28 15:15:56 -05001558 /* SYN-ACK: See if we can switch to ESTABLISHED state */
1559 if (tcp_ack (tcp0))
1560 {
1561 /* Our SYN is ACKed: we have iss < ack = snd_una */
1562
1563 /* TODO Dequeue acknowledged segments if we support Fast Open */
1564 new_tc0->snd_una = ack0;
1565 new_tc0->state = TCP_STATE_ESTABLISHED;
1566
Florin Corase04c2992017-03-01 08:17:34 -08001567 /* Make sure las is initialized for the wnd computation */
1568 new_tc0->rcv_las = new_tc0->rcv_nxt;
1569
Dave Barach68b0fb02017-02-28 15:15:56 -05001570 /* Notify app that we have connection */
1571 stream_session_connect_notify (&new_tc0->connection, sst, 0);
1572
Florin Corasc28764f2017-04-26 00:08:42 -07001573 stream_session_init_fifos_pointers (&new_tc0->connection,
1574 new_tc0->irs + 1,
1575 new_tc0->iss + 1);
Dave Barach68b0fb02017-02-28 15:15:56 -05001576 /* Make sure after data segment processing ACK is sent */
1577 new_tc0->flags |= TCP_CONN_SNDACK;
1578 }
1579 /* SYN: Simultaneous open. Change state to SYN-RCVD and send SYN-ACK */
1580 else
1581 {
1582 new_tc0->state = TCP_STATE_SYN_RCVD;
1583
Florin Corase69f4952017-03-07 10:06:24 -08001584 /* Notify app that we have connection */
Dave Barach68b0fb02017-02-28 15:15:56 -05001585 stream_session_connect_notify (&new_tc0->connection, sst, 0);
Florin Corasc28764f2017-04-26 00:08:42 -07001586 stream_session_init_fifos_pointers (&new_tc0->connection,
1587 new_tc0->irs + 1,
1588 new_tc0->iss + 1);
Dave Barach68b0fb02017-02-28 15:15:56 -05001589 tcp_make_synack (new_tc0, b0);
1590 next0 = tcp_next_output (is_ip4);
1591
1592 goto drop;
1593 }
1594
1595 /* Read data, if any */
Florin Coras82b13a82017-04-25 11:58:06 -07001596 if (vnet_buffer (b0)->tcp.data_len)
Dave Barach68b0fb02017-02-28 15:15:56 -05001597 {
Florin Coras82b13a82017-04-25 11:58:06 -07001598 vlib_buffer_advance (b0, vnet_buffer (b0)->tcp.data_offset);
1599 error0 = tcp_segment_rcv (tm, new_tc0, b0,
1600 vnet_buffer (b0)->tcp.data_len,
1601 &next0);
Dave Barach68b0fb02017-02-28 15:15:56 -05001602 if (error0 == TCP_ERROR_PURE_ACK)
1603 error0 = TCP_ERROR_SYN_ACKS_RCVD;
1604 }
1605 else
1606 {
1607 tcp_make_ack (new_tc0, b0);
1608 next0 = tcp_next_output (new_tc0->c_is_ip4);
1609 }
1610
1611 drop:
1612
1613 b0->error = error0 ? node->errors[error0] : 0;
1614 if (PREDICT_FALSE (b0->flags & VLIB_BUFFER_IS_TRACED))
1615 {
Clement Durand6cf260c2017-04-13 13:27:04 +02001616 t0 = vlib_add_trace (vm, node, b0, sizeof (*t0));
1617 clib_memcpy (&t0->tcp_header, tcp0, sizeof (t0->tcp_header));
1618 clib_memcpy (&t0->tcp_connection, tc0,
1619 sizeof (t0->tcp_connection));
Dave Barach68b0fb02017-02-28 15:15:56 -05001620 }
1621
1622 vlib_validate_buffer_enqueue_x1 (vm, node, next_index, to_next,
1623 n_left_to_next, bi0, next0);
1624 }
1625
1626 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
1627 }
1628
1629 errors = session_manager_flush_enqueue_events (my_thread_index);
1630 if (errors)
1631 {
1632 if (is_ip4)
1633 vlib_node_increment_counter (vm, tcp4_established_node.index,
1634 TCP_ERROR_EVENT_FIFO_FULL, errors);
1635 else
1636 vlib_node_increment_counter (vm, tcp6_established_node.index,
1637 TCP_ERROR_EVENT_FIFO_FULL, errors);
1638 }
1639
1640 return from_frame->n_vectors;
1641}
1642
1643static uword
1644tcp4_syn_sent (vlib_main_t * vm, vlib_node_runtime_t * node,
1645 vlib_frame_t * from_frame)
1646{
1647 return tcp46_syn_sent_inline (vm, node, from_frame, 1 /* is_ip4 */ );
1648}
1649
1650static uword
1651tcp6_syn_sent_rcv (vlib_main_t * vm, vlib_node_runtime_t * node,
1652 vlib_frame_t * from_frame)
1653{
1654 return tcp46_syn_sent_inline (vm, node, from_frame, 0 /* is_ip4 */ );
1655}
1656
1657/* *INDENT-OFF* */
1658VLIB_REGISTER_NODE (tcp4_syn_sent_node) =
1659{
1660 .function = tcp4_syn_sent,
1661 .name = "tcp4-syn-sent",
1662 /* Takes a vector of packets. */
1663 .vector_size = sizeof (u32),
1664 .n_errors = TCP_N_ERROR,
1665 .error_strings = tcp_error_strings,
1666 .n_next_nodes = TCP_SYN_SENT_N_NEXT,
1667 .next_nodes =
1668 {
1669#define _(s,n) [TCP_SYN_SENT_NEXT_##s] = n,
1670 foreach_tcp_state_next
1671#undef _
1672 },
Clement Durand6cf260c2017-04-13 13:27:04 +02001673 .format_trace = format_tcp_rx_trace_short,
Dave Barach68b0fb02017-02-28 15:15:56 -05001674};
1675/* *INDENT-ON* */
1676
1677VLIB_NODE_FUNCTION_MULTIARCH (tcp4_syn_sent_node, tcp4_syn_sent);
1678
1679/* *INDENT-OFF* */
1680VLIB_REGISTER_NODE (tcp6_syn_sent_node) =
1681{
1682 .function = tcp6_syn_sent_rcv,
1683 .name = "tcp6-syn-sent",
1684 /* Takes a vector of packets. */
1685 .vector_size = sizeof (u32),
1686 .n_errors = TCP_N_ERROR,
1687 .error_strings = tcp_error_strings,
1688 .n_next_nodes = TCP_SYN_SENT_N_NEXT,
1689 .next_nodes =
1690 {
1691#define _(s,n) [TCP_SYN_SENT_NEXT_##s] = n,
1692 foreach_tcp_state_next
1693#undef _
Clement Durand6cf260c2017-04-13 13:27:04 +02001694 },
1695 .format_trace = format_tcp_rx_trace_short,
1696};
Dave Barach68b0fb02017-02-28 15:15:56 -05001697/* *INDENT-ON* */
1698
1699VLIB_NODE_FUNCTION_MULTIARCH (tcp6_syn_sent_node, tcp6_syn_sent_rcv);
1700/**
Florin Corasd79b41e2017-03-04 05:37:52 -08001701 * Handles reception for all states except LISTEN, SYN-SENT and ESTABLISHED
Dave Barach68b0fb02017-02-28 15:15:56 -05001702 * as per RFC793 p. 64
1703 */
1704always_inline uword
1705tcp46_rcv_process_inline (vlib_main_t * vm, vlib_node_runtime_t * node,
1706 vlib_frame_t * from_frame, int is_ip4)
1707{
1708 tcp_main_t *tm = vnet_get_tcp_main ();
1709 u32 n_left_from, next_index, *from, *to_next;
Damjan Marion586afd72017-04-05 19:18:20 +02001710 u32 my_thread_index = vm->thread_index, errors = 0;
Dave Barach68b0fb02017-02-28 15:15:56 -05001711
1712 from = vlib_frame_vector_args (from_frame);
1713 n_left_from = from_frame->n_vectors;
1714
1715 next_index = node->cached_next_index;
1716
1717 while (n_left_from > 0)
1718 {
1719 u32 n_left_to_next;
1720
1721 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
1722
1723 while (n_left_from > 0 && n_left_to_next > 0)
1724 {
1725 u32 bi0;
1726 vlib_buffer_t *b0;
1727 tcp_header_t *tcp0 = 0;
1728 tcp_connection_t *tc0;
Dave Barach68b0fb02017-02-28 15:15:56 -05001729 u32 next0 = TCP_RCV_PROCESS_NEXT_DROP, error0 = TCP_ERROR_ENQUEUED;
1730
1731 bi0 = from[0];
1732 to_next[0] = bi0;
1733 from += 1;
1734 to_next += 1;
1735 n_left_from -= 1;
1736 n_left_to_next -= 1;
1737
1738 b0 = vlib_get_buffer (vm, bi0);
1739 tc0 = tcp_connection_get (vnet_buffer (b0)->tcp.connection_index,
1740 my_thread_index);
Florin Corasd79b41e2017-03-04 05:37:52 -08001741 if (PREDICT_FALSE (tc0 == 0))
1742 {
1743 error0 = TCP_ERROR_INVALID_CONNECTION;
1744 goto drop;
1745 }
Dave Barach68b0fb02017-02-28 15:15:56 -05001746
Florin Coras82b13a82017-04-25 11:58:06 -07001747 tcp0 = tcp_buffer_hdr (b0);
Dave Barach68b0fb02017-02-28 15:15:56 -05001748
1749 /* SYNs, FINs and data consume sequence numbers */
1750 vnet_buffer (b0)->tcp.seq_end = vnet_buffer (b0)->tcp.seq_number
Florin Coras82b13a82017-04-25 11:58:06 -07001751 + tcp_is_syn (tcp0) + tcp_is_fin (tcp0)
1752 + vnet_buffer (b0)->tcp.data_len;
Dave Barach68b0fb02017-02-28 15:15:56 -05001753
1754 /*
1755 * Special treatment for CLOSED
1756 */
1757 switch (tc0->state)
1758 {
1759 case TCP_STATE_CLOSED:
1760 goto drop;
1761 break;
1762 }
1763
1764 /*
1765 * For all other states (except LISTEN)
1766 */
1767
1768 /* 1-4: check SEQ, RST, SYN */
1769 if (PREDICT_FALSE
1770 (tcp_segment_validate (vm, tc0, b0, tcp0, &next0)))
1771 {
1772 error0 = TCP_ERROR_SEGMENT_INVALID;
1773 goto drop;
1774 }
1775
1776 /* 5: check the ACK field */
1777 switch (tc0->state)
1778 {
1779 case TCP_STATE_SYN_RCVD:
1780 /*
1781 * If the segment acknowledgment is not acceptable, form a
1782 * reset segment,
1783 * <SEQ=SEG.ACK><CTL=RST>
1784 * and send it.
1785 */
1786 if (!tcp_rcv_ack_is_acceptable (tc0, b0))
1787 {
1788 tcp_send_reset (b0, is_ip4);
1789 goto drop;
1790 }
Florin Coras3af90fc2017-05-03 21:09:42 -07001791
1792 /* Update rtt and rto */
1793 tc0->bytes_acked = 1;
1794 tcp_update_rtt (tc0, vnet_buffer (b0)->tcp.ack_number);
1795
Dave Barach68b0fb02017-02-28 15:15:56 -05001796 /* Switch state to ESTABLISHED */
1797 tc0->state = TCP_STATE_ESTABLISHED;
1798
1799 /* Initialize session variables */
1800 tc0->snd_una = vnet_buffer (b0)->tcp.ack_number;
Florin Corase04c2992017-03-01 08:17:34 -08001801 tc0->snd_wnd = clib_net_to_host_u16 (tcp0->window)
Dave Barach68b0fb02017-02-28 15:15:56 -05001802 << tc0->opt.wscale;
1803 tc0->snd_wl1 = vnet_buffer (b0)->tcp.seq_number;
1804 tc0->snd_wl2 = vnet_buffer (b0)->tcp.ack_number;
1805
1806 /* Shoulder tap the server */
1807 stream_session_accept_notify (&tc0->connection);
1808
Florin Corasd79b41e2017-03-04 05:37:52 -08001809 /* Reset SYN-ACK retransmit timer */
Florin Coras6792ec02017-03-13 03:49:51 -07001810 tcp_retransmit_timer_reset (tc0);
Dave Barach68b0fb02017-02-28 15:15:56 -05001811 break;
1812 case TCP_STATE_ESTABLISHED:
1813 /* We can get packets in established state here because they
1814 * were enqueued before state change */
1815 if (tcp_rcv_ack (tc0, b0, tcp0, &next0, &error0))
1816 goto drop;
1817
1818 break;
1819 case TCP_STATE_FIN_WAIT_1:
1820 /* In addition to the processing for the ESTABLISHED state, if
1821 * our FIN is now acknowledged then enter FIN-WAIT-2 and
1822 * continue processing in that state. */
1823 if (tcp_rcv_ack (tc0, b0, tcp0, &next0, &error0))
1824 goto drop;
Florin Corasd79b41e2017-03-04 05:37:52 -08001825
1826 /* If FIN is ACKed */
1827 if (tc0->snd_una == tc0->snd_una_max)
1828 {
1829 tc0->state = TCP_STATE_FIN_WAIT_2;
1830 /* Stop all timers, 2MSL will be set lower */
1831 tcp_connection_timers_reset (tc0);
1832 }
Dave Barach68b0fb02017-02-28 15:15:56 -05001833 break;
1834 case TCP_STATE_FIN_WAIT_2:
1835 /* In addition to the processing for the ESTABLISHED state, if
1836 * the retransmission queue is empty, the user's CLOSE can be
1837 * acknowledged ("ok") but do not delete the TCB. */
1838 if (tcp_rcv_ack (tc0, b0, tcp0, &next0, &error0))
1839 goto drop;
1840 /* check if rtx queue is empty and ack CLOSE TODO */
1841 break;
1842 case TCP_STATE_CLOSE_WAIT:
1843 /* Do the same processing as for the ESTABLISHED state. */
1844 if (tcp_rcv_ack (tc0, b0, tcp0, &next0, &error0))
1845 goto drop;
1846 break;
1847 case TCP_STATE_CLOSING:
1848 /* In addition to the processing for the ESTABLISHED state, if
1849 * the ACK acknowledges our FIN then enter the TIME-WAIT state,
1850 * otherwise ignore the segment. */
1851 if (tcp_rcv_ack (tc0, b0, tcp0, &next0, &error0))
1852 goto drop;
1853
1854 /* XXX test that send queue empty */
1855 tc0->state = TCP_STATE_TIME_WAIT;
1856 goto drop;
1857
1858 break;
1859 case TCP_STATE_LAST_ACK:
1860 /* The only thing that can arrive in this state is an
1861 * acknowledgment of our FIN. If our FIN is now acknowledged,
1862 * delete the TCB, enter the CLOSED state, and return. */
1863
1864 if (!tcp_rcv_ack_is_acceptable (tc0, b0))
1865 goto drop;
1866
Florin Corasd79b41e2017-03-04 05:37:52 -08001867 tc0->state = TCP_STATE_CLOSED;
1868
1869 /* Don't delete the connection/session yet. Instead, wait a
1870 * reasonable amount of time until the pipes are cleared. In
1871 * particular, this makes sure that we won't have dead sessions
1872 * when processing events on the tx path */
1873 tcp_timer_update (tc0, TCP_TIMER_WAITCLOSE, TCP_CLEANUP_TIME);
1874
1875 /* Stop retransmit */
Florin Coras6792ec02017-03-13 03:49:51 -07001876 tcp_retransmit_timer_reset (tc0);
Florin Corasd79b41e2017-03-04 05:37:52 -08001877
Dave Barach68b0fb02017-02-28 15:15:56 -05001878 goto drop;
1879
1880 break;
1881 case TCP_STATE_TIME_WAIT:
1882 /* The only thing that can arrive in this state is a
1883 * retransmission of the remote FIN. Acknowledge it, and restart
1884 * the 2 MSL timeout. */
1885
1886 /* TODO */
1887 goto drop;
1888 break;
1889 default:
1890 ASSERT (0);
1891 }
1892
1893 /* 6: check the URG bit TODO */
1894
1895 /* 7: process the segment text */
1896 switch (tc0->state)
1897 {
1898 case TCP_STATE_ESTABLISHED:
1899 case TCP_STATE_FIN_WAIT_1:
1900 case TCP_STATE_FIN_WAIT_2:
Florin Coras82b13a82017-04-25 11:58:06 -07001901 vlib_buffer_advance (b0, vnet_buffer (b0)->tcp.data_offset);
1902 error0 = tcp_segment_rcv (tm, tc0, b0,
1903 vnet_buffer (b0)->tcp.data_len,
1904 &next0);
Dave Barach68b0fb02017-02-28 15:15:56 -05001905 break;
1906 case TCP_STATE_CLOSE_WAIT:
1907 case TCP_STATE_CLOSING:
1908 case TCP_STATE_LAST_ACK:
1909 case TCP_STATE_TIME_WAIT:
1910 /* This should not occur, since a FIN has been received from the
1911 * remote side. Ignore the segment text. */
1912 break;
1913 }
1914
1915 /* 8: check the FIN bit */
1916 if (!tcp_fin (tcp0))
1917 goto drop;
1918
1919 switch (tc0->state)
1920 {
1921 case TCP_STATE_ESTABLISHED:
1922 case TCP_STATE_SYN_RCVD:
1923 /* Send FIN-ACK notify app and enter CLOSE-WAIT */
1924 tcp_connection_timers_reset (tc0);
Florin Corasd79b41e2017-03-04 05:37:52 -08001925 tcp_make_fin (tc0, b0);
Dave Barach68b0fb02017-02-28 15:15:56 -05001926 next0 = tcp_next_output (tc0->c_is_ip4);
1927 stream_session_disconnect_notify (&tc0->connection);
1928 tc0->state = TCP_STATE_CLOSE_WAIT;
1929 break;
1930 case TCP_STATE_CLOSE_WAIT:
1931 case TCP_STATE_CLOSING:
1932 case TCP_STATE_LAST_ACK:
1933 /* move along .. */
1934 break;
1935 case TCP_STATE_FIN_WAIT_1:
1936 tc0->state = TCP_STATE_TIME_WAIT;
1937 tcp_connection_timers_reset (tc0);
Florin Corasd79b41e2017-03-04 05:37:52 -08001938 tcp_timer_set (tc0, TCP_TIMER_WAITCLOSE, TCP_2MSL_TIME);
Dave Barach68b0fb02017-02-28 15:15:56 -05001939 break;
1940 case TCP_STATE_FIN_WAIT_2:
1941 /* Got FIN, send ACK! */
1942 tc0->state = TCP_STATE_TIME_WAIT;
Florin Corase69f4952017-03-07 10:06:24 -08001943 tcp_timer_set (tc0, TCP_TIMER_WAITCLOSE, TCP_CLOSEWAIT_TIME);
Dave Barach68b0fb02017-02-28 15:15:56 -05001944 tcp_make_ack (tc0, b0);
1945 next0 = tcp_next_output (is_ip4);
1946 break;
1947 case TCP_STATE_TIME_WAIT:
1948 /* Remain in the TIME-WAIT state. Restart the 2 MSL time-wait
1949 * timeout.
1950 */
Florin Corasd79b41e2017-03-04 05:37:52 -08001951 tcp_timer_update (tc0, TCP_TIMER_WAITCLOSE, TCP_2MSL_TIME);
Dave Barach68b0fb02017-02-28 15:15:56 -05001952 break;
1953 }
Florin Corase69f4952017-03-07 10:06:24 -08001954 TCP_EVT_DBG (TCP_EVT_FIN_RCVD, tc0);
Dave Barach68b0fb02017-02-28 15:15:56 -05001955
Florin Coras82b13a82017-04-25 11:58:06 -07001956 drop:
Dave Barach68b0fb02017-02-28 15:15:56 -05001957 b0->error = error0 ? node->errors[error0] : 0;
1958
Dave Barach68b0fb02017-02-28 15:15:56 -05001959 if (PREDICT_FALSE (b0->flags & VLIB_BUFFER_IS_TRACED))
1960 {
Florin Coras82b13a82017-04-25 11:58:06 -07001961 tcp_rx_trace_t *t0 =
1962 vlib_add_trace (vm, node, b0, sizeof (*t0));
1963 tcp_set_rx_trace_data (t0, tc0, tcp0, b0, is_ip4);
Dave Barach68b0fb02017-02-28 15:15:56 -05001964 }
1965
1966 vlib_validate_buffer_enqueue_x1 (vm, node, next_index, to_next,
1967 n_left_to_next, bi0, next0);
1968 }
1969
1970 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
1971 }
1972
1973 errors = session_manager_flush_enqueue_events (my_thread_index);
1974 if (errors)
1975 {
1976 if (is_ip4)
1977 vlib_node_increment_counter (vm, tcp4_established_node.index,
1978 TCP_ERROR_EVENT_FIFO_FULL, errors);
1979 else
1980 vlib_node_increment_counter (vm, tcp6_established_node.index,
1981 TCP_ERROR_EVENT_FIFO_FULL, errors);
1982 }
1983
1984 return from_frame->n_vectors;
1985}
1986
1987static uword
1988tcp4_rcv_process (vlib_main_t * vm, vlib_node_runtime_t * node,
1989 vlib_frame_t * from_frame)
1990{
1991 return tcp46_rcv_process_inline (vm, node, from_frame, 1 /* is_ip4 */ );
1992}
1993
1994static uword
1995tcp6_rcv_process (vlib_main_t * vm, vlib_node_runtime_t * node,
1996 vlib_frame_t * from_frame)
1997{
1998 return tcp46_rcv_process_inline (vm, node, from_frame, 0 /* is_ip4 */ );
1999}
2000
2001/* *INDENT-OFF* */
2002VLIB_REGISTER_NODE (tcp4_rcv_process_node) =
2003{
2004 .function = tcp4_rcv_process,
2005 .name = "tcp4-rcv-process",
2006 /* Takes a vector of packets. */
2007 .vector_size = sizeof (u32),
2008 .n_errors = TCP_N_ERROR,
2009 .error_strings = tcp_error_strings,
2010 .n_next_nodes = TCP_RCV_PROCESS_N_NEXT,
2011 .next_nodes =
2012 {
2013#define _(s,n) [TCP_RCV_PROCESS_NEXT_##s] = n,
2014 foreach_tcp_state_next
2015#undef _
2016 },
Clement Durand6cf260c2017-04-13 13:27:04 +02002017 .format_trace = format_tcp_rx_trace_short,
Dave Barach68b0fb02017-02-28 15:15:56 -05002018};
2019/* *INDENT-ON* */
2020
2021VLIB_NODE_FUNCTION_MULTIARCH (tcp4_rcv_process_node, tcp4_rcv_process);
2022
2023/* *INDENT-OFF* */
2024VLIB_REGISTER_NODE (tcp6_rcv_process_node) =
2025{
2026 .function = tcp6_rcv_process,
2027 .name = "tcp6-rcv-process",
2028 /* Takes a vector of packets. */
2029 .vector_size = sizeof (u32),
2030 .n_errors = TCP_N_ERROR,
2031 .error_strings = tcp_error_strings,
2032 .n_next_nodes = TCP_RCV_PROCESS_N_NEXT,
2033 .next_nodes =
2034 {
2035#define _(s,n) [TCP_RCV_PROCESS_NEXT_##s] = n,
2036 foreach_tcp_state_next
2037#undef _
2038 },
Clement Durand6cf260c2017-04-13 13:27:04 +02002039 .format_trace = format_tcp_rx_trace_short,
Dave Barach68b0fb02017-02-28 15:15:56 -05002040};
2041/* *INDENT-ON* */
2042
2043VLIB_NODE_FUNCTION_MULTIARCH (tcp6_rcv_process_node, tcp6_rcv_process);
2044
2045vlib_node_registration_t tcp4_listen_node;
2046vlib_node_registration_t tcp6_listen_node;
2047
2048/**
2049 * LISTEN state processing as per RFC 793 p. 65
2050 */
2051always_inline uword
2052tcp46_listen_inline (vlib_main_t * vm, vlib_node_runtime_t * node,
2053 vlib_frame_t * from_frame, int is_ip4)
2054{
2055 u32 n_left_from, next_index, *from, *to_next;
Damjan Marion586afd72017-04-05 19:18:20 +02002056 u32 my_thread_index = vm->thread_index;
Dave Barach68b0fb02017-02-28 15:15:56 -05002057 tcp_main_t *tm = vnet_get_tcp_main ();
2058 u8 sst = is_ip4 ? SESSION_TYPE_IP4_TCP : SESSION_TYPE_IP6_TCP;
2059
2060 from = vlib_frame_vector_args (from_frame);
2061 n_left_from = from_frame->n_vectors;
2062
2063 next_index = node->cached_next_index;
2064
2065 while (n_left_from > 0)
2066 {
2067 u32 n_left_to_next;
2068
2069 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
2070
2071 while (n_left_from > 0 && n_left_to_next > 0)
2072 {
2073 u32 bi0;
2074 vlib_buffer_t *b0;
Clement Durand6cf260c2017-04-13 13:27:04 +02002075 tcp_rx_trace_t *t0;
Dave Barach68b0fb02017-02-28 15:15:56 -05002076 tcp_header_t *th0 = 0;
2077 tcp_connection_t *lc0;
2078 ip4_header_t *ip40;
2079 ip6_header_t *ip60;
2080 tcp_connection_t *child0;
2081 u32 error0 = TCP_ERROR_SYNS_RCVD, next0 = TCP_LISTEN_NEXT_DROP;
2082
2083 bi0 = from[0];
2084 to_next[0] = bi0;
2085 from += 1;
2086 to_next += 1;
2087 n_left_from -= 1;
2088 n_left_to_next -= 1;
2089
2090 b0 = vlib_get_buffer (vm, bi0);
2091 lc0 = tcp_listener_get (vnet_buffer (b0)->tcp.connection_index);
2092
2093 if (is_ip4)
2094 {
2095 ip40 = vlib_buffer_get_current (b0);
2096 th0 = ip4_next_header (ip40);
2097 }
2098 else
2099 {
2100 ip60 = vlib_buffer_get_current (b0);
2101 th0 = ip6_next_header (ip60);
2102 }
2103
2104 /* Create child session. For syn-flood protection use filter */
2105
2106 /* 1. first check for an RST */
2107 if (tcp_rst (th0))
2108 goto drop;
2109
2110 /* 2. second check for an ACK */
2111 if (tcp_ack (th0))
2112 {
2113 tcp_send_reset (b0, is_ip4);
2114 goto drop;
2115 }
2116
2117 /* 3. check for a SYN (did that already) */
2118
2119 /* Create child session and send SYN-ACK */
2120 pool_get (tm->connections[my_thread_index], child0);
2121 memset (child0, 0, sizeof (*child0));
2122
2123 child0->c_c_index = child0 - tm->connections[my_thread_index];
2124 child0->c_lcl_port = lc0->c_lcl_port;
2125 child0->c_rmt_port = th0->src_port;
2126 child0->c_is_ip4 = is_ip4;
2127 child0->c_thread_index = my_thread_index;
2128
2129 if (is_ip4)
2130 {
2131 child0->c_lcl_ip4.as_u32 = ip40->dst_address.as_u32;
2132 child0->c_rmt_ip4.as_u32 = ip40->src_address.as_u32;
2133 }
2134 else
2135 {
2136 clib_memcpy (&child0->c_lcl_ip6, &ip60->dst_address,
2137 sizeof (ip6_address_t));
2138 clib_memcpy (&child0->c_rmt_ip6, &ip60->src_address,
2139 sizeof (ip6_address_t));
2140 }
2141
2142 if (stream_session_accept (&child0->connection, lc0->c_s_index, sst,
2143 0 /* notify */ ))
2144 {
2145 error0 = TCP_ERROR_CREATE_SESSION_FAIL;
2146 goto drop;
2147 }
2148
2149 tcp_options_parse (th0, &child0->opt);
Dave Barach68b0fb02017-02-28 15:15:56 -05002150
2151 child0->irs = vnet_buffer (b0)->tcp.seq_number;
2152 child0->rcv_nxt = vnet_buffer (b0)->tcp.seq_number + 1;
Florin Coras3e350af2017-03-30 02:54:28 -07002153 child0->rcv_las = child0->rcv_nxt;
Dave Barach68b0fb02017-02-28 15:15:56 -05002154 child0->state = TCP_STATE_SYN_RCVD;
2155
2156 /* RFC1323: TSval timestamps sent on {SYN} and {SYN,ACK}
2157 * segments are used to initialize PAWS. */
2158 if (tcp_opts_tstamp (&child0->opt))
2159 {
2160 child0->tsval_recent = child0->opt.tsval;
2161 child0->tsval_recent_age = tcp_time_now ();
2162 }
2163
Florin Corase04c2992017-03-01 08:17:34 -08002164 if (tcp_opts_wscale (&child0->opt))
2165 child0->snd_wscale = child0->opt.wscale;
2166
2167 /* No scaling */
2168 child0->snd_wnd = clib_net_to_host_u16 (th0->window);
2169 child0->snd_wl1 = vnet_buffer (b0)->tcp.seq_number;
2170 child0->snd_wl2 = vnet_buffer (b0)->tcp.ack_number;
2171
2172 tcp_connection_init_vars (child0);
2173
Florin Corase69f4952017-03-07 10:06:24 -08002174 TCP_EVT_DBG (TCP_EVT_SYN_RCVD, child0);
2175
Dave Barach68b0fb02017-02-28 15:15:56 -05002176 /* Reuse buffer to make syn-ack and send */
2177 tcp_make_synack (child0, b0);
2178 next0 = tcp_next_output (is_ip4);
2179
Florin Corasc28764f2017-04-26 00:08:42 -07002180 /* Init fifo pointers after we have iss */
2181 stream_session_init_fifos_pointers (&child0->connection,
2182 child0->irs + 1,
2183 child0->iss + 1);
Dave Barach68b0fb02017-02-28 15:15:56 -05002184 drop:
2185 if (PREDICT_FALSE (b0->flags & VLIB_BUFFER_IS_TRACED))
2186 {
Clement Durand6cf260c2017-04-13 13:27:04 +02002187 t0 = vlib_add_trace (vm, node, b0, sizeof (*t0));
2188 clib_memcpy (&t0->tcp_header, th0, sizeof (t0->tcp_header));
2189 clib_memcpy (&t0->tcp_connection, lc0,
2190 sizeof (t0->tcp_connection));
Dave Barach68b0fb02017-02-28 15:15:56 -05002191 }
2192
Florin Corase04c2992017-03-01 08:17:34 -08002193 b0->error = node->errors[error0];
Dave Barach68b0fb02017-02-28 15:15:56 -05002194
2195 vlib_validate_buffer_enqueue_x1 (vm, node, next_index, to_next,
2196 n_left_to_next, bi0, next0);
2197 }
2198
2199 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
2200 }
2201 return from_frame->n_vectors;
2202}
2203
2204static uword
2205tcp4_listen (vlib_main_t * vm, vlib_node_runtime_t * node,
2206 vlib_frame_t * from_frame)
2207{
2208 return tcp46_listen_inline (vm, node, from_frame, 1 /* is_ip4 */ );
2209}
2210
2211static uword
2212tcp6_listen (vlib_main_t * vm, vlib_node_runtime_t * node,
2213 vlib_frame_t * from_frame)
2214{
2215 return tcp46_listen_inline (vm, node, from_frame, 0 /* is_ip4 */ );
2216}
2217
2218/* *INDENT-OFF* */
2219VLIB_REGISTER_NODE (tcp4_listen_node) =
2220{
2221 .function = tcp4_listen,
2222 .name = "tcp4-listen",
2223 /* Takes a vector of packets. */
2224 .vector_size = sizeof (u32),
2225 .n_errors = TCP_N_ERROR,
2226 .error_strings = tcp_error_strings,
2227 .n_next_nodes = TCP_LISTEN_N_NEXT,
2228 .next_nodes =
2229 {
2230#define _(s,n) [TCP_LISTEN_NEXT_##s] = n,
2231 foreach_tcp_state_next
2232#undef _
2233 },
Clement Durand6cf260c2017-04-13 13:27:04 +02002234 .format_trace = format_tcp_rx_trace_short,
Dave Barach68b0fb02017-02-28 15:15:56 -05002235};
2236/* *INDENT-ON* */
2237
2238VLIB_NODE_FUNCTION_MULTIARCH (tcp4_listen_node, tcp4_listen);
2239
2240/* *INDENT-OFF* */
2241VLIB_REGISTER_NODE (tcp6_listen_node) =
2242{
2243 .function = tcp6_listen,
2244 .name = "tcp6-listen",
2245 /* Takes a vector of packets. */
2246 .vector_size = sizeof (u32),
2247 .n_errors = TCP_N_ERROR,
2248 .error_strings = tcp_error_strings,
2249 .n_next_nodes = TCP_LISTEN_N_NEXT,
2250 .next_nodes =
2251 {
2252#define _(s,n) [TCP_LISTEN_NEXT_##s] = n,
2253 foreach_tcp_state_next
2254#undef _
2255 },
Clement Durand6cf260c2017-04-13 13:27:04 +02002256 .format_trace = format_tcp_rx_trace_short,
Dave Barach68b0fb02017-02-28 15:15:56 -05002257};
2258/* *INDENT-ON* */
2259
2260VLIB_NODE_FUNCTION_MULTIARCH (tcp6_listen_node, tcp6_listen);
2261
2262vlib_node_registration_t tcp4_input_node;
2263vlib_node_registration_t tcp6_input_node;
2264
2265typedef enum _tcp_input_next
2266{
2267 TCP_INPUT_NEXT_DROP,
2268 TCP_INPUT_NEXT_LISTEN,
2269 TCP_INPUT_NEXT_RCV_PROCESS,
2270 TCP_INPUT_NEXT_SYN_SENT,
2271 TCP_INPUT_NEXT_ESTABLISHED,
2272 TCP_INPUT_NEXT_RESET,
2273 TCP_INPUT_N_NEXT
2274} tcp_input_next_t;
2275
2276#define foreach_tcp4_input_next \
2277 _ (DROP, "error-drop") \
2278 _ (LISTEN, "tcp4-listen") \
2279 _ (RCV_PROCESS, "tcp4-rcv-process") \
2280 _ (SYN_SENT, "tcp4-syn-sent") \
2281 _ (ESTABLISHED, "tcp4-established") \
2282 _ (RESET, "tcp4-reset")
2283
2284#define foreach_tcp6_input_next \
2285 _ (DROP, "error-drop") \
2286 _ (LISTEN, "tcp6-listen") \
2287 _ (RCV_PROCESS, "tcp6-rcv-process") \
2288 _ (SYN_SENT, "tcp6-syn-sent") \
2289 _ (ESTABLISHED, "tcp6-established") \
2290 _ (RESET, "tcp6-reset")
2291
Dave Barach68b0fb02017-02-28 15:15:56 -05002292#define filter_flags (TCP_FLAG_SYN|TCP_FLAG_ACK|TCP_FLAG_RST|TCP_FLAG_FIN)
2293
2294always_inline uword
2295tcp46_input_inline (vlib_main_t * vm, vlib_node_runtime_t * node,
2296 vlib_frame_t * from_frame, int is_ip4)
2297{
2298 u32 n_left_from, next_index, *from, *to_next;
Damjan Marion586afd72017-04-05 19:18:20 +02002299 u32 my_thread_index = vm->thread_index;
Dave Barach68b0fb02017-02-28 15:15:56 -05002300 tcp_main_t *tm = vnet_get_tcp_main ();
Dave Barach68b0fb02017-02-28 15:15:56 -05002301
2302 from = vlib_frame_vector_args (from_frame);
2303 n_left_from = from_frame->n_vectors;
2304
2305 next_index = node->cached_next_index;
2306
2307 while (n_left_from > 0)
2308 {
2309 u32 n_left_to_next;
2310
2311 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
2312
2313 while (n_left_from > 0 && n_left_to_next > 0)
2314 {
Florin Coras82b13a82017-04-25 11:58:06 -07002315 int n_advance_bytes0, n_data_bytes0;
Dave Barach68b0fb02017-02-28 15:15:56 -05002316 u32 bi0;
2317 vlib_buffer_t *b0;
2318 tcp_header_t *tcp0 = 0;
2319 tcp_connection_t *tc0;
2320 ip4_header_t *ip40;
2321 ip6_header_t *ip60;
2322 u32 error0 = TCP_ERROR_NO_LISTENER, next0 = TCP_INPUT_NEXT_DROP;
2323 u8 flags0;
2324
2325 bi0 = from[0];
2326 to_next[0] = bi0;
2327 from += 1;
2328 to_next += 1;
2329 n_left_from -= 1;
2330 n_left_to_next -= 1;
2331
2332 b0 = vlib_get_buffer (vm, bi0);
Florin Corasd79b41e2017-03-04 05:37:52 -08002333 vnet_buffer (b0)->tcp.flags = 0;
Dave Barach68b0fb02017-02-28 15:15:56 -05002334
Florin Coras82b13a82017-04-25 11:58:06 -07002335 /* Checksum computed by ipx_local no need to compute again */
2336
Dave Barach68b0fb02017-02-28 15:15:56 -05002337 if (is_ip4)
2338 {
2339 ip40 = vlib_buffer_get_current (b0);
2340 tcp0 = ip4_next_header (ip40);
Florin Coras82b13a82017-04-25 11:58:06 -07002341 n_advance_bytes0 = (ip4_header_bytes (ip40)
2342 + tcp_header_bytes (tcp0));
2343 n_data_bytes0 = clib_net_to_host_u16 (ip40->length)
2344 - n_advance_bytes0;
Dave Barach68b0fb02017-02-28 15:15:56 -05002345
2346 /* lookup session */
2347 tc0 =
Florin Corase04c2992017-03-01 08:17:34 -08002348 (tcp_connection_t *)
2349 stream_session_lookup_transport4 (&ip40->dst_address,
2350 &ip40->src_address,
2351 tcp0->dst_port,
2352 tcp0->src_port,
2353 SESSION_TYPE_IP4_TCP,
2354 my_thread_index);
Dave Barach68b0fb02017-02-28 15:15:56 -05002355 }
2356 else
2357 {
2358 ip60 = vlib_buffer_get_current (b0);
2359 tcp0 = ip6_next_header (ip60);
Florin Coras82b13a82017-04-25 11:58:06 -07002360 n_advance_bytes0 = tcp_header_bytes (tcp0);
2361 n_data_bytes0 = clib_net_to_host_u16 (ip60->payload_length)
2362 - n_advance_bytes0;
2363 n_advance_bytes0 += sizeof (ip60[0]);
2364
Dave Barach68b0fb02017-02-28 15:15:56 -05002365 tc0 =
Florin Corase04c2992017-03-01 08:17:34 -08002366 (tcp_connection_t *)
2367 stream_session_lookup_transport6 (&ip60->src_address,
2368 &ip60->dst_address,
2369 tcp0->src_port,
2370 tcp0->dst_port,
2371 SESSION_TYPE_IP6_TCP,
2372 my_thread_index);
Dave Barach68b0fb02017-02-28 15:15:56 -05002373 }
2374
Florin Coras82b13a82017-04-25 11:58:06 -07002375 /* Length check */
2376 if (PREDICT_FALSE (n_advance_bytes0 < 0))
2377 {
2378 error0 = TCP_ERROR_LENGTH;
2379 goto done;
2380 }
2381
Dave Barach68b0fb02017-02-28 15:15:56 -05002382 /* Session exists */
2383 if (PREDICT_TRUE (0 != tc0))
2384 {
2385 /* Save connection index */
2386 vnet_buffer (b0)->tcp.connection_index = tc0->c_c_index;
2387 vnet_buffer (b0)->tcp.seq_number =
2388 clib_net_to_host_u32 (tcp0->seq_number);
2389 vnet_buffer (b0)->tcp.ack_number =
2390 clib_net_to_host_u32 (tcp0->ack_number);
2391
Florin Coras82b13a82017-04-25 11:58:06 -07002392 vnet_buffer (b0)->tcp.hdr_offset = (u8 *) tcp0
2393 - (u8 *) vlib_buffer_get_current (b0);
2394 vnet_buffer (b0)->tcp.data_offset = n_advance_bytes0;
2395 vnet_buffer (b0)->tcp.data_len = n_data_bytes0;
2396
Dave Barach68b0fb02017-02-28 15:15:56 -05002397 flags0 = tcp0->flags & filter_flags;
2398 next0 = tm->dispatch_table[tc0->state][flags0].next;
2399 error0 = tm->dispatch_table[tc0->state][flags0].error;
2400
2401 if (PREDICT_FALSE (error0 == TCP_ERROR_DISPATCH))
2402 {
Dave Barach1f75cfd2017-04-14 16:46:44 -04002403 tcp_state_t state0 = tc0->state;
Dave Barach68b0fb02017-02-28 15:15:56 -05002404 /* Overload tcp flags to store state */
2405 vnet_buffer (b0)->tcp.flags = tc0->state;
Dave Barach1f75cfd2017-04-14 16:46:44 -04002406 clib_warning ("disp error state %U flags %U",
2407 format_tcp_state, &state0,
Dave Barach63681512017-04-20 17:50:39 -04002408 format_tcp_flags, (int) flags0);
Dave Barach68b0fb02017-02-28 15:15:56 -05002409 }
2410 }
2411 else
2412 {
2413 /* Send reset */
2414 next0 = TCP_INPUT_NEXT_RESET;
2415 error0 = TCP_ERROR_NO_LISTENER;
Dave Barach68b0fb02017-02-28 15:15:56 -05002416 }
2417
Florin Coras82b13a82017-04-25 11:58:06 -07002418 done:
Dave Barach68b0fb02017-02-28 15:15:56 -05002419 b0->error = error0 ? node->errors[error0] : 0;
2420
2421 if (PREDICT_FALSE (b0->flags & VLIB_BUFFER_IS_TRACED))
2422 {
Florin Coras82b13a82017-04-25 11:58:06 -07002423 tcp_rx_trace_t *t0 =
2424 vlib_add_trace (vm, node, b0, sizeof (*t0));
2425 tcp_set_rx_trace_data (t0, tc0, tcp0, b0, is_ip4);
Dave Barach68b0fb02017-02-28 15:15:56 -05002426 }
2427
2428 vlib_validate_buffer_enqueue_x1 (vm, node, next_index, to_next,
2429 n_left_to_next, bi0, next0);
2430 }
2431
2432 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
2433 }
2434
2435 return from_frame->n_vectors;
2436}
2437
2438static uword
2439tcp4_input (vlib_main_t * vm, vlib_node_runtime_t * node,
2440 vlib_frame_t * from_frame)
2441{
2442 return tcp46_input_inline (vm, node, from_frame, 1 /* is_ip4 */ );
2443}
2444
2445static uword
2446tcp6_input (vlib_main_t * vm, vlib_node_runtime_t * node,
2447 vlib_frame_t * from_frame)
2448{
2449 return tcp46_input_inline (vm, node, from_frame, 0 /* is_ip4 */ );
2450}
2451
2452/* *INDENT-OFF* */
2453VLIB_REGISTER_NODE (tcp4_input_node) =
2454{
2455 .function = tcp4_input,
2456 .name = "tcp4-input",
2457 /* Takes a vector of packets. */
2458 .vector_size = sizeof (u32),
2459 .n_errors = TCP_N_ERROR,
2460 .error_strings = tcp_error_strings,
2461 .n_next_nodes = TCP_INPUT_N_NEXT,
2462 .next_nodes =
2463 {
2464#define _(s,n) [TCP_INPUT_NEXT_##s] = n,
2465 foreach_tcp4_input_next
2466#undef _
2467 },
2468 .format_buffer = format_tcp_header,
2469 .format_trace = format_tcp_rx_trace,
2470};
2471/* *INDENT-ON* */
2472
2473VLIB_NODE_FUNCTION_MULTIARCH (tcp4_input_node, tcp4_input);
2474
2475/* *INDENT-OFF* */
2476VLIB_REGISTER_NODE (tcp6_input_node) =
2477{
2478 .function = tcp6_input,
2479 .name = "tcp6-input",
2480 /* Takes a vector of packets. */
2481 .vector_size = sizeof (u32),
2482 .n_errors = TCP_N_ERROR,
2483 .error_strings = tcp_error_strings,
2484 .n_next_nodes = TCP_INPUT_N_NEXT,
2485 .next_nodes =
2486 {
2487#define _(s,n) [TCP_INPUT_NEXT_##s] = n,
2488 foreach_tcp6_input_next
2489#undef _
2490 },
2491 .format_buffer = format_tcp_header,
2492 .format_trace = format_tcp_rx_trace,
2493};
2494/* *INDENT-ON* */
2495
2496VLIB_NODE_FUNCTION_MULTIARCH (tcp6_input_node, tcp6_input);
Dave Barach68b0fb02017-02-28 15:15:56 -05002497
2498static void
2499tcp_dispatch_table_init (tcp_main_t * tm)
2500{
2501 int i, j;
2502 for (i = 0; i < ARRAY_LEN (tm->dispatch_table); i++)
2503 for (j = 0; j < ARRAY_LEN (tm->dispatch_table[i]); j++)
2504 {
2505 tm->dispatch_table[i][j].next = TCP_INPUT_NEXT_DROP;
2506 tm->dispatch_table[i][j].error = TCP_ERROR_DISPATCH;
2507 }
2508
2509#define _(t,f,n,e) \
2510do { \
2511 tm->dispatch_table[TCP_STATE_##t][f].next = (n); \
2512 tm->dispatch_table[TCP_STATE_##t][f].error = (e); \
2513} while (0)
2514
2515 /* SYNs for new connections -> tcp-listen. */
2516 _(LISTEN, TCP_FLAG_SYN, TCP_INPUT_NEXT_LISTEN, TCP_ERROR_NONE);
2517 /* ACK for for a SYN-ACK -> tcp-rcv-process. */
2518 _(SYN_RCVD, TCP_FLAG_ACK, TCP_INPUT_NEXT_RCV_PROCESS, TCP_ERROR_NONE);
Florin Corasc28764f2017-04-26 00:08:42 -07002519 _(SYN_RCVD, TCP_FLAG_RST, TCP_INPUT_NEXT_RCV_PROCESS, TCP_ERROR_NONE);
Dave Barach68b0fb02017-02-28 15:15:56 -05002520 /* SYN-ACK for a SYN */
2521 _(SYN_SENT, TCP_FLAG_SYN | TCP_FLAG_ACK, TCP_INPUT_NEXT_SYN_SENT,
2522 TCP_ERROR_NONE);
2523 _(SYN_SENT, TCP_FLAG_ACK, TCP_INPUT_NEXT_SYN_SENT, TCP_ERROR_NONE);
2524 _(SYN_SENT, TCP_FLAG_RST, TCP_INPUT_NEXT_SYN_SENT, TCP_ERROR_NONE);
2525 _(SYN_SENT, TCP_FLAG_RST | TCP_FLAG_ACK, TCP_INPUT_NEXT_SYN_SENT,
2526 TCP_ERROR_NONE);
2527 /* ACK for for established connection -> tcp-established. */
2528 _(ESTABLISHED, TCP_FLAG_ACK, TCP_INPUT_NEXT_ESTABLISHED, TCP_ERROR_NONE);
2529 /* FIN for for established connection -> tcp-established. */
2530 _(ESTABLISHED, TCP_FLAG_FIN, TCP_INPUT_NEXT_ESTABLISHED, TCP_ERROR_NONE);
2531 _(ESTABLISHED, TCP_FLAG_FIN | TCP_FLAG_ACK, TCP_INPUT_NEXT_ESTABLISHED,
2532 TCP_ERROR_NONE);
Florin Corasd79b41e2017-03-04 05:37:52 -08002533 _(ESTABLISHED, TCP_FLAG_RST, TCP_INPUT_NEXT_ESTABLISHED, TCP_ERROR_NONE);
Dave Barach68b0fb02017-02-28 15:15:56 -05002534 /* ACK or FIN-ACK to our FIN */
2535 _(FIN_WAIT_1, TCP_FLAG_ACK, TCP_INPUT_NEXT_RCV_PROCESS, TCP_ERROR_NONE);
2536 _(FIN_WAIT_1, TCP_FLAG_ACK | TCP_FLAG_FIN, TCP_INPUT_NEXT_RCV_PROCESS,
2537 TCP_ERROR_NONE);
2538 /* FIN in reply to our FIN from the other side */
2539 _(FIN_WAIT_1, TCP_FLAG_FIN, TCP_INPUT_NEXT_RCV_PROCESS, TCP_ERROR_NONE);
2540 /* FIN confirming that the peer (app) has closed */
2541 _(FIN_WAIT_2, TCP_FLAG_FIN, TCP_INPUT_NEXT_RCV_PROCESS, TCP_ERROR_NONE);
2542 _(FIN_WAIT_2, TCP_FLAG_FIN | TCP_FLAG_ACK, TCP_INPUT_NEXT_RCV_PROCESS,
2543 TCP_ERROR_NONE);
2544 _(LAST_ACK, TCP_FLAG_ACK, TCP_INPUT_NEXT_RCV_PROCESS, TCP_ERROR_NONE);
Florin Corasc28764f2017-04-26 00:08:42 -07002545 _(CLOSED, TCP_FLAG_ACK, TCP_INPUT_NEXT_DROP, TCP_ERROR_CONNECTION_CLOSED);
Dave Barach68b0fb02017-02-28 15:15:56 -05002546#undef _
2547}
2548
2549clib_error_t *
2550tcp_input_init (vlib_main_t * vm)
2551{
2552 clib_error_t *error = 0;
2553 tcp_main_t *tm = vnet_get_tcp_main ();
2554
2555 if ((error = vlib_call_init_function (vm, tcp_init)))
2556 return error;
2557
2558 /* Initialize dispatch table. */
2559 tcp_dispatch_table_init (tm);
2560
2561 return error;
2562}
2563
2564VLIB_INIT_FUNCTION (tcp_input_init);
2565
2566/*
2567 * fd.io coding-style-patch-verification: ON
2568 *
2569 * Local Variables:
2570 * eval: (c-set-style "gnu")
2571 * End:
2572 */