blob: 35bc90943b482f20bd44e9058d25acc6cb95be7c [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
Florin Corasdb84e572017-05-09 18:54:52 -0700115/**
116 * Parse TCP header options.
117 *
118 * @param th TCP header
119 * @param to TCP options data structure to be populated
120 * @return -1 if parsing failed
121 */
122int
Dave Barach68b0fb02017-02-28 15:15:56 -0500123tcp_options_parse (tcp_header_t * th, tcp_options_t * to)
124{
125 const u8 *data;
126 u8 opt_len, opts_len, kind;
127 int j;
128 sack_block_t b;
129
130 opts_len = (tcp_doff (th) << 2) - sizeof (tcp_header_t);
131 data = (const u8 *) (th + 1);
132
133 /* Zero out all flags but those set in SYN */
134 to->flags &= (TCP_OPTS_FLAG_SACK_PERMITTED | TCP_OPTS_FLAG_WSCALE);
135
136 for (; opts_len > 0; opts_len -= opt_len, data += opt_len)
137 {
138 kind = data[0];
139
140 /* Get options length */
141 if (kind == TCP_OPTION_EOL)
142 break;
143 else if (kind == TCP_OPTION_NOOP)
Florin Corasdb84e572017-05-09 18:54:52 -0700144 {
145 opt_len = 1;
146 continue;
147 }
Dave Barach68b0fb02017-02-28 15:15:56 -0500148 else
149 {
150 /* broken options */
151 if (opts_len < 2)
Florin Corasdb84e572017-05-09 18:54:52 -0700152 return -1;
Dave Barach68b0fb02017-02-28 15:15:56 -0500153 opt_len = data[1];
154
155 /* weird option length */
156 if (opt_len < 2 || opt_len > opts_len)
Florin Corasdb84e572017-05-09 18:54:52 -0700157 return -1;
Dave Barach68b0fb02017-02-28 15:15:56 -0500158 }
159
160 /* Parse options */
161 switch (kind)
162 {
163 case TCP_OPTION_MSS:
164 if ((opt_len == TCP_OPTION_LEN_MSS) && tcp_syn (th))
165 {
166 to->flags |= TCP_OPTS_FLAG_MSS;
167 to->mss = clib_net_to_host_u16 (*(u16 *) (data + 2));
168 }
169 break;
170 case TCP_OPTION_WINDOW_SCALE:
171 if ((opt_len == TCP_OPTION_LEN_WINDOW_SCALE) && tcp_syn (th))
172 {
173 to->flags |= TCP_OPTS_FLAG_WSCALE;
174 to->wscale = data[2];
175 if (to->wscale > TCP_MAX_WND_SCALE)
176 {
177 clib_warning ("Illegal window scaling value: %d",
178 to->wscale);
179 to->wscale = TCP_MAX_WND_SCALE;
180 }
181 }
182 break;
183 case TCP_OPTION_TIMESTAMP:
184 if (opt_len == TCP_OPTION_LEN_TIMESTAMP)
185 {
186 to->flags |= TCP_OPTS_FLAG_TSTAMP;
187 to->tsval = clib_net_to_host_u32 (*(u32 *) (data + 2));
188 to->tsecr = clib_net_to_host_u32 (*(u32 *) (data + 6));
189 }
190 break;
191 case TCP_OPTION_SACK_PERMITTED:
192 if (opt_len == TCP_OPTION_LEN_SACK_PERMITTED && tcp_syn (th))
193 to->flags |= TCP_OPTS_FLAG_SACK_PERMITTED;
194 break;
195 case TCP_OPTION_SACK_BLOCK:
196 /* If SACK permitted was not advertised or a SYN, break */
197 if ((to->flags & TCP_OPTS_FLAG_SACK_PERMITTED) == 0 || tcp_syn (th))
198 break;
199
200 /* If too short or not correctly formatted, break */
201 if (opt_len < 10 || ((opt_len - 2) % TCP_OPTION_LEN_SACK_BLOCK))
202 break;
203
204 to->flags |= TCP_OPTS_FLAG_SACK;
205 to->n_sack_blocks = (opt_len - 2) / TCP_OPTION_LEN_SACK_BLOCK;
206 vec_reset_length (to->sacks);
207 for (j = 0; j < to->n_sack_blocks; j++)
208 {
209 b.start = clib_net_to_host_u32 (*(u32 *) (data + 2 + 4 * j));
210 b.end = clib_net_to_host_u32 (*(u32 *) (data + 6 + 4 * j));
211 vec_add1 (to->sacks, b);
212 }
213 break;
214 default:
215 /* Nothing to see here */
216 continue;
217 }
218 }
Florin Corasdb84e572017-05-09 18:54:52 -0700219 return 0;
Dave Barach68b0fb02017-02-28 15:15:56 -0500220}
221
Florin Corasc28764f2017-04-26 00:08:42 -0700222/**
223 * RFC1323: Check against wrapped sequence numbers (PAWS). If we have
224 * timestamp to echo and it's less than tsval_recent, drop segment
225 * but still send an ACK in order to retain TCP's mechanism for detecting
226 * and recovering from half-open connections
227 *
228 * Or at least that's what the theory says. It seems that this might not work
229 * very well with packet reordering and fast retransmit. XXX
230 */
Dave Barach68b0fb02017-02-28 15:15:56 -0500231always_inline int
232tcp_segment_check_paws (tcp_connection_t * tc)
233{
Dave Barach68b0fb02017-02-28 15:15:56 -0500234 return tcp_opts_tstamp (&tc->opt) && tc->tsval_recent
235 && timestamp_lt (tc->opt.tsval, tc->tsval_recent);
236}
237
238/**
Florin Corasc28764f2017-04-26 00:08:42 -0700239 * Update tsval recent
240 */
241always_inline void
242tcp_update_timestamp (tcp_connection_t * tc, u32 seq, u32 seq_end)
243{
244 /*
245 * RFC1323: If Last.ACK.sent falls within the range of sequence numbers
246 * of an incoming segment:
247 * SEG.SEQ <= Last.ACK.sent < SEG.SEQ + SEG.LEN
248 * then the TSval from the segment is copied to TS.Recent;
249 * otherwise, the TSval is ignored.
250 */
251 if (tcp_opts_tstamp (&tc->opt) && tc->tsval_recent
252 && seq_leq (seq, tc->rcv_las) && seq_leq (tc->rcv_las, seq_end))
253 {
254 tc->tsval_recent = tc->opt.tsval;
255 tc->tsval_recent_age = tcp_time_now ();
256 }
257}
258
259/**
Dave Barach68b0fb02017-02-28 15:15:56 -0500260 * Validate incoming segment as per RFC793 p. 69 and RFC1323 p. 19
261 *
262 * It first verifies if segment has a wrapped sequence number (PAWS) and then
263 * does the processing associated to the first four steps (ignoring security
264 * and precedence): sequence number, rst bit and syn bit checks.
265 *
266 * @return 0 if segments passes validation.
267 */
268static int
269tcp_segment_validate (vlib_main_t * vm, tcp_connection_t * tc0,
270 vlib_buffer_t * b0, tcp_header_t * th0, u32 * next0)
271{
Dave Barach68b0fb02017-02-28 15:15:56 -0500272 if (PREDICT_FALSE (!tcp_ack (th0) && !tcp_rst (th0) && !tcp_syn (th0)))
273 return -1;
274
Florin Corasdb84e572017-05-09 18:54:52 -0700275 if (PREDICT_FALSE (tcp_options_parse (th0, &tc0->opt)))
276 {
277 return -1;
278 }
Dave Barach68b0fb02017-02-28 15:15:56 -0500279
Florin Corasc28764f2017-04-26 00:08:42 -0700280 if (tcp_segment_check_paws (tc0))
Dave Barach68b0fb02017-02-28 15:15:56 -0500281 {
282 clib_warning ("paws failed");
Florin Corasc28764f2017-04-26 00:08:42 -0700283 TCP_EVT_DBG (TCP_EVT_PAWS_FAIL, tc0, vnet_buffer (b0)->tcp.seq_number,
284 vnet_buffer (b0)->tcp.seq_end);
Dave Barach68b0fb02017-02-28 15:15:56 -0500285
286 /* If it just so happens that a segment updates tsval_recent for a
287 * segment over 24 days old, invalidate tsval_recent. */
288 if (timestamp_lt (tc0->tsval_recent_age + TCP_PAWS_IDLE,
289 tcp_time_now ()))
290 {
291 /* Age isn't reset until we get a valid tsval (bsd inspired) */
292 tc0->tsval_recent = 0;
Florin Corasc28764f2017-04-26 00:08:42 -0700293 clib_warning ("paws failed - really old segment. REALLY?");
Dave Barach68b0fb02017-02-28 15:15:56 -0500294 }
295 else
296 {
297 /* Drop after ack if not rst */
298 if (!tcp_rst (th0))
299 {
300 tcp_make_ack (tc0, b0);
301 *next0 = tcp_next_output (tc0->c_is_ip4);
Florin Coras6792ec02017-03-13 03:49:51 -0700302 TCP_EVT_DBG (TCP_EVT_DUPACK_SENT, tc0);
Dave Barach68b0fb02017-02-28 15:15:56 -0500303 return -1;
304 }
305 }
306 }
307
308 /* 1st: check sequence number */
309 if (!tcp_segment_in_rcv_wnd (tc0, vnet_buffer (b0)->tcp.seq_number,
310 vnet_buffer (b0)->tcp.seq_end))
311 {
Florin Coras6792ec02017-03-13 03:49:51 -0700312 /* If our window is 0 and the packet is in sequence, let it pass
313 * through for ack processing. It should be dropped later.*/
314 if (tc0->rcv_wnd == 0
315 && tc0->rcv_nxt == vnet_buffer (b0)->tcp.seq_number)
Dave Barach68b0fb02017-02-28 15:15:56 -0500316 {
Florin Coras3e350af2017-03-30 02:54:28 -0700317 /* TODO Should segment be tagged? */
Dave Barach68b0fb02017-02-28 15:15:56 -0500318 }
Florin Coras6792ec02017-03-13 03:49:51 -0700319 else
320 {
321 /* If not RST, send dup ack */
322 if (!tcp_rst (th0))
323 {
324 tcp_make_ack (tc0, b0);
325 *next0 = tcp_next_output (tc0->c_is_ip4);
326 TCP_EVT_DBG (TCP_EVT_DUPACK_SENT, tc0);
327 }
328 return -1;
329 }
Dave Barach68b0fb02017-02-28 15:15:56 -0500330 }
331
332 /* 2nd: check the RST bit */
333 if (tcp_rst (th0))
334 {
Florin Corasd79b41e2017-03-04 05:37:52 -0800335 tcp_connection_reset (tc0);
Dave Barach68b0fb02017-02-28 15:15:56 -0500336 return -1;
337 }
338
339 /* 3rd: check security and precedence (skip) */
340
341 /* 4th: check the SYN bit */
342 if (tcp_syn (th0))
343 {
344 tcp_send_reset (b0, tc0->c_is_ip4);
345 return -1;
346 }
347
Florin Corasc28764f2017-04-26 00:08:42 -0700348 /* If segment in window, save timestamp */
349 tcp_update_timestamp (tc0, vnet_buffer (b0)->tcp.seq_number,
350 vnet_buffer (b0)->tcp.seq_end);
Dave Barach68b0fb02017-02-28 15:15:56 -0500351
352 return 0;
353}
354
355always_inline int
356tcp_rcv_ack_is_acceptable (tcp_connection_t * tc0, vlib_buffer_t * tb0)
357{
358 /* SND.UNA =< SEG.ACK =< SND.NXT */
359 return (seq_leq (tc0->snd_una, vnet_buffer (tb0)->tcp.ack_number)
360 && seq_leq (vnet_buffer (tb0)->tcp.ack_number, tc0->snd_nxt));
361}
362
363/**
364 * Compute smoothed RTT as per VJ's '88 SIGCOMM and RFC6298
365 *
366 * Note that although the original article, srtt and rttvar are scaled
367 * to minimize round-off errors, here we don't. Instead, we rely on
368 * better precision time measurements.
369 *
370 * TODO support us rtt resolution
371 */
372static void
373tcp_estimate_rtt (tcp_connection_t * tc, u32 mrtt)
374{
375 int err;
376
377 if (tc->srtt != 0)
378 {
379 err = mrtt - tc->srtt;
380 tc->srtt += err >> 3;
381
382 /* XXX Drop in RTT results in RTTVAR increase and bigger RTO.
383 * The increase should be bound */
Florin Coras6792ec02017-03-13 03:49:51 -0700384 tc->rttvar += ((int) clib_abs (err) - (int) tc->rttvar) >> 2;
Dave Barach68b0fb02017-02-28 15:15:56 -0500385 }
386 else
387 {
388 /* First measurement. */
389 tc->srtt = mrtt;
Florin Coras6792ec02017-03-13 03:49:51 -0700390 tc->rttvar = mrtt >> 1;
Dave Barach68b0fb02017-02-28 15:15:56 -0500391 }
392}
393
394/** Update RTT estimate and RTO timer
395 *
396 * Measure RTT: We have two sources of RTT measurements: TSOPT and ACK
397 * timing. Middle boxes are known to fiddle with TCP options so we
398 * should give higher priority to ACK timing.
399 *
400 * return 1 if valid rtt 0 otherwise
401 */
402static int
403tcp_update_rtt (tcp_connection_t * tc, u32 ack)
404{
405 u32 mrtt = 0;
Florin Corasc8343412017-05-04 14:25:50 -0700406 u8 rtx_acked;
407
408 /* Determine if only rtx bytes are acked. TODO fast retransmit */
409 rtx_acked = tc->rto_boff && (tc->bytes_acked <= tc->snd_mss);
Dave Barach68b0fb02017-02-28 15:15:56 -0500410
411 /* Karn's rule, part 1. Don't use retransmitted segments to estimate
412 * RTT because they're ambiguous. */
Florin Corasc8343412017-05-04 14:25:50 -0700413 if (tc->rtt_ts && seq_geq (ack, tc->rtt_seq) && !rtx_acked)
Dave Barach68b0fb02017-02-28 15:15:56 -0500414 {
415 mrtt = tcp_time_now () - tc->rtt_ts;
Dave Barach68b0fb02017-02-28 15:15:56 -0500416 }
Dave Barach68b0fb02017-02-28 15:15:56 -0500417 /* As per RFC7323 TSecr can be used for RTTM only if the segment advances
418 * snd_una, i.e., the left side of the send window:
419 * seq_lt (tc->snd_una, ack). Note: last condition could be dropped, we don't
420 * try to update rtt for dupacks */
421 else if (tcp_opts_tstamp (&tc->opt) && tc->opt.tsecr && tc->bytes_acked)
422 {
423 mrtt = tcp_time_now () - tc->opt.tsecr;
424 }
425
Florin Coras3af90fc2017-05-03 21:09:42 -0700426 /* Allow measuring of a new RTT */
427 tc->rtt_ts = 0;
428
429 /* If ACK moves left side of the wnd make sure boff is 0, even if mrtt is
430 * not valid */
431 if (tc->bytes_acked)
432 tc->rto_boff = 0;
433
Dave Barach68b0fb02017-02-28 15:15:56 -0500434 /* Ignore dubious measurements */
435 if (mrtt == 0 || mrtt > TCP_RTT_MAX)
436 return 0;
437
438 tcp_estimate_rtt (tc, mrtt);
Dave Barach68b0fb02017-02-28 15:15:56 -0500439 tc->rto = clib_min (tc->srtt + (tc->rttvar << 2), TCP_RTO_MAX);
440
Florin Coras3af90fc2017-05-03 21:09:42 -0700441 return 0;
Dave Barach68b0fb02017-02-28 15:15:56 -0500442}
443
444/**
445 * Dequeue bytes that have been acked and while at it update RTT estimates.
446 */
447static void
448tcp_dequeue_acked (tcp_connection_t * tc, u32 ack)
449{
450 /* Dequeue the newly ACKed bytes */
451 stream_session_dequeue_drop (&tc->connection, tc->bytes_acked);
452
453 /* Update rtt and rto */
Florin Coras3e350af2017-03-30 02:54:28 -0700454 tcp_update_rtt (tc, ack);
Dave Barach68b0fb02017-02-28 15:15:56 -0500455}
456
Florin Coras6792ec02017-03-13 03:49:51 -0700457/**
458 * Check if dupack as per RFC5681 Sec. 2
459 *
460 * This works only if called before updating snd_wnd.
461 * */
Dave Barach68b0fb02017-02-28 15:15:56 -0500462always_inline u8
463tcp_ack_is_dupack (tcp_connection_t * tc, vlib_buffer_t * b, u32 new_snd_wnd)
464{
465 return ((vnet_buffer (b)->tcp.ack_number == tc->snd_una)
466 && seq_gt (tc->snd_una_max, tc->snd_una)
467 && (vnet_buffer (b)->tcp.seq_end == vnet_buffer (b)->tcp.seq_number)
468 && (new_snd_wnd == tc->snd_wnd));
469}
470
471void
472scoreboard_remove_hole (sack_scoreboard_t * sb, sack_scoreboard_hole_t * hole)
473{
474 sack_scoreboard_hole_t *next, *prev;
475
476 if (hole->next != TCP_INVALID_SACK_HOLE_INDEX)
477 {
478 next = pool_elt_at_index (sb->holes, hole->next);
479 next->prev = hole->prev;
480 }
481
482 if (hole->prev != TCP_INVALID_SACK_HOLE_INDEX)
483 {
484 prev = pool_elt_at_index (sb->holes, hole->prev);
485 prev->next = hole->next;
486 }
487 else
488 {
489 sb->head = hole->next;
490 }
491
492 pool_put (sb->holes, hole);
493}
494
495sack_scoreboard_hole_t *
Florin Coras6792ec02017-03-13 03:49:51 -0700496scoreboard_insert_hole (sack_scoreboard_t * sb, u32 prev_index,
Dave Barach68b0fb02017-02-28 15:15:56 -0500497 u32 start, u32 end)
498{
Florin Coras6792ec02017-03-13 03:49:51 -0700499 sack_scoreboard_hole_t *hole, *next, *prev;
Dave Barach68b0fb02017-02-28 15:15:56 -0500500 u32 hole_index;
501
502 pool_get (sb->holes, hole);
503 memset (hole, 0, sizeof (*hole));
504
505 hole->start = start;
506 hole->end = end;
507 hole_index = hole - sb->holes;
508
Florin Coras6792ec02017-03-13 03:49:51 -0700509 prev = scoreboard_get_hole (sb, prev_index);
Dave Barach68b0fb02017-02-28 15:15:56 -0500510 if (prev)
511 {
512 hole->prev = prev - sb->holes;
513 hole->next = prev->next;
514
515 if ((next = scoreboard_next_hole (sb, hole)))
516 next->prev = hole_index;
517
518 prev->next = hole_index;
519 }
520 else
521 {
522 sb->head = hole_index;
523 hole->prev = TCP_INVALID_SACK_HOLE_INDEX;
524 hole->next = TCP_INVALID_SACK_HOLE_INDEX;
525 }
526
527 return hole;
528}
529
Florin Coras6792ec02017-03-13 03:49:51 -0700530void
Dave Barach68b0fb02017-02-28 15:15:56 -0500531tcp_rcv_sacks (tcp_connection_t * tc, u32 ack)
532{
533 sack_scoreboard_t *sb = &tc->sack_sb;
534 sack_block_t *blk, tmp;
Florin Coras6792ec02017-03-13 03:49:51 -0700535 sack_scoreboard_hole_t *hole, *next_hole, *last_hole, *new_hole;
Florin Coras06d11012017-05-17 14:21:51 -0700536 u32 blk_index = 0, old_sacked_bytes, delivered_bytes, hole_index;
Dave Barach68b0fb02017-02-28 15:15:56 -0500537 int i, j;
538
Florin Coras6792ec02017-03-13 03:49:51 -0700539 sb->last_sacked_bytes = 0;
540 sb->snd_una_adv = 0;
541 old_sacked_bytes = sb->sacked_bytes;
Florin Coras06d11012017-05-17 14:21:51 -0700542 delivered_bytes = 0;
Florin Coras6792ec02017-03-13 03:49:51 -0700543
544 if (!tcp_opts_sack (&tc->opt) && sb->head == TCP_INVALID_SACK_HOLE_INDEX)
Dave Barach68b0fb02017-02-28 15:15:56 -0500545 return;
546
547 /* Remove invalid blocks */
Florin Coras6792ec02017-03-13 03:49:51 -0700548 blk = tc->opt.sacks;
549 while (blk < vec_end (tc->opt.sacks))
550 {
551 if (seq_lt (blk->start, blk->end)
552 && seq_gt (blk->start, tc->snd_una)
553 && seq_gt (blk->start, ack) && seq_leq (blk->end, tc->snd_nxt))
554 {
555 blk++;
556 continue;
557 }
558 vec_del1 (tc->opt.sacks, blk - tc->opt.sacks);
559 }
Dave Barach68b0fb02017-02-28 15:15:56 -0500560
561 /* Add block for cumulative ack */
562 if (seq_gt (ack, tc->snd_una))
563 {
564 tmp.start = tc->snd_una;
565 tmp.end = ack;
566 vec_add1 (tc->opt.sacks, tmp);
567 }
568
569 if (vec_len (tc->opt.sacks) == 0)
570 return;
571
572 /* Make sure blocks are ordered */
573 for (i = 0; i < vec_len (tc->opt.sacks); i++)
Florin Coras6792ec02017-03-13 03:49:51 -0700574 for (j = i + 1; j < vec_len (tc->opt.sacks); j++)
Dave Barach68b0fb02017-02-28 15:15:56 -0500575 if (seq_lt (tc->opt.sacks[j].start, tc->opt.sacks[i].start))
576 {
577 tmp = tc->opt.sacks[i];
578 tc->opt.sacks[i] = tc->opt.sacks[j];
579 tc->opt.sacks[j] = tmp;
580 }
581
Dave Barach68b0fb02017-02-28 15:15:56 -0500582 if (sb->head == TCP_INVALID_SACK_HOLE_INDEX)
583 {
Florin Coras6792ec02017-03-13 03:49:51 -0700584 /* If no holes, insert the first that covers all outstanding bytes */
585 last_hole = scoreboard_insert_hole (sb, TCP_INVALID_SACK_HOLE_INDEX,
586 tc->snd_una, tc->snd_una_max);
587 sb->tail = scoreboard_hole_index (sb, last_hole);
Florin Coras06d11012017-05-17 14:21:51 -0700588 tmp = tc->opt.sacks[vec_len (tc->opt.sacks) - 1];
589 sb->max_byte_sacked = tmp.end;
Florin Coras6792ec02017-03-13 03:49:51 -0700590 }
591 else
592 {
593 /* If we have holes but snd_una_max is beyond the last hole, update
594 * last hole end */
595 tmp = tc->opt.sacks[vec_len (tc->opt.sacks) - 1];
596 last_hole = scoreboard_last_hole (sb);
597 if (seq_gt (tc->snd_una_max, sb->max_byte_sacked)
598 && seq_gt (tc->snd_una_max, last_hole->end))
599 last_hole->end = tc->snd_una_max;
Dave Barach68b0fb02017-02-28 15:15:56 -0500600 }
601
602 /* Walk the holes with the SACK blocks */
603 hole = pool_elt_at_index (sb->holes, sb->head);
604 while (hole && blk_index < vec_len (tc->opt.sacks))
605 {
606 blk = &tc->opt.sacks[blk_index];
607
608 if (seq_leq (blk->start, hole->start))
609 {
610 /* Block covers hole. Remove hole */
611 if (seq_geq (blk->end, hole->end))
612 {
613 next_hole = scoreboard_next_hole (sb, hole);
614
615 /* Byte accounting */
Florin Coras6792ec02017-03-13 03:49:51 -0700616 if (seq_leq (hole->end, ack))
Dave Barach68b0fb02017-02-28 15:15:56 -0500617 {
Florin Coras6792ec02017-03-13 03:49:51 -0700618 /* Bytes lost because snd_wnd left edge advances */
619 if (next_hole && seq_leq (next_hole->start, ack))
Florin Coras06d11012017-05-17 14:21:51 -0700620 delivered_bytes += next_hole->start - hole->end;
Dave Barach68b0fb02017-02-28 15:15:56 -0500621 else
Florin Coras06d11012017-05-17 14:21:51 -0700622 delivered_bytes += ack - hole->end;
Dave Barach68b0fb02017-02-28 15:15:56 -0500623 }
624 else
625 {
626 sb->sacked_bytes += scoreboard_hole_bytes (hole);
627 }
628
Florin Coras6792ec02017-03-13 03:49:51 -0700629 /* About to remove last hole */
630 if (hole == last_hole)
631 {
632 sb->tail = hole->prev;
633 last_hole = scoreboard_last_hole (sb);
Florin Coras06d11012017-05-17 14:21:51 -0700634 /* keep track of max byte sacked for when the last hole
Florin Coras6792ec02017-03-13 03:49:51 -0700635 * is acked */
636 if (seq_gt (hole->end, sb->max_byte_sacked))
637 sb->max_byte_sacked = hole->end;
638 }
Florin Coras06d11012017-05-17 14:21:51 -0700639
640 /* snd_una needs to be advanced */
641 if (blk->end == ack && seq_geq (ack, hole->end))
642 {
643 if (next_hole && seq_lt (ack, next_hole->start))
644 {
645 sb->snd_una_adv = next_hole->start - ack;
646
647 /* all these can be delivered */
648 delivered_bytes += sb->snd_una_adv;
649 }
650 else if (!next_hole)
651 {
652 sb->snd_una_adv = sb->max_byte_sacked - ack;
653 delivered_bytes += sb->snd_una_adv;
654 }
655 }
656
Dave Barach68b0fb02017-02-28 15:15:56 -0500657 scoreboard_remove_hole (sb, hole);
658 hole = next_hole;
659 }
Florin Coras6792ec02017-03-13 03:49:51 -0700660 /* Partial 'head' overlap */
Dave Barach68b0fb02017-02-28 15:15:56 -0500661 else
662 {
Florin Coras6792ec02017-03-13 03:49:51 -0700663 if (seq_gt (blk->end, hole->start))
664 {
665 sb->sacked_bytes += blk->end - hole->start;
666 hole->start = blk->end;
667 }
Dave Barach68b0fb02017-02-28 15:15:56 -0500668 blk_index++;
669 }
670 }
671 else
672 {
673 /* Hole must be split */
Florin Coras6792ec02017-03-13 03:49:51 -0700674 if (seq_lt (blk->end, hole->end))
Dave Barach68b0fb02017-02-28 15:15:56 -0500675 {
676 sb->sacked_bytes += blk->end - blk->start;
Florin Coras6792ec02017-03-13 03:49:51 -0700677 hole_index = scoreboard_hole_index (sb, hole);
678 new_hole = scoreboard_insert_hole (sb, hole_index, blk->end,
679 hole->end);
680
681 /* Pool might've moved */
682 hole = scoreboard_get_hole (sb, hole_index);
683 hole->end = blk->start;
684
685 /* New or split of tail */
686 if ((last_hole->end == new_hole->end)
687 || seq_lt (last_hole->end, new_hole->start))
688 {
689 last_hole = new_hole;
690 sb->tail = scoreboard_hole_index (sb, new_hole);
691 }
692
Dave Barach68b0fb02017-02-28 15:15:56 -0500693 blk_index++;
Florin Coras6792ec02017-03-13 03:49:51 -0700694 hole = scoreboard_next_hole (sb, hole);
Dave Barach68b0fb02017-02-28 15:15:56 -0500695 }
696 else
697 {
Florin Coras6792ec02017-03-13 03:49:51 -0700698 sb->sacked_bytes += hole->end - blk->start;
699 hole->end = blk->start;
Dave Barach68b0fb02017-02-28 15:15:56 -0500700 hole = scoreboard_next_hole (sb, hole);
701 }
702 }
703 }
Florin Coras6792ec02017-03-13 03:49:51 -0700704
Florin Coras06d11012017-05-17 14:21:51 -0700705 sb->last_sacked_bytes = sb->sacked_bytes - old_sacked_bytes;
706 sb->sacked_bytes -= delivered_bytes;
Dave Barach68b0fb02017-02-28 15:15:56 -0500707}
708
709/** Update snd_wnd
710 *
711 * If (SND.WL1 < SEG.SEQ or (SND.WL1 = SEG.SEQ and SND.WL2 =< SEG.ACK)), set
712 * SND.WND <- SEG.WND, set SND.WL1 <- SEG.SEQ, and set SND.WL2 <- SEG.ACK */
713static void
714tcp_update_snd_wnd (tcp_connection_t * tc, u32 seq, u32 ack, u32 snd_wnd)
715{
Florin Coras6792ec02017-03-13 03:49:51 -0700716 if (seq_lt (tc->snd_wl1, seq)
717 || (tc->snd_wl1 == seq && seq_leq (tc->snd_wl2, ack)))
Dave Barach68b0fb02017-02-28 15:15:56 -0500718 {
719 tc->snd_wnd = snd_wnd;
720 tc->snd_wl1 = seq;
721 tc->snd_wl2 = ack;
Florin Coras6792ec02017-03-13 03:49:51 -0700722 TCP_EVT_DBG (TCP_EVT_SND_WND, tc);
Florin Coras3e350af2017-03-30 02:54:28 -0700723
724 /* Set probe timer if we just got 0 wnd */
Florin Corasbb292f42017-05-19 09:49:19 -0700725 if (tc->snd_wnd < tc->snd_mss)
726 {
727 if (!tcp_timer_is_active (tc, TCP_TIMER_PERSIST))
728 tcp_persist_timer_set (tc);
729 }
Florin Coras3e350af2017-03-30 02:54:28 -0700730 else
731 tcp_persist_timer_reset (tc);
Dave Barach68b0fb02017-02-28 15:15:56 -0500732 }
733}
734
Florin Coras6792ec02017-03-13 03:49:51 -0700735void
Dave Barach68b0fb02017-02-28 15:15:56 -0500736tcp_cc_congestion (tcp_connection_t * tc)
737{
Florin Coras6792ec02017-03-13 03:49:51 -0700738 tc->snd_congestion = tc->snd_nxt;
Dave Barach68b0fb02017-02-28 15:15:56 -0500739 tc->cc_algo->congestion (tc);
Florin Coras6792ec02017-03-13 03:49:51 -0700740 TCP_EVT_DBG (TCP_EVT_CC_EVT, tc, 4);
Dave Barach68b0fb02017-02-28 15:15:56 -0500741}
742
Florin Coras6792ec02017-03-13 03:49:51 -0700743void
Dave Barach68b0fb02017-02-28 15:15:56 -0500744tcp_cc_recover (tcp_connection_t * tc)
745{
Florin Coras3e350af2017-03-30 02:54:28 -0700746 /* TODO: check if time to recover was small. It might be that RTO popped
747 * too soon.
748 */
749
Florin Coras6792ec02017-03-13 03:49:51 -0700750 tc->cc_algo->recovered (tc);
751
752 tc->rtx_bytes = 0;
753 tc->rcv_dupacks = 0;
754 tc->snd_nxt = tc->snd_una;
755
756 tc->cc_algo->rcv_ack (tc);
757 tc->tsecr_last_ack = tc->opt.tsecr;
758
Florin Coras3e350af2017-03-30 02:54:28 -0700759 tcp_cong_recovery_off (tc);
Florin Coras6792ec02017-03-13 03:49:51 -0700760
761 TCP_EVT_DBG (TCP_EVT_CC_EVT, tc, 3);
Dave Barach68b0fb02017-02-28 15:15:56 -0500762}
763
764static void
Florin Coras6792ec02017-03-13 03:49:51 -0700765tcp_cc_rcv_ack (tcp_connection_t * tc, vlib_buffer_t * b)
Dave Barach68b0fb02017-02-28 15:15:56 -0500766{
767 u8 partial_ack;
Florin Corasbb292f42017-05-19 09:49:19 -0700768 u32 bytes_advanced;
Dave Barach68b0fb02017-02-28 15:15:56 -0500769
Florin Coras3af90fc2017-05-03 21:09:42 -0700770 if (tcp_in_fastrecovery (tc))
Dave Barach68b0fb02017-02-28 15:15:56 -0500771 {
Florin Coras6792ec02017-03-13 03:49:51 -0700772 partial_ack = seq_lt (tc->snd_una, tc->snd_congestion);
Dave Barach68b0fb02017-02-28 15:15:56 -0500773 if (!partial_ack)
774 {
775 /* Clear retransmitted bytes. */
Dave Barach68b0fb02017-02-28 15:15:56 -0500776 tcp_cc_recover (tc);
777 }
778 else
779 {
Florin Coras6792ec02017-03-13 03:49:51 -0700780 TCP_EVT_DBG (TCP_EVT_CC_PACK, tc);
781
Dave Barach68b0fb02017-02-28 15:15:56 -0500782 /* Clear retransmitted bytes. XXX should we clear all? */
783 tc->rtx_bytes = 0;
Florin Coras3af90fc2017-05-03 21:09:42 -0700784
Dave Barach68b0fb02017-02-28 15:15:56 -0500785 tc->cc_algo->rcv_cong_ack (tc, TCP_CC_PARTIALACK);
786
Florin Coras6792ec02017-03-13 03:49:51 -0700787 /* In case snd_nxt is still in the past and output tries to
788 * shove some new bytes */
Florin Coras3e350af2017-03-30 02:54:28 -0700789 tc->snd_nxt = tc->snd_una_max;
Florin Coras6792ec02017-03-13 03:49:51 -0700790
791 /* XXX need proper RFC6675 support */
Florin Coras3e350af2017-03-30 02:54:28 -0700792 if (tc->sack_sb.last_sacked_bytes && !tcp_in_recovery (tc))
Florin Coras6792ec02017-03-13 03:49:51 -0700793 {
794 tcp_fast_retransmit (tc);
795 }
796 else
797 {
798 /* Retransmit first unacked segment */
799 tcp_retransmit_first_unacked (tc);
Florin Coras6792ec02017-03-13 03:49:51 -0700800 }
Dave Barach68b0fb02017-02-28 15:15:56 -0500801 }
802 }
803 else
804 {
805 tc->cc_algo->rcv_ack (tc);
Florin Coras6792ec02017-03-13 03:49:51 -0700806 tc->tsecr_last_ack = tc->opt.tsecr;
807 tc->rcv_dupacks = 0;
Florin Coras3af90fc2017-05-03 21:09:42 -0700808 if (tcp_in_recovery (tc))
809 {
Florin Corasbb292f42017-05-19 09:49:19 -0700810 bytes_advanced = tc->bytes_acked + tc->sack_sb.snd_una_adv;
811 tc->rtx_bytes -= clib_min (bytes_advanced, tc->rtx_bytes);
Florin Coras3af90fc2017-05-03 21:09:42 -0700812 tc->rto = clib_min (tc->srtt + (tc->rttvar << 2), TCP_RTO_MAX);
813 if (seq_geq (tc->snd_una, tc->snd_congestion))
Florin Corasbb292f42017-05-19 09:49:19 -0700814 {
815 tc->rtx_bytes = 0;
816 tcp_recovery_off (tc);
817 }
Florin Coras3af90fc2017-05-03 21:09:42 -0700818 }
Dave Barach68b0fb02017-02-28 15:15:56 -0500819 }
Dave Barach68b0fb02017-02-28 15:15:56 -0500820}
821
822static void
823tcp_cc_rcv_dupack (tcp_connection_t * tc, u32 ack)
824{
Florin Coras6792ec02017-03-13 03:49:51 -0700825// ASSERT (seq_geq(tc->snd_una, ack));
Dave Barach68b0fb02017-02-28 15:15:56 -0500826
827 tc->rcv_dupacks++;
828 if (tc->rcv_dupacks == TCP_DUPACK_THRESHOLD)
829 {
830 /* RFC6582 NewReno heuristic to avoid multiple fast retransmits */
831 if (tc->opt.tsecr != tc->tsecr_last_ack)
832 {
833 tc->rcv_dupacks = 0;
834 return;
835 }
836
837 tcp_fastrecovery_on (tc);
838
839 /* Handle congestion and dupack */
840 tcp_cc_congestion (tc);
841 tc->cc_algo->rcv_cong_ack (tc, TCP_CC_DUPACK);
842
843 tcp_fast_retransmit (tc);
844
845 /* Post retransmit update cwnd to ssthresh and account for the
846 * three segments that have left the network and should've been
847 * buffered at the receiver */
848 tc->cwnd = tc->ssthresh + TCP_DUPACK_THRESHOLD * tc->snd_mss;
849 }
850 else if (tc->rcv_dupacks > TCP_DUPACK_THRESHOLD)
851 {
852 ASSERT (tcp_in_fastrecovery (tc));
853
854 tc->cc_algo->rcv_cong_ack (tc, TCP_CC_DUPACK);
855 }
856}
857
858void
859tcp_cc_init (tcp_connection_t * tc)
860{
861 tc->cc_algo = tcp_cc_algo_get (TCP_CC_NEWRENO);
862 tc->cc_algo->init (tc);
863}
864
865static int
866tcp_rcv_ack (tcp_connection_t * tc, vlib_buffer_t * b,
867 tcp_header_t * th, u32 * next, u32 * error)
868{
869 u32 new_snd_wnd;
870
Florin Coras6792ec02017-03-13 03:49:51 -0700871 /* If the ACK acks something not yet sent (SEG.ACK > SND.NXT) */
Dave Barach68b0fb02017-02-28 15:15:56 -0500872 if (seq_gt (vnet_buffer (b)->tcp.ack_number, tc->snd_nxt))
873 {
Florin Coras6792ec02017-03-13 03:49:51 -0700874 /* If we have outstanding data and this is within the window, accept it,
875 * probably retransmit has timed out. Otherwise ACK segment and then
876 * drop it */
877 if (seq_gt (vnet_buffer (b)->tcp.ack_number, tc->snd_una_max))
878 {
879 tcp_make_ack (tc, b);
880 *next = tcp_next_output (tc->c_is_ip4);
881 *error = TCP_ERROR_ACK_INVALID;
882 TCP_EVT_DBG (TCP_EVT_ACK_RCV_ERR, tc, 0,
883 vnet_buffer (b)->tcp.ack_number);
884 return -1;
885 }
886
Florin Coras6792ec02017-03-13 03:49:51 -0700887 TCP_EVT_DBG (TCP_EVT_ACK_RCV_ERR, tc, 2,
888 vnet_buffer (b)->tcp.ack_number);
Florin Coras3e350af2017-03-30 02:54:28 -0700889
890 tc->snd_nxt = vnet_buffer (b)->tcp.ack_number;
891 *error = TCP_ERROR_ACK_FUTURE;
Dave Barach68b0fb02017-02-28 15:15:56 -0500892 }
893
Florin Coras6792ec02017-03-13 03:49:51 -0700894 /* If old ACK, probably it's an old dupack */
Dave Barach68b0fb02017-02-28 15:15:56 -0500895 if (seq_lt (vnet_buffer (b)->tcp.ack_number, tc->snd_una))
896 {
897 *error = TCP_ERROR_ACK_OLD;
Florin Coras6792ec02017-03-13 03:49:51 -0700898 TCP_EVT_DBG (TCP_EVT_ACK_RCV_ERR, tc, 1,
899 vnet_buffer (b)->tcp.ack_number);
900 if (tcp_in_fastrecovery (tc) && tc->rcv_dupacks == TCP_DUPACK_THRESHOLD)
901 {
902 TCP_EVT_DBG (TCP_EVT_DUPACK_RCVD, tc);
903 tcp_cc_rcv_dupack (tc, vnet_buffer (b)->tcp.ack_number);
904 }
Florin Corasc28764f2017-04-26 00:08:42 -0700905 /* Don't drop yet */
906 return 0;
Dave Barach68b0fb02017-02-28 15:15:56 -0500907 }
908
909 if (tcp_opts_sack_permitted (&tc->opt))
910 tcp_rcv_sacks (tc, vnet_buffer (b)->tcp.ack_number);
911
Florin Corase04c2992017-03-01 08:17:34 -0800912 new_snd_wnd = clib_net_to_host_u16 (th->window) << tc->snd_wscale;
Dave Barach68b0fb02017-02-28 15:15:56 -0500913
914 if (tcp_ack_is_dupack (tc, b, new_snd_wnd))
915 {
Florin Coras6792ec02017-03-13 03:49:51 -0700916 TCP_EVT_DBG (TCP_EVT_DUPACK_RCVD, tc, 1);
Dave Barach68b0fb02017-02-28 15:15:56 -0500917 tcp_cc_rcv_dupack (tc, vnet_buffer (b)->tcp.ack_number);
918 *error = TCP_ERROR_ACK_DUP;
919 return -1;
920 }
921
Florin Coras6792ec02017-03-13 03:49:51 -0700922 /*
923 * Valid ACK
924 */
925
Dave Barach68b0fb02017-02-28 15:15:56 -0500926 tc->bytes_acked = vnet_buffer (b)->tcp.ack_number - tc->snd_una;
Florin Coras6792ec02017-03-13 03:49:51 -0700927 tc->snd_una = vnet_buffer (b)->tcp.ack_number + tc->sack_sb.snd_una_adv;
Dave Barach68b0fb02017-02-28 15:15:56 -0500928
Florin Coras6792ec02017-03-13 03:49:51 -0700929 /* Dequeue ACKed data and update RTT */
Dave Barach68b0fb02017-02-28 15:15:56 -0500930 tcp_dequeue_acked (tc, vnet_buffer (b)->tcp.ack_number);
Dave Barach68b0fb02017-02-28 15:15:56 -0500931 tcp_update_snd_wnd (tc, vnet_buffer (b)->tcp.seq_number,
932 vnet_buffer (b)->tcp.ack_number, new_snd_wnd);
933
Florin Coras6792ec02017-03-13 03:49:51 -0700934 /* If some of our sent bytes have been acked, update cc and retransmit
935 * timer. */
936 if (tc->bytes_acked)
937 {
Florin Coras3e350af2017-03-30 02:54:28 -0700938 TCP_EVT_DBG (TCP_EVT_ACK_RCVD, tc);
Dave Barach68b0fb02017-02-28 15:15:56 -0500939
Florin Coras6792ec02017-03-13 03:49:51 -0700940 /* Updates congestion control (slow start/congestion avoidance) */
941 tcp_cc_rcv_ack (tc, b);
Florin Corase69f4952017-03-07 10:06:24 -0800942
Florin Coras6792ec02017-03-13 03:49:51 -0700943 /* If everything has been acked, stop retransmit timer
Florin Coras3af90fc2017-05-03 21:09:42 -0700944 * otherwise update. */
Florin Coras6792ec02017-03-13 03:49:51 -0700945 if (tc->snd_una == tc->snd_una_max)
946 tcp_retransmit_timer_reset (tc);
947 else
948 tcp_retransmit_timer_update (tc);
949 }
Dave Barach68b0fb02017-02-28 15:15:56 -0500950
951 return 0;
952}
953
954/**
955 * Build SACK list as per RFC2018.
956 *
957 * Makes sure the first block contains the segment that generated the current
958 * ACK and the following ones are the ones most recently reported in SACK
959 * blocks.
960 *
961 * @param tc TCP connection for which the SACK list is updated
962 * @param start Start sequence number of the newest SACK block
963 * @param end End sequence of the newest SACK block
964 */
Florin Coras45d34962017-04-25 00:05:27 -0700965void
Dave Barach68b0fb02017-02-28 15:15:56 -0500966tcp_update_sack_list (tcp_connection_t * tc, u32 start, u32 end)
967{
Florin Coras45d34962017-04-25 00:05:27 -0700968 sack_block_t *new_list = 0, *block = 0;
Dave Barach68b0fb02017-02-28 15:15:56 -0500969 int i;
Dave Barach68b0fb02017-02-28 15:15:56 -0500970
971 /* If the first segment is ooo add it to the list. Last write might've moved
972 * rcv_nxt over the first segment. */
973 if (seq_lt (tc->rcv_nxt, start))
974 {
Florin Coras45d34962017-04-25 00:05:27 -0700975 vec_add2 (new_list, block, 1);
976 block->start = start;
977 block->end = end;
Dave Barach68b0fb02017-02-28 15:15:56 -0500978 }
979
980 /* Find the blocks still worth keeping. */
981 for (i = 0; i < vec_len (tc->snd_sacks); i++)
982 {
Florin Coras45d34962017-04-25 00:05:27 -0700983 /* Discard if rcv_nxt advanced beyond current block */
984 if (seq_leq (tc->snd_sacks[i].start, tc->rcv_nxt))
Dave Barach68b0fb02017-02-28 15:15:56 -0500985 continue;
986
Florin Coras45d34962017-04-25 00:05:27 -0700987 /* Merge or drop if segment overlapped by the new segment */
988 if (block && (seq_geq (tc->snd_sacks[i].end, new_list[0].start)
989 && seq_leq (tc->snd_sacks[i].start, new_list[0].end)))
990 {
991 if (seq_lt (tc->snd_sacks[i].start, new_list[0].start))
992 new_list[0].start = tc->snd_sacks[i].start;
993 if (seq_lt (new_list[0].end, tc->snd_sacks[i].end))
994 new_list[0].end = tc->snd_sacks[i].end;
995 continue;
996 }
997
998 /* Save to new SACK list if we have space. */
999 if (vec_len (new_list) < TCP_MAX_SACK_BLOCKS)
1000 {
1001 vec_add1 (new_list, tc->snd_sacks[i]);
1002 }
Dave Barach68b0fb02017-02-28 15:15:56 -05001003 }
1004
Florin Coras45d34962017-04-25 00:05:27 -07001005 ASSERT (vec_len (new_list) <= TCP_MAX_SACK_BLOCKS);
Florin Coras6792ec02017-03-13 03:49:51 -07001006
Dave Barach68b0fb02017-02-28 15:15:56 -05001007 /* Replace old vector with new one */
1008 vec_free (tc->snd_sacks);
1009 tc->snd_sacks = new_list;
1010}
1011
1012/** Enqueue data for delivery to application */
Florin Coras6792ec02017-03-13 03:49:51 -07001013always_inline int
Dave Barach68b0fb02017-02-28 15:15:56 -05001014tcp_session_enqueue_data (tcp_connection_t * tc, vlib_buffer_t * b,
1015 u16 data_len)
1016{
1017 int written;
1018
1019 /* Pure ACK. Update rcv_nxt and be done. */
1020 if (PREDICT_FALSE (data_len == 0))
1021 {
1022 tc->rcv_nxt = vnet_buffer (b)->tcp.seq_end;
1023 return TCP_ERROR_PURE_ACK;
1024 }
1025
Florin Corasf6d68ed2017-05-07 19:12:02 -07001026 written = stream_session_enqueue_data (&tc->connection, b, 0,
1027 1 /* queue event */ , 1);
Dave Barach68b0fb02017-02-28 15:15:56 -05001028
Florin Coras6792ec02017-03-13 03:49:51 -07001029 TCP_EVT_DBG (TCP_EVT_INPUT, tc, 0, data_len, written);
1030
Dave Barach68b0fb02017-02-28 15:15:56 -05001031 /* Update rcv_nxt */
1032 if (PREDICT_TRUE (written == data_len))
1033 {
1034 tc->rcv_nxt = vnet_buffer (b)->tcp.seq_end;
1035 }
1036 /* If more data written than expected, account for out-of-order bytes. */
1037 else if (written > data_len)
1038 {
1039 tc->rcv_nxt = vnet_buffer (b)->tcp.seq_end + written - data_len;
1040
1041 /* Send ACK confirming the update */
1042 tc->flags |= TCP_CONN_SNDACK;
Florin Coras6792ec02017-03-13 03:49:51 -07001043 }
1044 else if (written > 0)
1045 {
1046 /* We've written something but FIFO is probably full now */
1047 tc->rcv_nxt += written;
Dave Barach68b0fb02017-02-28 15:15:56 -05001048
Florin Coras6792ec02017-03-13 03:49:51 -07001049 /* Depending on how fast the app is, all remaining buffers in burst will
Florin Coras3e350af2017-03-30 02:54:28 -07001050 * not be enqueued. Inform peer */
1051 tc->flags |= TCP_CONN_SNDACK;
1052
Florin Coras6792ec02017-03-13 03:49:51 -07001053 return TCP_ERROR_PARTIALLY_ENQUEUED;
Dave Barach68b0fb02017-02-28 15:15:56 -05001054 }
1055 else
1056 {
Florin Coras3e350af2017-03-30 02:54:28 -07001057 tc->flags |= TCP_CONN_SNDACK;
Dave Barach68b0fb02017-02-28 15:15:56 -05001058 return TCP_ERROR_FIFO_FULL;
1059 }
1060
Florin Coras6792ec02017-03-13 03:49:51 -07001061 /* Update SACK list if need be */
1062 if (tcp_opts_sack_permitted (&tc->opt))
1063 {
1064 /* Remove SACK blocks that have been delivered */
1065 tcp_update_sack_list (tc, tc->rcv_nxt, tc->rcv_nxt);
1066 }
1067
Dave Barach68b0fb02017-02-28 15:15:56 -05001068 return TCP_ERROR_ENQUEUED;
1069}
1070
1071/** Enqueue out-of-order data */
Florin Coras6792ec02017-03-13 03:49:51 -07001072always_inline int
Dave Barach68b0fb02017-02-28 15:15:56 -05001073tcp_session_enqueue_ooo (tcp_connection_t * tc, vlib_buffer_t * b,
1074 u16 data_len)
1075{
1076 stream_session_t *s0;
Florin Coras6792ec02017-03-13 03:49:51 -07001077 int rv;
1078
1079 /* Pure ACK. Do nothing */
1080 if (PREDICT_FALSE (data_len == 0))
1081 {
1082 return TCP_ERROR_PURE_ACK;
1083 }
Dave Barach68b0fb02017-02-28 15:15:56 -05001084
Florin Corasc28764f2017-04-26 00:08:42 -07001085 /* Enqueue out-of-order data with absolute offset */
Florin Corasf6d68ed2017-05-07 19:12:02 -07001086 rv = stream_session_enqueue_data (&tc->connection, b,
1087 vnet_buffer (b)->tcp.seq_number,
1088 0 /* queue event */ , 0);
Florin Coras6792ec02017-03-13 03:49:51 -07001089
1090 /* Nothing written */
1091 if (rv)
1092 {
1093 TCP_EVT_DBG (TCP_EVT_INPUT, tc, 1, data_len, 0);
1094 return TCP_ERROR_FIFO_FULL;
1095 }
1096
1097 TCP_EVT_DBG (TCP_EVT_INPUT, tc, 1, data_len, data_len);
Dave Barach68b0fb02017-02-28 15:15:56 -05001098
1099 /* Update SACK list if in use */
1100 if (tcp_opts_sack_permitted (&tc->opt))
1101 {
1102 ooo_segment_t *newest;
1103 u32 start, end;
1104
Florin Corasf6d68ed2017-05-07 19:12:02 -07001105 s0 = stream_session_get (tc->c_s_index, tc->c_thread_index);
1106
Dave Barach68b0fb02017-02-28 15:15:56 -05001107 /* Get the newest segment from the fifo */
1108 newest = svm_fifo_newest_ooo_segment (s0->server_rx_fifo);
Dave Barach1f75cfd2017-04-14 16:46:44 -04001109 start = ooo_segment_offset (s0->server_rx_fifo, newest);
1110 end = ooo_segment_end_offset (s0->server_rx_fifo, newest);
Dave Barach68b0fb02017-02-28 15:15:56 -05001111
1112 tcp_update_sack_list (tc, start, end);
1113 }
1114
1115 return TCP_ERROR_ENQUEUED;
1116}
1117
1118/**
Florin Coras6792ec02017-03-13 03:49:51 -07001119 * Check if ACK could be delayed. If ack can be delayed, it should return
1120 * true for a full frame. If we're always acking return 0.
Dave Barach68b0fb02017-02-28 15:15:56 -05001121 */
1122always_inline int
1123tcp_can_delack (tcp_connection_t * tc)
1124{
Florin Coras6792ec02017-03-13 03:49:51 -07001125 /* Send ack if ... */
1126 if (TCP_ALWAYS_ACK
1127 /* just sent a rcv wnd 0 */
1128 || (tc->flags & TCP_CONN_SENT_RCV_WND0) != 0
1129 /* constrained to send ack */
1130 || (tc->flags & TCP_CONN_SNDACK) != 0
1131 /* we're almost out of tx wnd */
1132 || tcp_available_snd_space (tc) < 2 * tc->snd_mss)
1133 return 0;
Dave Barach68b0fb02017-02-28 15:15:56 -05001134
Florin Coras6792ec02017-03-13 03:49:51 -07001135 return 1;
Dave Barach68b0fb02017-02-28 15:15:56 -05001136}
1137
1138static int
1139tcp_segment_rcv (tcp_main_t * tm, tcp_connection_t * tc, vlib_buffer_t * b,
1140 u16 n_data_bytes, u32 * next0)
1141{
Florin Corasdb84e572017-05-09 18:54:52 -07001142 u32 error = 0, n_bytes_to_drop;
Dave Barach68b0fb02017-02-28 15:15:56 -05001143
1144 /* Handle out-of-order data */
1145 if (PREDICT_FALSE (vnet_buffer (b)->tcp.seq_number != tc->rcv_nxt))
1146 {
Florin Coras6792ec02017-03-13 03:49:51 -07001147 /* Old sequence numbers allowed through because they overlapped
1148 * the rx window */
1149 if (seq_lt (vnet_buffer (b)->tcp.seq_number, tc->rcv_nxt))
Dave Barach68b0fb02017-02-28 15:15:56 -05001150 {
Florin Coras6792ec02017-03-13 03:49:51 -07001151 error = TCP_ERROR_SEGMENT_OLD;
1152 *next0 = TCP_NEXT_DROP;
Florin Corasdb84e572017-05-09 18:54:52 -07001153
Dave Barach259cdae2017-05-15 16:27:05 -04001154 /* Completely in the past (possible retransmit) */
1155 if (seq_lt (vnet_buffer (b)->tcp.seq_end, tc->rcv_nxt))
1156 goto done;
1157
Florin Corasdb84e572017-05-09 18:54:52 -07001158 /* Chop off the bytes in the past */
1159 n_bytes_to_drop = tc->rcv_nxt - vnet_buffer (b)->tcp.seq_number;
1160 n_data_bytes -= n_bytes_to_drop;
1161 vlib_buffer_advance (b, n_bytes_to_drop);
1162
1163 goto in_order;
Dave Barach68b0fb02017-02-28 15:15:56 -05001164 }
1165
Florin Coras6792ec02017-03-13 03:49:51 -07001166 error = tcp_session_enqueue_ooo (tc, b, n_data_bytes);
1167
1168 /* N.B. Should not filter burst of dupacks. Two issues 1) dupacks open
1169 * cwnd on remote peer when congested 2) acks leaving should have the
1170 * latest rcv_wnd since the burst may eaten up all of it, so only the
1171 * old ones could be filtered.
1172 */
1173
1174 /* RFC2581: Send DUPACK for fast retransmit */
1175 tcp_make_ack (tc, b);
1176 *next0 = tcp_next_output (tc->c_is_ip4);
1177
1178 /* Mark as DUPACK. We may filter these in output if
1179 * the burst fills the holes. */
1180 if (n_data_bytes)
1181 vnet_buffer (b)->tcp.flags = TCP_BUF_FLAG_DUPACK;
1182
1183 TCP_EVT_DBG (TCP_EVT_DUPACK_SENT, tc);
Dave Barach68b0fb02017-02-28 15:15:56 -05001184 goto done;
1185 }
1186
Florin Corasdb84e572017-05-09 18:54:52 -07001187in_order:
1188
Dave Barach68b0fb02017-02-28 15:15:56 -05001189 /* In order data, enqueue. Fifo figures out by itself if any out-of-order
1190 * segments can be enqueued after fifo tail offset changes. */
1191 error = tcp_session_enqueue_data (tc, b, n_data_bytes);
1192
Florin Coras6792ec02017-03-13 03:49:51 -07001193 if (n_data_bytes == 0)
1194 {
1195 *next0 = TCP_NEXT_DROP;
1196 goto done;
1197 }
1198
Dave Barach68b0fb02017-02-28 15:15:56 -05001199 /* Check if ACK can be delayed */
Florin Coras3e350af2017-03-30 02:54:28 -07001200 if (tcp_can_delack (tc))
Dave Barach68b0fb02017-02-28 15:15:56 -05001201 {
Florin Coras6792ec02017-03-13 03:49:51 -07001202 if (!tcp_timer_is_active (tc, TCP_TIMER_DELACK))
1203 tcp_timer_set (tc, TCP_TIMER_DELACK, TCP_DELACK_TIME);
Florin Coras3e350af2017-03-30 02:54:28 -07001204 goto done;
Dave Barach68b0fb02017-02-28 15:15:56 -05001205 }
1206
Florin Coras3e350af2017-03-30 02:54:28 -07001207 *next0 = tcp_next_output (tc->c_is_ip4);
1208 tcp_make_ack (tc, b);
1209
Dave Barach68b0fb02017-02-28 15:15:56 -05001210done:
1211 return error;
1212}
1213
Clement Durand6cf260c2017-04-13 13:27:04 +02001214typedef struct
1215{
1216 tcp_header_t tcp_header;
1217 tcp_connection_t tcp_connection;
1218} tcp_rx_trace_t;
1219
1220u8 *
1221format_tcp_rx_trace (u8 * s, va_list * args)
1222{
1223 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
1224 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
1225 tcp_rx_trace_t *t = va_arg (*args, tcp_rx_trace_t *);
1226 uword indent = format_get_indent (s);
1227
1228 s = format (s, "%U\n%U%U",
1229 format_tcp_header, &t->tcp_header, 128,
1230 format_white_space, indent,
Florin Corasbb292f42017-05-19 09:49:19 -07001231 format_tcp_connection, &t->tcp_connection, 1);
Clement Durand6cf260c2017-04-13 13:27:04 +02001232
1233 return s;
1234}
1235
1236u8 *
1237format_tcp_rx_trace_short (u8 * s, va_list * args)
1238{
1239 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
1240 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
1241 tcp_rx_trace_t *t = va_arg (*args, tcp_rx_trace_t *);
1242
1243 s = format (s, "%d -> %d (%U)",
1244 clib_net_to_host_u16 (t->tcp_header.src_port),
1245 clib_net_to_host_u16 (t->tcp_header.dst_port), format_tcp_state,
Florin Corasbb292f42017-05-19 09:49:19 -07001246 t->tcp_connection.state);
Clement Durand6cf260c2017-04-13 13:27:04 +02001247
1248 return s;
1249}
1250
Florin Coras82b13a82017-04-25 11:58:06 -07001251void
1252tcp_set_rx_trace_data (tcp_rx_trace_t * t0, tcp_connection_t * tc0,
1253 tcp_header_t * th0, vlib_buffer_t * b0, u8 is_ip4)
1254{
1255 if (tc0)
1256 {
1257 clib_memcpy (&t0->tcp_connection, tc0, sizeof (t0->tcp_connection));
1258 }
1259 else
1260 {
1261 th0 = tcp_buffer_hdr (b0);
1262 }
1263 clib_memcpy (&t0->tcp_header, th0, sizeof (t0->tcp_header));
1264}
1265
Florin Coras6792ec02017-03-13 03:49:51 -07001266always_inline void
1267tcp_established_inc_counter (vlib_main_t * vm, u8 is_ip4, u8 evt, u8 val)
Dave Barach68b0fb02017-02-28 15:15:56 -05001268{
Florin Coras6792ec02017-03-13 03:49:51 -07001269 if (PREDICT_TRUE (!val))
1270 return;
Dave Barach68b0fb02017-02-28 15:15:56 -05001271
Florin Coras6792ec02017-03-13 03:49:51 -07001272 if (is_ip4)
1273 vlib_node_increment_counter (vm, tcp4_established_node.index, evt, val);
1274 else
1275 vlib_node_increment_counter (vm, tcp6_established_node.index, evt, val);
Dave Barach68b0fb02017-02-28 15:15:56 -05001276}
1277
1278always_inline uword
1279tcp46_established_inline (vlib_main_t * vm, vlib_node_runtime_t * node,
1280 vlib_frame_t * from_frame, int is_ip4)
1281{
1282 u32 n_left_from, next_index, *from, *to_next;
Damjan Marion586afd72017-04-05 19:18:20 +02001283 u32 my_thread_index = vm->thread_index, errors = 0;
Dave Barach68b0fb02017-02-28 15:15:56 -05001284 tcp_main_t *tm = vnet_get_tcp_main ();
Dave Barach1f75cfd2017-04-14 16:46:44 -04001285 u8 is_fin = 0;
Dave Barach68b0fb02017-02-28 15:15:56 -05001286
1287 from = vlib_frame_vector_args (from_frame);
1288 n_left_from = from_frame->n_vectors;
1289
1290 next_index = node->cached_next_index;
1291
1292 while (n_left_from > 0)
1293 {
1294 u32 n_left_to_next;
1295
1296 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
1297
1298 while (n_left_from > 0 && n_left_to_next > 0)
1299 {
1300 u32 bi0;
1301 vlib_buffer_t *b0;
1302 tcp_header_t *th0 = 0;
1303 tcp_connection_t *tc0;
Dave Barach68b0fb02017-02-28 15:15:56 -05001304 u32 next0 = TCP_ESTABLISHED_NEXT_DROP, error0 = TCP_ERROR_ENQUEUED;
1305
1306 bi0 = from[0];
1307 to_next[0] = bi0;
1308 from += 1;
1309 to_next += 1;
1310 n_left_from -= 1;
1311 n_left_to_next -= 1;
1312
1313 b0 = vlib_get_buffer (vm, bi0);
1314 tc0 = tcp_connection_get (vnet_buffer (b0)->tcp.connection_index,
1315 my_thread_index);
1316
Florin Corasd79b41e2017-03-04 05:37:52 -08001317 if (PREDICT_FALSE (tc0 == 0))
1318 {
1319 error0 = TCP_ERROR_INVALID_CONNECTION;
Florin Coras6792ec02017-03-13 03:49:51 -07001320 goto done;
Florin Corasd79b41e2017-03-04 05:37:52 -08001321 }
1322
Florin Coras82b13a82017-04-25 11:58:06 -07001323 th0 = tcp_buffer_hdr (b0);
Dave Barach68b0fb02017-02-28 15:15:56 -05001324
Dave Barach1f75cfd2017-04-14 16:46:44 -04001325 is_fin = (th0->flags & TCP_FLAG_FIN) != 0;
1326
Dave Barach68b0fb02017-02-28 15:15:56 -05001327 /* SYNs, FINs and data consume sequence numbers */
1328 vnet_buffer (b0)->tcp.seq_end = vnet_buffer (b0)->tcp.seq_number
Florin Coras82b13a82017-04-25 11:58:06 -07001329 + tcp_is_syn (th0) + is_fin + vnet_buffer (b0)->tcp.data_len;
Dave Barach68b0fb02017-02-28 15:15:56 -05001330
1331 /* TODO header prediction fast path */
1332
1333 /* 1-4: check SEQ, RST, SYN */
1334 if (PREDICT_FALSE (tcp_segment_validate (vm, tc0, b0, th0, &next0)))
1335 {
1336 error0 = TCP_ERROR_SEGMENT_INVALID;
Florin Coras6792ec02017-03-13 03:49:51 -07001337 TCP_EVT_DBG (TCP_EVT_SEG_INVALID, tc0,
1338 vnet_buffer (b0)->tcp.seq_number,
1339 vnet_buffer (b0)->tcp.seq_end);
1340 goto done;
Dave Barach68b0fb02017-02-28 15:15:56 -05001341 }
1342
1343 /* 5: check the ACK field */
1344 if (tcp_rcv_ack (tc0, b0, th0, &next0, &error0))
1345 {
Florin Coras6792ec02017-03-13 03:49:51 -07001346 goto done;
Dave Barach68b0fb02017-02-28 15:15:56 -05001347 }
1348
1349 /* 6: check the URG bit TODO */
1350
1351 /* 7: process the segment text */
Florin Coras6792ec02017-03-13 03:49:51 -07001352
Florin Coras82b13a82017-04-25 11:58:06 -07001353 vlib_buffer_advance (b0, vnet_buffer (b0)->tcp.data_offset);
1354 error0 = tcp_segment_rcv (tm, tc0, b0,
1355 vnet_buffer (b0)->tcp.data_len, &next0);
Dave Barach68b0fb02017-02-28 15:15:56 -05001356
Dave Barach1f75cfd2017-04-14 16:46:44 -04001357 /* N.B. buffer is rewritten if segment is ooo. Thus, th0 becomes a
1358 * dangling reference. */
1359
Dave Barach68b0fb02017-02-28 15:15:56 -05001360 /* 8: check the FIN bit */
Dave Barach1f75cfd2017-04-14 16:46:44 -04001361 if (is_fin)
Dave Barach68b0fb02017-02-28 15:15:56 -05001362 {
Florin Corasd79b41e2017-03-04 05:37:52 -08001363 /* Enter CLOSE-WAIT and notify session. Don't send ACK, instead
1364 * wait for session to call close. To avoid lingering
1365 * in CLOSE-WAIT, set timer (reuse WAITCLOSE). */
Dave Barach68b0fb02017-02-28 15:15:56 -05001366 tc0->state = TCP_STATE_CLOSE_WAIT;
Florin Corase69f4952017-03-07 10:06:24 -08001367 TCP_EVT_DBG (TCP_EVT_FIN_RCVD, tc0);
Dave Barach68b0fb02017-02-28 15:15:56 -05001368 stream_session_disconnect_notify (&tc0->connection);
Florin Corasd79b41e2017-03-04 05:37:52 -08001369 tcp_timer_set (tc0, TCP_TIMER_WAITCLOSE, TCP_CLOSEWAIT_TIME);
Dave Barach68b0fb02017-02-28 15:15:56 -05001370 }
1371
Florin Coras6792ec02017-03-13 03:49:51 -07001372 done:
Dave Barach68b0fb02017-02-28 15:15:56 -05001373 b0->error = node->errors[error0];
1374 if (PREDICT_FALSE (b0->flags & VLIB_BUFFER_IS_TRACED))
1375 {
Florin Coras82b13a82017-04-25 11:58:06 -07001376 tcp_rx_trace_t *t0 =
1377 vlib_add_trace (vm, node, b0, sizeof (*t0));
1378 tcp_set_rx_trace_data (t0, tc0, th0, b0, is_ip4);
Dave Barach68b0fb02017-02-28 15:15:56 -05001379 }
1380
1381 vlib_validate_buffer_enqueue_x1 (vm, node, next_index, to_next,
1382 n_left_to_next, bi0, next0);
1383 }
1384
1385 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
1386 }
1387
1388 errors = session_manager_flush_enqueue_events (my_thread_index);
Florin Coras6792ec02017-03-13 03:49:51 -07001389 tcp_established_inc_counter (vm, is_ip4, TCP_ERROR_EVENT_FIFO_FULL, errors);
Dave Barach68b0fb02017-02-28 15:15:56 -05001390
1391 return from_frame->n_vectors;
1392}
1393
1394static uword
1395tcp4_established (vlib_main_t * vm, vlib_node_runtime_t * node,
1396 vlib_frame_t * from_frame)
1397{
1398 return tcp46_established_inline (vm, node, from_frame, 1 /* is_ip4 */ );
1399}
1400
1401static uword
1402tcp6_established (vlib_main_t * vm, vlib_node_runtime_t * node,
1403 vlib_frame_t * from_frame)
1404{
1405 return tcp46_established_inline (vm, node, from_frame, 0 /* is_ip4 */ );
1406}
1407
1408/* *INDENT-OFF* */
1409VLIB_REGISTER_NODE (tcp4_established_node) =
1410{
1411 .function = tcp4_established,
1412 .name = "tcp4-established",
1413 /* Takes a vector of packets. */
1414 .vector_size = sizeof (u32),
Florin Corase69f4952017-03-07 10:06:24 -08001415 .n_errors = TCP_N_ERROR,
1416 .error_strings = tcp_error_strings,
Dave Barach68b0fb02017-02-28 15:15:56 -05001417 .n_next_nodes = TCP_ESTABLISHED_N_NEXT,
1418 .next_nodes =
1419 {
1420#define _(s,n) [TCP_ESTABLISHED_NEXT_##s] = n,
1421 foreach_tcp_state_next
1422#undef _
1423 },
Clement Durand6cf260c2017-04-13 13:27:04 +02001424 .format_trace = format_tcp_rx_trace_short,
Dave Barach68b0fb02017-02-28 15:15:56 -05001425};
1426/* *INDENT-ON* */
1427
1428VLIB_NODE_FUNCTION_MULTIARCH (tcp4_established_node, tcp4_established);
1429
1430/* *INDENT-OFF* */
1431VLIB_REGISTER_NODE (tcp6_established_node) =
1432{
1433 .function = tcp6_established,
1434 .name = "tcp6-established",
1435 /* Takes a vector of packets. */
1436 .vector_size = sizeof (u32),
1437 .n_errors = TCP_N_ERROR,
1438 .error_strings = tcp_error_strings,
1439 .n_next_nodes = TCP_ESTABLISHED_N_NEXT,
1440 .next_nodes =
1441 {
1442#define _(s,n) [TCP_ESTABLISHED_NEXT_##s] = n,
1443 foreach_tcp_state_next
1444#undef _
1445 },
Clement Durand6cf260c2017-04-13 13:27:04 +02001446 .format_trace = format_tcp_rx_trace_short,
Dave Barach68b0fb02017-02-28 15:15:56 -05001447};
1448/* *INDENT-ON* */
1449
1450
1451VLIB_NODE_FUNCTION_MULTIARCH (tcp6_established_node, tcp6_established);
1452
1453vlib_node_registration_t tcp4_syn_sent_node;
1454vlib_node_registration_t tcp6_syn_sent_node;
1455
1456always_inline uword
1457tcp46_syn_sent_inline (vlib_main_t * vm, vlib_node_runtime_t * node,
1458 vlib_frame_t * from_frame, int is_ip4)
1459{
1460 tcp_main_t *tm = vnet_get_tcp_main ();
1461 u32 n_left_from, next_index, *from, *to_next;
Damjan Marion586afd72017-04-05 19:18:20 +02001462 u32 my_thread_index = vm->thread_index, errors = 0;
Dave Barach68b0fb02017-02-28 15:15:56 -05001463 u8 sst = is_ip4 ? SESSION_TYPE_IP4_TCP : SESSION_TYPE_IP6_TCP;
1464
1465 from = vlib_frame_vector_args (from_frame);
1466 n_left_from = from_frame->n_vectors;
1467
1468 next_index = node->cached_next_index;
1469
1470 while (n_left_from > 0)
1471 {
1472 u32 n_left_to_next;
1473
1474 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
1475
1476 while (n_left_from > 0 && n_left_to_next > 0)
1477 {
1478 u32 bi0, ack0, seq0;
1479 vlib_buffer_t *b0;
Clement Durand6cf260c2017-04-13 13:27:04 +02001480 tcp_rx_trace_t *t0;
Dave Barach68b0fb02017-02-28 15:15:56 -05001481 tcp_header_t *tcp0 = 0;
1482 tcp_connection_t *tc0;
Dave Barach68b0fb02017-02-28 15:15:56 -05001483 tcp_connection_t *new_tc0;
1484 u32 next0 = TCP_SYN_SENT_NEXT_DROP, error0 = TCP_ERROR_ENQUEUED;
1485
1486 bi0 = from[0];
1487 to_next[0] = bi0;
1488 from += 1;
1489 to_next += 1;
1490 n_left_from -= 1;
1491 n_left_to_next -= 1;
1492
1493 b0 = vlib_get_buffer (vm, bi0);
1494 tc0 =
1495 tcp_half_open_connection_get (vnet_buffer (b0)->
1496 tcp.connection_index);
1497
1498 ack0 = vnet_buffer (b0)->tcp.ack_number;
1499 seq0 = vnet_buffer (b0)->tcp.seq_number;
Florin Coras82b13a82017-04-25 11:58:06 -07001500 tcp0 = tcp_buffer_hdr (b0);
Dave Barach68b0fb02017-02-28 15:15:56 -05001501
1502 if (PREDICT_FALSE
1503 (!tcp_ack (tcp0) && !tcp_rst (tcp0) && !tcp_syn (tcp0)))
1504 goto drop;
1505
1506 /* SYNs, FINs and data consume sequence numbers */
1507 vnet_buffer (b0)->tcp.seq_end = seq0 + tcp_is_syn (tcp0)
Florin Coras82b13a82017-04-25 11:58:06 -07001508 + tcp_is_fin (tcp0) + vnet_buffer (b0)->tcp.data_len;
Dave Barach68b0fb02017-02-28 15:15:56 -05001509
1510 /*
1511 * 1. check the ACK bit
1512 */
1513
1514 /*
1515 * If the ACK bit is set
1516 * If SEG.ACK =< ISS, or SEG.ACK > SND.NXT, send a reset (unless
1517 * the RST bit is set, if so drop the segment and return)
1518 * <SEQ=SEG.ACK><CTL=RST>
1519 * and discard the segment. Return.
1520 * If SND.UNA =< SEG.ACK =< SND.NXT then the ACK is acceptable.
1521 */
1522 if (tcp_ack (tcp0))
1523 {
1524 if (ack0 <= tc0->iss || ack0 > tc0->snd_nxt)
1525 {
1526 if (!tcp_rst (tcp0))
1527 tcp_send_reset (b0, is_ip4);
1528
1529 goto drop;
1530 }
1531
1532 /* Make sure ACK is valid */
1533 if (tc0->snd_una > ack0)
1534 goto drop;
1535 }
1536
1537 /*
1538 * 2. check the RST bit
1539 */
1540
1541 if (tcp_rst (tcp0))
1542 {
1543 /* If ACK is acceptable, signal client that peer is not
1544 * willing to accept connection and drop connection*/
1545 if (tcp_ack (tcp0))
1546 {
1547 stream_session_connect_notify (&tc0->connection, sst,
1548 1 /* fail */ );
1549 tcp_connection_cleanup (tc0);
1550 }
1551 goto drop;
1552 }
1553
1554 /*
1555 * 3. check the security and precedence (skipped)
1556 */
1557
1558 /*
1559 * 4. check the SYN bit
1560 */
1561
1562 /* No SYN flag. Drop. */
1563 if (!tcp_syn (tcp0))
1564 goto drop;
1565
1566 /* Stop connection establishment and retransmit timers */
1567 tcp_timer_reset (tc0, TCP_TIMER_ESTABLISH);
1568 tcp_timer_reset (tc0, TCP_TIMER_RETRANSMIT_SYN);
1569
1570 /* Valid SYN or SYN-ACK. Move connection from half-open pool to
1571 * current thread pool. */
1572 pool_get (tm->connections[my_thread_index], new_tc0);
1573 clib_memcpy (new_tc0, tc0, sizeof (*new_tc0));
1574
1575 new_tc0->c_thread_index = my_thread_index;
Dave Barach259cdae2017-05-15 16:27:05 -04001576 new_tc0->c_c_index = new_tc0 - tm->connections[my_thread_index];
Dave Barach68b0fb02017-02-28 15:15:56 -05001577
1578 /* Cleanup half-open connection XXX lock */
1579 pool_put (tm->half_open_connections, tc0);
1580
1581 new_tc0->rcv_nxt = vnet_buffer (b0)->tcp.seq_end;
1582 new_tc0->irs = seq0;
1583
1584 /* Parse options */
Florin Corasdb84e572017-05-09 18:54:52 -07001585 if (tcp_options_parse (tcp0, &new_tc0->opt))
1586 goto drop;
Dave Barach68b0fb02017-02-28 15:15:56 -05001587
1588 if (tcp_opts_tstamp (&new_tc0->opt))
1589 {
1590 new_tc0->tsval_recent = new_tc0->opt.tsval;
1591 new_tc0->tsval_recent_age = tcp_time_now ();
1592 }
1593
1594 if (tcp_opts_wscale (&new_tc0->opt))
1595 new_tc0->snd_wscale = new_tc0->opt.wscale;
1596
Florin Corase04c2992017-03-01 08:17:34 -08001597 /* No scaling */
1598 new_tc0->snd_wnd = clib_net_to_host_u16 (tcp0->window);
Dave Barach68b0fb02017-02-28 15:15:56 -05001599 new_tc0->snd_wl1 = seq0;
1600 new_tc0->snd_wl2 = ack0;
1601
Florin Corase04c2992017-03-01 08:17:34 -08001602 tcp_connection_init_vars (new_tc0);
1603
Dave Barach68b0fb02017-02-28 15:15:56 -05001604 /* SYN-ACK: See if we can switch to ESTABLISHED state */
1605 if (tcp_ack (tcp0))
1606 {
1607 /* Our SYN is ACKed: we have iss < ack = snd_una */
1608
1609 /* TODO Dequeue acknowledged segments if we support Fast Open */
1610 new_tc0->snd_una = ack0;
1611 new_tc0->state = TCP_STATE_ESTABLISHED;
1612
Florin Corase04c2992017-03-01 08:17:34 -08001613 /* Make sure las is initialized for the wnd computation */
1614 new_tc0->rcv_las = new_tc0->rcv_nxt;
1615
Dave Barach68b0fb02017-02-28 15:15:56 -05001616 /* Notify app that we have connection */
1617 stream_session_connect_notify (&new_tc0->connection, sst, 0);
1618
Florin Corasc28764f2017-04-26 00:08:42 -07001619 stream_session_init_fifos_pointers (&new_tc0->connection,
1620 new_tc0->irs + 1,
1621 new_tc0->iss + 1);
Dave Barach68b0fb02017-02-28 15:15:56 -05001622 /* Make sure after data segment processing ACK is sent */
1623 new_tc0->flags |= TCP_CONN_SNDACK;
1624 }
1625 /* SYN: Simultaneous open. Change state to SYN-RCVD and send SYN-ACK */
1626 else
1627 {
1628 new_tc0->state = TCP_STATE_SYN_RCVD;
1629
Florin Corase69f4952017-03-07 10:06:24 -08001630 /* Notify app that we have connection */
Dave Barach68b0fb02017-02-28 15:15:56 -05001631 stream_session_connect_notify (&new_tc0->connection, sst, 0);
Florin Corasc28764f2017-04-26 00:08:42 -07001632 stream_session_init_fifos_pointers (&new_tc0->connection,
1633 new_tc0->irs + 1,
1634 new_tc0->iss + 1);
Dave Barach68b0fb02017-02-28 15:15:56 -05001635 tcp_make_synack (new_tc0, b0);
1636 next0 = tcp_next_output (is_ip4);
1637
1638 goto drop;
1639 }
1640
1641 /* Read data, if any */
Florin Coras82b13a82017-04-25 11:58:06 -07001642 if (vnet_buffer (b0)->tcp.data_len)
Dave Barach68b0fb02017-02-28 15:15:56 -05001643 {
Florin Coras82b13a82017-04-25 11:58:06 -07001644 vlib_buffer_advance (b0, vnet_buffer (b0)->tcp.data_offset);
1645 error0 = tcp_segment_rcv (tm, new_tc0, b0,
1646 vnet_buffer (b0)->tcp.data_len,
1647 &next0);
Dave Barach68b0fb02017-02-28 15:15:56 -05001648 if (error0 == TCP_ERROR_PURE_ACK)
1649 error0 = TCP_ERROR_SYN_ACKS_RCVD;
1650 }
1651 else
1652 {
1653 tcp_make_ack (new_tc0, b0);
1654 next0 = tcp_next_output (new_tc0->c_is_ip4);
1655 }
1656
1657 drop:
1658
1659 b0->error = error0 ? node->errors[error0] : 0;
1660 if (PREDICT_FALSE (b0->flags & VLIB_BUFFER_IS_TRACED))
1661 {
Clement Durand6cf260c2017-04-13 13:27:04 +02001662 t0 = vlib_add_trace (vm, node, b0, sizeof (*t0));
1663 clib_memcpy (&t0->tcp_header, tcp0, sizeof (t0->tcp_header));
1664 clib_memcpy (&t0->tcp_connection, tc0,
1665 sizeof (t0->tcp_connection));
Dave Barach68b0fb02017-02-28 15:15:56 -05001666 }
1667
1668 vlib_validate_buffer_enqueue_x1 (vm, node, next_index, to_next,
1669 n_left_to_next, bi0, next0);
1670 }
1671
1672 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
1673 }
1674
1675 errors = session_manager_flush_enqueue_events (my_thread_index);
1676 if (errors)
1677 {
1678 if (is_ip4)
1679 vlib_node_increment_counter (vm, tcp4_established_node.index,
1680 TCP_ERROR_EVENT_FIFO_FULL, errors);
1681 else
1682 vlib_node_increment_counter (vm, tcp6_established_node.index,
1683 TCP_ERROR_EVENT_FIFO_FULL, errors);
1684 }
1685
1686 return from_frame->n_vectors;
1687}
1688
1689static uword
1690tcp4_syn_sent (vlib_main_t * vm, vlib_node_runtime_t * node,
1691 vlib_frame_t * from_frame)
1692{
1693 return tcp46_syn_sent_inline (vm, node, from_frame, 1 /* is_ip4 */ );
1694}
1695
1696static uword
1697tcp6_syn_sent_rcv (vlib_main_t * vm, vlib_node_runtime_t * node,
1698 vlib_frame_t * from_frame)
1699{
1700 return tcp46_syn_sent_inline (vm, node, from_frame, 0 /* is_ip4 */ );
1701}
1702
1703/* *INDENT-OFF* */
1704VLIB_REGISTER_NODE (tcp4_syn_sent_node) =
1705{
1706 .function = tcp4_syn_sent,
1707 .name = "tcp4-syn-sent",
1708 /* Takes a vector of packets. */
1709 .vector_size = sizeof (u32),
1710 .n_errors = TCP_N_ERROR,
1711 .error_strings = tcp_error_strings,
1712 .n_next_nodes = TCP_SYN_SENT_N_NEXT,
1713 .next_nodes =
1714 {
1715#define _(s,n) [TCP_SYN_SENT_NEXT_##s] = n,
1716 foreach_tcp_state_next
1717#undef _
1718 },
Clement Durand6cf260c2017-04-13 13:27:04 +02001719 .format_trace = format_tcp_rx_trace_short,
Dave Barach68b0fb02017-02-28 15:15:56 -05001720};
1721/* *INDENT-ON* */
1722
1723VLIB_NODE_FUNCTION_MULTIARCH (tcp4_syn_sent_node, tcp4_syn_sent);
1724
1725/* *INDENT-OFF* */
1726VLIB_REGISTER_NODE (tcp6_syn_sent_node) =
1727{
1728 .function = tcp6_syn_sent_rcv,
1729 .name = "tcp6-syn-sent",
1730 /* Takes a vector of packets. */
1731 .vector_size = sizeof (u32),
1732 .n_errors = TCP_N_ERROR,
1733 .error_strings = tcp_error_strings,
1734 .n_next_nodes = TCP_SYN_SENT_N_NEXT,
1735 .next_nodes =
1736 {
1737#define _(s,n) [TCP_SYN_SENT_NEXT_##s] = n,
1738 foreach_tcp_state_next
1739#undef _
Clement Durand6cf260c2017-04-13 13:27:04 +02001740 },
1741 .format_trace = format_tcp_rx_trace_short,
1742};
Dave Barach68b0fb02017-02-28 15:15:56 -05001743/* *INDENT-ON* */
1744
1745VLIB_NODE_FUNCTION_MULTIARCH (tcp6_syn_sent_node, tcp6_syn_sent_rcv);
1746/**
Florin Corasd79b41e2017-03-04 05:37:52 -08001747 * Handles reception for all states except LISTEN, SYN-SENT and ESTABLISHED
Dave Barach68b0fb02017-02-28 15:15:56 -05001748 * as per RFC793 p. 64
1749 */
1750always_inline uword
1751tcp46_rcv_process_inline (vlib_main_t * vm, vlib_node_runtime_t * node,
1752 vlib_frame_t * from_frame, int is_ip4)
1753{
1754 tcp_main_t *tm = vnet_get_tcp_main ();
1755 u32 n_left_from, next_index, *from, *to_next;
Damjan Marion586afd72017-04-05 19:18:20 +02001756 u32 my_thread_index = vm->thread_index, errors = 0;
Dave Barach68b0fb02017-02-28 15:15:56 -05001757
1758 from = vlib_frame_vector_args (from_frame);
1759 n_left_from = from_frame->n_vectors;
1760
1761 next_index = node->cached_next_index;
1762
1763 while (n_left_from > 0)
1764 {
1765 u32 n_left_to_next;
1766
1767 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
1768
1769 while (n_left_from > 0 && n_left_to_next > 0)
1770 {
1771 u32 bi0;
1772 vlib_buffer_t *b0;
1773 tcp_header_t *tcp0 = 0;
1774 tcp_connection_t *tc0;
Dave Barach68b0fb02017-02-28 15:15:56 -05001775 u32 next0 = TCP_RCV_PROCESS_NEXT_DROP, error0 = TCP_ERROR_ENQUEUED;
1776
1777 bi0 = from[0];
1778 to_next[0] = bi0;
1779 from += 1;
1780 to_next += 1;
1781 n_left_from -= 1;
1782 n_left_to_next -= 1;
1783
1784 b0 = vlib_get_buffer (vm, bi0);
1785 tc0 = tcp_connection_get (vnet_buffer (b0)->tcp.connection_index,
1786 my_thread_index);
Florin Corasd79b41e2017-03-04 05:37:52 -08001787 if (PREDICT_FALSE (tc0 == 0))
1788 {
1789 error0 = TCP_ERROR_INVALID_CONNECTION;
1790 goto drop;
1791 }
Dave Barach68b0fb02017-02-28 15:15:56 -05001792
Florin Coras82b13a82017-04-25 11:58:06 -07001793 tcp0 = tcp_buffer_hdr (b0);
Dave Barach68b0fb02017-02-28 15:15:56 -05001794
1795 /* SYNs, FINs and data consume sequence numbers */
1796 vnet_buffer (b0)->tcp.seq_end = vnet_buffer (b0)->tcp.seq_number
Florin Coras82b13a82017-04-25 11:58:06 -07001797 + tcp_is_syn (tcp0) + tcp_is_fin (tcp0)
1798 + vnet_buffer (b0)->tcp.data_len;
Dave Barach68b0fb02017-02-28 15:15:56 -05001799
1800 /*
1801 * Special treatment for CLOSED
1802 */
1803 switch (tc0->state)
1804 {
1805 case TCP_STATE_CLOSED:
1806 goto drop;
1807 break;
1808 }
1809
1810 /*
1811 * For all other states (except LISTEN)
1812 */
1813
1814 /* 1-4: check SEQ, RST, SYN */
1815 if (PREDICT_FALSE
1816 (tcp_segment_validate (vm, tc0, b0, tcp0, &next0)))
1817 {
1818 error0 = TCP_ERROR_SEGMENT_INVALID;
1819 goto drop;
1820 }
1821
1822 /* 5: check the ACK field */
1823 switch (tc0->state)
1824 {
1825 case TCP_STATE_SYN_RCVD:
1826 /*
1827 * If the segment acknowledgment is not acceptable, form a
1828 * reset segment,
1829 * <SEQ=SEG.ACK><CTL=RST>
1830 * and send it.
1831 */
1832 if (!tcp_rcv_ack_is_acceptable (tc0, b0))
1833 {
1834 tcp_send_reset (b0, is_ip4);
1835 goto drop;
1836 }
Florin Coras3af90fc2017-05-03 21:09:42 -07001837
1838 /* Update rtt and rto */
1839 tc0->bytes_acked = 1;
1840 tcp_update_rtt (tc0, vnet_buffer (b0)->tcp.ack_number);
1841
Dave Barach68b0fb02017-02-28 15:15:56 -05001842 /* Switch state to ESTABLISHED */
1843 tc0->state = TCP_STATE_ESTABLISHED;
1844
1845 /* Initialize session variables */
1846 tc0->snd_una = vnet_buffer (b0)->tcp.ack_number;
Florin Corase04c2992017-03-01 08:17:34 -08001847 tc0->snd_wnd = clib_net_to_host_u16 (tcp0->window)
Dave Barach68b0fb02017-02-28 15:15:56 -05001848 << tc0->opt.wscale;
1849 tc0->snd_wl1 = vnet_buffer (b0)->tcp.seq_number;
1850 tc0->snd_wl2 = vnet_buffer (b0)->tcp.ack_number;
1851
1852 /* Shoulder tap the server */
1853 stream_session_accept_notify (&tc0->connection);
1854
Florin Corasd79b41e2017-03-04 05:37:52 -08001855 /* Reset SYN-ACK retransmit timer */
Florin Coras6792ec02017-03-13 03:49:51 -07001856 tcp_retransmit_timer_reset (tc0);
Dave Barach68b0fb02017-02-28 15:15:56 -05001857 break;
1858 case TCP_STATE_ESTABLISHED:
1859 /* We can get packets in established state here because they
1860 * were enqueued before state change */
1861 if (tcp_rcv_ack (tc0, b0, tcp0, &next0, &error0))
1862 goto drop;
1863
1864 break;
1865 case TCP_STATE_FIN_WAIT_1:
1866 /* In addition to the processing for the ESTABLISHED state, if
1867 * our FIN is now acknowledged then enter FIN-WAIT-2 and
1868 * continue processing in that state. */
1869 if (tcp_rcv_ack (tc0, b0, tcp0, &next0, &error0))
1870 goto drop;
Florin Corasd79b41e2017-03-04 05:37:52 -08001871
1872 /* If FIN is ACKed */
1873 if (tc0->snd_una == tc0->snd_una_max)
1874 {
1875 tc0->state = TCP_STATE_FIN_WAIT_2;
1876 /* Stop all timers, 2MSL will be set lower */
1877 tcp_connection_timers_reset (tc0);
1878 }
Dave Barach68b0fb02017-02-28 15:15:56 -05001879 break;
1880 case TCP_STATE_FIN_WAIT_2:
1881 /* In addition to the processing for the ESTABLISHED state, if
1882 * the retransmission queue is empty, the user's CLOSE can be
1883 * acknowledged ("ok") but do not delete the TCB. */
1884 if (tcp_rcv_ack (tc0, b0, tcp0, &next0, &error0))
1885 goto drop;
1886 /* check if rtx queue is empty and ack CLOSE TODO */
1887 break;
1888 case TCP_STATE_CLOSE_WAIT:
1889 /* Do the same processing as for the ESTABLISHED state. */
1890 if (tcp_rcv_ack (tc0, b0, tcp0, &next0, &error0))
1891 goto drop;
1892 break;
1893 case TCP_STATE_CLOSING:
1894 /* In addition to the processing for the ESTABLISHED state, if
1895 * the ACK acknowledges our FIN then enter the TIME-WAIT state,
1896 * otherwise ignore the segment. */
1897 if (tcp_rcv_ack (tc0, b0, tcp0, &next0, &error0))
1898 goto drop;
1899
1900 /* XXX test that send queue empty */
1901 tc0->state = TCP_STATE_TIME_WAIT;
1902 goto drop;
1903
1904 break;
1905 case TCP_STATE_LAST_ACK:
1906 /* The only thing that can arrive in this state is an
1907 * acknowledgment of our FIN. If our FIN is now acknowledged,
1908 * delete the TCB, enter the CLOSED state, and return. */
1909
1910 if (!tcp_rcv_ack_is_acceptable (tc0, b0))
1911 goto drop;
1912
Florin Corasd79b41e2017-03-04 05:37:52 -08001913 tc0->state = TCP_STATE_CLOSED;
1914
1915 /* Don't delete the connection/session yet. Instead, wait a
1916 * reasonable amount of time until the pipes are cleared. In
1917 * particular, this makes sure that we won't have dead sessions
1918 * when processing events on the tx path */
1919 tcp_timer_update (tc0, TCP_TIMER_WAITCLOSE, TCP_CLEANUP_TIME);
1920
1921 /* Stop retransmit */
Florin Coras6792ec02017-03-13 03:49:51 -07001922 tcp_retransmit_timer_reset (tc0);
Florin Corasd79b41e2017-03-04 05:37:52 -08001923
Dave Barach68b0fb02017-02-28 15:15:56 -05001924 goto drop;
1925
1926 break;
1927 case TCP_STATE_TIME_WAIT:
1928 /* The only thing that can arrive in this state is a
1929 * retransmission of the remote FIN. Acknowledge it, and restart
1930 * the 2 MSL timeout. */
1931
1932 /* TODO */
1933 goto drop;
1934 break;
1935 default:
1936 ASSERT (0);
1937 }
1938
1939 /* 6: check the URG bit TODO */
1940
1941 /* 7: process the segment text */
1942 switch (tc0->state)
1943 {
1944 case TCP_STATE_ESTABLISHED:
1945 case TCP_STATE_FIN_WAIT_1:
1946 case TCP_STATE_FIN_WAIT_2:
Florin Coras82b13a82017-04-25 11:58:06 -07001947 vlib_buffer_advance (b0, vnet_buffer (b0)->tcp.data_offset);
1948 error0 = tcp_segment_rcv (tm, tc0, b0,
1949 vnet_buffer (b0)->tcp.data_len,
1950 &next0);
Dave Barach68b0fb02017-02-28 15:15:56 -05001951 break;
1952 case TCP_STATE_CLOSE_WAIT:
1953 case TCP_STATE_CLOSING:
1954 case TCP_STATE_LAST_ACK:
1955 case TCP_STATE_TIME_WAIT:
1956 /* This should not occur, since a FIN has been received from the
1957 * remote side. Ignore the segment text. */
1958 break;
1959 }
1960
1961 /* 8: check the FIN bit */
1962 if (!tcp_fin (tcp0))
1963 goto drop;
1964
1965 switch (tc0->state)
1966 {
1967 case TCP_STATE_ESTABLISHED:
1968 case TCP_STATE_SYN_RCVD:
1969 /* Send FIN-ACK notify app and enter CLOSE-WAIT */
1970 tcp_connection_timers_reset (tc0);
Florin Corasd79b41e2017-03-04 05:37:52 -08001971 tcp_make_fin (tc0, b0);
Dave Barach68b0fb02017-02-28 15:15:56 -05001972 next0 = tcp_next_output (tc0->c_is_ip4);
1973 stream_session_disconnect_notify (&tc0->connection);
1974 tc0->state = TCP_STATE_CLOSE_WAIT;
1975 break;
1976 case TCP_STATE_CLOSE_WAIT:
1977 case TCP_STATE_CLOSING:
1978 case TCP_STATE_LAST_ACK:
1979 /* move along .. */
1980 break;
1981 case TCP_STATE_FIN_WAIT_1:
1982 tc0->state = TCP_STATE_TIME_WAIT;
1983 tcp_connection_timers_reset (tc0);
Florin Corasd79b41e2017-03-04 05:37:52 -08001984 tcp_timer_set (tc0, TCP_TIMER_WAITCLOSE, TCP_2MSL_TIME);
Dave Barach68b0fb02017-02-28 15:15:56 -05001985 break;
1986 case TCP_STATE_FIN_WAIT_2:
1987 /* Got FIN, send ACK! */
1988 tc0->state = TCP_STATE_TIME_WAIT;
Florin Corasdb84e572017-05-09 18:54:52 -07001989 tcp_connection_timers_reset (tc0);
Florin Corase69f4952017-03-07 10:06:24 -08001990 tcp_timer_set (tc0, TCP_TIMER_WAITCLOSE, TCP_CLOSEWAIT_TIME);
Dave Barach68b0fb02017-02-28 15:15:56 -05001991 tcp_make_ack (tc0, b0);
1992 next0 = tcp_next_output (is_ip4);
1993 break;
1994 case TCP_STATE_TIME_WAIT:
1995 /* Remain in the TIME-WAIT state. Restart the 2 MSL time-wait
1996 * timeout.
1997 */
Florin Corasd79b41e2017-03-04 05:37:52 -08001998 tcp_timer_update (tc0, TCP_TIMER_WAITCLOSE, TCP_2MSL_TIME);
Dave Barach68b0fb02017-02-28 15:15:56 -05001999 break;
2000 }
Florin Corase69f4952017-03-07 10:06:24 -08002001 TCP_EVT_DBG (TCP_EVT_FIN_RCVD, tc0);
Dave Barach68b0fb02017-02-28 15:15:56 -05002002
Florin Coras82b13a82017-04-25 11:58:06 -07002003 drop:
Dave Barach68b0fb02017-02-28 15:15:56 -05002004 b0->error = error0 ? node->errors[error0] : 0;
2005
Dave Barach68b0fb02017-02-28 15:15:56 -05002006 if (PREDICT_FALSE (b0->flags & VLIB_BUFFER_IS_TRACED))
2007 {
Florin Coras82b13a82017-04-25 11:58:06 -07002008 tcp_rx_trace_t *t0 =
2009 vlib_add_trace (vm, node, b0, sizeof (*t0));
2010 tcp_set_rx_trace_data (t0, tc0, tcp0, b0, is_ip4);
Dave Barach68b0fb02017-02-28 15:15:56 -05002011 }
2012
2013 vlib_validate_buffer_enqueue_x1 (vm, node, next_index, to_next,
2014 n_left_to_next, bi0, next0);
2015 }
2016
2017 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
2018 }
2019
2020 errors = session_manager_flush_enqueue_events (my_thread_index);
2021 if (errors)
2022 {
2023 if (is_ip4)
2024 vlib_node_increment_counter (vm, tcp4_established_node.index,
2025 TCP_ERROR_EVENT_FIFO_FULL, errors);
2026 else
2027 vlib_node_increment_counter (vm, tcp6_established_node.index,
2028 TCP_ERROR_EVENT_FIFO_FULL, errors);
2029 }
2030
2031 return from_frame->n_vectors;
2032}
2033
2034static uword
2035tcp4_rcv_process (vlib_main_t * vm, vlib_node_runtime_t * node,
2036 vlib_frame_t * from_frame)
2037{
2038 return tcp46_rcv_process_inline (vm, node, from_frame, 1 /* is_ip4 */ );
2039}
2040
2041static uword
2042tcp6_rcv_process (vlib_main_t * vm, vlib_node_runtime_t * node,
2043 vlib_frame_t * from_frame)
2044{
2045 return tcp46_rcv_process_inline (vm, node, from_frame, 0 /* is_ip4 */ );
2046}
2047
2048/* *INDENT-OFF* */
2049VLIB_REGISTER_NODE (tcp4_rcv_process_node) =
2050{
2051 .function = tcp4_rcv_process,
2052 .name = "tcp4-rcv-process",
2053 /* Takes a vector of packets. */
2054 .vector_size = sizeof (u32),
2055 .n_errors = TCP_N_ERROR,
2056 .error_strings = tcp_error_strings,
2057 .n_next_nodes = TCP_RCV_PROCESS_N_NEXT,
2058 .next_nodes =
2059 {
2060#define _(s,n) [TCP_RCV_PROCESS_NEXT_##s] = n,
2061 foreach_tcp_state_next
2062#undef _
2063 },
Clement Durand6cf260c2017-04-13 13:27:04 +02002064 .format_trace = format_tcp_rx_trace_short,
Dave Barach68b0fb02017-02-28 15:15:56 -05002065};
2066/* *INDENT-ON* */
2067
2068VLIB_NODE_FUNCTION_MULTIARCH (tcp4_rcv_process_node, tcp4_rcv_process);
2069
2070/* *INDENT-OFF* */
2071VLIB_REGISTER_NODE (tcp6_rcv_process_node) =
2072{
2073 .function = tcp6_rcv_process,
2074 .name = "tcp6-rcv-process",
2075 /* Takes a vector of packets. */
2076 .vector_size = sizeof (u32),
2077 .n_errors = TCP_N_ERROR,
2078 .error_strings = tcp_error_strings,
2079 .n_next_nodes = TCP_RCV_PROCESS_N_NEXT,
2080 .next_nodes =
2081 {
2082#define _(s,n) [TCP_RCV_PROCESS_NEXT_##s] = n,
2083 foreach_tcp_state_next
2084#undef _
2085 },
Clement Durand6cf260c2017-04-13 13:27:04 +02002086 .format_trace = format_tcp_rx_trace_short,
Dave Barach68b0fb02017-02-28 15:15:56 -05002087};
2088/* *INDENT-ON* */
2089
2090VLIB_NODE_FUNCTION_MULTIARCH (tcp6_rcv_process_node, tcp6_rcv_process);
2091
2092vlib_node_registration_t tcp4_listen_node;
2093vlib_node_registration_t tcp6_listen_node;
2094
2095/**
2096 * LISTEN state processing as per RFC 793 p. 65
2097 */
2098always_inline uword
2099tcp46_listen_inline (vlib_main_t * vm, vlib_node_runtime_t * node,
2100 vlib_frame_t * from_frame, int is_ip4)
2101{
2102 u32 n_left_from, next_index, *from, *to_next;
Damjan Marion586afd72017-04-05 19:18:20 +02002103 u32 my_thread_index = vm->thread_index;
Dave Barach68b0fb02017-02-28 15:15:56 -05002104 tcp_main_t *tm = vnet_get_tcp_main ();
2105 u8 sst = is_ip4 ? SESSION_TYPE_IP4_TCP : SESSION_TYPE_IP6_TCP;
2106
2107 from = vlib_frame_vector_args (from_frame);
2108 n_left_from = from_frame->n_vectors;
2109
2110 next_index = node->cached_next_index;
2111
2112 while (n_left_from > 0)
2113 {
2114 u32 n_left_to_next;
2115
2116 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
2117
2118 while (n_left_from > 0 && n_left_to_next > 0)
2119 {
2120 u32 bi0;
2121 vlib_buffer_t *b0;
Clement Durand6cf260c2017-04-13 13:27:04 +02002122 tcp_rx_trace_t *t0;
Dave Barach68b0fb02017-02-28 15:15:56 -05002123 tcp_header_t *th0 = 0;
2124 tcp_connection_t *lc0;
2125 ip4_header_t *ip40;
2126 ip6_header_t *ip60;
2127 tcp_connection_t *child0;
2128 u32 error0 = TCP_ERROR_SYNS_RCVD, next0 = TCP_LISTEN_NEXT_DROP;
2129
2130 bi0 = from[0];
2131 to_next[0] = bi0;
2132 from += 1;
2133 to_next += 1;
2134 n_left_from -= 1;
2135 n_left_to_next -= 1;
2136
2137 b0 = vlib_get_buffer (vm, bi0);
2138 lc0 = tcp_listener_get (vnet_buffer (b0)->tcp.connection_index);
2139
2140 if (is_ip4)
2141 {
2142 ip40 = vlib_buffer_get_current (b0);
2143 th0 = ip4_next_header (ip40);
2144 }
2145 else
2146 {
2147 ip60 = vlib_buffer_get_current (b0);
2148 th0 = ip6_next_header (ip60);
2149 }
2150
2151 /* Create child session. For syn-flood protection use filter */
2152
Florin Corasdc629cd2017-05-09 00:52:37 -07002153 /* 1. first check for an RST: handled in dispatch */
2154 /* if (tcp_rst (th0))
2155 goto drop; */
Dave Barach68b0fb02017-02-28 15:15:56 -05002156
Florin Corasdc629cd2017-05-09 00:52:37 -07002157 /* 2. second check for an ACK: handled in dispatch */
2158 /* if (tcp_ack (th0))
2159 {
2160 tcp_send_reset (b0, is_ip4);
2161 goto drop;
2162 } */
Dave Barach68b0fb02017-02-28 15:15:56 -05002163
2164 /* 3. check for a SYN (did that already) */
2165
2166 /* Create child session and send SYN-ACK */
2167 pool_get (tm->connections[my_thread_index], child0);
2168 memset (child0, 0, sizeof (*child0));
2169
2170 child0->c_c_index = child0 - tm->connections[my_thread_index];
2171 child0->c_lcl_port = lc0->c_lcl_port;
2172 child0->c_rmt_port = th0->src_port;
2173 child0->c_is_ip4 = is_ip4;
2174 child0->c_thread_index = my_thread_index;
Florin Corasbb292f42017-05-19 09:49:19 -07002175 child0->state = TCP_STATE_SYN_RCVD;
Dave Barach68b0fb02017-02-28 15:15:56 -05002176
2177 if (is_ip4)
2178 {
2179 child0->c_lcl_ip4.as_u32 = ip40->dst_address.as_u32;
2180 child0->c_rmt_ip4.as_u32 = ip40->src_address.as_u32;
2181 }
2182 else
2183 {
2184 clib_memcpy (&child0->c_lcl_ip6, &ip60->dst_address,
2185 sizeof (ip6_address_t));
2186 clib_memcpy (&child0->c_rmt_ip6, &ip60->src_address,
2187 sizeof (ip6_address_t));
2188 }
2189
2190 if (stream_session_accept (&child0->connection, lc0->c_s_index, sst,
2191 0 /* notify */ ))
2192 {
2193 error0 = TCP_ERROR_CREATE_SESSION_FAIL;
2194 goto drop;
2195 }
2196
Florin Corasdb84e572017-05-09 18:54:52 -07002197 if (tcp_options_parse (th0, &child0->opt))
2198 {
2199 goto drop;
2200 }
Dave Barach68b0fb02017-02-28 15:15:56 -05002201
2202 child0->irs = vnet_buffer (b0)->tcp.seq_number;
2203 child0->rcv_nxt = vnet_buffer (b0)->tcp.seq_number + 1;
Florin Coras3e350af2017-03-30 02:54:28 -07002204 child0->rcv_las = child0->rcv_nxt;
Dave Barach68b0fb02017-02-28 15:15:56 -05002205
2206 /* RFC1323: TSval timestamps sent on {SYN} and {SYN,ACK}
2207 * segments are used to initialize PAWS. */
2208 if (tcp_opts_tstamp (&child0->opt))
2209 {
2210 child0->tsval_recent = child0->opt.tsval;
2211 child0->tsval_recent_age = tcp_time_now ();
2212 }
2213
Florin Corase04c2992017-03-01 08:17:34 -08002214 if (tcp_opts_wscale (&child0->opt))
2215 child0->snd_wscale = child0->opt.wscale;
2216
2217 /* No scaling */
2218 child0->snd_wnd = clib_net_to_host_u16 (th0->window);
2219 child0->snd_wl1 = vnet_buffer (b0)->tcp.seq_number;
2220 child0->snd_wl2 = vnet_buffer (b0)->tcp.ack_number;
2221
2222 tcp_connection_init_vars (child0);
2223
Florin Corase69f4952017-03-07 10:06:24 -08002224 TCP_EVT_DBG (TCP_EVT_SYN_RCVD, child0);
2225
Dave Barach68b0fb02017-02-28 15:15:56 -05002226 /* Reuse buffer to make syn-ack and send */
2227 tcp_make_synack (child0, b0);
2228 next0 = tcp_next_output (is_ip4);
2229
Florin Corasc28764f2017-04-26 00:08:42 -07002230 /* Init fifo pointers after we have iss */
2231 stream_session_init_fifos_pointers (&child0->connection,
2232 child0->irs + 1,
2233 child0->iss + 1);
Dave Barach68b0fb02017-02-28 15:15:56 -05002234 drop:
2235 if (PREDICT_FALSE (b0->flags & VLIB_BUFFER_IS_TRACED))
2236 {
Clement Durand6cf260c2017-04-13 13:27:04 +02002237 t0 = vlib_add_trace (vm, node, b0, sizeof (*t0));
2238 clib_memcpy (&t0->tcp_header, th0, sizeof (t0->tcp_header));
2239 clib_memcpy (&t0->tcp_connection, lc0,
2240 sizeof (t0->tcp_connection));
Dave Barach68b0fb02017-02-28 15:15:56 -05002241 }
2242
Florin Corase04c2992017-03-01 08:17:34 -08002243 b0->error = node->errors[error0];
Dave Barach68b0fb02017-02-28 15:15:56 -05002244
2245 vlib_validate_buffer_enqueue_x1 (vm, node, next_index, to_next,
2246 n_left_to_next, bi0, next0);
2247 }
2248
2249 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
2250 }
2251 return from_frame->n_vectors;
2252}
2253
2254static uword
2255tcp4_listen (vlib_main_t * vm, vlib_node_runtime_t * node,
2256 vlib_frame_t * from_frame)
2257{
2258 return tcp46_listen_inline (vm, node, from_frame, 1 /* is_ip4 */ );
2259}
2260
2261static uword
2262tcp6_listen (vlib_main_t * vm, vlib_node_runtime_t * node,
2263 vlib_frame_t * from_frame)
2264{
2265 return tcp46_listen_inline (vm, node, from_frame, 0 /* is_ip4 */ );
2266}
2267
2268/* *INDENT-OFF* */
2269VLIB_REGISTER_NODE (tcp4_listen_node) =
2270{
2271 .function = tcp4_listen,
2272 .name = "tcp4-listen",
2273 /* Takes a vector of packets. */
2274 .vector_size = sizeof (u32),
2275 .n_errors = TCP_N_ERROR,
2276 .error_strings = tcp_error_strings,
2277 .n_next_nodes = TCP_LISTEN_N_NEXT,
2278 .next_nodes =
2279 {
2280#define _(s,n) [TCP_LISTEN_NEXT_##s] = n,
2281 foreach_tcp_state_next
2282#undef _
2283 },
Clement Durand6cf260c2017-04-13 13:27:04 +02002284 .format_trace = format_tcp_rx_trace_short,
Dave Barach68b0fb02017-02-28 15:15:56 -05002285};
2286/* *INDENT-ON* */
2287
2288VLIB_NODE_FUNCTION_MULTIARCH (tcp4_listen_node, tcp4_listen);
2289
2290/* *INDENT-OFF* */
2291VLIB_REGISTER_NODE (tcp6_listen_node) =
2292{
2293 .function = tcp6_listen,
2294 .name = "tcp6-listen",
2295 /* Takes a vector of packets. */
2296 .vector_size = sizeof (u32),
2297 .n_errors = TCP_N_ERROR,
2298 .error_strings = tcp_error_strings,
2299 .n_next_nodes = TCP_LISTEN_N_NEXT,
2300 .next_nodes =
2301 {
2302#define _(s,n) [TCP_LISTEN_NEXT_##s] = n,
2303 foreach_tcp_state_next
2304#undef _
2305 },
Clement Durand6cf260c2017-04-13 13:27:04 +02002306 .format_trace = format_tcp_rx_trace_short,
Dave Barach68b0fb02017-02-28 15:15:56 -05002307};
2308/* *INDENT-ON* */
2309
2310VLIB_NODE_FUNCTION_MULTIARCH (tcp6_listen_node, tcp6_listen);
2311
2312vlib_node_registration_t tcp4_input_node;
2313vlib_node_registration_t tcp6_input_node;
2314
2315typedef enum _tcp_input_next
2316{
2317 TCP_INPUT_NEXT_DROP,
2318 TCP_INPUT_NEXT_LISTEN,
2319 TCP_INPUT_NEXT_RCV_PROCESS,
2320 TCP_INPUT_NEXT_SYN_SENT,
2321 TCP_INPUT_NEXT_ESTABLISHED,
2322 TCP_INPUT_NEXT_RESET,
2323 TCP_INPUT_N_NEXT
2324} tcp_input_next_t;
2325
2326#define foreach_tcp4_input_next \
2327 _ (DROP, "error-drop") \
2328 _ (LISTEN, "tcp4-listen") \
2329 _ (RCV_PROCESS, "tcp4-rcv-process") \
2330 _ (SYN_SENT, "tcp4-syn-sent") \
2331 _ (ESTABLISHED, "tcp4-established") \
2332 _ (RESET, "tcp4-reset")
2333
2334#define foreach_tcp6_input_next \
2335 _ (DROP, "error-drop") \
2336 _ (LISTEN, "tcp6-listen") \
2337 _ (RCV_PROCESS, "tcp6-rcv-process") \
2338 _ (SYN_SENT, "tcp6-syn-sent") \
2339 _ (ESTABLISHED, "tcp6-established") \
2340 _ (RESET, "tcp6-reset")
2341
Dave Barach68b0fb02017-02-28 15:15:56 -05002342#define filter_flags (TCP_FLAG_SYN|TCP_FLAG_ACK|TCP_FLAG_RST|TCP_FLAG_FIN)
2343
2344always_inline uword
2345tcp46_input_inline (vlib_main_t * vm, vlib_node_runtime_t * node,
2346 vlib_frame_t * from_frame, int is_ip4)
2347{
2348 u32 n_left_from, next_index, *from, *to_next;
Damjan Marion586afd72017-04-05 19:18:20 +02002349 u32 my_thread_index = vm->thread_index;
Dave Barach68b0fb02017-02-28 15:15:56 -05002350 tcp_main_t *tm = vnet_get_tcp_main ();
Dave Barach68b0fb02017-02-28 15:15:56 -05002351
2352 from = vlib_frame_vector_args (from_frame);
2353 n_left_from = from_frame->n_vectors;
2354
2355 next_index = node->cached_next_index;
2356
2357 while (n_left_from > 0)
2358 {
2359 u32 n_left_to_next;
2360
2361 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
2362
2363 while (n_left_from > 0 && n_left_to_next > 0)
2364 {
Florin Coras82b13a82017-04-25 11:58:06 -07002365 int n_advance_bytes0, n_data_bytes0;
Dave Barach68b0fb02017-02-28 15:15:56 -05002366 u32 bi0;
2367 vlib_buffer_t *b0;
2368 tcp_header_t *tcp0 = 0;
2369 tcp_connection_t *tc0;
2370 ip4_header_t *ip40;
2371 ip6_header_t *ip60;
2372 u32 error0 = TCP_ERROR_NO_LISTENER, next0 = TCP_INPUT_NEXT_DROP;
2373 u8 flags0;
2374
2375 bi0 = from[0];
2376 to_next[0] = bi0;
2377 from += 1;
2378 to_next += 1;
2379 n_left_from -= 1;
2380 n_left_to_next -= 1;
2381
2382 b0 = vlib_get_buffer (vm, bi0);
Florin Corasd79b41e2017-03-04 05:37:52 -08002383 vnet_buffer (b0)->tcp.flags = 0;
Dave Barach68b0fb02017-02-28 15:15:56 -05002384
Florin Coras82b13a82017-04-25 11:58:06 -07002385 /* Checksum computed by ipx_local no need to compute again */
2386
Dave Barach68b0fb02017-02-28 15:15:56 -05002387 if (is_ip4)
2388 {
2389 ip40 = vlib_buffer_get_current (b0);
2390 tcp0 = ip4_next_header (ip40);
Florin Coras82b13a82017-04-25 11:58:06 -07002391 n_advance_bytes0 = (ip4_header_bytes (ip40)
2392 + tcp_header_bytes (tcp0));
2393 n_data_bytes0 = clib_net_to_host_u16 (ip40->length)
2394 - n_advance_bytes0;
Dave Barach68b0fb02017-02-28 15:15:56 -05002395
2396 /* lookup session */
2397 tc0 =
Florin Corase04c2992017-03-01 08:17:34 -08002398 (tcp_connection_t *)
2399 stream_session_lookup_transport4 (&ip40->dst_address,
2400 &ip40->src_address,
2401 tcp0->dst_port,
2402 tcp0->src_port,
2403 SESSION_TYPE_IP4_TCP,
2404 my_thread_index);
Dave Barach68b0fb02017-02-28 15:15:56 -05002405 }
2406 else
2407 {
2408 ip60 = vlib_buffer_get_current (b0);
2409 tcp0 = ip6_next_header (ip60);
Florin Coras82b13a82017-04-25 11:58:06 -07002410 n_advance_bytes0 = tcp_header_bytes (tcp0);
2411 n_data_bytes0 = clib_net_to_host_u16 (ip60->payload_length)
2412 - n_advance_bytes0;
2413 n_advance_bytes0 += sizeof (ip60[0]);
2414
Dave Barach68b0fb02017-02-28 15:15:56 -05002415 tc0 =
Florin Corase04c2992017-03-01 08:17:34 -08002416 (tcp_connection_t *)
2417 stream_session_lookup_transport6 (&ip60->src_address,
2418 &ip60->dst_address,
2419 tcp0->src_port,
2420 tcp0->dst_port,
2421 SESSION_TYPE_IP6_TCP,
2422 my_thread_index);
Dave Barach68b0fb02017-02-28 15:15:56 -05002423 }
2424
Florin Coras82b13a82017-04-25 11:58:06 -07002425 /* Length check */
2426 if (PREDICT_FALSE (n_advance_bytes0 < 0))
2427 {
2428 error0 = TCP_ERROR_LENGTH;
2429 goto done;
2430 }
2431
Dave Barach68b0fb02017-02-28 15:15:56 -05002432 /* Session exists */
2433 if (PREDICT_TRUE (0 != tc0))
2434 {
2435 /* Save connection index */
2436 vnet_buffer (b0)->tcp.connection_index = tc0->c_c_index;
2437 vnet_buffer (b0)->tcp.seq_number =
2438 clib_net_to_host_u32 (tcp0->seq_number);
2439 vnet_buffer (b0)->tcp.ack_number =
2440 clib_net_to_host_u32 (tcp0->ack_number);
2441
Florin Coras82b13a82017-04-25 11:58:06 -07002442 vnet_buffer (b0)->tcp.hdr_offset = (u8 *) tcp0
2443 - (u8 *) vlib_buffer_get_current (b0);
2444 vnet_buffer (b0)->tcp.data_offset = n_advance_bytes0;
2445 vnet_buffer (b0)->tcp.data_len = n_data_bytes0;
2446
Dave Barach68b0fb02017-02-28 15:15:56 -05002447 flags0 = tcp0->flags & filter_flags;
2448 next0 = tm->dispatch_table[tc0->state][flags0].next;
2449 error0 = tm->dispatch_table[tc0->state][flags0].error;
2450
Florin Corasdc629cd2017-05-09 00:52:37 -07002451 if (PREDICT_FALSE (error0 == TCP_ERROR_DISPATCH
2452 || next0 == TCP_INPUT_NEXT_RESET))
Dave Barach68b0fb02017-02-28 15:15:56 -05002453 {
2454 /* Overload tcp flags to store state */
Florin Corasdc629cd2017-05-09 00:52:37 -07002455 tcp_state_t state0 = tc0->state;
Dave Barach68b0fb02017-02-28 15:15:56 -05002456 vnet_buffer (b0)->tcp.flags = tc0->state;
Florin Corasdc629cd2017-05-09 00:52:37 -07002457
2458 if (error0 == TCP_ERROR_DISPATCH)
2459 clib_warning ("disp error state %U flags %U",
Florin Corasbb292f42017-05-19 09:49:19 -07002460 format_tcp_state, state0, format_tcp_flags,
Florin Corasdc629cd2017-05-09 00:52:37 -07002461 (int) flags0);
Dave Barach68b0fb02017-02-28 15:15:56 -05002462 }
2463 }
2464 else
2465 {
2466 /* Send reset */
2467 next0 = TCP_INPUT_NEXT_RESET;
2468 error0 = TCP_ERROR_NO_LISTENER;
Dave Barach68b0fb02017-02-28 15:15:56 -05002469 }
2470
Florin Coras82b13a82017-04-25 11:58:06 -07002471 done:
Dave Barach68b0fb02017-02-28 15:15:56 -05002472 b0->error = error0 ? node->errors[error0] : 0;
2473
2474 if (PREDICT_FALSE (b0->flags & VLIB_BUFFER_IS_TRACED))
2475 {
Florin Coras82b13a82017-04-25 11:58:06 -07002476 tcp_rx_trace_t *t0 =
2477 vlib_add_trace (vm, node, b0, sizeof (*t0));
2478 tcp_set_rx_trace_data (t0, tc0, tcp0, b0, is_ip4);
Dave Barach68b0fb02017-02-28 15:15:56 -05002479 }
2480
2481 vlib_validate_buffer_enqueue_x1 (vm, node, next_index, to_next,
2482 n_left_to_next, bi0, next0);
2483 }
2484
2485 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
2486 }
2487
2488 return from_frame->n_vectors;
2489}
2490
2491static uword
2492tcp4_input (vlib_main_t * vm, vlib_node_runtime_t * node,
2493 vlib_frame_t * from_frame)
2494{
2495 return tcp46_input_inline (vm, node, from_frame, 1 /* is_ip4 */ );
2496}
2497
2498static uword
2499tcp6_input (vlib_main_t * vm, vlib_node_runtime_t * node,
2500 vlib_frame_t * from_frame)
2501{
2502 return tcp46_input_inline (vm, node, from_frame, 0 /* is_ip4 */ );
2503}
2504
2505/* *INDENT-OFF* */
2506VLIB_REGISTER_NODE (tcp4_input_node) =
2507{
2508 .function = tcp4_input,
2509 .name = "tcp4-input",
2510 /* Takes a vector of packets. */
2511 .vector_size = sizeof (u32),
2512 .n_errors = TCP_N_ERROR,
2513 .error_strings = tcp_error_strings,
2514 .n_next_nodes = TCP_INPUT_N_NEXT,
2515 .next_nodes =
2516 {
2517#define _(s,n) [TCP_INPUT_NEXT_##s] = n,
2518 foreach_tcp4_input_next
2519#undef _
2520 },
2521 .format_buffer = format_tcp_header,
2522 .format_trace = format_tcp_rx_trace,
2523};
2524/* *INDENT-ON* */
2525
2526VLIB_NODE_FUNCTION_MULTIARCH (tcp4_input_node, tcp4_input);
2527
2528/* *INDENT-OFF* */
2529VLIB_REGISTER_NODE (tcp6_input_node) =
2530{
2531 .function = tcp6_input,
2532 .name = "tcp6-input",
2533 /* Takes a vector of packets. */
2534 .vector_size = sizeof (u32),
2535 .n_errors = TCP_N_ERROR,
2536 .error_strings = tcp_error_strings,
2537 .n_next_nodes = TCP_INPUT_N_NEXT,
2538 .next_nodes =
2539 {
2540#define _(s,n) [TCP_INPUT_NEXT_##s] = n,
2541 foreach_tcp6_input_next
2542#undef _
2543 },
2544 .format_buffer = format_tcp_header,
2545 .format_trace = format_tcp_rx_trace,
2546};
2547/* *INDENT-ON* */
2548
2549VLIB_NODE_FUNCTION_MULTIARCH (tcp6_input_node, tcp6_input);
Dave Barach68b0fb02017-02-28 15:15:56 -05002550
2551static void
2552tcp_dispatch_table_init (tcp_main_t * tm)
2553{
2554 int i, j;
2555 for (i = 0; i < ARRAY_LEN (tm->dispatch_table); i++)
2556 for (j = 0; j < ARRAY_LEN (tm->dispatch_table[i]); j++)
2557 {
2558 tm->dispatch_table[i][j].next = TCP_INPUT_NEXT_DROP;
2559 tm->dispatch_table[i][j].error = TCP_ERROR_DISPATCH;
2560 }
2561
2562#define _(t,f,n,e) \
2563do { \
2564 tm->dispatch_table[TCP_STATE_##t][f].next = (n); \
2565 tm->dispatch_table[TCP_STATE_##t][f].error = (e); \
2566} while (0)
2567
2568 /* SYNs for new connections -> tcp-listen. */
2569 _(LISTEN, TCP_FLAG_SYN, TCP_INPUT_NEXT_LISTEN, TCP_ERROR_NONE);
Florin Corasdc629cd2017-05-09 00:52:37 -07002570 _(LISTEN, TCP_FLAG_ACK, TCP_INPUT_NEXT_RESET, TCP_ERROR_NONE);
2571 _(LISTEN, TCP_FLAG_RST, TCP_INPUT_NEXT_DROP, TCP_ERROR_NONE);
Dave Barach68b0fb02017-02-28 15:15:56 -05002572 /* ACK for for a SYN-ACK -> tcp-rcv-process. */
2573 _(SYN_RCVD, TCP_FLAG_ACK, TCP_INPUT_NEXT_RCV_PROCESS, TCP_ERROR_NONE);
Florin Corasc28764f2017-04-26 00:08:42 -07002574 _(SYN_RCVD, TCP_FLAG_RST, TCP_INPUT_NEXT_RCV_PROCESS, TCP_ERROR_NONE);
Dave Barach68b0fb02017-02-28 15:15:56 -05002575 /* SYN-ACK for a SYN */
2576 _(SYN_SENT, TCP_FLAG_SYN | TCP_FLAG_ACK, TCP_INPUT_NEXT_SYN_SENT,
2577 TCP_ERROR_NONE);
2578 _(SYN_SENT, TCP_FLAG_ACK, TCP_INPUT_NEXT_SYN_SENT, TCP_ERROR_NONE);
2579 _(SYN_SENT, TCP_FLAG_RST, TCP_INPUT_NEXT_SYN_SENT, TCP_ERROR_NONE);
2580 _(SYN_SENT, TCP_FLAG_RST | TCP_FLAG_ACK, TCP_INPUT_NEXT_SYN_SENT,
2581 TCP_ERROR_NONE);
2582 /* ACK for for established connection -> tcp-established. */
2583 _(ESTABLISHED, TCP_FLAG_ACK, TCP_INPUT_NEXT_ESTABLISHED, TCP_ERROR_NONE);
2584 /* FIN for for established connection -> tcp-established. */
2585 _(ESTABLISHED, TCP_FLAG_FIN, TCP_INPUT_NEXT_ESTABLISHED, TCP_ERROR_NONE);
2586 _(ESTABLISHED, TCP_FLAG_FIN | TCP_FLAG_ACK, TCP_INPUT_NEXT_ESTABLISHED,
2587 TCP_ERROR_NONE);
Florin Corasd79b41e2017-03-04 05:37:52 -08002588 _(ESTABLISHED, TCP_FLAG_RST, TCP_INPUT_NEXT_ESTABLISHED, TCP_ERROR_NONE);
Florin Corasdc629cd2017-05-09 00:52:37 -07002589 _(ESTABLISHED, TCP_FLAG_RST | TCP_FLAG_ACK, TCP_INPUT_NEXT_ESTABLISHED,
2590 TCP_ERROR_NONE);
Dave Barach68b0fb02017-02-28 15:15:56 -05002591 /* ACK or FIN-ACK to our FIN */
2592 _(FIN_WAIT_1, TCP_FLAG_ACK, TCP_INPUT_NEXT_RCV_PROCESS, TCP_ERROR_NONE);
2593 _(FIN_WAIT_1, TCP_FLAG_ACK | TCP_FLAG_FIN, TCP_INPUT_NEXT_RCV_PROCESS,
2594 TCP_ERROR_NONE);
2595 /* FIN in reply to our FIN from the other side */
2596 _(FIN_WAIT_1, TCP_FLAG_FIN, TCP_INPUT_NEXT_RCV_PROCESS, TCP_ERROR_NONE);
2597 /* FIN confirming that the peer (app) has closed */
2598 _(FIN_WAIT_2, TCP_FLAG_FIN, TCP_INPUT_NEXT_RCV_PROCESS, TCP_ERROR_NONE);
Florin Corasf6d68ed2017-05-07 19:12:02 -07002599 _(FIN_WAIT_2, TCP_FLAG_ACK, TCP_INPUT_NEXT_RCV_PROCESS, TCP_ERROR_NONE);
Dave Barach68b0fb02017-02-28 15:15:56 -05002600 _(FIN_WAIT_2, TCP_FLAG_FIN | TCP_FLAG_ACK, TCP_INPUT_NEXT_RCV_PROCESS,
2601 TCP_ERROR_NONE);
2602 _(LAST_ACK, TCP_FLAG_ACK, TCP_INPUT_NEXT_RCV_PROCESS, TCP_ERROR_NONE);
Florin Corasdb84e572017-05-09 18:54:52 -07002603 _(LAST_ACK, TCP_FLAG_RST, TCP_INPUT_NEXT_RCV_PROCESS, TCP_ERROR_NONE);
Florin Corasdc629cd2017-05-09 00:52:37 -07002604 _(CLOSED, TCP_FLAG_ACK, TCP_INPUT_NEXT_RESET, TCP_ERROR_CONNECTION_CLOSED);
2605 _(CLOSED, TCP_FLAG_RST, TCP_INPUT_NEXT_DROP, TCP_ERROR_CONNECTION_CLOSED);
Dave Barach68b0fb02017-02-28 15:15:56 -05002606#undef _
2607}
2608
2609clib_error_t *
2610tcp_input_init (vlib_main_t * vm)
2611{
2612 clib_error_t *error = 0;
2613 tcp_main_t *tm = vnet_get_tcp_main ();
2614
2615 if ((error = vlib_call_init_function (vm, tcp_init)))
2616 return error;
2617
2618 /* Initialize dispatch table. */
2619 tcp_dispatch_table_init (tm);
2620
2621 return error;
2622}
2623
2624VLIB_INIT_FUNCTION (tcp_input_init);
2625
2626/*
2627 * fd.io coding-style-patch-verification: ON
2628 *
2629 * Local Variables:
2630 * eval: (c-set-style "gnu")
2631 * End:
2632 */