blob: ddee41e0b9e92d0f3ed60b87dbc931710ed76a03 [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;
536 u32 blk_index = 0, old_sacked_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;
542
543 if (!tcp_opts_sack (&tc->opt) && sb->head == TCP_INVALID_SACK_HOLE_INDEX)
Dave Barach68b0fb02017-02-28 15:15:56 -0500544 return;
545
546 /* Remove invalid blocks */
Florin Coras6792ec02017-03-13 03:49:51 -0700547 blk = tc->opt.sacks;
548 while (blk < vec_end (tc->opt.sacks))
549 {
550 if (seq_lt (blk->start, blk->end)
551 && seq_gt (blk->start, tc->snd_una)
552 && seq_gt (blk->start, ack) && seq_leq (blk->end, tc->snd_nxt))
553 {
554 blk++;
555 continue;
556 }
557 vec_del1 (tc->opt.sacks, blk - tc->opt.sacks);
558 }
Dave Barach68b0fb02017-02-28 15:15:56 -0500559
560 /* Add block for cumulative ack */
561 if (seq_gt (ack, tc->snd_una))
562 {
563 tmp.start = tc->snd_una;
564 tmp.end = ack;
565 vec_add1 (tc->opt.sacks, tmp);
566 }
567
568 if (vec_len (tc->opt.sacks) == 0)
569 return;
570
571 /* Make sure blocks are ordered */
572 for (i = 0; i < vec_len (tc->opt.sacks); i++)
Florin Coras6792ec02017-03-13 03:49:51 -0700573 for (j = i + 1; j < vec_len (tc->opt.sacks); j++)
Dave Barach68b0fb02017-02-28 15:15:56 -0500574 if (seq_lt (tc->opt.sacks[j].start, tc->opt.sacks[i].start))
575 {
576 tmp = tc->opt.sacks[i];
577 tc->opt.sacks[i] = tc->opt.sacks[j];
578 tc->opt.sacks[j] = tmp;
579 }
580
Dave Barach68b0fb02017-02-28 15:15:56 -0500581 if (sb->head == TCP_INVALID_SACK_HOLE_INDEX)
582 {
Florin Coras6792ec02017-03-13 03:49:51 -0700583 /* If no holes, insert the first that covers all outstanding bytes */
584 last_hole = scoreboard_insert_hole (sb, TCP_INVALID_SACK_HOLE_INDEX,
585 tc->snd_una, tc->snd_una_max);
586 sb->tail = scoreboard_hole_index (sb, last_hole);
587 }
588 else
589 {
590 /* If we have holes but snd_una_max is beyond the last hole, update
591 * last hole end */
592 tmp = tc->opt.sacks[vec_len (tc->opt.sacks) - 1];
593 last_hole = scoreboard_last_hole (sb);
594 if (seq_gt (tc->snd_una_max, sb->max_byte_sacked)
595 && seq_gt (tc->snd_una_max, last_hole->end))
596 last_hole->end = tc->snd_una_max;
Dave Barach68b0fb02017-02-28 15:15:56 -0500597 }
598
599 /* Walk the holes with the SACK blocks */
600 hole = pool_elt_at_index (sb->holes, sb->head);
601 while (hole && blk_index < vec_len (tc->opt.sacks))
602 {
603 blk = &tc->opt.sacks[blk_index];
604
605 if (seq_leq (blk->start, hole->start))
606 {
607 /* Block covers hole. Remove hole */
608 if (seq_geq (blk->end, hole->end))
609 {
610 next_hole = scoreboard_next_hole (sb, hole);
611
612 /* Byte accounting */
Florin Coras6792ec02017-03-13 03:49:51 -0700613 if (seq_leq (hole->end, ack))
Dave Barach68b0fb02017-02-28 15:15:56 -0500614 {
Florin Coras6792ec02017-03-13 03:49:51 -0700615 /* Bytes lost because snd_wnd left edge advances */
616 if (next_hole && seq_leq (next_hole->start, ack))
Dave Barach68b0fb02017-02-28 15:15:56 -0500617 sb->sacked_bytes -= next_hole->start - hole->end;
618 else
619 sb->sacked_bytes -= ack - hole->end;
620 }
621 else
622 {
623 sb->sacked_bytes += scoreboard_hole_bytes (hole);
624 }
625
Florin Coras6792ec02017-03-13 03:49:51 -0700626 /* snd_una needs to be advanced */
627 if (seq_geq (ack, hole->end))
628 {
629 if (next_hole && seq_lt (ack, next_hole->start))
630 sb->snd_una_adv = next_hole->start - ack;
631 else
632 sb->snd_una_adv = sb->max_byte_sacked - ack;
633
634 /* all these can be delivered */
635 sb->sacked_bytes -= sb->snd_una_adv;
636 }
637
638 /* About to remove last hole */
639 if (hole == last_hole)
640 {
641 sb->tail = hole->prev;
642 last_hole = scoreboard_last_hole (sb);
643 /* keep track of max byte sacked in case the last hole
644 * is acked */
645 if (seq_gt (hole->end, sb->max_byte_sacked))
646 sb->max_byte_sacked = hole->end;
647 }
Dave Barach68b0fb02017-02-28 15:15:56 -0500648 scoreboard_remove_hole (sb, hole);
649 hole = next_hole;
650 }
Florin Coras6792ec02017-03-13 03:49:51 -0700651 /* Partial 'head' overlap */
Dave Barach68b0fb02017-02-28 15:15:56 -0500652 else
653 {
Florin Coras6792ec02017-03-13 03:49:51 -0700654 if (seq_gt (blk->end, hole->start))
655 {
656 sb->sacked_bytes += blk->end - hole->start;
657 hole->start = blk->end;
658 }
Dave Barach68b0fb02017-02-28 15:15:56 -0500659 blk_index++;
660 }
661 }
662 else
663 {
664 /* Hole must be split */
Florin Coras6792ec02017-03-13 03:49:51 -0700665 if (seq_lt (blk->end, hole->end))
Dave Barach68b0fb02017-02-28 15:15:56 -0500666 {
667 sb->sacked_bytes += blk->end - blk->start;
Florin Coras6792ec02017-03-13 03:49:51 -0700668 hole_index = scoreboard_hole_index (sb, hole);
669 new_hole = scoreboard_insert_hole (sb, hole_index, blk->end,
670 hole->end);
671
672 /* Pool might've moved */
673 hole = scoreboard_get_hole (sb, hole_index);
674 hole->end = blk->start;
675
676 /* New or split of tail */
677 if ((last_hole->end == new_hole->end)
678 || seq_lt (last_hole->end, new_hole->start))
679 {
680 last_hole = new_hole;
681 sb->tail = scoreboard_hole_index (sb, new_hole);
682 }
683
Dave Barach68b0fb02017-02-28 15:15:56 -0500684 blk_index++;
Florin Coras6792ec02017-03-13 03:49:51 -0700685 hole = scoreboard_next_hole (sb, hole);
Dave Barach68b0fb02017-02-28 15:15:56 -0500686 }
687 else
688 {
Florin Coras6792ec02017-03-13 03:49:51 -0700689 sb->sacked_bytes += hole->end - blk->start;
690 hole->end = blk->start;
Dave Barach68b0fb02017-02-28 15:15:56 -0500691 hole = scoreboard_next_hole (sb, hole);
692 }
693 }
694 }
Florin Coras6792ec02017-03-13 03:49:51 -0700695
696 sb->last_sacked_bytes = sb->sacked_bytes + sb->snd_una_adv
697 - old_sacked_bytes;
Dave Barach68b0fb02017-02-28 15:15:56 -0500698}
699
700/** Update snd_wnd
701 *
702 * If (SND.WL1 < SEG.SEQ or (SND.WL1 = SEG.SEQ and SND.WL2 =< SEG.ACK)), set
703 * SND.WND <- SEG.WND, set SND.WL1 <- SEG.SEQ, and set SND.WL2 <- SEG.ACK */
704static void
705tcp_update_snd_wnd (tcp_connection_t * tc, u32 seq, u32 ack, u32 snd_wnd)
706{
Florin Coras6792ec02017-03-13 03:49:51 -0700707 if (seq_lt (tc->snd_wl1, seq)
708 || (tc->snd_wl1 == seq && seq_leq (tc->snd_wl2, ack)))
Dave Barach68b0fb02017-02-28 15:15:56 -0500709 {
710 tc->snd_wnd = snd_wnd;
711 tc->snd_wl1 = seq;
712 tc->snd_wl2 = ack;
Florin Coras6792ec02017-03-13 03:49:51 -0700713 TCP_EVT_DBG (TCP_EVT_SND_WND, tc);
Florin Coras3e350af2017-03-30 02:54:28 -0700714
715 /* Set probe timer if we just got 0 wnd */
716 if (tc->snd_wnd < tc->snd_mss
717 && !tcp_timer_is_active (tc, TCP_TIMER_PERSIST))
718 tcp_persist_timer_set (tc);
719 else
720 tcp_persist_timer_reset (tc);
Dave Barach68b0fb02017-02-28 15:15:56 -0500721 }
722}
723
Florin Coras6792ec02017-03-13 03:49:51 -0700724void
Dave Barach68b0fb02017-02-28 15:15:56 -0500725tcp_cc_congestion (tcp_connection_t * tc)
726{
Florin Coras6792ec02017-03-13 03:49:51 -0700727 tc->snd_congestion = tc->snd_nxt;
Dave Barach68b0fb02017-02-28 15:15:56 -0500728 tc->cc_algo->congestion (tc);
Florin Coras6792ec02017-03-13 03:49:51 -0700729 TCP_EVT_DBG (TCP_EVT_CC_EVT, tc, 4);
Dave Barach68b0fb02017-02-28 15:15:56 -0500730}
731
Florin Coras6792ec02017-03-13 03:49:51 -0700732void
Dave Barach68b0fb02017-02-28 15:15:56 -0500733tcp_cc_recover (tcp_connection_t * tc)
734{
Florin Coras3e350af2017-03-30 02:54:28 -0700735 /* TODO: check if time to recover was small. It might be that RTO popped
736 * too soon.
737 */
738
Florin Coras6792ec02017-03-13 03:49:51 -0700739 tc->cc_algo->recovered (tc);
740
741 tc->rtx_bytes = 0;
742 tc->rcv_dupacks = 0;
743 tc->snd_nxt = tc->snd_una;
744
745 tc->cc_algo->rcv_ack (tc);
746 tc->tsecr_last_ack = tc->opt.tsecr;
747
Florin Coras3e350af2017-03-30 02:54:28 -0700748 tcp_cong_recovery_off (tc);
Florin Coras6792ec02017-03-13 03:49:51 -0700749
750 TCP_EVT_DBG (TCP_EVT_CC_EVT, tc, 3);
Dave Barach68b0fb02017-02-28 15:15:56 -0500751}
752
753static void
Florin Coras6792ec02017-03-13 03:49:51 -0700754tcp_cc_rcv_ack (tcp_connection_t * tc, vlib_buffer_t * b)
Dave Barach68b0fb02017-02-28 15:15:56 -0500755{
756 u8 partial_ack;
757
Florin Coras3af90fc2017-05-03 21:09:42 -0700758 if (tcp_in_fastrecovery (tc))
Dave Barach68b0fb02017-02-28 15:15:56 -0500759 {
Florin Coras6792ec02017-03-13 03:49:51 -0700760 partial_ack = seq_lt (tc->snd_una, tc->snd_congestion);
Dave Barach68b0fb02017-02-28 15:15:56 -0500761 if (!partial_ack)
762 {
763 /* Clear retransmitted bytes. */
Dave Barach68b0fb02017-02-28 15:15:56 -0500764 tcp_cc_recover (tc);
765 }
766 else
767 {
Florin Coras6792ec02017-03-13 03:49:51 -0700768 TCP_EVT_DBG (TCP_EVT_CC_PACK, tc);
769
Dave Barach68b0fb02017-02-28 15:15:56 -0500770 /* Clear retransmitted bytes. XXX should we clear all? */
771 tc->rtx_bytes = 0;
Florin Coras3af90fc2017-05-03 21:09:42 -0700772
Dave Barach68b0fb02017-02-28 15:15:56 -0500773 tc->cc_algo->rcv_cong_ack (tc, TCP_CC_PARTIALACK);
774
Florin Coras6792ec02017-03-13 03:49:51 -0700775 /* In case snd_nxt is still in the past and output tries to
776 * shove some new bytes */
Florin Coras3e350af2017-03-30 02:54:28 -0700777 tc->snd_nxt = tc->snd_una_max;
Florin Coras6792ec02017-03-13 03:49:51 -0700778
779 /* XXX need proper RFC6675 support */
Florin Coras3e350af2017-03-30 02:54:28 -0700780 if (tc->sack_sb.last_sacked_bytes && !tcp_in_recovery (tc))
Florin Coras6792ec02017-03-13 03:49:51 -0700781 {
782 tcp_fast_retransmit (tc);
783 }
784 else
785 {
786 /* Retransmit first unacked segment */
787 tcp_retransmit_first_unacked (tc);
Florin Coras6792ec02017-03-13 03:49:51 -0700788 }
Dave Barach68b0fb02017-02-28 15:15:56 -0500789 }
790 }
791 else
792 {
793 tc->cc_algo->rcv_ack (tc);
Florin Coras6792ec02017-03-13 03:49:51 -0700794 tc->tsecr_last_ack = tc->opt.tsecr;
795 tc->rcv_dupacks = 0;
Florin Coras3af90fc2017-05-03 21:09:42 -0700796 if (tcp_in_recovery (tc))
797 {
798 tc->rtx_bytes -= clib_min (tc->bytes_acked, tc->rtx_bytes);
799 tc->rto = clib_min (tc->srtt + (tc->rttvar << 2), TCP_RTO_MAX);
800 if (seq_geq (tc->snd_una, tc->snd_congestion))
801 tcp_recovery_off (tc);
802 }
Dave Barach68b0fb02017-02-28 15:15:56 -0500803 }
Dave Barach68b0fb02017-02-28 15:15:56 -0500804}
805
806static void
807tcp_cc_rcv_dupack (tcp_connection_t * tc, u32 ack)
808{
Florin Coras6792ec02017-03-13 03:49:51 -0700809// ASSERT (seq_geq(tc->snd_una, ack));
Dave Barach68b0fb02017-02-28 15:15:56 -0500810
811 tc->rcv_dupacks++;
812 if (tc->rcv_dupacks == TCP_DUPACK_THRESHOLD)
813 {
814 /* RFC6582 NewReno heuristic to avoid multiple fast retransmits */
815 if (tc->opt.tsecr != tc->tsecr_last_ack)
816 {
817 tc->rcv_dupacks = 0;
818 return;
819 }
820
821 tcp_fastrecovery_on (tc);
822
823 /* Handle congestion and dupack */
824 tcp_cc_congestion (tc);
825 tc->cc_algo->rcv_cong_ack (tc, TCP_CC_DUPACK);
826
827 tcp_fast_retransmit (tc);
828
829 /* Post retransmit update cwnd to ssthresh and account for the
830 * three segments that have left the network and should've been
831 * buffered at the receiver */
832 tc->cwnd = tc->ssthresh + TCP_DUPACK_THRESHOLD * tc->snd_mss;
833 }
834 else if (tc->rcv_dupacks > TCP_DUPACK_THRESHOLD)
835 {
836 ASSERT (tcp_in_fastrecovery (tc));
837
838 tc->cc_algo->rcv_cong_ack (tc, TCP_CC_DUPACK);
839 }
840}
841
842void
843tcp_cc_init (tcp_connection_t * tc)
844{
845 tc->cc_algo = tcp_cc_algo_get (TCP_CC_NEWRENO);
846 tc->cc_algo->init (tc);
847}
848
849static int
850tcp_rcv_ack (tcp_connection_t * tc, vlib_buffer_t * b,
851 tcp_header_t * th, u32 * next, u32 * error)
852{
853 u32 new_snd_wnd;
854
Florin Coras6792ec02017-03-13 03:49:51 -0700855 /* If the ACK acks something not yet sent (SEG.ACK > SND.NXT) */
Dave Barach68b0fb02017-02-28 15:15:56 -0500856 if (seq_gt (vnet_buffer (b)->tcp.ack_number, tc->snd_nxt))
857 {
Florin Coras6792ec02017-03-13 03:49:51 -0700858 /* If we have outstanding data and this is within the window, accept it,
859 * probably retransmit has timed out. Otherwise ACK segment and then
860 * drop it */
861 if (seq_gt (vnet_buffer (b)->tcp.ack_number, tc->snd_una_max))
862 {
863 tcp_make_ack (tc, b);
864 *next = tcp_next_output (tc->c_is_ip4);
865 *error = TCP_ERROR_ACK_INVALID;
866 TCP_EVT_DBG (TCP_EVT_ACK_RCV_ERR, tc, 0,
867 vnet_buffer (b)->tcp.ack_number);
868 return -1;
869 }
870
Florin Coras6792ec02017-03-13 03:49:51 -0700871 TCP_EVT_DBG (TCP_EVT_ACK_RCV_ERR, tc, 2,
872 vnet_buffer (b)->tcp.ack_number);
Florin Coras3e350af2017-03-30 02:54:28 -0700873
874 tc->snd_nxt = vnet_buffer (b)->tcp.ack_number;
875 *error = TCP_ERROR_ACK_FUTURE;
Dave Barach68b0fb02017-02-28 15:15:56 -0500876 }
877
Florin Coras6792ec02017-03-13 03:49:51 -0700878 /* If old ACK, probably it's an old dupack */
Dave Barach68b0fb02017-02-28 15:15:56 -0500879 if (seq_lt (vnet_buffer (b)->tcp.ack_number, tc->snd_una))
880 {
881 *error = TCP_ERROR_ACK_OLD;
Florin Coras6792ec02017-03-13 03:49:51 -0700882 TCP_EVT_DBG (TCP_EVT_ACK_RCV_ERR, tc, 1,
883 vnet_buffer (b)->tcp.ack_number);
884 if (tcp_in_fastrecovery (tc) && tc->rcv_dupacks == TCP_DUPACK_THRESHOLD)
885 {
886 TCP_EVT_DBG (TCP_EVT_DUPACK_RCVD, tc);
887 tcp_cc_rcv_dupack (tc, vnet_buffer (b)->tcp.ack_number);
888 }
Florin Corasc28764f2017-04-26 00:08:42 -0700889 /* Don't drop yet */
890 return 0;
Dave Barach68b0fb02017-02-28 15:15:56 -0500891 }
892
893 if (tcp_opts_sack_permitted (&tc->opt))
894 tcp_rcv_sacks (tc, vnet_buffer (b)->tcp.ack_number);
895
Florin Corase04c2992017-03-01 08:17:34 -0800896 new_snd_wnd = clib_net_to_host_u16 (th->window) << tc->snd_wscale;
Dave Barach68b0fb02017-02-28 15:15:56 -0500897
898 if (tcp_ack_is_dupack (tc, b, new_snd_wnd))
899 {
Florin Coras6792ec02017-03-13 03:49:51 -0700900 TCP_EVT_DBG (TCP_EVT_DUPACK_RCVD, tc, 1);
Dave Barach68b0fb02017-02-28 15:15:56 -0500901 tcp_cc_rcv_dupack (tc, vnet_buffer (b)->tcp.ack_number);
902 *error = TCP_ERROR_ACK_DUP;
903 return -1;
904 }
905
Florin Coras6792ec02017-03-13 03:49:51 -0700906 /*
907 * Valid ACK
908 */
909
Dave Barach68b0fb02017-02-28 15:15:56 -0500910 tc->bytes_acked = vnet_buffer (b)->tcp.ack_number - tc->snd_una;
Florin Coras6792ec02017-03-13 03:49:51 -0700911 tc->snd_una = vnet_buffer (b)->tcp.ack_number + tc->sack_sb.snd_una_adv;
Dave Barach68b0fb02017-02-28 15:15:56 -0500912
Florin Coras6792ec02017-03-13 03:49:51 -0700913 /* Dequeue ACKed data and update RTT */
Dave Barach68b0fb02017-02-28 15:15:56 -0500914 tcp_dequeue_acked (tc, vnet_buffer (b)->tcp.ack_number);
Dave Barach68b0fb02017-02-28 15:15:56 -0500915 tcp_update_snd_wnd (tc, vnet_buffer (b)->tcp.seq_number,
916 vnet_buffer (b)->tcp.ack_number, new_snd_wnd);
917
Florin Coras6792ec02017-03-13 03:49:51 -0700918 /* If some of our sent bytes have been acked, update cc and retransmit
919 * timer. */
920 if (tc->bytes_acked)
921 {
Florin Coras3e350af2017-03-30 02:54:28 -0700922 TCP_EVT_DBG (TCP_EVT_ACK_RCVD, tc);
Dave Barach68b0fb02017-02-28 15:15:56 -0500923
Florin Coras6792ec02017-03-13 03:49:51 -0700924 /* Updates congestion control (slow start/congestion avoidance) */
925 tcp_cc_rcv_ack (tc, b);
Florin Corase69f4952017-03-07 10:06:24 -0800926
Florin Coras6792ec02017-03-13 03:49:51 -0700927 /* If everything has been acked, stop retransmit timer
Florin Coras3af90fc2017-05-03 21:09:42 -0700928 * otherwise update. */
Florin Coras6792ec02017-03-13 03:49:51 -0700929 if (tc->snd_una == tc->snd_una_max)
930 tcp_retransmit_timer_reset (tc);
931 else
932 tcp_retransmit_timer_update (tc);
933 }
Dave Barach68b0fb02017-02-28 15:15:56 -0500934
935 return 0;
936}
937
938/**
939 * Build SACK list as per RFC2018.
940 *
941 * Makes sure the first block contains the segment that generated the current
942 * ACK and the following ones are the ones most recently reported in SACK
943 * blocks.
944 *
945 * @param tc TCP connection for which the SACK list is updated
946 * @param start Start sequence number of the newest SACK block
947 * @param end End sequence of the newest SACK block
948 */
Florin Coras45d34962017-04-25 00:05:27 -0700949void
Dave Barach68b0fb02017-02-28 15:15:56 -0500950tcp_update_sack_list (tcp_connection_t * tc, u32 start, u32 end)
951{
Florin Coras45d34962017-04-25 00:05:27 -0700952 sack_block_t *new_list = 0, *block = 0;
Dave Barach68b0fb02017-02-28 15:15:56 -0500953 int i;
Dave Barach68b0fb02017-02-28 15:15:56 -0500954
955 /* If the first segment is ooo add it to the list. Last write might've moved
956 * rcv_nxt over the first segment. */
957 if (seq_lt (tc->rcv_nxt, start))
958 {
Florin Coras45d34962017-04-25 00:05:27 -0700959 vec_add2 (new_list, block, 1);
960 block->start = start;
961 block->end = end;
Dave Barach68b0fb02017-02-28 15:15:56 -0500962 }
963
964 /* Find the blocks still worth keeping. */
965 for (i = 0; i < vec_len (tc->snd_sacks); i++)
966 {
Florin Coras45d34962017-04-25 00:05:27 -0700967 /* Discard if rcv_nxt advanced beyond current block */
968 if (seq_leq (tc->snd_sacks[i].start, tc->rcv_nxt))
Dave Barach68b0fb02017-02-28 15:15:56 -0500969 continue;
970
Florin Coras45d34962017-04-25 00:05:27 -0700971 /* Merge or drop if segment overlapped by the new segment */
972 if (block && (seq_geq (tc->snd_sacks[i].end, new_list[0].start)
973 && seq_leq (tc->snd_sacks[i].start, new_list[0].end)))
974 {
975 if (seq_lt (tc->snd_sacks[i].start, new_list[0].start))
976 new_list[0].start = tc->snd_sacks[i].start;
977 if (seq_lt (new_list[0].end, tc->snd_sacks[i].end))
978 new_list[0].end = tc->snd_sacks[i].end;
979 continue;
980 }
981
982 /* Save to new SACK list if we have space. */
983 if (vec_len (new_list) < TCP_MAX_SACK_BLOCKS)
984 {
985 vec_add1 (new_list, tc->snd_sacks[i]);
986 }
Dave Barach68b0fb02017-02-28 15:15:56 -0500987 }
988
Florin Coras45d34962017-04-25 00:05:27 -0700989 ASSERT (vec_len (new_list) <= TCP_MAX_SACK_BLOCKS);
Florin Coras6792ec02017-03-13 03:49:51 -0700990
Dave Barach68b0fb02017-02-28 15:15:56 -0500991 /* Replace old vector with new one */
992 vec_free (tc->snd_sacks);
993 tc->snd_sacks = new_list;
994}
995
996/** Enqueue data for delivery to application */
Florin Coras6792ec02017-03-13 03:49:51 -0700997always_inline int
Dave Barach68b0fb02017-02-28 15:15:56 -0500998tcp_session_enqueue_data (tcp_connection_t * tc, vlib_buffer_t * b,
999 u16 data_len)
1000{
1001 int written;
1002
1003 /* Pure ACK. Update rcv_nxt and be done. */
1004 if (PREDICT_FALSE (data_len == 0))
1005 {
1006 tc->rcv_nxt = vnet_buffer (b)->tcp.seq_end;
1007 return TCP_ERROR_PURE_ACK;
1008 }
1009
Florin Corasf6d68ed2017-05-07 19:12:02 -07001010 written = stream_session_enqueue_data (&tc->connection, b, 0,
1011 1 /* queue event */ , 1);
Dave Barach68b0fb02017-02-28 15:15:56 -05001012
Florin Coras6792ec02017-03-13 03:49:51 -07001013 TCP_EVT_DBG (TCP_EVT_INPUT, tc, 0, data_len, written);
1014
Dave Barach68b0fb02017-02-28 15:15:56 -05001015 /* Update rcv_nxt */
1016 if (PREDICT_TRUE (written == data_len))
1017 {
1018 tc->rcv_nxt = vnet_buffer (b)->tcp.seq_end;
1019 }
1020 /* If more data written than expected, account for out-of-order bytes. */
1021 else if (written > data_len)
1022 {
1023 tc->rcv_nxt = vnet_buffer (b)->tcp.seq_end + written - data_len;
1024
1025 /* Send ACK confirming the update */
1026 tc->flags |= TCP_CONN_SNDACK;
Florin Coras6792ec02017-03-13 03:49:51 -07001027 }
1028 else if (written > 0)
1029 {
1030 /* We've written something but FIFO is probably full now */
1031 tc->rcv_nxt += written;
Dave Barach68b0fb02017-02-28 15:15:56 -05001032
Florin Coras6792ec02017-03-13 03:49:51 -07001033 /* Depending on how fast the app is, all remaining buffers in burst will
Florin Coras3e350af2017-03-30 02:54:28 -07001034 * not be enqueued. Inform peer */
1035 tc->flags |= TCP_CONN_SNDACK;
1036
Florin Coras6792ec02017-03-13 03:49:51 -07001037 return TCP_ERROR_PARTIALLY_ENQUEUED;
Dave Barach68b0fb02017-02-28 15:15:56 -05001038 }
1039 else
1040 {
Florin Coras3e350af2017-03-30 02:54:28 -07001041 tc->flags |= TCP_CONN_SNDACK;
Dave Barach68b0fb02017-02-28 15:15:56 -05001042 return TCP_ERROR_FIFO_FULL;
1043 }
1044
Florin Coras6792ec02017-03-13 03:49:51 -07001045 /* Update SACK list if need be */
1046 if (tcp_opts_sack_permitted (&tc->opt))
1047 {
1048 /* Remove SACK blocks that have been delivered */
1049 tcp_update_sack_list (tc, tc->rcv_nxt, tc->rcv_nxt);
1050 }
1051
Dave Barach68b0fb02017-02-28 15:15:56 -05001052 return TCP_ERROR_ENQUEUED;
1053}
1054
1055/** Enqueue out-of-order data */
Florin Coras6792ec02017-03-13 03:49:51 -07001056always_inline int
Dave Barach68b0fb02017-02-28 15:15:56 -05001057tcp_session_enqueue_ooo (tcp_connection_t * tc, vlib_buffer_t * b,
1058 u16 data_len)
1059{
1060 stream_session_t *s0;
Florin Coras6792ec02017-03-13 03:49:51 -07001061 int rv;
1062
1063 /* Pure ACK. Do nothing */
1064 if (PREDICT_FALSE (data_len == 0))
1065 {
1066 return TCP_ERROR_PURE_ACK;
1067 }
Dave Barach68b0fb02017-02-28 15:15:56 -05001068
Florin Corasc28764f2017-04-26 00:08:42 -07001069 /* Enqueue out-of-order data with absolute offset */
Florin Corasf6d68ed2017-05-07 19:12:02 -07001070 rv = stream_session_enqueue_data (&tc->connection, b,
1071 vnet_buffer (b)->tcp.seq_number,
1072 0 /* queue event */ , 0);
Florin Coras6792ec02017-03-13 03:49:51 -07001073
1074 /* Nothing written */
1075 if (rv)
1076 {
1077 TCP_EVT_DBG (TCP_EVT_INPUT, tc, 1, data_len, 0);
1078 return TCP_ERROR_FIFO_FULL;
1079 }
1080
1081 TCP_EVT_DBG (TCP_EVT_INPUT, tc, 1, data_len, data_len);
Dave Barach68b0fb02017-02-28 15:15:56 -05001082
1083 /* Update SACK list if in use */
1084 if (tcp_opts_sack_permitted (&tc->opt))
1085 {
1086 ooo_segment_t *newest;
1087 u32 start, end;
1088
Florin Corasf6d68ed2017-05-07 19:12:02 -07001089 s0 = stream_session_get (tc->c_s_index, tc->c_thread_index);
1090
Dave Barach68b0fb02017-02-28 15:15:56 -05001091 /* Get the newest segment from the fifo */
1092 newest = svm_fifo_newest_ooo_segment (s0->server_rx_fifo);
Dave Barach1f75cfd2017-04-14 16:46:44 -04001093 start = ooo_segment_offset (s0->server_rx_fifo, newest);
1094 end = ooo_segment_end_offset (s0->server_rx_fifo, newest);
Dave Barach68b0fb02017-02-28 15:15:56 -05001095
1096 tcp_update_sack_list (tc, start, end);
1097 }
1098
1099 return TCP_ERROR_ENQUEUED;
1100}
1101
1102/**
Florin Coras6792ec02017-03-13 03:49:51 -07001103 * Check if ACK could be delayed. If ack can be delayed, it should return
1104 * true for a full frame. If we're always acking return 0.
Dave Barach68b0fb02017-02-28 15:15:56 -05001105 */
1106always_inline int
1107tcp_can_delack (tcp_connection_t * tc)
1108{
Florin Coras6792ec02017-03-13 03:49:51 -07001109 /* Send ack if ... */
1110 if (TCP_ALWAYS_ACK
1111 /* just sent a rcv wnd 0 */
1112 || (tc->flags & TCP_CONN_SENT_RCV_WND0) != 0
1113 /* constrained to send ack */
1114 || (tc->flags & TCP_CONN_SNDACK) != 0
1115 /* we're almost out of tx wnd */
1116 || tcp_available_snd_space (tc) < 2 * tc->snd_mss)
1117 return 0;
Dave Barach68b0fb02017-02-28 15:15:56 -05001118
Florin Coras6792ec02017-03-13 03:49:51 -07001119 return 1;
Dave Barach68b0fb02017-02-28 15:15:56 -05001120}
1121
1122static int
1123tcp_segment_rcv (tcp_main_t * tm, tcp_connection_t * tc, vlib_buffer_t * b,
1124 u16 n_data_bytes, u32 * next0)
1125{
Florin Corasdb84e572017-05-09 18:54:52 -07001126 u32 error = 0, n_bytes_to_drop;
Dave Barach68b0fb02017-02-28 15:15:56 -05001127
1128 /* Handle out-of-order data */
1129 if (PREDICT_FALSE (vnet_buffer (b)->tcp.seq_number != tc->rcv_nxt))
1130 {
Florin Coras6792ec02017-03-13 03:49:51 -07001131 /* Old sequence numbers allowed through because they overlapped
1132 * the rx window */
1133 if (seq_lt (vnet_buffer (b)->tcp.seq_number, tc->rcv_nxt))
Dave Barach68b0fb02017-02-28 15:15:56 -05001134 {
Florin Coras6792ec02017-03-13 03:49:51 -07001135 error = TCP_ERROR_SEGMENT_OLD;
1136 *next0 = TCP_NEXT_DROP;
Florin Corasdb84e572017-05-09 18:54:52 -07001137
Dave Barach259cdae2017-05-15 16:27:05 -04001138 /* Completely in the past (possible retransmit) */
1139 if (seq_lt (vnet_buffer (b)->tcp.seq_end, tc->rcv_nxt))
1140 goto done;
1141
Florin Corasdb84e572017-05-09 18:54:52 -07001142 /* Chop off the bytes in the past */
1143 n_bytes_to_drop = tc->rcv_nxt - vnet_buffer (b)->tcp.seq_number;
1144 n_data_bytes -= n_bytes_to_drop;
1145 vlib_buffer_advance (b, n_bytes_to_drop);
1146
1147 goto in_order;
Dave Barach68b0fb02017-02-28 15:15:56 -05001148 }
1149
Florin Coras6792ec02017-03-13 03:49:51 -07001150 error = tcp_session_enqueue_ooo (tc, b, n_data_bytes);
1151
1152 /* N.B. Should not filter burst of dupacks. Two issues 1) dupacks open
1153 * cwnd on remote peer when congested 2) acks leaving should have the
1154 * latest rcv_wnd since the burst may eaten up all of it, so only the
1155 * old ones could be filtered.
1156 */
1157
1158 /* RFC2581: Send DUPACK for fast retransmit */
1159 tcp_make_ack (tc, b);
1160 *next0 = tcp_next_output (tc->c_is_ip4);
1161
1162 /* Mark as DUPACK. We may filter these in output if
1163 * the burst fills the holes. */
1164 if (n_data_bytes)
1165 vnet_buffer (b)->tcp.flags = TCP_BUF_FLAG_DUPACK;
1166
1167 TCP_EVT_DBG (TCP_EVT_DUPACK_SENT, tc);
Dave Barach68b0fb02017-02-28 15:15:56 -05001168 goto done;
1169 }
1170
Florin Corasdb84e572017-05-09 18:54:52 -07001171in_order:
1172
Dave Barach68b0fb02017-02-28 15:15:56 -05001173 /* In order data, enqueue. Fifo figures out by itself if any out-of-order
1174 * segments can be enqueued after fifo tail offset changes. */
1175 error = tcp_session_enqueue_data (tc, b, n_data_bytes);
1176
Florin Coras6792ec02017-03-13 03:49:51 -07001177 if (n_data_bytes == 0)
1178 {
1179 *next0 = TCP_NEXT_DROP;
1180 goto done;
1181 }
1182
Dave Barach68b0fb02017-02-28 15:15:56 -05001183 /* Check if ACK can be delayed */
Florin Coras3e350af2017-03-30 02:54:28 -07001184 if (tcp_can_delack (tc))
Dave Barach68b0fb02017-02-28 15:15:56 -05001185 {
Florin Coras6792ec02017-03-13 03:49:51 -07001186 if (!tcp_timer_is_active (tc, TCP_TIMER_DELACK))
1187 tcp_timer_set (tc, TCP_TIMER_DELACK, TCP_DELACK_TIME);
Florin Coras3e350af2017-03-30 02:54:28 -07001188 goto done;
Dave Barach68b0fb02017-02-28 15:15:56 -05001189 }
1190
Florin Coras3e350af2017-03-30 02:54:28 -07001191 *next0 = tcp_next_output (tc->c_is_ip4);
1192 tcp_make_ack (tc, b);
1193
Dave Barach68b0fb02017-02-28 15:15:56 -05001194done:
1195 return error;
1196}
1197
Clement Durand6cf260c2017-04-13 13:27:04 +02001198typedef struct
1199{
1200 tcp_header_t tcp_header;
1201 tcp_connection_t tcp_connection;
1202} tcp_rx_trace_t;
1203
1204u8 *
1205format_tcp_rx_trace (u8 * s, va_list * args)
1206{
1207 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
1208 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
1209 tcp_rx_trace_t *t = va_arg (*args, tcp_rx_trace_t *);
1210 uword indent = format_get_indent (s);
1211
1212 s = format (s, "%U\n%U%U",
1213 format_tcp_header, &t->tcp_header, 128,
1214 format_white_space, indent,
1215 format_tcp_connection_verbose, &t->tcp_connection);
1216
1217 return s;
1218}
1219
1220u8 *
1221format_tcp_rx_trace_short (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
1227 s = format (s, "%d -> %d (%U)",
1228 clib_net_to_host_u16 (t->tcp_header.src_port),
1229 clib_net_to_host_u16 (t->tcp_header.dst_port), format_tcp_state,
1230 &t->tcp_connection.state);
1231
1232 return s;
1233}
1234
Florin Coras82b13a82017-04-25 11:58:06 -07001235void
1236tcp_set_rx_trace_data (tcp_rx_trace_t * t0, tcp_connection_t * tc0,
1237 tcp_header_t * th0, vlib_buffer_t * b0, u8 is_ip4)
1238{
1239 if (tc0)
1240 {
1241 clib_memcpy (&t0->tcp_connection, tc0, sizeof (t0->tcp_connection));
1242 }
1243 else
1244 {
1245 th0 = tcp_buffer_hdr (b0);
1246 }
1247 clib_memcpy (&t0->tcp_header, th0, sizeof (t0->tcp_header));
1248}
1249
Florin Coras6792ec02017-03-13 03:49:51 -07001250always_inline void
1251tcp_established_inc_counter (vlib_main_t * vm, u8 is_ip4, u8 evt, u8 val)
Dave Barach68b0fb02017-02-28 15:15:56 -05001252{
Florin Coras6792ec02017-03-13 03:49:51 -07001253 if (PREDICT_TRUE (!val))
1254 return;
Dave Barach68b0fb02017-02-28 15:15:56 -05001255
Florin Coras6792ec02017-03-13 03:49:51 -07001256 if (is_ip4)
1257 vlib_node_increment_counter (vm, tcp4_established_node.index, evt, val);
1258 else
1259 vlib_node_increment_counter (vm, tcp6_established_node.index, evt, val);
Dave Barach68b0fb02017-02-28 15:15:56 -05001260}
1261
1262always_inline uword
1263tcp46_established_inline (vlib_main_t * vm, vlib_node_runtime_t * node,
1264 vlib_frame_t * from_frame, int is_ip4)
1265{
1266 u32 n_left_from, next_index, *from, *to_next;
Damjan Marion586afd72017-04-05 19:18:20 +02001267 u32 my_thread_index = vm->thread_index, errors = 0;
Dave Barach68b0fb02017-02-28 15:15:56 -05001268 tcp_main_t *tm = vnet_get_tcp_main ();
Dave Barach1f75cfd2017-04-14 16:46:44 -04001269 u8 is_fin = 0;
Dave Barach68b0fb02017-02-28 15:15:56 -05001270
1271 from = vlib_frame_vector_args (from_frame);
1272 n_left_from = from_frame->n_vectors;
1273
1274 next_index = node->cached_next_index;
1275
1276 while (n_left_from > 0)
1277 {
1278 u32 n_left_to_next;
1279
1280 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
1281
1282 while (n_left_from > 0 && n_left_to_next > 0)
1283 {
1284 u32 bi0;
1285 vlib_buffer_t *b0;
1286 tcp_header_t *th0 = 0;
1287 tcp_connection_t *tc0;
Dave Barach68b0fb02017-02-28 15:15:56 -05001288 u32 next0 = TCP_ESTABLISHED_NEXT_DROP, error0 = TCP_ERROR_ENQUEUED;
1289
1290 bi0 = from[0];
1291 to_next[0] = bi0;
1292 from += 1;
1293 to_next += 1;
1294 n_left_from -= 1;
1295 n_left_to_next -= 1;
1296
1297 b0 = vlib_get_buffer (vm, bi0);
1298 tc0 = tcp_connection_get (vnet_buffer (b0)->tcp.connection_index,
1299 my_thread_index);
1300
Florin Corasd79b41e2017-03-04 05:37:52 -08001301 if (PREDICT_FALSE (tc0 == 0))
1302 {
1303 error0 = TCP_ERROR_INVALID_CONNECTION;
Florin Coras6792ec02017-03-13 03:49:51 -07001304 goto done;
Florin Corasd79b41e2017-03-04 05:37:52 -08001305 }
1306
Florin Coras82b13a82017-04-25 11:58:06 -07001307 th0 = tcp_buffer_hdr (b0);
Dave Barach68b0fb02017-02-28 15:15:56 -05001308
Dave Barach1f75cfd2017-04-14 16:46:44 -04001309 is_fin = (th0->flags & TCP_FLAG_FIN) != 0;
1310
Dave Barach68b0fb02017-02-28 15:15:56 -05001311 /* SYNs, FINs and data consume sequence numbers */
1312 vnet_buffer (b0)->tcp.seq_end = vnet_buffer (b0)->tcp.seq_number
Florin Coras82b13a82017-04-25 11:58:06 -07001313 + tcp_is_syn (th0) + is_fin + vnet_buffer (b0)->tcp.data_len;
Dave Barach68b0fb02017-02-28 15:15:56 -05001314
1315 /* TODO header prediction fast path */
1316
1317 /* 1-4: check SEQ, RST, SYN */
1318 if (PREDICT_FALSE (tcp_segment_validate (vm, tc0, b0, th0, &next0)))
1319 {
1320 error0 = TCP_ERROR_SEGMENT_INVALID;
Florin Coras6792ec02017-03-13 03:49:51 -07001321 TCP_EVT_DBG (TCP_EVT_SEG_INVALID, tc0,
1322 vnet_buffer (b0)->tcp.seq_number,
1323 vnet_buffer (b0)->tcp.seq_end);
1324 goto done;
Dave Barach68b0fb02017-02-28 15:15:56 -05001325 }
1326
1327 /* 5: check the ACK field */
1328 if (tcp_rcv_ack (tc0, b0, th0, &next0, &error0))
1329 {
Florin Coras6792ec02017-03-13 03:49:51 -07001330 goto done;
Dave Barach68b0fb02017-02-28 15:15:56 -05001331 }
1332
1333 /* 6: check the URG bit TODO */
1334
1335 /* 7: process the segment text */
Florin Coras6792ec02017-03-13 03:49:51 -07001336
Florin Coras82b13a82017-04-25 11:58:06 -07001337 vlib_buffer_advance (b0, vnet_buffer (b0)->tcp.data_offset);
1338 error0 = tcp_segment_rcv (tm, tc0, b0,
1339 vnet_buffer (b0)->tcp.data_len, &next0);
Dave Barach68b0fb02017-02-28 15:15:56 -05001340
Dave Barach1f75cfd2017-04-14 16:46:44 -04001341 /* N.B. buffer is rewritten if segment is ooo. Thus, th0 becomes a
1342 * dangling reference. */
1343
Dave Barach68b0fb02017-02-28 15:15:56 -05001344 /* 8: check the FIN bit */
Dave Barach1f75cfd2017-04-14 16:46:44 -04001345 if (is_fin)
Dave Barach68b0fb02017-02-28 15:15:56 -05001346 {
Florin Corasd79b41e2017-03-04 05:37:52 -08001347 /* Enter CLOSE-WAIT and notify session. Don't send ACK, instead
1348 * wait for session to call close. To avoid lingering
1349 * in CLOSE-WAIT, set timer (reuse WAITCLOSE). */
Dave Barach68b0fb02017-02-28 15:15:56 -05001350 tc0->state = TCP_STATE_CLOSE_WAIT;
Florin Corase69f4952017-03-07 10:06:24 -08001351 TCP_EVT_DBG (TCP_EVT_FIN_RCVD, tc0);
Dave Barach68b0fb02017-02-28 15:15:56 -05001352 stream_session_disconnect_notify (&tc0->connection);
Florin Corasd79b41e2017-03-04 05:37:52 -08001353 tcp_timer_set (tc0, TCP_TIMER_WAITCLOSE, TCP_CLOSEWAIT_TIME);
Dave Barach68b0fb02017-02-28 15:15:56 -05001354 }
1355
Florin Coras6792ec02017-03-13 03:49:51 -07001356 done:
Dave Barach68b0fb02017-02-28 15:15:56 -05001357 b0->error = node->errors[error0];
1358 if (PREDICT_FALSE (b0->flags & VLIB_BUFFER_IS_TRACED))
1359 {
Florin Coras82b13a82017-04-25 11:58:06 -07001360 tcp_rx_trace_t *t0 =
1361 vlib_add_trace (vm, node, b0, sizeof (*t0));
1362 tcp_set_rx_trace_data (t0, tc0, th0, b0, is_ip4);
Dave Barach68b0fb02017-02-28 15:15:56 -05001363 }
1364
1365 vlib_validate_buffer_enqueue_x1 (vm, node, next_index, to_next,
1366 n_left_to_next, bi0, next0);
1367 }
1368
1369 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
1370 }
1371
1372 errors = session_manager_flush_enqueue_events (my_thread_index);
Florin Coras6792ec02017-03-13 03:49:51 -07001373 tcp_established_inc_counter (vm, is_ip4, TCP_ERROR_EVENT_FIFO_FULL, errors);
Dave Barach68b0fb02017-02-28 15:15:56 -05001374
1375 return from_frame->n_vectors;
1376}
1377
1378static uword
1379tcp4_established (vlib_main_t * vm, vlib_node_runtime_t * node,
1380 vlib_frame_t * from_frame)
1381{
1382 return tcp46_established_inline (vm, node, from_frame, 1 /* is_ip4 */ );
1383}
1384
1385static uword
1386tcp6_established (vlib_main_t * vm, vlib_node_runtime_t * node,
1387 vlib_frame_t * from_frame)
1388{
1389 return tcp46_established_inline (vm, node, from_frame, 0 /* is_ip4 */ );
1390}
1391
1392/* *INDENT-OFF* */
1393VLIB_REGISTER_NODE (tcp4_established_node) =
1394{
1395 .function = tcp4_established,
1396 .name = "tcp4-established",
1397 /* Takes a vector of packets. */
1398 .vector_size = sizeof (u32),
Florin Corase69f4952017-03-07 10:06:24 -08001399 .n_errors = TCP_N_ERROR,
1400 .error_strings = tcp_error_strings,
Dave Barach68b0fb02017-02-28 15:15:56 -05001401 .n_next_nodes = TCP_ESTABLISHED_N_NEXT,
1402 .next_nodes =
1403 {
1404#define _(s,n) [TCP_ESTABLISHED_NEXT_##s] = n,
1405 foreach_tcp_state_next
1406#undef _
1407 },
Clement Durand6cf260c2017-04-13 13:27:04 +02001408 .format_trace = format_tcp_rx_trace_short,
Dave Barach68b0fb02017-02-28 15:15:56 -05001409};
1410/* *INDENT-ON* */
1411
1412VLIB_NODE_FUNCTION_MULTIARCH (tcp4_established_node, tcp4_established);
1413
1414/* *INDENT-OFF* */
1415VLIB_REGISTER_NODE (tcp6_established_node) =
1416{
1417 .function = tcp6_established,
1418 .name = "tcp6-established",
1419 /* Takes a vector of packets. */
1420 .vector_size = sizeof (u32),
1421 .n_errors = TCP_N_ERROR,
1422 .error_strings = tcp_error_strings,
1423 .n_next_nodes = TCP_ESTABLISHED_N_NEXT,
1424 .next_nodes =
1425 {
1426#define _(s,n) [TCP_ESTABLISHED_NEXT_##s] = n,
1427 foreach_tcp_state_next
1428#undef _
1429 },
Clement Durand6cf260c2017-04-13 13:27:04 +02001430 .format_trace = format_tcp_rx_trace_short,
Dave Barach68b0fb02017-02-28 15:15:56 -05001431};
1432/* *INDENT-ON* */
1433
1434
1435VLIB_NODE_FUNCTION_MULTIARCH (tcp6_established_node, tcp6_established);
1436
1437vlib_node_registration_t tcp4_syn_sent_node;
1438vlib_node_registration_t tcp6_syn_sent_node;
1439
1440always_inline uword
1441tcp46_syn_sent_inline (vlib_main_t * vm, vlib_node_runtime_t * node,
1442 vlib_frame_t * from_frame, int is_ip4)
1443{
1444 tcp_main_t *tm = vnet_get_tcp_main ();
1445 u32 n_left_from, next_index, *from, *to_next;
Damjan Marion586afd72017-04-05 19:18:20 +02001446 u32 my_thread_index = vm->thread_index, errors = 0;
Dave Barach68b0fb02017-02-28 15:15:56 -05001447 u8 sst = is_ip4 ? SESSION_TYPE_IP4_TCP : SESSION_TYPE_IP6_TCP;
1448
1449 from = vlib_frame_vector_args (from_frame);
1450 n_left_from = from_frame->n_vectors;
1451
1452 next_index = node->cached_next_index;
1453
1454 while (n_left_from > 0)
1455 {
1456 u32 n_left_to_next;
1457
1458 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
1459
1460 while (n_left_from > 0 && n_left_to_next > 0)
1461 {
1462 u32 bi0, ack0, seq0;
1463 vlib_buffer_t *b0;
Clement Durand6cf260c2017-04-13 13:27:04 +02001464 tcp_rx_trace_t *t0;
Dave Barach68b0fb02017-02-28 15:15:56 -05001465 tcp_header_t *tcp0 = 0;
1466 tcp_connection_t *tc0;
Dave Barach68b0fb02017-02-28 15:15:56 -05001467 tcp_connection_t *new_tc0;
1468 u32 next0 = TCP_SYN_SENT_NEXT_DROP, error0 = TCP_ERROR_ENQUEUED;
1469
1470 bi0 = from[0];
1471 to_next[0] = bi0;
1472 from += 1;
1473 to_next += 1;
1474 n_left_from -= 1;
1475 n_left_to_next -= 1;
1476
1477 b0 = vlib_get_buffer (vm, bi0);
1478 tc0 =
1479 tcp_half_open_connection_get (vnet_buffer (b0)->
1480 tcp.connection_index);
1481
1482 ack0 = vnet_buffer (b0)->tcp.ack_number;
1483 seq0 = vnet_buffer (b0)->tcp.seq_number;
Florin Coras82b13a82017-04-25 11:58:06 -07001484 tcp0 = tcp_buffer_hdr (b0);
Dave Barach68b0fb02017-02-28 15:15:56 -05001485
1486 if (PREDICT_FALSE
1487 (!tcp_ack (tcp0) && !tcp_rst (tcp0) && !tcp_syn (tcp0)))
1488 goto drop;
1489
1490 /* SYNs, FINs and data consume sequence numbers */
1491 vnet_buffer (b0)->tcp.seq_end = seq0 + tcp_is_syn (tcp0)
Florin Coras82b13a82017-04-25 11:58:06 -07001492 + tcp_is_fin (tcp0) + vnet_buffer (b0)->tcp.data_len;
Dave Barach68b0fb02017-02-28 15:15:56 -05001493
1494 /*
1495 * 1. check the ACK bit
1496 */
1497
1498 /*
1499 * If the ACK bit is set
1500 * If SEG.ACK =< ISS, or SEG.ACK > SND.NXT, send a reset (unless
1501 * the RST bit is set, if so drop the segment and return)
1502 * <SEQ=SEG.ACK><CTL=RST>
1503 * and discard the segment. Return.
1504 * If SND.UNA =< SEG.ACK =< SND.NXT then the ACK is acceptable.
1505 */
1506 if (tcp_ack (tcp0))
1507 {
1508 if (ack0 <= tc0->iss || ack0 > tc0->snd_nxt)
1509 {
1510 if (!tcp_rst (tcp0))
1511 tcp_send_reset (b0, is_ip4);
1512
1513 goto drop;
1514 }
1515
1516 /* Make sure ACK is valid */
1517 if (tc0->snd_una > ack0)
1518 goto drop;
1519 }
1520
1521 /*
1522 * 2. check the RST bit
1523 */
1524
1525 if (tcp_rst (tcp0))
1526 {
1527 /* If ACK is acceptable, signal client that peer is not
1528 * willing to accept connection and drop connection*/
1529 if (tcp_ack (tcp0))
1530 {
1531 stream_session_connect_notify (&tc0->connection, sst,
1532 1 /* fail */ );
1533 tcp_connection_cleanup (tc0);
1534 }
1535 goto drop;
1536 }
1537
1538 /*
1539 * 3. check the security and precedence (skipped)
1540 */
1541
1542 /*
1543 * 4. check the SYN bit
1544 */
1545
1546 /* No SYN flag. Drop. */
1547 if (!tcp_syn (tcp0))
1548 goto drop;
1549
1550 /* Stop connection establishment and retransmit timers */
1551 tcp_timer_reset (tc0, TCP_TIMER_ESTABLISH);
1552 tcp_timer_reset (tc0, TCP_TIMER_RETRANSMIT_SYN);
1553
1554 /* Valid SYN or SYN-ACK. Move connection from half-open pool to
1555 * current thread pool. */
1556 pool_get (tm->connections[my_thread_index], new_tc0);
1557 clib_memcpy (new_tc0, tc0, sizeof (*new_tc0));
1558
1559 new_tc0->c_thread_index = my_thread_index;
Dave Barach259cdae2017-05-15 16:27:05 -04001560 new_tc0->c_c_index = new_tc0 - tm->connections[my_thread_index];
Dave Barach68b0fb02017-02-28 15:15:56 -05001561
1562 /* Cleanup half-open connection XXX lock */
1563 pool_put (tm->half_open_connections, tc0);
1564
1565 new_tc0->rcv_nxt = vnet_buffer (b0)->tcp.seq_end;
1566 new_tc0->irs = seq0;
1567
1568 /* Parse options */
Florin Corasdb84e572017-05-09 18:54:52 -07001569 if (tcp_options_parse (tcp0, &new_tc0->opt))
1570 goto drop;
Dave Barach68b0fb02017-02-28 15:15:56 -05001571
1572 if (tcp_opts_tstamp (&new_tc0->opt))
1573 {
1574 new_tc0->tsval_recent = new_tc0->opt.tsval;
1575 new_tc0->tsval_recent_age = tcp_time_now ();
1576 }
1577
1578 if (tcp_opts_wscale (&new_tc0->opt))
1579 new_tc0->snd_wscale = new_tc0->opt.wscale;
1580
Florin Corase04c2992017-03-01 08:17:34 -08001581 /* No scaling */
1582 new_tc0->snd_wnd = clib_net_to_host_u16 (tcp0->window);
Dave Barach68b0fb02017-02-28 15:15:56 -05001583 new_tc0->snd_wl1 = seq0;
1584 new_tc0->snd_wl2 = ack0;
1585
Florin Corase04c2992017-03-01 08:17:34 -08001586 tcp_connection_init_vars (new_tc0);
1587
Dave Barach68b0fb02017-02-28 15:15:56 -05001588 /* SYN-ACK: See if we can switch to ESTABLISHED state */
1589 if (tcp_ack (tcp0))
1590 {
1591 /* Our SYN is ACKed: we have iss < ack = snd_una */
1592
1593 /* TODO Dequeue acknowledged segments if we support Fast Open */
1594 new_tc0->snd_una = ack0;
1595 new_tc0->state = TCP_STATE_ESTABLISHED;
1596
Florin Corase04c2992017-03-01 08:17:34 -08001597 /* Make sure las is initialized for the wnd computation */
1598 new_tc0->rcv_las = new_tc0->rcv_nxt;
1599
Dave Barach68b0fb02017-02-28 15:15:56 -05001600 /* Notify app that we have connection */
1601 stream_session_connect_notify (&new_tc0->connection, sst, 0);
1602
Florin Corasc28764f2017-04-26 00:08:42 -07001603 stream_session_init_fifos_pointers (&new_tc0->connection,
1604 new_tc0->irs + 1,
1605 new_tc0->iss + 1);
Dave Barach68b0fb02017-02-28 15:15:56 -05001606 /* Make sure after data segment processing ACK is sent */
1607 new_tc0->flags |= TCP_CONN_SNDACK;
1608 }
1609 /* SYN: Simultaneous open. Change state to SYN-RCVD and send SYN-ACK */
1610 else
1611 {
1612 new_tc0->state = TCP_STATE_SYN_RCVD;
1613
Florin Corase69f4952017-03-07 10:06:24 -08001614 /* Notify app that we have connection */
Dave Barach68b0fb02017-02-28 15:15:56 -05001615 stream_session_connect_notify (&new_tc0->connection, sst, 0);
Florin Corasc28764f2017-04-26 00:08:42 -07001616 stream_session_init_fifos_pointers (&new_tc0->connection,
1617 new_tc0->irs + 1,
1618 new_tc0->iss + 1);
Dave Barach68b0fb02017-02-28 15:15:56 -05001619 tcp_make_synack (new_tc0, b0);
1620 next0 = tcp_next_output (is_ip4);
1621
1622 goto drop;
1623 }
1624
1625 /* Read data, if any */
Florin Coras82b13a82017-04-25 11:58:06 -07001626 if (vnet_buffer (b0)->tcp.data_len)
Dave Barach68b0fb02017-02-28 15:15:56 -05001627 {
Florin Coras82b13a82017-04-25 11:58:06 -07001628 vlib_buffer_advance (b0, vnet_buffer (b0)->tcp.data_offset);
1629 error0 = tcp_segment_rcv (tm, new_tc0, b0,
1630 vnet_buffer (b0)->tcp.data_len,
1631 &next0);
Dave Barach68b0fb02017-02-28 15:15:56 -05001632 if (error0 == TCP_ERROR_PURE_ACK)
1633 error0 = TCP_ERROR_SYN_ACKS_RCVD;
1634 }
1635 else
1636 {
1637 tcp_make_ack (new_tc0, b0);
1638 next0 = tcp_next_output (new_tc0->c_is_ip4);
1639 }
1640
1641 drop:
1642
1643 b0->error = error0 ? node->errors[error0] : 0;
1644 if (PREDICT_FALSE (b0->flags & VLIB_BUFFER_IS_TRACED))
1645 {
Clement Durand6cf260c2017-04-13 13:27:04 +02001646 t0 = vlib_add_trace (vm, node, b0, sizeof (*t0));
1647 clib_memcpy (&t0->tcp_header, tcp0, sizeof (t0->tcp_header));
1648 clib_memcpy (&t0->tcp_connection, tc0,
1649 sizeof (t0->tcp_connection));
Dave Barach68b0fb02017-02-28 15:15:56 -05001650 }
1651
1652 vlib_validate_buffer_enqueue_x1 (vm, node, next_index, to_next,
1653 n_left_to_next, bi0, next0);
1654 }
1655
1656 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
1657 }
1658
1659 errors = session_manager_flush_enqueue_events (my_thread_index);
1660 if (errors)
1661 {
1662 if (is_ip4)
1663 vlib_node_increment_counter (vm, tcp4_established_node.index,
1664 TCP_ERROR_EVENT_FIFO_FULL, errors);
1665 else
1666 vlib_node_increment_counter (vm, tcp6_established_node.index,
1667 TCP_ERROR_EVENT_FIFO_FULL, errors);
1668 }
1669
1670 return from_frame->n_vectors;
1671}
1672
1673static uword
1674tcp4_syn_sent (vlib_main_t * vm, vlib_node_runtime_t * node,
1675 vlib_frame_t * from_frame)
1676{
1677 return tcp46_syn_sent_inline (vm, node, from_frame, 1 /* is_ip4 */ );
1678}
1679
1680static uword
1681tcp6_syn_sent_rcv (vlib_main_t * vm, vlib_node_runtime_t * node,
1682 vlib_frame_t * from_frame)
1683{
1684 return tcp46_syn_sent_inline (vm, node, from_frame, 0 /* is_ip4 */ );
1685}
1686
1687/* *INDENT-OFF* */
1688VLIB_REGISTER_NODE (tcp4_syn_sent_node) =
1689{
1690 .function = tcp4_syn_sent,
1691 .name = "tcp4-syn-sent",
1692 /* Takes a vector of packets. */
1693 .vector_size = sizeof (u32),
1694 .n_errors = TCP_N_ERROR,
1695 .error_strings = tcp_error_strings,
1696 .n_next_nodes = TCP_SYN_SENT_N_NEXT,
1697 .next_nodes =
1698 {
1699#define _(s,n) [TCP_SYN_SENT_NEXT_##s] = n,
1700 foreach_tcp_state_next
1701#undef _
1702 },
Clement Durand6cf260c2017-04-13 13:27:04 +02001703 .format_trace = format_tcp_rx_trace_short,
Dave Barach68b0fb02017-02-28 15:15:56 -05001704};
1705/* *INDENT-ON* */
1706
1707VLIB_NODE_FUNCTION_MULTIARCH (tcp4_syn_sent_node, tcp4_syn_sent);
1708
1709/* *INDENT-OFF* */
1710VLIB_REGISTER_NODE (tcp6_syn_sent_node) =
1711{
1712 .function = tcp6_syn_sent_rcv,
1713 .name = "tcp6-syn-sent",
1714 /* Takes a vector of packets. */
1715 .vector_size = sizeof (u32),
1716 .n_errors = TCP_N_ERROR,
1717 .error_strings = tcp_error_strings,
1718 .n_next_nodes = TCP_SYN_SENT_N_NEXT,
1719 .next_nodes =
1720 {
1721#define _(s,n) [TCP_SYN_SENT_NEXT_##s] = n,
1722 foreach_tcp_state_next
1723#undef _
Clement Durand6cf260c2017-04-13 13:27:04 +02001724 },
1725 .format_trace = format_tcp_rx_trace_short,
1726};
Dave Barach68b0fb02017-02-28 15:15:56 -05001727/* *INDENT-ON* */
1728
1729VLIB_NODE_FUNCTION_MULTIARCH (tcp6_syn_sent_node, tcp6_syn_sent_rcv);
1730/**
Florin Corasd79b41e2017-03-04 05:37:52 -08001731 * Handles reception for all states except LISTEN, SYN-SENT and ESTABLISHED
Dave Barach68b0fb02017-02-28 15:15:56 -05001732 * as per RFC793 p. 64
1733 */
1734always_inline uword
1735tcp46_rcv_process_inline (vlib_main_t * vm, vlib_node_runtime_t * node,
1736 vlib_frame_t * from_frame, int is_ip4)
1737{
1738 tcp_main_t *tm = vnet_get_tcp_main ();
1739 u32 n_left_from, next_index, *from, *to_next;
Damjan Marion586afd72017-04-05 19:18:20 +02001740 u32 my_thread_index = vm->thread_index, errors = 0;
Dave Barach68b0fb02017-02-28 15:15:56 -05001741
1742 from = vlib_frame_vector_args (from_frame);
1743 n_left_from = from_frame->n_vectors;
1744
1745 next_index = node->cached_next_index;
1746
1747 while (n_left_from > 0)
1748 {
1749 u32 n_left_to_next;
1750
1751 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
1752
1753 while (n_left_from > 0 && n_left_to_next > 0)
1754 {
1755 u32 bi0;
1756 vlib_buffer_t *b0;
1757 tcp_header_t *tcp0 = 0;
1758 tcp_connection_t *tc0;
Dave Barach68b0fb02017-02-28 15:15:56 -05001759 u32 next0 = TCP_RCV_PROCESS_NEXT_DROP, error0 = TCP_ERROR_ENQUEUED;
1760
1761 bi0 = from[0];
1762 to_next[0] = bi0;
1763 from += 1;
1764 to_next += 1;
1765 n_left_from -= 1;
1766 n_left_to_next -= 1;
1767
1768 b0 = vlib_get_buffer (vm, bi0);
1769 tc0 = tcp_connection_get (vnet_buffer (b0)->tcp.connection_index,
1770 my_thread_index);
Florin Corasd79b41e2017-03-04 05:37:52 -08001771 if (PREDICT_FALSE (tc0 == 0))
1772 {
1773 error0 = TCP_ERROR_INVALID_CONNECTION;
1774 goto drop;
1775 }
Dave Barach68b0fb02017-02-28 15:15:56 -05001776
Florin Coras82b13a82017-04-25 11:58:06 -07001777 tcp0 = tcp_buffer_hdr (b0);
Dave Barach68b0fb02017-02-28 15:15:56 -05001778
1779 /* SYNs, FINs and data consume sequence numbers */
1780 vnet_buffer (b0)->tcp.seq_end = vnet_buffer (b0)->tcp.seq_number
Florin Coras82b13a82017-04-25 11:58:06 -07001781 + tcp_is_syn (tcp0) + tcp_is_fin (tcp0)
1782 + vnet_buffer (b0)->tcp.data_len;
Dave Barach68b0fb02017-02-28 15:15:56 -05001783
1784 /*
1785 * Special treatment for CLOSED
1786 */
1787 switch (tc0->state)
1788 {
1789 case TCP_STATE_CLOSED:
1790 goto drop;
1791 break;
1792 }
1793
1794 /*
1795 * For all other states (except LISTEN)
1796 */
1797
1798 /* 1-4: check SEQ, RST, SYN */
1799 if (PREDICT_FALSE
1800 (tcp_segment_validate (vm, tc0, b0, tcp0, &next0)))
1801 {
1802 error0 = TCP_ERROR_SEGMENT_INVALID;
1803 goto drop;
1804 }
1805
1806 /* 5: check the ACK field */
1807 switch (tc0->state)
1808 {
1809 case TCP_STATE_SYN_RCVD:
1810 /*
1811 * If the segment acknowledgment is not acceptable, form a
1812 * reset segment,
1813 * <SEQ=SEG.ACK><CTL=RST>
1814 * and send it.
1815 */
1816 if (!tcp_rcv_ack_is_acceptable (tc0, b0))
1817 {
1818 tcp_send_reset (b0, is_ip4);
1819 goto drop;
1820 }
Florin Coras3af90fc2017-05-03 21:09:42 -07001821
1822 /* Update rtt and rto */
1823 tc0->bytes_acked = 1;
1824 tcp_update_rtt (tc0, vnet_buffer (b0)->tcp.ack_number);
1825
Dave Barach68b0fb02017-02-28 15:15:56 -05001826 /* Switch state to ESTABLISHED */
1827 tc0->state = TCP_STATE_ESTABLISHED;
1828
1829 /* Initialize session variables */
1830 tc0->snd_una = vnet_buffer (b0)->tcp.ack_number;
Florin Corase04c2992017-03-01 08:17:34 -08001831 tc0->snd_wnd = clib_net_to_host_u16 (tcp0->window)
Dave Barach68b0fb02017-02-28 15:15:56 -05001832 << tc0->opt.wscale;
1833 tc0->snd_wl1 = vnet_buffer (b0)->tcp.seq_number;
1834 tc0->snd_wl2 = vnet_buffer (b0)->tcp.ack_number;
1835
1836 /* Shoulder tap the server */
1837 stream_session_accept_notify (&tc0->connection);
1838
Florin Corasd79b41e2017-03-04 05:37:52 -08001839 /* Reset SYN-ACK retransmit timer */
Florin Coras6792ec02017-03-13 03:49:51 -07001840 tcp_retransmit_timer_reset (tc0);
Dave Barach68b0fb02017-02-28 15:15:56 -05001841 break;
1842 case TCP_STATE_ESTABLISHED:
1843 /* We can get packets in established state here because they
1844 * were enqueued before state change */
1845 if (tcp_rcv_ack (tc0, b0, tcp0, &next0, &error0))
1846 goto drop;
1847
1848 break;
1849 case TCP_STATE_FIN_WAIT_1:
1850 /* In addition to the processing for the ESTABLISHED state, if
1851 * our FIN is now acknowledged then enter FIN-WAIT-2 and
1852 * continue processing in that state. */
1853 if (tcp_rcv_ack (tc0, b0, tcp0, &next0, &error0))
1854 goto drop;
Florin Corasd79b41e2017-03-04 05:37:52 -08001855
1856 /* If FIN is ACKed */
1857 if (tc0->snd_una == tc0->snd_una_max)
1858 {
1859 tc0->state = TCP_STATE_FIN_WAIT_2;
1860 /* Stop all timers, 2MSL will be set lower */
1861 tcp_connection_timers_reset (tc0);
1862 }
Dave Barach68b0fb02017-02-28 15:15:56 -05001863 break;
1864 case TCP_STATE_FIN_WAIT_2:
1865 /* In addition to the processing for the ESTABLISHED state, if
1866 * the retransmission queue is empty, the user's CLOSE can be
1867 * acknowledged ("ok") but do not delete the TCB. */
1868 if (tcp_rcv_ack (tc0, b0, tcp0, &next0, &error0))
1869 goto drop;
1870 /* check if rtx queue is empty and ack CLOSE TODO */
1871 break;
1872 case TCP_STATE_CLOSE_WAIT:
1873 /* Do the same processing as for the ESTABLISHED state. */
1874 if (tcp_rcv_ack (tc0, b0, tcp0, &next0, &error0))
1875 goto drop;
1876 break;
1877 case TCP_STATE_CLOSING:
1878 /* In addition to the processing for the ESTABLISHED state, if
1879 * the ACK acknowledges our FIN then enter the TIME-WAIT state,
1880 * otherwise ignore the segment. */
1881 if (tcp_rcv_ack (tc0, b0, tcp0, &next0, &error0))
1882 goto drop;
1883
1884 /* XXX test that send queue empty */
1885 tc0->state = TCP_STATE_TIME_WAIT;
1886 goto drop;
1887
1888 break;
1889 case TCP_STATE_LAST_ACK:
1890 /* The only thing that can arrive in this state is an
1891 * acknowledgment of our FIN. If our FIN is now acknowledged,
1892 * delete the TCB, enter the CLOSED state, and return. */
1893
1894 if (!tcp_rcv_ack_is_acceptable (tc0, b0))
1895 goto drop;
1896
Florin Corasd79b41e2017-03-04 05:37:52 -08001897 tc0->state = TCP_STATE_CLOSED;
1898
1899 /* Don't delete the connection/session yet. Instead, wait a
1900 * reasonable amount of time until the pipes are cleared. In
1901 * particular, this makes sure that we won't have dead sessions
1902 * when processing events on the tx path */
1903 tcp_timer_update (tc0, TCP_TIMER_WAITCLOSE, TCP_CLEANUP_TIME);
1904
1905 /* Stop retransmit */
Florin Coras6792ec02017-03-13 03:49:51 -07001906 tcp_retransmit_timer_reset (tc0);
Florin Corasd79b41e2017-03-04 05:37:52 -08001907
Dave Barach68b0fb02017-02-28 15:15:56 -05001908 goto drop;
1909
1910 break;
1911 case TCP_STATE_TIME_WAIT:
1912 /* The only thing that can arrive in this state is a
1913 * retransmission of the remote FIN. Acknowledge it, and restart
1914 * the 2 MSL timeout. */
1915
1916 /* TODO */
1917 goto drop;
1918 break;
1919 default:
1920 ASSERT (0);
1921 }
1922
1923 /* 6: check the URG bit TODO */
1924
1925 /* 7: process the segment text */
1926 switch (tc0->state)
1927 {
1928 case TCP_STATE_ESTABLISHED:
1929 case TCP_STATE_FIN_WAIT_1:
1930 case TCP_STATE_FIN_WAIT_2:
Florin Coras82b13a82017-04-25 11:58:06 -07001931 vlib_buffer_advance (b0, vnet_buffer (b0)->tcp.data_offset);
1932 error0 = tcp_segment_rcv (tm, tc0, b0,
1933 vnet_buffer (b0)->tcp.data_len,
1934 &next0);
Dave Barach68b0fb02017-02-28 15:15:56 -05001935 break;
1936 case TCP_STATE_CLOSE_WAIT:
1937 case TCP_STATE_CLOSING:
1938 case TCP_STATE_LAST_ACK:
1939 case TCP_STATE_TIME_WAIT:
1940 /* This should not occur, since a FIN has been received from the
1941 * remote side. Ignore the segment text. */
1942 break;
1943 }
1944
1945 /* 8: check the FIN bit */
1946 if (!tcp_fin (tcp0))
1947 goto drop;
1948
1949 switch (tc0->state)
1950 {
1951 case TCP_STATE_ESTABLISHED:
1952 case TCP_STATE_SYN_RCVD:
1953 /* Send FIN-ACK notify app and enter CLOSE-WAIT */
1954 tcp_connection_timers_reset (tc0);
Florin Corasd79b41e2017-03-04 05:37:52 -08001955 tcp_make_fin (tc0, b0);
Dave Barach68b0fb02017-02-28 15:15:56 -05001956 next0 = tcp_next_output (tc0->c_is_ip4);
1957 stream_session_disconnect_notify (&tc0->connection);
1958 tc0->state = TCP_STATE_CLOSE_WAIT;
1959 break;
1960 case TCP_STATE_CLOSE_WAIT:
1961 case TCP_STATE_CLOSING:
1962 case TCP_STATE_LAST_ACK:
1963 /* move along .. */
1964 break;
1965 case TCP_STATE_FIN_WAIT_1:
1966 tc0->state = TCP_STATE_TIME_WAIT;
1967 tcp_connection_timers_reset (tc0);
Florin Corasd79b41e2017-03-04 05:37:52 -08001968 tcp_timer_set (tc0, TCP_TIMER_WAITCLOSE, TCP_2MSL_TIME);
Dave Barach68b0fb02017-02-28 15:15:56 -05001969 break;
1970 case TCP_STATE_FIN_WAIT_2:
1971 /* Got FIN, send ACK! */
1972 tc0->state = TCP_STATE_TIME_WAIT;
Florin Corasdb84e572017-05-09 18:54:52 -07001973 tcp_connection_timers_reset (tc0);
Florin Corase69f4952017-03-07 10:06:24 -08001974 tcp_timer_set (tc0, TCP_TIMER_WAITCLOSE, TCP_CLOSEWAIT_TIME);
Dave Barach68b0fb02017-02-28 15:15:56 -05001975 tcp_make_ack (tc0, b0);
1976 next0 = tcp_next_output (is_ip4);
1977 break;
1978 case TCP_STATE_TIME_WAIT:
1979 /* Remain in the TIME-WAIT state. Restart the 2 MSL time-wait
1980 * timeout.
1981 */
Florin Corasd79b41e2017-03-04 05:37:52 -08001982 tcp_timer_update (tc0, TCP_TIMER_WAITCLOSE, TCP_2MSL_TIME);
Dave Barach68b0fb02017-02-28 15:15:56 -05001983 break;
1984 }
Florin Corase69f4952017-03-07 10:06:24 -08001985 TCP_EVT_DBG (TCP_EVT_FIN_RCVD, tc0);
Dave Barach68b0fb02017-02-28 15:15:56 -05001986
Florin Coras82b13a82017-04-25 11:58:06 -07001987 drop:
Dave Barach68b0fb02017-02-28 15:15:56 -05001988 b0->error = error0 ? node->errors[error0] : 0;
1989
Dave Barach68b0fb02017-02-28 15:15:56 -05001990 if (PREDICT_FALSE (b0->flags & VLIB_BUFFER_IS_TRACED))
1991 {
Florin Coras82b13a82017-04-25 11:58:06 -07001992 tcp_rx_trace_t *t0 =
1993 vlib_add_trace (vm, node, b0, sizeof (*t0));
1994 tcp_set_rx_trace_data (t0, tc0, tcp0, b0, is_ip4);
Dave Barach68b0fb02017-02-28 15:15:56 -05001995 }
1996
1997 vlib_validate_buffer_enqueue_x1 (vm, node, next_index, to_next,
1998 n_left_to_next, bi0, next0);
1999 }
2000
2001 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
2002 }
2003
2004 errors = session_manager_flush_enqueue_events (my_thread_index);
2005 if (errors)
2006 {
2007 if (is_ip4)
2008 vlib_node_increment_counter (vm, tcp4_established_node.index,
2009 TCP_ERROR_EVENT_FIFO_FULL, errors);
2010 else
2011 vlib_node_increment_counter (vm, tcp6_established_node.index,
2012 TCP_ERROR_EVENT_FIFO_FULL, errors);
2013 }
2014
2015 return from_frame->n_vectors;
2016}
2017
2018static uword
2019tcp4_rcv_process (vlib_main_t * vm, vlib_node_runtime_t * node,
2020 vlib_frame_t * from_frame)
2021{
2022 return tcp46_rcv_process_inline (vm, node, from_frame, 1 /* is_ip4 */ );
2023}
2024
2025static uword
2026tcp6_rcv_process (vlib_main_t * vm, vlib_node_runtime_t * node,
2027 vlib_frame_t * from_frame)
2028{
2029 return tcp46_rcv_process_inline (vm, node, from_frame, 0 /* is_ip4 */ );
2030}
2031
2032/* *INDENT-OFF* */
2033VLIB_REGISTER_NODE (tcp4_rcv_process_node) =
2034{
2035 .function = tcp4_rcv_process,
2036 .name = "tcp4-rcv-process",
2037 /* Takes a vector of packets. */
2038 .vector_size = sizeof (u32),
2039 .n_errors = TCP_N_ERROR,
2040 .error_strings = tcp_error_strings,
2041 .n_next_nodes = TCP_RCV_PROCESS_N_NEXT,
2042 .next_nodes =
2043 {
2044#define _(s,n) [TCP_RCV_PROCESS_NEXT_##s] = n,
2045 foreach_tcp_state_next
2046#undef _
2047 },
Clement Durand6cf260c2017-04-13 13:27:04 +02002048 .format_trace = format_tcp_rx_trace_short,
Dave Barach68b0fb02017-02-28 15:15:56 -05002049};
2050/* *INDENT-ON* */
2051
2052VLIB_NODE_FUNCTION_MULTIARCH (tcp4_rcv_process_node, tcp4_rcv_process);
2053
2054/* *INDENT-OFF* */
2055VLIB_REGISTER_NODE (tcp6_rcv_process_node) =
2056{
2057 .function = tcp6_rcv_process,
2058 .name = "tcp6-rcv-process",
2059 /* Takes a vector of packets. */
2060 .vector_size = sizeof (u32),
2061 .n_errors = TCP_N_ERROR,
2062 .error_strings = tcp_error_strings,
2063 .n_next_nodes = TCP_RCV_PROCESS_N_NEXT,
2064 .next_nodes =
2065 {
2066#define _(s,n) [TCP_RCV_PROCESS_NEXT_##s] = n,
2067 foreach_tcp_state_next
2068#undef _
2069 },
Clement Durand6cf260c2017-04-13 13:27:04 +02002070 .format_trace = format_tcp_rx_trace_short,
Dave Barach68b0fb02017-02-28 15:15:56 -05002071};
2072/* *INDENT-ON* */
2073
2074VLIB_NODE_FUNCTION_MULTIARCH (tcp6_rcv_process_node, tcp6_rcv_process);
2075
2076vlib_node_registration_t tcp4_listen_node;
2077vlib_node_registration_t tcp6_listen_node;
2078
2079/**
2080 * LISTEN state processing as per RFC 793 p. 65
2081 */
2082always_inline uword
2083tcp46_listen_inline (vlib_main_t * vm, vlib_node_runtime_t * node,
2084 vlib_frame_t * from_frame, int is_ip4)
2085{
2086 u32 n_left_from, next_index, *from, *to_next;
Damjan Marion586afd72017-04-05 19:18:20 +02002087 u32 my_thread_index = vm->thread_index;
Dave Barach68b0fb02017-02-28 15:15:56 -05002088 tcp_main_t *tm = vnet_get_tcp_main ();
2089 u8 sst = is_ip4 ? SESSION_TYPE_IP4_TCP : SESSION_TYPE_IP6_TCP;
2090
2091 from = vlib_frame_vector_args (from_frame);
2092 n_left_from = from_frame->n_vectors;
2093
2094 next_index = node->cached_next_index;
2095
2096 while (n_left_from > 0)
2097 {
2098 u32 n_left_to_next;
2099
2100 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
2101
2102 while (n_left_from > 0 && n_left_to_next > 0)
2103 {
2104 u32 bi0;
2105 vlib_buffer_t *b0;
Clement Durand6cf260c2017-04-13 13:27:04 +02002106 tcp_rx_trace_t *t0;
Dave Barach68b0fb02017-02-28 15:15:56 -05002107 tcp_header_t *th0 = 0;
2108 tcp_connection_t *lc0;
2109 ip4_header_t *ip40;
2110 ip6_header_t *ip60;
2111 tcp_connection_t *child0;
2112 u32 error0 = TCP_ERROR_SYNS_RCVD, next0 = TCP_LISTEN_NEXT_DROP;
2113
2114 bi0 = from[0];
2115 to_next[0] = bi0;
2116 from += 1;
2117 to_next += 1;
2118 n_left_from -= 1;
2119 n_left_to_next -= 1;
2120
2121 b0 = vlib_get_buffer (vm, bi0);
2122 lc0 = tcp_listener_get (vnet_buffer (b0)->tcp.connection_index);
2123
2124 if (is_ip4)
2125 {
2126 ip40 = vlib_buffer_get_current (b0);
2127 th0 = ip4_next_header (ip40);
2128 }
2129 else
2130 {
2131 ip60 = vlib_buffer_get_current (b0);
2132 th0 = ip6_next_header (ip60);
2133 }
2134
2135 /* Create child session. For syn-flood protection use filter */
2136
Florin Corasdc629cd2017-05-09 00:52:37 -07002137 /* 1. first check for an RST: handled in dispatch */
2138 /* if (tcp_rst (th0))
2139 goto drop; */
Dave Barach68b0fb02017-02-28 15:15:56 -05002140
Florin Corasdc629cd2017-05-09 00:52:37 -07002141 /* 2. second check for an ACK: handled in dispatch */
2142 /* if (tcp_ack (th0))
2143 {
2144 tcp_send_reset (b0, is_ip4);
2145 goto drop;
2146 } */
Dave Barach68b0fb02017-02-28 15:15:56 -05002147
2148 /* 3. check for a SYN (did that already) */
2149
2150 /* Create child session and send SYN-ACK */
2151 pool_get (tm->connections[my_thread_index], child0);
2152 memset (child0, 0, sizeof (*child0));
2153
2154 child0->c_c_index = child0 - tm->connections[my_thread_index];
2155 child0->c_lcl_port = lc0->c_lcl_port;
2156 child0->c_rmt_port = th0->src_port;
2157 child0->c_is_ip4 = is_ip4;
2158 child0->c_thread_index = my_thread_index;
2159
2160 if (is_ip4)
2161 {
2162 child0->c_lcl_ip4.as_u32 = ip40->dst_address.as_u32;
2163 child0->c_rmt_ip4.as_u32 = ip40->src_address.as_u32;
2164 }
2165 else
2166 {
2167 clib_memcpy (&child0->c_lcl_ip6, &ip60->dst_address,
2168 sizeof (ip6_address_t));
2169 clib_memcpy (&child0->c_rmt_ip6, &ip60->src_address,
2170 sizeof (ip6_address_t));
2171 }
2172
2173 if (stream_session_accept (&child0->connection, lc0->c_s_index, sst,
2174 0 /* notify */ ))
2175 {
2176 error0 = TCP_ERROR_CREATE_SESSION_FAIL;
2177 goto drop;
2178 }
2179
Florin Corasdb84e572017-05-09 18:54:52 -07002180 if (tcp_options_parse (th0, &child0->opt))
2181 {
2182 goto drop;
2183 }
Dave Barach68b0fb02017-02-28 15:15:56 -05002184
2185 child0->irs = vnet_buffer (b0)->tcp.seq_number;
2186 child0->rcv_nxt = vnet_buffer (b0)->tcp.seq_number + 1;
Florin Coras3e350af2017-03-30 02:54:28 -07002187 child0->rcv_las = child0->rcv_nxt;
Dave Barach68b0fb02017-02-28 15:15:56 -05002188 child0->state = TCP_STATE_SYN_RCVD;
2189
2190 /* RFC1323: TSval timestamps sent on {SYN} and {SYN,ACK}
2191 * segments are used to initialize PAWS. */
2192 if (tcp_opts_tstamp (&child0->opt))
2193 {
2194 child0->tsval_recent = child0->opt.tsval;
2195 child0->tsval_recent_age = tcp_time_now ();
2196 }
2197
Florin Corase04c2992017-03-01 08:17:34 -08002198 if (tcp_opts_wscale (&child0->opt))
2199 child0->snd_wscale = child0->opt.wscale;
2200
2201 /* No scaling */
2202 child0->snd_wnd = clib_net_to_host_u16 (th0->window);
2203 child0->snd_wl1 = vnet_buffer (b0)->tcp.seq_number;
2204 child0->snd_wl2 = vnet_buffer (b0)->tcp.ack_number;
2205
2206 tcp_connection_init_vars (child0);
2207
Florin Corase69f4952017-03-07 10:06:24 -08002208 TCP_EVT_DBG (TCP_EVT_SYN_RCVD, child0);
2209
Dave Barach68b0fb02017-02-28 15:15:56 -05002210 /* Reuse buffer to make syn-ack and send */
2211 tcp_make_synack (child0, b0);
2212 next0 = tcp_next_output (is_ip4);
2213
Florin Corasc28764f2017-04-26 00:08:42 -07002214 /* Init fifo pointers after we have iss */
2215 stream_session_init_fifos_pointers (&child0->connection,
2216 child0->irs + 1,
2217 child0->iss + 1);
Dave Barach68b0fb02017-02-28 15:15:56 -05002218 drop:
2219 if (PREDICT_FALSE (b0->flags & VLIB_BUFFER_IS_TRACED))
2220 {
Clement Durand6cf260c2017-04-13 13:27:04 +02002221 t0 = vlib_add_trace (vm, node, b0, sizeof (*t0));
2222 clib_memcpy (&t0->tcp_header, th0, sizeof (t0->tcp_header));
2223 clib_memcpy (&t0->tcp_connection, lc0,
2224 sizeof (t0->tcp_connection));
Dave Barach68b0fb02017-02-28 15:15:56 -05002225 }
2226
Florin Corase04c2992017-03-01 08:17:34 -08002227 b0->error = node->errors[error0];
Dave Barach68b0fb02017-02-28 15:15:56 -05002228
2229 vlib_validate_buffer_enqueue_x1 (vm, node, next_index, to_next,
2230 n_left_to_next, bi0, next0);
2231 }
2232
2233 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
2234 }
2235 return from_frame->n_vectors;
2236}
2237
2238static uword
2239tcp4_listen (vlib_main_t * vm, vlib_node_runtime_t * node,
2240 vlib_frame_t * from_frame)
2241{
2242 return tcp46_listen_inline (vm, node, from_frame, 1 /* is_ip4 */ );
2243}
2244
2245static uword
2246tcp6_listen (vlib_main_t * vm, vlib_node_runtime_t * node,
2247 vlib_frame_t * from_frame)
2248{
2249 return tcp46_listen_inline (vm, node, from_frame, 0 /* is_ip4 */ );
2250}
2251
2252/* *INDENT-OFF* */
2253VLIB_REGISTER_NODE (tcp4_listen_node) =
2254{
2255 .function = tcp4_listen,
2256 .name = "tcp4-listen",
2257 /* Takes a vector of packets. */
2258 .vector_size = sizeof (u32),
2259 .n_errors = TCP_N_ERROR,
2260 .error_strings = tcp_error_strings,
2261 .n_next_nodes = TCP_LISTEN_N_NEXT,
2262 .next_nodes =
2263 {
2264#define _(s,n) [TCP_LISTEN_NEXT_##s] = n,
2265 foreach_tcp_state_next
2266#undef _
2267 },
Clement Durand6cf260c2017-04-13 13:27:04 +02002268 .format_trace = format_tcp_rx_trace_short,
Dave Barach68b0fb02017-02-28 15:15:56 -05002269};
2270/* *INDENT-ON* */
2271
2272VLIB_NODE_FUNCTION_MULTIARCH (tcp4_listen_node, tcp4_listen);
2273
2274/* *INDENT-OFF* */
2275VLIB_REGISTER_NODE (tcp6_listen_node) =
2276{
2277 .function = tcp6_listen,
2278 .name = "tcp6-listen",
2279 /* Takes a vector of packets. */
2280 .vector_size = sizeof (u32),
2281 .n_errors = TCP_N_ERROR,
2282 .error_strings = tcp_error_strings,
2283 .n_next_nodes = TCP_LISTEN_N_NEXT,
2284 .next_nodes =
2285 {
2286#define _(s,n) [TCP_LISTEN_NEXT_##s] = n,
2287 foreach_tcp_state_next
2288#undef _
2289 },
Clement Durand6cf260c2017-04-13 13:27:04 +02002290 .format_trace = format_tcp_rx_trace_short,
Dave Barach68b0fb02017-02-28 15:15:56 -05002291};
2292/* *INDENT-ON* */
2293
2294VLIB_NODE_FUNCTION_MULTIARCH (tcp6_listen_node, tcp6_listen);
2295
2296vlib_node_registration_t tcp4_input_node;
2297vlib_node_registration_t tcp6_input_node;
2298
2299typedef enum _tcp_input_next
2300{
2301 TCP_INPUT_NEXT_DROP,
2302 TCP_INPUT_NEXT_LISTEN,
2303 TCP_INPUT_NEXT_RCV_PROCESS,
2304 TCP_INPUT_NEXT_SYN_SENT,
2305 TCP_INPUT_NEXT_ESTABLISHED,
2306 TCP_INPUT_NEXT_RESET,
2307 TCP_INPUT_N_NEXT
2308} tcp_input_next_t;
2309
2310#define foreach_tcp4_input_next \
2311 _ (DROP, "error-drop") \
2312 _ (LISTEN, "tcp4-listen") \
2313 _ (RCV_PROCESS, "tcp4-rcv-process") \
2314 _ (SYN_SENT, "tcp4-syn-sent") \
2315 _ (ESTABLISHED, "tcp4-established") \
2316 _ (RESET, "tcp4-reset")
2317
2318#define foreach_tcp6_input_next \
2319 _ (DROP, "error-drop") \
2320 _ (LISTEN, "tcp6-listen") \
2321 _ (RCV_PROCESS, "tcp6-rcv-process") \
2322 _ (SYN_SENT, "tcp6-syn-sent") \
2323 _ (ESTABLISHED, "tcp6-established") \
2324 _ (RESET, "tcp6-reset")
2325
Dave Barach68b0fb02017-02-28 15:15:56 -05002326#define filter_flags (TCP_FLAG_SYN|TCP_FLAG_ACK|TCP_FLAG_RST|TCP_FLAG_FIN)
2327
2328always_inline uword
2329tcp46_input_inline (vlib_main_t * vm, vlib_node_runtime_t * node,
2330 vlib_frame_t * from_frame, int is_ip4)
2331{
2332 u32 n_left_from, next_index, *from, *to_next;
Damjan Marion586afd72017-04-05 19:18:20 +02002333 u32 my_thread_index = vm->thread_index;
Dave Barach68b0fb02017-02-28 15:15:56 -05002334 tcp_main_t *tm = vnet_get_tcp_main ();
Dave Barach68b0fb02017-02-28 15:15:56 -05002335
2336 from = vlib_frame_vector_args (from_frame);
2337 n_left_from = from_frame->n_vectors;
2338
2339 next_index = node->cached_next_index;
2340
2341 while (n_left_from > 0)
2342 {
2343 u32 n_left_to_next;
2344
2345 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
2346
2347 while (n_left_from > 0 && n_left_to_next > 0)
2348 {
Florin Coras82b13a82017-04-25 11:58:06 -07002349 int n_advance_bytes0, n_data_bytes0;
Dave Barach68b0fb02017-02-28 15:15:56 -05002350 u32 bi0;
2351 vlib_buffer_t *b0;
2352 tcp_header_t *tcp0 = 0;
2353 tcp_connection_t *tc0;
2354 ip4_header_t *ip40;
2355 ip6_header_t *ip60;
2356 u32 error0 = TCP_ERROR_NO_LISTENER, next0 = TCP_INPUT_NEXT_DROP;
2357 u8 flags0;
2358
2359 bi0 = from[0];
2360 to_next[0] = bi0;
2361 from += 1;
2362 to_next += 1;
2363 n_left_from -= 1;
2364 n_left_to_next -= 1;
2365
2366 b0 = vlib_get_buffer (vm, bi0);
Florin Corasd79b41e2017-03-04 05:37:52 -08002367 vnet_buffer (b0)->tcp.flags = 0;
Dave Barach68b0fb02017-02-28 15:15:56 -05002368
Florin Coras82b13a82017-04-25 11:58:06 -07002369 /* Checksum computed by ipx_local no need to compute again */
2370
Dave Barach68b0fb02017-02-28 15:15:56 -05002371 if (is_ip4)
2372 {
2373 ip40 = vlib_buffer_get_current (b0);
2374 tcp0 = ip4_next_header (ip40);
Florin Coras82b13a82017-04-25 11:58:06 -07002375 n_advance_bytes0 = (ip4_header_bytes (ip40)
2376 + tcp_header_bytes (tcp0));
2377 n_data_bytes0 = clib_net_to_host_u16 (ip40->length)
2378 - n_advance_bytes0;
Dave Barach68b0fb02017-02-28 15:15:56 -05002379
2380 /* lookup session */
2381 tc0 =
Florin Corase04c2992017-03-01 08:17:34 -08002382 (tcp_connection_t *)
2383 stream_session_lookup_transport4 (&ip40->dst_address,
2384 &ip40->src_address,
2385 tcp0->dst_port,
2386 tcp0->src_port,
2387 SESSION_TYPE_IP4_TCP,
2388 my_thread_index);
Dave Barach68b0fb02017-02-28 15:15:56 -05002389 }
2390 else
2391 {
2392 ip60 = vlib_buffer_get_current (b0);
2393 tcp0 = ip6_next_header (ip60);
Florin Coras82b13a82017-04-25 11:58:06 -07002394 n_advance_bytes0 = tcp_header_bytes (tcp0);
2395 n_data_bytes0 = clib_net_to_host_u16 (ip60->payload_length)
2396 - n_advance_bytes0;
2397 n_advance_bytes0 += sizeof (ip60[0]);
2398
Dave Barach68b0fb02017-02-28 15:15:56 -05002399 tc0 =
Florin Corase04c2992017-03-01 08:17:34 -08002400 (tcp_connection_t *)
2401 stream_session_lookup_transport6 (&ip60->src_address,
2402 &ip60->dst_address,
2403 tcp0->src_port,
2404 tcp0->dst_port,
2405 SESSION_TYPE_IP6_TCP,
2406 my_thread_index);
Dave Barach68b0fb02017-02-28 15:15:56 -05002407 }
2408
Florin Coras82b13a82017-04-25 11:58:06 -07002409 /* Length check */
2410 if (PREDICT_FALSE (n_advance_bytes0 < 0))
2411 {
2412 error0 = TCP_ERROR_LENGTH;
2413 goto done;
2414 }
2415
Dave Barach68b0fb02017-02-28 15:15:56 -05002416 /* Session exists */
2417 if (PREDICT_TRUE (0 != tc0))
2418 {
2419 /* Save connection index */
2420 vnet_buffer (b0)->tcp.connection_index = tc0->c_c_index;
2421 vnet_buffer (b0)->tcp.seq_number =
2422 clib_net_to_host_u32 (tcp0->seq_number);
2423 vnet_buffer (b0)->tcp.ack_number =
2424 clib_net_to_host_u32 (tcp0->ack_number);
2425
Florin Coras82b13a82017-04-25 11:58:06 -07002426 vnet_buffer (b0)->tcp.hdr_offset = (u8 *) tcp0
2427 - (u8 *) vlib_buffer_get_current (b0);
2428 vnet_buffer (b0)->tcp.data_offset = n_advance_bytes0;
2429 vnet_buffer (b0)->tcp.data_len = n_data_bytes0;
2430
Dave Barach68b0fb02017-02-28 15:15:56 -05002431 flags0 = tcp0->flags & filter_flags;
2432 next0 = tm->dispatch_table[tc0->state][flags0].next;
2433 error0 = tm->dispatch_table[tc0->state][flags0].error;
2434
Florin Corasdc629cd2017-05-09 00:52:37 -07002435 if (PREDICT_FALSE (error0 == TCP_ERROR_DISPATCH
2436 || next0 == TCP_INPUT_NEXT_RESET))
Dave Barach68b0fb02017-02-28 15:15:56 -05002437 {
2438 /* Overload tcp flags to store state */
Florin Corasdc629cd2017-05-09 00:52:37 -07002439 tcp_state_t state0 = tc0->state;
Dave Barach68b0fb02017-02-28 15:15:56 -05002440 vnet_buffer (b0)->tcp.flags = tc0->state;
Florin Corasdc629cd2017-05-09 00:52:37 -07002441
2442 if (error0 == TCP_ERROR_DISPATCH)
2443 clib_warning ("disp error state %U flags %U",
2444 format_tcp_state, &state0, format_tcp_flags,
2445 (int) flags0);
Dave Barach68b0fb02017-02-28 15:15:56 -05002446 }
2447 }
2448 else
2449 {
2450 /* Send reset */
2451 next0 = TCP_INPUT_NEXT_RESET;
2452 error0 = TCP_ERROR_NO_LISTENER;
Dave Barach68b0fb02017-02-28 15:15:56 -05002453 }
2454
Florin Coras82b13a82017-04-25 11:58:06 -07002455 done:
Dave Barach68b0fb02017-02-28 15:15:56 -05002456 b0->error = error0 ? node->errors[error0] : 0;
2457
2458 if (PREDICT_FALSE (b0->flags & VLIB_BUFFER_IS_TRACED))
2459 {
Florin Coras82b13a82017-04-25 11:58:06 -07002460 tcp_rx_trace_t *t0 =
2461 vlib_add_trace (vm, node, b0, sizeof (*t0));
2462 tcp_set_rx_trace_data (t0, tc0, tcp0, b0, is_ip4);
Dave Barach68b0fb02017-02-28 15:15:56 -05002463 }
2464
2465 vlib_validate_buffer_enqueue_x1 (vm, node, next_index, to_next,
2466 n_left_to_next, bi0, next0);
2467 }
2468
2469 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
2470 }
2471
2472 return from_frame->n_vectors;
2473}
2474
2475static uword
2476tcp4_input (vlib_main_t * vm, vlib_node_runtime_t * node,
2477 vlib_frame_t * from_frame)
2478{
2479 return tcp46_input_inline (vm, node, from_frame, 1 /* is_ip4 */ );
2480}
2481
2482static uword
2483tcp6_input (vlib_main_t * vm, vlib_node_runtime_t * node,
2484 vlib_frame_t * from_frame)
2485{
2486 return tcp46_input_inline (vm, node, from_frame, 0 /* is_ip4 */ );
2487}
2488
2489/* *INDENT-OFF* */
2490VLIB_REGISTER_NODE (tcp4_input_node) =
2491{
2492 .function = tcp4_input,
2493 .name = "tcp4-input",
2494 /* Takes a vector of packets. */
2495 .vector_size = sizeof (u32),
2496 .n_errors = TCP_N_ERROR,
2497 .error_strings = tcp_error_strings,
2498 .n_next_nodes = TCP_INPUT_N_NEXT,
2499 .next_nodes =
2500 {
2501#define _(s,n) [TCP_INPUT_NEXT_##s] = n,
2502 foreach_tcp4_input_next
2503#undef _
2504 },
2505 .format_buffer = format_tcp_header,
2506 .format_trace = format_tcp_rx_trace,
2507};
2508/* *INDENT-ON* */
2509
2510VLIB_NODE_FUNCTION_MULTIARCH (tcp4_input_node, tcp4_input);
2511
2512/* *INDENT-OFF* */
2513VLIB_REGISTER_NODE (tcp6_input_node) =
2514{
2515 .function = tcp6_input,
2516 .name = "tcp6-input",
2517 /* Takes a vector of packets. */
2518 .vector_size = sizeof (u32),
2519 .n_errors = TCP_N_ERROR,
2520 .error_strings = tcp_error_strings,
2521 .n_next_nodes = TCP_INPUT_N_NEXT,
2522 .next_nodes =
2523 {
2524#define _(s,n) [TCP_INPUT_NEXT_##s] = n,
2525 foreach_tcp6_input_next
2526#undef _
2527 },
2528 .format_buffer = format_tcp_header,
2529 .format_trace = format_tcp_rx_trace,
2530};
2531/* *INDENT-ON* */
2532
2533VLIB_NODE_FUNCTION_MULTIARCH (tcp6_input_node, tcp6_input);
Dave Barach68b0fb02017-02-28 15:15:56 -05002534
2535static void
2536tcp_dispatch_table_init (tcp_main_t * tm)
2537{
2538 int i, j;
2539 for (i = 0; i < ARRAY_LEN (tm->dispatch_table); i++)
2540 for (j = 0; j < ARRAY_LEN (tm->dispatch_table[i]); j++)
2541 {
2542 tm->dispatch_table[i][j].next = TCP_INPUT_NEXT_DROP;
2543 tm->dispatch_table[i][j].error = TCP_ERROR_DISPATCH;
2544 }
2545
2546#define _(t,f,n,e) \
2547do { \
2548 tm->dispatch_table[TCP_STATE_##t][f].next = (n); \
2549 tm->dispatch_table[TCP_STATE_##t][f].error = (e); \
2550} while (0)
2551
2552 /* SYNs for new connections -> tcp-listen. */
2553 _(LISTEN, TCP_FLAG_SYN, TCP_INPUT_NEXT_LISTEN, TCP_ERROR_NONE);
Florin Corasdc629cd2017-05-09 00:52:37 -07002554 _(LISTEN, TCP_FLAG_ACK, TCP_INPUT_NEXT_RESET, TCP_ERROR_NONE);
2555 _(LISTEN, TCP_FLAG_RST, TCP_INPUT_NEXT_DROP, TCP_ERROR_NONE);
Dave Barach68b0fb02017-02-28 15:15:56 -05002556 /* ACK for for a SYN-ACK -> tcp-rcv-process. */
2557 _(SYN_RCVD, TCP_FLAG_ACK, TCP_INPUT_NEXT_RCV_PROCESS, TCP_ERROR_NONE);
Florin Corasc28764f2017-04-26 00:08:42 -07002558 _(SYN_RCVD, TCP_FLAG_RST, TCP_INPUT_NEXT_RCV_PROCESS, TCP_ERROR_NONE);
Dave Barach68b0fb02017-02-28 15:15:56 -05002559 /* SYN-ACK for a SYN */
2560 _(SYN_SENT, TCP_FLAG_SYN | TCP_FLAG_ACK, TCP_INPUT_NEXT_SYN_SENT,
2561 TCP_ERROR_NONE);
2562 _(SYN_SENT, TCP_FLAG_ACK, TCP_INPUT_NEXT_SYN_SENT, TCP_ERROR_NONE);
2563 _(SYN_SENT, TCP_FLAG_RST, TCP_INPUT_NEXT_SYN_SENT, TCP_ERROR_NONE);
2564 _(SYN_SENT, TCP_FLAG_RST | TCP_FLAG_ACK, TCP_INPUT_NEXT_SYN_SENT,
2565 TCP_ERROR_NONE);
2566 /* ACK for for established connection -> tcp-established. */
2567 _(ESTABLISHED, TCP_FLAG_ACK, TCP_INPUT_NEXT_ESTABLISHED, TCP_ERROR_NONE);
2568 /* FIN for for established connection -> tcp-established. */
2569 _(ESTABLISHED, TCP_FLAG_FIN, TCP_INPUT_NEXT_ESTABLISHED, TCP_ERROR_NONE);
2570 _(ESTABLISHED, TCP_FLAG_FIN | TCP_FLAG_ACK, TCP_INPUT_NEXT_ESTABLISHED,
2571 TCP_ERROR_NONE);
Florin Corasd79b41e2017-03-04 05:37:52 -08002572 _(ESTABLISHED, TCP_FLAG_RST, TCP_INPUT_NEXT_ESTABLISHED, TCP_ERROR_NONE);
Florin Corasdc629cd2017-05-09 00:52:37 -07002573 _(ESTABLISHED, TCP_FLAG_RST | TCP_FLAG_ACK, TCP_INPUT_NEXT_ESTABLISHED,
2574 TCP_ERROR_NONE);
Dave Barach68b0fb02017-02-28 15:15:56 -05002575 /* ACK or FIN-ACK to our FIN */
2576 _(FIN_WAIT_1, TCP_FLAG_ACK, TCP_INPUT_NEXT_RCV_PROCESS, TCP_ERROR_NONE);
2577 _(FIN_WAIT_1, TCP_FLAG_ACK | TCP_FLAG_FIN, TCP_INPUT_NEXT_RCV_PROCESS,
2578 TCP_ERROR_NONE);
2579 /* FIN in reply to our FIN from the other side */
2580 _(FIN_WAIT_1, TCP_FLAG_FIN, TCP_INPUT_NEXT_RCV_PROCESS, TCP_ERROR_NONE);
2581 /* FIN confirming that the peer (app) has closed */
2582 _(FIN_WAIT_2, TCP_FLAG_FIN, TCP_INPUT_NEXT_RCV_PROCESS, TCP_ERROR_NONE);
Florin Corasf6d68ed2017-05-07 19:12:02 -07002583 _(FIN_WAIT_2, TCP_FLAG_ACK, TCP_INPUT_NEXT_RCV_PROCESS, TCP_ERROR_NONE);
Dave Barach68b0fb02017-02-28 15:15:56 -05002584 _(FIN_WAIT_2, TCP_FLAG_FIN | TCP_FLAG_ACK, TCP_INPUT_NEXT_RCV_PROCESS,
2585 TCP_ERROR_NONE);
2586 _(LAST_ACK, TCP_FLAG_ACK, TCP_INPUT_NEXT_RCV_PROCESS, TCP_ERROR_NONE);
Florin Corasdb84e572017-05-09 18:54:52 -07002587 _(LAST_ACK, TCP_FLAG_RST, TCP_INPUT_NEXT_RCV_PROCESS, TCP_ERROR_NONE);
Florin Corasdc629cd2017-05-09 00:52:37 -07002588 _(CLOSED, TCP_FLAG_ACK, TCP_INPUT_NEXT_RESET, TCP_ERROR_CONNECTION_CLOSED);
2589 _(CLOSED, TCP_FLAG_RST, TCP_INPUT_NEXT_DROP, TCP_ERROR_CONNECTION_CLOSED);
Dave Barach68b0fb02017-02-28 15:15:56 -05002590#undef _
2591}
2592
2593clib_error_t *
2594tcp_input_init (vlib_main_t * vm)
2595{
2596 clib_error_t *error = 0;
2597 tcp_main_t *tm = vnet_get_tcp_main ();
2598
2599 if ((error = vlib_call_init_function (vm, tcp_init)))
2600 return error;
2601
2602 /* Initialize dispatch table. */
2603 tcp_dispatch_table_init (tm);
2604
2605 return error;
2606}
2607
2608VLIB_INIT_FUNCTION (tcp_input_init);
2609
2610/*
2611 * fd.io coding-style-patch-verification: ON
2612 *
2613 * Local Variables:
2614 * eval: (c-set-style "gnu")
2615 * End:
2616 */