blob: f19fbf874dca1a8b30dce2887c7b297fa25a1946 [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 *
98 * This accepts only segments within the window.
99 */
100always_inline u8
101tcp_segment_in_rcv_wnd (tcp_connection_t * tc, u32 seq, u32 end_seq)
102{
103 return seq_leq (end_seq, tc->rcv_las + tc->rcv_wnd)
104 && seq_geq (seq, tc->rcv_nxt);
105}
106
107void
108tcp_options_parse (tcp_header_t * th, tcp_options_t * to)
109{
110 const u8 *data;
111 u8 opt_len, opts_len, kind;
112 int j;
113 sack_block_t b;
114
115 opts_len = (tcp_doff (th) << 2) - sizeof (tcp_header_t);
116 data = (const u8 *) (th + 1);
117
118 /* Zero out all flags but those set in SYN */
119 to->flags &= (TCP_OPTS_FLAG_SACK_PERMITTED | TCP_OPTS_FLAG_WSCALE);
120
121 for (; opts_len > 0; opts_len -= opt_len, data += opt_len)
122 {
123 kind = data[0];
124
125 /* Get options length */
126 if (kind == TCP_OPTION_EOL)
127 break;
128 else if (kind == TCP_OPTION_NOOP)
129 opt_len = 1;
130 else
131 {
132 /* broken options */
133 if (opts_len < 2)
134 break;
135 opt_len = data[1];
136
137 /* weird option length */
138 if (opt_len < 2 || opt_len > opts_len)
139 break;
140 }
141
142 /* Parse options */
143 switch (kind)
144 {
145 case TCP_OPTION_MSS:
146 if ((opt_len == TCP_OPTION_LEN_MSS) && tcp_syn (th))
147 {
148 to->flags |= TCP_OPTS_FLAG_MSS;
149 to->mss = clib_net_to_host_u16 (*(u16 *) (data + 2));
150 }
151 break;
152 case TCP_OPTION_WINDOW_SCALE:
153 if ((opt_len == TCP_OPTION_LEN_WINDOW_SCALE) && tcp_syn (th))
154 {
155 to->flags |= TCP_OPTS_FLAG_WSCALE;
156 to->wscale = data[2];
157 if (to->wscale > TCP_MAX_WND_SCALE)
158 {
159 clib_warning ("Illegal window scaling value: %d",
160 to->wscale);
161 to->wscale = TCP_MAX_WND_SCALE;
162 }
163 }
164 break;
165 case TCP_OPTION_TIMESTAMP:
166 if (opt_len == TCP_OPTION_LEN_TIMESTAMP)
167 {
168 to->flags |= TCP_OPTS_FLAG_TSTAMP;
169 to->tsval = clib_net_to_host_u32 (*(u32 *) (data + 2));
170 to->tsecr = clib_net_to_host_u32 (*(u32 *) (data + 6));
171 }
172 break;
173 case TCP_OPTION_SACK_PERMITTED:
174 if (opt_len == TCP_OPTION_LEN_SACK_PERMITTED && tcp_syn (th))
175 to->flags |= TCP_OPTS_FLAG_SACK_PERMITTED;
176 break;
177 case TCP_OPTION_SACK_BLOCK:
178 /* If SACK permitted was not advertised or a SYN, break */
179 if ((to->flags & TCP_OPTS_FLAG_SACK_PERMITTED) == 0 || tcp_syn (th))
180 break;
181
182 /* If too short or not correctly formatted, break */
183 if (opt_len < 10 || ((opt_len - 2) % TCP_OPTION_LEN_SACK_BLOCK))
184 break;
185
186 to->flags |= TCP_OPTS_FLAG_SACK;
187 to->n_sack_blocks = (opt_len - 2) / TCP_OPTION_LEN_SACK_BLOCK;
188 vec_reset_length (to->sacks);
189 for (j = 0; j < to->n_sack_blocks; j++)
190 {
191 b.start = clib_net_to_host_u32 (*(u32 *) (data + 2 + 4 * j));
192 b.end = clib_net_to_host_u32 (*(u32 *) (data + 6 + 4 * j));
193 vec_add1 (to->sacks, b);
194 }
195 break;
196 default:
197 /* Nothing to see here */
198 continue;
199 }
200 }
201}
202
203always_inline int
204tcp_segment_check_paws (tcp_connection_t * tc)
205{
206 /* XXX normally test for timestamp should be lt instead of leq, but for
207 * local testing this is not enough */
208 return tcp_opts_tstamp (&tc->opt) && tc->tsval_recent
209 && timestamp_lt (tc->opt.tsval, tc->tsval_recent);
210}
211
212/**
213 * Validate incoming segment as per RFC793 p. 69 and RFC1323 p. 19
214 *
215 * It first verifies if segment has a wrapped sequence number (PAWS) and then
216 * does the processing associated to the first four steps (ignoring security
217 * and precedence): sequence number, rst bit and syn bit checks.
218 *
219 * @return 0 if segments passes validation.
220 */
221static int
222tcp_segment_validate (vlib_main_t * vm, tcp_connection_t * tc0,
223 vlib_buffer_t * b0, tcp_header_t * th0, u32 * next0)
224{
225 u8 paws_failed;
226
227 if (PREDICT_FALSE (!tcp_ack (th0) && !tcp_rst (th0) && !tcp_syn (th0)))
228 return -1;
229
230 tcp_options_parse (th0, &tc0->opt);
231
232 /* RFC1323: Check against wrapped sequence numbers (PAWS). If we have
233 * timestamp to echo and it's less than tsval_recent, drop segment
234 * but still send an ACK in order to retain TCP's mechanism for detecting
235 * and recovering from half-open connections */
236 paws_failed = tcp_segment_check_paws (tc0);
237 if (paws_failed)
238 {
239 clib_warning ("paws failed");
240
241 /* If it just so happens that a segment updates tsval_recent for a
242 * segment over 24 days old, invalidate tsval_recent. */
243 if (timestamp_lt (tc0->tsval_recent_age + TCP_PAWS_IDLE,
244 tcp_time_now ()))
245 {
246 /* Age isn't reset until we get a valid tsval (bsd inspired) */
247 tc0->tsval_recent = 0;
248 }
249 else
250 {
251 /* Drop after ack if not rst */
252 if (!tcp_rst (th0))
253 {
254 tcp_make_ack (tc0, b0);
255 *next0 = tcp_next_output (tc0->c_is_ip4);
256 return -1;
257 }
258 }
259 }
260
261 /* 1st: check sequence number */
262 if (!tcp_segment_in_rcv_wnd (tc0, vnet_buffer (b0)->tcp.seq_number,
263 vnet_buffer (b0)->tcp.seq_end))
264 {
265 if (!tcp_rst (th0))
266 {
267 /* Send dup ack */
268 tcp_make_ack (tc0, b0);
269 *next0 = tcp_next_output (tc0->c_is_ip4);
270 }
271 return -1;
272 }
273
274 /* 2nd: check the RST bit */
275 if (tcp_rst (th0))
276 {
Florin Corasd79b41e2017-03-04 05:37:52 -0800277 tcp_connection_reset (tc0);
Dave Barach68b0fb02017-02-28 15:15:56 -0500278 return -1;
279 }
280
281 /* 3rd: check security and precedence (skip) */
282
283 /* 4th: check the SYN bit */
284 if (tcp_syn (th0))
285 {
286 tcp_send_reset (b0, tc0->c_is_ip4);
287 return -1;
288 }
289
290 /* If PAWS passed and segment in window, save timestamp */
291 if (!paws_failed)
292 {
293 tc0->tsval_recent = tc0->opt.tsval;
294 tc0->tsval_recent_age = tcp_time_now ();
295 }
296
297 return 0;
298}
299
300always_inline int
301tcp_rcv_ack_is_acceptable (tcp_connection_t * tc0, vlib_buffer_t * tb0)
302{
303 /* SND.UNA =< SEG.ACK =< SND.NXT */
304 return (seq_leq (tc0->snd_una, vnet_buffer (tb0)->tcp.ack_number)
305 && seq_leq (vnet_buffer (tb0)->tcp.ack_number, tc0->snd_nxt));
306}
307
308/**
309 * Compute smoothed RTT as per VJ's '88 SIGCOMM and RFC6298
310 *
311 * Note that although the original article, srtt and rttvar are scaled
312 * to minimize round-off errors, here we don't. Instead, we rely on
313 * better precision time measurements.
314 *
315 * TODO support us rtt resolution
316 */
317static void
318tcp_estimate_rtt (tcp_connection_t * tc, u32 mrtt)
319{
320 int err;
321
322 if (tc->srtt != 0)
323 {
324 err = mrtt - tc->srtt;
325 tc->srtt += err >> 3;
326
327 /* XXX Drop in RTT results in RTTVAR increase and bigger RTO.
328 * The increase should be bound */
329 tc->rttvar += (clib_abs (err) - tc->rttvar) >> 2;
330 }
331 else
332 {
333 /* First measurement. */
334 tc->srtt = mrtt;
335 tc->rttvar = mrtt << 1;
336 }
337}
338
339/** Update RTT estimate and RTO timer
340 *
341 * Measure RTT: We have two sources of RTT measurements: TSOPT and ACK
342 * timing. Middle boxes are known to fiddle with TCP options so we
343 * should give higher priority to ACK timing.
344 *
345 * return 1 if valid rtt 0 otherwise
346 */
347static int
348tcp_update_rtt (tcp_connection_t * tc, u32 ack)
349{
350 u32 mrtt = 0;
351
352 /* Karn's rule, part 1. Don't use retransmitted segments to estimate
353 * RTT because they're ambiguous. */
354 if (tc->rtt_seq && seq_gt (ack, tc->rtt_seq) && !tc->rto_boff)
355 {
356 mrtt = tcp_time_now () - tc->rtt_ts;
357 tc->rtt_seq = 0;
358 }
359
360 /* As per RFC7323 TSecr can be used for RTTM only if the segment advances
361 * snd_una, i.e., the left side of the send window:
362 * seq_lt (tc->snd_una, ack). Note: last condition could be dropped, we don't
363 * try to update rtt for dupacks */
364 else if (tcp_opts_tstamp (&tc->opt) && tc->opt.tsecr && tc->bytes_acked)
365 {
366 mrtt = tcp_time_now () - tc->opt.tsecr;
367 }
368
369 /* Ignore dubious measurements */
370 if (mrtt == 0 || mrtt > TCP_RTT_MAX)
371 return 0;
372
373 tcp_estimate_rtt (tc, mrtt);
374
375 tc->rto = clib_min (tc->srtt + (tc->rttvar << 2), TCP_RTO_MAX);
376
377 return 1;
378}
379
380/**
381 * Dequeue bytes that have been acked and while at it update RTT estimates.
382 */
383static void
384tcp_dequeue_acked (tcp_connection_t * tc, u32 ack)
385{
386 /* Dequeue the newly ACKed bytes */
387 stream_session_dequeue_drop (&tc->connection, tc->bytes_acked);
388
389 /* Update rtt and rto */
390 if (tcp_update_rtt (tc, ack))
391 {
392 /* Good ACK received and valid RTT, make sure retransmit backoff is 0 */
393 tc->rto_boff = 0;
394 }
395}
396
397/** Check if dupack as per RFC5681 Sec. 2 */
398always_inline u8
399tcp_ack_is_dupack (tcp_connection_t * tc, vlib_buffer_t * b, u32 new_snd_wnd)
400{
401 return ((vnet_buffer (b)->tcp.ack_number == tc->snd_una)
402 && seq_gt (tc->snd_una_max, tc->snd_una)
403 && (vnet_buffer (b)->tcp.seq_end == vnet_buffer (b)->tcp.seq_number)
404 && (new_snd_wnd == tc->snd_wnd));
405}
406
407void
408scoreboard_remove_hole (sack_scoreboard_t * sb, sack_scoreboard_hole_t * hole)
409{
410 sack_scoreboard_hole_t *next, *prev;
411
412 if (hole->next != TCP_INVALID_SACK_HOLE_INDEX)
413 {
414 next = pool_elt_at_index (sb->holes, hole->next);
415 next->prev = hole->prev;
416 }
417
418 if (hole->prev != TCP_INVALID_SACK_HOLE_INDEX)
419 {
420 prev = pool_elt_at_index (sb->holes, hole->prev);
421 prev->next = hole->next;
422 }
423 else
424 {
425 sb->head = hole->next;
426 }
427
428 pool_put (sb->holes, hole);
429}
430
431sack_scoreboard_hole_t *
432scoreboard_insert_hole (sack_scoreboard_t * sb, sack_scoreboard_hole_t * prev,
433 u32 start, u32 end)
434{
435 sack_scoreboard_hole_t *hole, *next;
436 u32 hole_index;
437
438 pool_get (sb->holes, hole);
439 memset (hole, 0, sizeof (*hole));
440
441 hole->start = start;
442 hole->end = end;
443 hole_index = hole - sb->holes;
444
445 if (prev)
446 {
447 hole->prev = prev - sb->holes;
448 hole->next = prev->next;
449
450 if ((next = scoreboard_next_hole (sb, hole)))
451 next->prev = hole_index;
452
453 prev->next = hole_index;
454 }
455 else
456 {
457 sb->head = hole_index;
458 hole->prev = TCP_INVALID_SACK_HOLE_INDEX;
459 hole->next = TCP_INVALID_SACK_HOLE_INDEX;
460 }
461
462 return hole;
463}
464
465static void
466tcp_rcv_sacks (tcp_connection_t * tc, u32 ack)
467{
468 sack_scoreboard_t *sb = &tc->sack_sb;
469 sack_block_t *blk, tmp;
470 sack_scoreboard_hole_t *hole, *next_hole;
471 u32 blk_index = 0;
472 int i, j;
473
474 if (!tcp_opts_sack (tc) && sb->head == TCP_INVALID_SACK_HOLE_INDEX)
475 return;
476
477 /* Remove invalid blocks */
478 vec_foreach (blk, tc->opt.sacks)
479 {
480 if (seq_lt (blk->start, blk->end)
481 && seq_gt (blk->start, tc->snd_una)
482 && seq_gt (blk->start, ack) && seq_lt (blk->end, tc->snd_nxt))
483 continue;
484
485 vec_del1 (tc->opt.sacks, blk - tc->opt.sacks);
486 }
487
488 /* Add block for cumulative ack */
489 if (seq_gt (ack, tc->snd_una))
490 {
491 tmp.start = tc->snd_una;
492 tmp.end = ack;
493 vec_add1 (tc->opt.sacks, tmp);
494 }
495
496 if (vec_len (tc->opt.sacks) == 0)
497 return;
498
499 /* Make sure blocks are ordered */
500 for (i = 0; i < vec_len (tc->opt.sacks); i++)
501 for (j = i; j < vec_len (tc->opt.sacks); j++)
502 if (seq_lt (tc->opt.sacks[j].start, tc->opt.sacks[i].start))
503 {
504 tmp = tc->opt.sacks[i];
505 tc->opt.sacks[i] = tc->opt.sacks[j];
506 tc->opt.sacks[j] = tmp;
507 }
508
509 /* If no holes, insert the first that covers all outstanding bytes */
510 if (sb->head == TCP_INVALID_SACK_HOLE_INDEX)
511 {
512 scoreboard_insert_hole (sb, 0, tc->snd_una, tc->snd_una_max);
513 }
514
515 /* Walk the holes with the SACK blocks */
516 hole = pool_elt_at_index (sb->holes, sb->head);
517 while (hole && blk_index < vec_len (tc->opt.sacks))
518 {
519 blk = &tc->opt.sacks[blk_index];
520
521 if (seq_leq (blk->start, hole->start))
522 {
523 /* Block covers hole. Remove hole */
524 if (seq_geq (blk->end, hole->end))
525 {
526 next_hole = scoreboard_next_hole (sb, hole);
527
528 /* Byte accounting */
529 if (seq_lt (hole->end, ack))
530 {
531 /* Bytes lost because snd wnd left edge advances */
532 if (seq_lt (next_hole->start, ack))
533 sb->sacked_bytes -= next_hole->start - hole->end;
534 else
535 sb->sacked_bytes -= ack - hole->end;
536 }
537 else
538 {
539 sb->sacked_bytes += scoreboard_hole_bytes (hole);
540 }
541
542 scoreboard_remove_hole (sb, hole);
543 hole = next_hole;
544 }
545 /* Partial overlap */
546 else
547 {
548 sb->sacked_bytes += blk->end - hole->start;
549 hole->start = blk->end;
550 blk_index++;
551 }
552 }
553 else
554 {
555 /* Hole must be split */
556 if (seq_leq (blk->end, hole->end))
557 {
558 sb->sacked_bytes += blk->end - blk->start;
559 scoreboard_insert_hole (sb, hole, blk->end, hole->end);
560 hole->end = blk->start - 1;
561 blk_index++;
562 }
563 else
564 {
565 sb->sacked_bytes += hole->end - blk->start + 1;
566 hole->end = blk->start - 1;
567 hole = scoreboard_next_hole (sb, hole);
568 }
569 }
570 }
571}
572
573/** Update snd_wnd
574 *
575 * If (SND.WL1 < SEG.SEQ or (SND.WL1 = SEG.SEQ and SND.WL2 =< SEG.ACK)), set
576 * SND.WND <- SEG.WND, set SND.WL1 <- SEG.SEQ, and set SND.WL2 <- SEG.ACK */
577static void
578tcp_update_snd_wnd (tcp_connection_t * tc, u32 seq, u32 ack, u32 snd_wnd)
579{
580 if (tc->snd_wl1 < seq || (tc->snd_wl1 == seq && tc->snd_wl2 <= ack))
581 {
582 tc->snd_wnd = snd_wnd;
583 tc->snd_wl1 = seq;
584 tc->snd_wl2 = ack;
585 }
586}
587
588static void
589tcp_cc_congestion (tcp_connection_t * tc)
590{
591 tc->cc_algo->congestion (tc);
592}
593
594static void
595tcp_cc_recover (tcp_connection_t * tc)
596{
597 if (tcp_in_fastrecovery (tc))
598 {
599 tc->cc_algo->recovered (tc);
600 tcp_recovery_off (tc);
601 }
602 else if (tcp_in_recovery (tc))
603 {
604 tcp_recovery_off (tc);
605 tc->cwnd = tcp_loss_wnd (tc);
606 }
607}
608
609static void
610tcp_cc_rcv_ack (tcp_connection_t * tc)
611{
612 u8 partial_ack;
613
614 if (tcp_in_recovery (tc))
615 {
616 partial_ack = seq_lt (tc->snd_una, tc->snd_una_max);
617 if (!partial_ack)
618 {
619 /* Clear retransmitted bytes. */
620 tc->rtx_bytes = 0;
621 tcp_cc_recover (tc);
622 }
623 else
624 {
625 /* Clear retransmitted bytes. XXX should we clear all? */
626 tc->rtx_bytes = 0;
627 tc->cc_algo->rcv_cong_ack (tc, TCP_CC_PARTIALACK);
628
629 /* Retransmit first unacked segment */
630 tcp_retransmit_first_unacked (tc);
631 }
632 }
633 else
634 {
635 tc->cc_algo->rcv_ack (tc);
636 }
637
638 tc->rcv_dupacks = 0;
639 tc->tsecr_last_ack = tc->opt.tsecr;
640}
641
642static void
643tcp_cc_rcv_dupack (tcp_connection_t * tc, u32 ack)
644{
645 ASSERT (tc->snd_una == ack);
646
647 tc->rcv_dupacks++;
648 if (tc->rcv_dupacks == TCP_DUPACK_THRESHOLD)
649 {
650 /* RFC6582 NewReno heuristic to avoid multiple fast retransmits */
651 if (tc->opt.tsecr != tc->tsecr_last_ack)
652 {
653 tc->rcv_dupacks = 0;
654 return;
655 }
656
657 tcp_fastrecovery_on (tc);
658
659 /* Handle congestion and dupack */
660 tcp_cc_congestion (tc);
661 tc->cc_algo->rcv_cong_ack (tc, TCP_CC_DUPACK);
662
663 tcp_fast_retransmit (tc);
664
665 /* Post retransmit update cwnd to ssthresh and account for the
666 * three segments that have left the network and should've been
667 * buffered at the receiver */
668 tc->cwnd = tc->ssthresh + TCP_DUPACK_THRESHOLD * tc->snd_mss;
669 }
670 else if (tc->rcv_dupacks > TCP_DUPACK_THRESHOLD)
671 {
672 ASSERT (tcp_in_fastrecovery (tc));
673
674 tc->cc_algo->rcv_cong_ack (tc, TCP_CC_DUPACK);
675 }
676}
677
678void
679tcp_cc_init (tcp_connection_t * tc)
680{
681 tc->cc_algo = tcp_cc_algo_get (TCP_CC_NEWRENO);
682 tc->cc_algo->init (tc);
683}
684
685static int
686tcp_rcv_ack (tcp_connection_t * tc, vlib_buffer_t * b,
687 tcp_header_t * th, u32 * next, u32 * error)
688{
689 u32 new_snd_wnd;
690
691 /* If the ACK acks something not yet sent (SEG.ACK > SND.NXT) then send an
692 * ACK, drop the segment, and return */
693 if (seq_gt (vnet_buffer (b)->tcp.ack_number, tc->snd_nxt))
694 {
695 tcp_make_ack (tc, b);
696 *next = tcp_next_output (tc->c_is_ip4);
697 *error = TCP_ERROR_ACK_INVALID;
698 return -1;
699 }
700
701 /* If old ACK, discard */
702 if (seq_lt (vnet_buffer (b)->tcp.ack_number, tc->snd_una))
703 {
704 *error = TCP_ERROR_ACK_OLD;
705 return -1;
706 }
707
708 if (tcp_opts_sack_permitted (&tc->opt))
709 tcp_rcv_sacks (tc, vnet_buffer (b)->tcp.ack_number);
710
Florin Corase04c2992017-03-01 08:17:34 -0800711 new_snd_wnd = clib_net_to_host_u16 (th->window) << tc->snd_wscale;
Dave Barach68b0fb02017-02-28 15:15:56 -0500712
713 if (tcp_ack_is_dupack (tc, b, new_snd_wnd))
714 {
715 tcp_cc_rcv_dupack (tc, vnet_buffer (b)->tcp.ack_number);
716 *error = TCP_ERROR_ACK_DUP;
717 return -1;
718 }
719
720 /* Valid ACK */
721 tc->bytes_acked = vnet_buffer (b)->tcp.ack_number - tc->snd_una;
722 tc->snd_una = vnet_buffer (b)->tcp.ack_number;
723
724 /* Dequeue ACKed packet and update RTT */
725 tcp_dequeue_acked (tc, vnet_buffer (b)->tcp.ack_number);
726
727 tcp_update_snd_wnd (tc, vnet_buffer (b)->tcp.seq_number,
728 vnet_buffer (b)->tcp.ack_number, new_snd_wnd);
729
730 /* Updates congestion control (slow start/congestion avoidance) */
731 tcp_cc_rcv_ack (tc);
732
733 /* If everything has been acked, stop retransmit timer
734 * otherwise update */
735 if (tc->snd_una == tc->snd_una_max)
736 tcp_timer_reset (tc, TCP_TIMER_RETRANSMIT);
737 else
738 tcp_timer_update (tc, TCP_TIMER_RETRANSMIT, tc->rto);
739
740 return 0;
741}
742
743/**
744 * Build SACK list as per RFC2018.
745 *
746 * Makes sure the first block contains the segment that generated the current
747 * ACK and the following ones are the ones most recently reported in SACK
748 * blocks.
749 *
750 * @param tc TCP connection for which the SACK list is updated
751 * @param start Start sequence number of the newest SACK block
752 * @param end End sequence of the newest SACK block
753 */
754static void
755tcp_update_sack_list (tcp_connection_t * tc, u32 start, u32 end)
756{
757 sack_block_t *new_list = 0, block;
758 u32 n_elts;
759 int i;
760 u8 new_head = 0;
761
762 /* If the first segment is ooo add it to the list. Last write might've moved
763 * rcv_nxt over the first segment. */
764 if (seq_lt (tc->rcv_nxt, start))
765 {
766 block.start = start;
767 block.end = end;
768 vec_add1 (new_list, block);
769 new_head = 1;
770 }
771
772 /* Find the blocks still worth keeping. */
773 for (i = 0; i < vec_len (tc->snd_sacks); i++)
774 {
775 /* Discard if:
776 * 1) rcv_nxt advanced beyond current block OR
777 * 2) Segment overlapped by the first segment, i.e., it has been merged
778 * into it.*/
779 if (seq_leq (tc->snd_sacks[i].start, tc->rcv_nxt)
780 || seq_leq (tc->snd_sacks[i].start, end))
781 continue;
782
783 /* Save subsequent segments to new SACK list. */
784 n_elts = clib_min (vec_len (tc->snd_sacks) - i,
785 TCP_MAX_SACK_BLOCKS - new_head);
786 vec_insert_elts (new_list, &tc->snd_sacks[i], n_elts, new_head);
787 break;
788 }
789
790 /* Replace old vector with new one */
791 vec_free (tc->snd_sacks);
792 tc->snd_sacks = new_list;
793}
794
795/** Enqueue data for delivery to application */
796always_inline u32
797tcp_session_enqueue_data (tcp_connection_t * tc, vlib_buffer_t * b,
798 u16 data_len)
799{
800 int written;
801
802 /* Pure ACK. Update rcv_nxt and be done. */
803 if (PREDICT_FALSE (data_len == 0))
804 {
805 tc->rcv_nxt = vnet_buffer (b)->tcp.seq_end;
806 return TCP_ERROR_PURE_ACK;
807 }
808
809 written = stream_session_enqueue_data (&tc->connection,
810 vlib_buffer_get_current (b),
811 data_len, 1 /* queue event */ );
812
813 /* Update rcv_nxt */
814 if (PREDICT_TRUE (written == data_len))
815 {
816 tc->rcv_nxt = vnet_buffer (b)->tcp.seq_end;
817 }
818 /* If more data written than expected, account for out-of-order bytes. */
819 else if (written > data_len)
820 {
821 tc->rcv_nxt = vnet_buffer (b)->tcp.seq_end + written - data_len;
822
823 /* Send ACK confirming the update */
824 tc->flags |= TCP_CONN_SNDACK;
825
826 /* Update SACK list if need be */
827 if (tcp_opts_sack_permitted (&tc->opt))
828 {
829 /* Remove SACK blocks that have been delivered */
830 tcp_update_sack_list (tc, tc->rcv_nxt, tc->rcv_nxt);
831 }
832 }
833 else
834 {
835 ASSERT (0);
836 return TCP_ERROR_FIFO_FULL;
837 }
838
839 return TCP_ERROR_ENQUEUED;
840}
841
842/** Enqueue out-of-order data */
843always_inline u32
844tcp_session_enqueue_ooo (tcp_connection_t * tc, vlib_buffer_t * b,
845 u16 data_len)
846{
847 stream_session_t *s0;
848 u32 offset, seq;
849
850 s0 = stream_session_get (tc->c_s_index, tc->c_thread_index);
851 seq = vnet_buffer (b)->tcp.seq_number;
852 offset = seq - tc->rcv_nxt;
853
854 if (svm_fifo_enqueue_with_offset (s0->server_rx_fifo, s0->pid, offset,
855 data_len, vlib_buffer_get_current (b)))
856 return TCP_ERROR_FIFO_FULL;
857
858 /* Update SACK list if in use */
859 if (tcp_opts_sack_permitted (&tc->opt))
860 {
861 ooo_segment_t *newest;
862 u32 start, end;
863
864 /* Get the newest segment from the fifo */
865 newest = svm_fifo_newest_ooo_segment (s0->server_rx_fifo);
866 start = tc->rcv_nxt + ooo_segment_offset (s0->server_rx_fifo, newest);
867 end = tc->rcv_nxt + ooo_segment_end_offset (s0->server_rx_fifo, newest);
868
869 tcp_update_sack_list (tc, start, end);
870 }
871
872 return TCP_ERROR_ENQUEUED;
873}
874
875/**
876 * Check if ACK could be delayed. DELACK timer is set only after frame is
877 * processed so this can return true for a full bursts of packets.
878 */
879always_inline int
880tcp_can_delack (tcp_connection_t * tc)
881{
882 /* If there's no DELACK timer set and the last window sent wasn't 0 we
883 * can safely delay. */
884 if (!tcp_timer_is_active (tc, TCP_TIMER_DELACK)
885 && (tc->flags & TCP_CONN_SENT_RCV_WND0) == 0
886 && (tc->flags & TCP_CONN_SNDACK) == 0)
887 return 1;
888
889 return 0;
890}
891
892static int
893tcp_segment_rcv (tcp_main_t * tm, tcp_connection_t * tc, vlib_buffer_t * b,
894 u16 n_data_bytes, u32 * next0)
895{
896 u32 error = 0;
897
898 /* Handle out-of-order data */
899 if (PREDICT_FALSE (vnet_buffer (b)->tcp.seq_number != tc->rcv_nxt))
900 {
901 error = tcp_session_enqueue_ooo (tc, b, n_data_bytes);
902
903 /* Don't send more than 3 dupacks per burst
904 * XXX decide if this is good */
905 if (tc->snt_dupacks < 3)
906 {
907 /* RFC2581: Send DUPACK for fast retransmit */
908 tcp_make_ack (tc, b);
909 *next0 = tcp_next_output (tc->c_is_ip4);
910
911 /* Mark as DUPACK. We may filter these in output if
912 * the burst fills the holes. */
913 vnet_buffer (b)->tcp.flags = TCP_BUF_FLAG_DUPACK;
914
915 tc->snt_dupacks++;
916 }
917
918 goto done;
919 }
920
921 /* In order data, enqueue. Fifo figures out by itself if any out-of-order
922 * segments can be enqueued after fifo tail offset changes. */
923 error = tcp_session_enqueue_data (tc, b, n_data_bytes);
924
925 /* Check if ACK can be delayed */
926 if (tcp_can_delack (tc))
927 {
928 /* Nothing to do for pure ACKs */
929 if (n_data_bytes == 0)
930 goto done;
931
932 /* If connection has not been previously marked for delay ack
933 * add it to the list and flag it */
934 if (!tc->flags & TCP_CONN_DELACK)
935 {
936 vec_add1 (tm->delack_connections[tc->c_thread_index],
937 tc->c_c_index);
938 tc->flags |= TCP_CONN_DELACK;
939 }
940 }
941 else
942 {
943 /* Check if a packet has already been enqueued to output for burst.
944 * If yes, then drop this one, otherwise, let it pass through to
945 * output */
946 if ((tc->flags & TCP_CONN_BURSTACK) == 0)
947 {
948 *next0 = tcp_next_output (tc->c_is_ip4);
949 tcp_make_ack (tc, b);
950 error = TCP_ERROR_ENQUEUED;
951
952 /* TODO: maybe add counter to ensure N acks will be sent/burst */
953 tc->flags |= TCP_CONN_BURSTACK;
954 }
955 }
956
957done:
958 return error;
959}
960
961void
962delack_timers_init (tcp_main_t * tm, u32 thread_index)
963{
964 tcp_connection_t *tc;
965 u32 i, *conns;
966 tw_timer_wheel_16t_2w_512sl_t *tw;
967
968 tw = &tm->timer_wheels[thread_index];
969 conns = tm->delack_connections[thread_index];
970 for (i = 0; i < vec_len (conns); i++)
971 {
972 tc = pool_elt_at_index (tm->connections[thread_index], conns[i]);
973 ASSERT (0 != tc);
974
975 tc->timers[TCP_TIMER_DELACK]
976 = tw_timer_start_16t_2w_512sl (tw, conns[i],
977 TCP_TIMER_DELACK, TCP_DELACK_TIME);
978 }
979 vec_reset_length (tm->delack_connections[thread_index]);
980}
981
982always_inline uword
983tcp46_established_inline (vlib_main_t * vm, vlib_node_runtime_t * node,
984 vlib_frame_t * from_frame, int is_ip4)
985{
986 u32 n_left_from, next_index, *from, *to_next;
987 u32 my_thread_index = vm->cpu_index, errors = 0;
988 tcp_main_t *tm = vnet_get_tcp_main ();
989
990 from = vlib_frame_vector_args (from_frame);
991 n_left_from = from_frame->n_vectors;
992
993 next_index = node->cached_next_index;
994
995 while (n_left_from > 0)
996 {
997 u32 n_left_to_next;
998
999 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
1000
1001 while (n_left_from > 0 && n_left_to_next > 0)
1002 {
1003 u32 bi0;
1004 vlib_buffer_t *b0;
1005 tcp_header_t *th0 = 0;
1006 tcp_connection_t *tc0;
1007 ip4_header_t *ip40;
1008 ip6_header_t *ip60;
1009 u32 n_advance_bytes0, n_data_bytes0;
1010 u32 next0 = TCP_ESTABLISHED_NEXT_DROP, error0 = TCP_ERROR_ENQUEUED;
1011
1012 bi0 = from[0];
1013 to_next[0] = bi0;
1014 from += 1;
1015 to_next += 1;
1016 n_left_from -= 1;
1017 n_left_to_next -= 1;
1018
1019 b0 = vlib_get_buffer (vm, bi0);
1020 tc0 = tcp_connection_get (vnet_buffer (b0)->tcp.connection_index,
1021 my_thread_index);
1022
Florin Corasd79b41e2017-03-04 05:37:52 -08001023 if (PREDICT_FALSE (tc0 == 0))
1024 {
1025 error0 = TCP_ERROR_INVALID_CONNECTION;
1026 goto drop;
1027 }
1028
Dave Barach68b0fb02017-02-28 15:15:56 -05001029 /* Checksum computed by ipx_local no need to compute again */
1030
1031 if (is_ip4)
1032 {
1033 ip40 = vlib_buffer_get_current (b0);
1034 th0 = ip4_next_header (ip40);
1035 n_advance_bytes0 = (ip4_header_bytes (ip40)
1036 + tcp_header_bytes (th0));
1037 n_data_bytes0 = clib_net_to_host_u16 (ip40->length)
1038 - n_advance_bytes0;
1039 }
1040 else
1041 {
1042 ip60 = vlib_buffer_get_current (b0);
1043 th0 = ip6_next_header (ip60);
1044 n_advance_bytes0 = tcp_header_bytes (th0);
1045 n_data_bytes0 = clib_net_to_host_u16 (ip60->payload_length)
1046 - n_advance_bytes0;
1047 n_advance_bytes0 += sizeof (ip60[0]);
1048 }
1049
1050 /* SYNs, FINs and data consume sequence numbers */
1051 vnet_buffer (b0)->tcp.seq_end = vnet_buffer (b0)->tcp.seq_number
1052 + tcp_is_syn (th0) + tcp_is_fin (th0) + n_data_bytes0;
1053
1054 /* TODO header prediction fast path */
1055
1056 /* 1-4: check SEQ, RST, SYN */
1057 if (PREDICT_FALSE (tcp_segment_validate (vm, tc0, b0, th0, &next0)))
1058 {
1059 error0 = TCP_ERROR_SEGMENT_INVALID;
1060 goto drop;
1061 }
1062
1063 /* 5: check the ACK field */
1064 if (tcp_rcv_ack (tc0, b0, th0, &next0, &error0))
1065 {
1066 goto drop;
1067 }
1068
1069 /* 6: check the URG bit TODO */
1070
1071 /* 7: process the segment text */
1072 vlib_buffer_advance (b0, n_advance_bytes0);
1073 error0 = tcp_segment_rcv (tm, tc0, b0, n_data_bytes0, &next0);
1074
1075 /* 8: check the FIN bit */
1076 if (tcp_fin (th0))
1077 {
Florin Corasd79b41e2017-03-04 05:37:52 -08001078 /* Enter CLOSE-WAIT and notify session. Don't send ACK, instead
1079 * wait for session to call close. To avoid lingering
1080 * in CLOSE-WAIT, set timer (reuse WAITCLOSE). */
Dave Barach68b0fb02017-02-28 15:15:56 -05001081 tc0->state = TCP_STATE_CLOSE_WAIT;
1082 stream_session_disconnect_notify (&tc0->connection);
Florin Corasd79b41e2017-03-04 05:37:52 -08001083 tcp_timer_set (tc0, TCP_TIMER_WAITCLOSE, TCP_CLOSEWAIT_TIME);
Dave Barach68b0fb02017-02-28 15:15:56 -05001084 }
1085
1086 drop:
1087 b0->error = node->errors[error0];
1088 if (PREDICT_FALSE (b0->flags & VLIB_BUFFER_IS_TRACED))
1089 {
1090
1091 }
1092
1093 vlib_validate_buffer_enqueue_x1 (vm, node, next_index, to_next,
1094 n_left_to_next, bi0, next0);
1095 }
1096
1097 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
1098 }
1099
1100 errors = session_manager_flush_enqueue_events (my_thread_index);
1101 if (errors)
1102 {
1103 if (is_ip4)
1104 vlib_node_increment_counter (vm, tcp4_established_node.index,
1105 TCP_ERROR_EVENT_FIFO_FULL, errors);
1106 else
1107 vlib_node_increment_counter (vm, tcp6_established_node.index,
1108 TCP_ERROR_EVENT_FIFO_FULL, errors);
1109 }
1110
1111 delack_timers_init (tm, my_thread_index);
1112
1113 return from_frame->n_vectors;
1114}
1115
1116static uword
1117tcp4_established (vlib_main_t * vm, vlib_node_runtime_t * node,
1118 vlib_frame_t * from_frame)
1119{
1120 return tcp46_established_inline (vm, node, from_frame, 1 /* is_ip4 */ );
1121}
1122
1123static uword
1124tcp6_established (vlib_main_t * vm, vlib_node_runtime_t * node,
1125 vlib_frame_t * from_frame)
1126{
1127 return tcp46_established_inline (vm, node, from_frame, 0 /* is_ip4 */ );
1128}
1129
1130/* *INDENT-OFF* */
1131VLIB_REGISTER_NODE (tcp4_established_node) =
1132{
1133 .function = tcp4_established,
1134 .name = "tcp4-established",
1135 /* Takes a vector of packets. */
1136 .vector_size = sizeof (u32),
1137 .n_errors = TCP_N_ERROR,.error_strings = tcp_error_strings,
1138 .n_next_nodes = TCP_ESTABLISHED_N_NEXT,
1139 .next_nodes =
1140 {
1141#define _(s,n) [TCP_ESTABLISHED_NEXT_##s] = n,
1142 foreach_tcp_state_next
1143#undef _
1144 },
1145};
1146/* *INDENT-ON* */
1147
1148VLIB_NODE_FUNCTION_MULTIARCH (tcp4_established_node, tcp4_established);
1149
1150/* *INDENT-OFF* */
1151VLIB_REGISTER_NODE (tcp6_established_node) =
1152{
1153 .function = tcp6_established,
1154 .name = "tcp6-established",
1155 /* Takes a vector of packets. */
1156 .vector_size = sizeof (u32),
1157 .n_errors = TCP_N_ERROR,
1158 .error_strings = tcp_error_strings,
1159 .n_next_nodes = TCP_ESTABLISHED_N_NEXT,
1160 .next_nodes =
1161 {
1162#define _(s,n) [TCP_ESTABLISHED_NEXT_##s] = n,
1163 foreach_tcp_state_next
1164#undef _
1165 },
1166};
1167/* *INDENT-ON* */
1168
1169
1170VLIB_NODE_FUNCTION_MULTIARCH (tcp6_established_node, tcp6_established);
1171
1172vlib_node_registration_t tcp4_syn_sent_node;
1173vlib_node_registration_t tcp6_syn_sent_node;
1174
1175always_inline uword
1176tcp46_syn_sent_inline (vlib_main_t * vm, vlib_node_runtime_t * node,
1177 vlib_frame_t * from_frame, int is_ip4)
1178{
1179 tcp_main_t *tm = vnet_get_tcp_main ();
1180 u32 n_left_from, next_index, *from, *to_next;
1181 u32 my_thread_index = vm->cpu_index, errors = 0;
1182 u8 sst = is_ip4 ? SESSION_TYPE_IP4_TCP : SESSION_TYPE_IP6_TCP;
1183
1184 from = vlib_frame_vector_args (from_frame);
1185 n_left_from = from_frame->n_vectors;
1186
1187 next_index = node->cached_next_index;
1188
1189 while (n_left_from > 0)
1190 {
1191 u32 n_left_to_next;
1192
1193 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
1194
1195 while (n_left_from > 0 && n_left_to_next > 0)
1196 {
1197 u32 bi0, ack0, seq0;
1198 vlib_buffer_t *b0;
1199 tcp_header_t *tcp0 = 0;
1200 tcp_connection_t *tc0;
1201 ip4_header_t *ip40;
1202 ip6_header_t *ip60;
1203 u32 n_advance_bytes0, n_data_bytes0;
1204 tcp_connection_t *new_tc0;
1205 u32 next0 = TCP_SYN_SENT_NEXT_DROP, error0 = TCP_ERROR_ENQUEUED;
1206
1207 bi0 = from[0];
1208 to_next[0] = bi0;
1209 from += 1;
1210 to_next += 1;
1211 n_left_from -= 1;
1212 n_left_to_next -= 1;
1213
1214 b0 = vlib_get_buffer (vm, bi0);
1215 tc0 =
1216 tcp_half_open_connection_get (vnet_buffer (b0)->
1217 tcp.connection_index);
1218
1219 ack0 = vnet_buffer (b0)->tcp.ack_number;
1220 seq0 = vnet_buffer (b0)->tcp.seq_number;
1221
1222 /* Checksum computed by ipx_local no need to compute again */
1223
1224 if (is_ip4)
1225 {
1226 ip40 = vlib_buffer_get_current (b0);
1227 tcp0 = ip4_next_header (ip40);
1228 n_advance_bytes0 = (ip4_header_bytes (ip40)
1229 + tcp_header_bytes (tcp0));
1230 n_data_bytes0 = clib_net_to_host_u16 (ip40->length)
1231 - n_advance_bytes0;
1232 }
1233 else
1234 {
1235 ip60 = vlib_buffer_get_current (b0);
1236 tcp0 = ip6_next_header (ip60);
1237 n_advance_bytes0 = tcp_header_bytes (tcp0);
1238 n_data_bytes0 = clib_net_to_host_u16 (ip60->payload_length)
1239 - n_advance_bytes0;
1240 n_advance_bytes0 += sizeof (ip60[0]);
1241 }
1242
1243 if (PREDICT_FALSE
1244 (!tcp_ack (tcp0) && !tcp_rst (tcp0) && !tcp_syn (tcp0)))
1245 goto drop;
1246
1247 /* SYNs, FINs and data consume sequence numbers */
1248 vnet_buffer (b0)->tcp.seq_end = seq0 + tcp_is_syn (tcp0)
1249 + tcp_is_fin (tcp0) + n_data_bytes0;
1250
1251 /*
1252 * 1. check the ACK bit
1253 */
1254
1255 /*
1256 * If the ACK bit is set
1257 * If SEG.ACK =< ISS, or SEG.ACK > SND.NXT, send a reset (unless
1258 * the RST bit is set, if so drop the segment and return)
1259 * <SEQ=SEG.ACK><CTL=RST>
1260 * and discard the segment. Return.
1261 * If SND.UNA =< SEG.ACK =< SND.NXT then the ACK is acceptable.
1262 */
1263 if (tcp_ack (tcp0))
1264 {
1265 if (ack0 <= tc0->iss || ack0 > tc0->snd_nxt)
1266 {
1267 if (!tcp_rst (tcp0))
1268 tcp_send_reset (b0, is_ip4);
1269
1270 goto drop;
1271 }
1272
1273 /* Make sure ACK is valid */
1274 if (tc0->snd_una > ack0)
1275 goto drop;
1276 }
1277
1278 /*
1279 * 2. check the RST bit
1280 */
1281
1282 if (tcp_rst (tcp0))
1283 {
1284 /* If ACK is acceptable, signal client that peer is not
1285 * willing to accept connection and drop connection*/
1286 if (tcp_ack (tcp0))
1287 {
1288 stream_session_connect_notify (&tc0->connection, sst,
1289 1 /* fail */ );
1290 tcp_connection_cleanup (tc0);
1291 }
1292 goto drop;
1293 }
1294
1295 /*
1296 * 3. check the security and precedence (skipped)
1297 */
1298
1299 /*
1300 * 4. check the SYN bit
1301 */
1302
1303 /* No SYN flag. Drop. */
1304 if (!tcp_syn (tcp0))
1305 goto drop;
1306
1307 /* Stop connection establishment and retransmit timers */
1308 tcp_timer_reset (tc0, TCP_TIMER_ESTABLISH);
1309 tcp_timer_reset (tc0, TCP_TIMER_RETRANSMIT_SYN);
1310
1311 /* Valid SYN or SYN-ACK. Move connection from half-open pool to
1312 * current thread pool. */
1313 pool_get (tm->connections[my_thread_index], new_tc0);
1314 clib_memcpy (new_tc0, tc0, sizeof (*new_tc0));
1315
1316 new_tc0->c_thread_index = my_thread_index;
1317
1318 /* Cleanup half-open connection XXX lock */
1319 pool_put (tm->half_open_connections, tc0);
1320
1321 new_tc0->rcv_nxt = vnet_buffer (b0)->tcp.seq_end;
1322 new_tc0->irs = seq0;
1323
1324 /* Parse options */
1325 tcp_options_parse (tcp0, &new_tc0->opt);
Dave Barach68b0fb02017-02-28 15:15:56 -05001326
1327 if (tcp_opts_tstamp (&new_tc0->opt))
1328 {
1329 new_tc0->tsval_recent = new_tc0->opt.tsval;
1330 new_tc0->tsval_recent_age = tcp_time_now ();
1331 }
1332
1333 if (tcp_opts_wscale (&new_tc0->opt))
1334 new_tc0->snd_wscale = new_tc0->opt.wscale;
1335
Florin Corase04c2992017-03-01 08:17:34 -08001336 /* No scaling */
1337 new_tc0->snd_wnd = clib_net_to_host_u16 (tcp0->window);
Dave Barach68b0fb02017-02-28 15:15:56 -05001338 new_tc0->snd_wl1 = seq0;
1339 new_tc0->snd_wl2 = ack0;
1340
Florin Corase04c2992017-03-01 08:17:34 -08001341 tcp_connection_init_vars (new_tc0);
1342
Dave Barach68b0fb02017-02-28 15:15:56 -05001343 /* SYN-ACK: See if we can switch to ESTABLISHED state */
1344 if (tcp_ack (tcp0))
1345 {
1346 /* Our SYN is ACKed: we have iss < ack = snd_una */
1347
1348 /* TODO Dequeue acknowledged segments if we support Fast Open */
1349 new_tc0->snd_una = ack0;
1350 new_tc0->state = TCP_STATE_ESTABLISHED;
1351
Florin Corase04c2992017-03-01 08:17:34 -08001352 /* Make sure las is initialized for the wnd computation */
1353 new_tc0->rcv_las = new_tc0->rcv_nxt;
1354
Dave Barach68b0fb02017-02-28 15:15:56 -05001355 /* Notify app that we have connection */
1356 stream_session_connect_notify (&new_tc0->connection, sst, 0);
1357
1358 /* Make sure after data segment processing ACK is sent */
1359 new_tc0->flags |= TCP_CONN_SNDACK;
1360 }
1361 /* SYN: Simultaneous open. Change state to SYN-RCVD and send SYN-ACK */
1362 else
1363 {
1364 new_tc0->state = TCP_STATE_SYN_RCVD;
1365
1366 /* Notify app that we have connection XXX */
1367 stream_session_connect_notify (&new_tc0->connection, sst, 0);
1368
1369 tcp_make_synack (new_tc0, b0);
1370 next0 = tcp_next_output (is_ip4);
1371
1372 goto drop;
1373 }
1374
1375 /* Read data, if any */
1376 if (n_data_bytes0)
1377 {
1378 error0 =
1379 tcp_segment_rcv (tm, new_tc0, b0, n_data_bytes0, &next0);
1380 if (error0 == TCP_ERROR_PURE_ACK)
1381 error0 = TCP_ERROR_SYN_ACKS_RCVD;
1382 }
1383 else
1384 {
1385 tcp_make_ack (new_tc0, b0);
1386 next0 = tcp_next_output (new_tc0->c_is_ip4);
1387 }
1388
1389 drop:
1390
1391 b0->error = error0 ? node->errors[error0] : 0;
1392 if (PREDICT_FALSE (b0->flags & VLIB_BUFFER_IS_TRACED))
1393 {
1394
1395 }
1396
1397 vlib_validate_buffer_enqueue_x1 (vm, node, next_index, to_next,
1398 n_left_to_next, bi0, next0);
1399 }
1400
1401 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
1402 }
1403
1404 errors = session_manager_flush_enqueue_events (my_thread_index);
1405 if (errors)
1406 {
1407 if (is_ip4)
1408 vlib_node_increment_counter (vm, tcp4_established_node.index,
1409 TCP_ERROR_EVENT_FIFO_FULL, errors);
1410 else
1411 vlib_node_increment_counter (vm, tcp6_established_node.index,
1412 TCP_ERROR_EVENT_FIFO_FULL, errors);
1413 }
1414
1415 return from_frame->n_vectors;
1416}
1417
1418static uword
1419tcp4_syn_sent (vlib_main_t * vm, vlib_node_runtime_t * node,
1420 vlib_frame_t * from_frame)
1421{
1422 return tcp46_syn_sent_inline (vm, node, from_frame, 1 /* is_ip4 */ );
1423}
1424
1425static uword
1426tcp6_syn_sent_rcv (vlib_main_t * vm, vlib_node_runtime_t * node,
1427 vlib_frame_t * from_frame)
1428{
1429 return tcp46_syn_sent_inline (vm, node, from_frame, 0 /* is_ip4 */ );
1430}
1431
1432/* *INDENT-OFF* */
1433VLIB_REGISTER_NODE (tcp4_syn_sent_node) =
1434{
1435 .function = tcp4_syn_sent,
1436 .name = "tcp4-syn-sent",
1437 /* Takes a vector of packets. */
1438 .vector_size = sizeof (u32),
1439 .n_errors = TCP_N_ERROR,
1440 .error_strings = tcp_error_strings,
1441 .n_next_nodes = TCP_SYN_SENT_N_NEXT,
1442 .next_nodes =
1443 {
1444#define _(s,n) [TCP_SYN_SENT_NEXT_##s] = n,
1445 foreach_tcp_state_next
1446#undef _
1447 },
1448};
1449/* *INDENT-ON* */
1450
1451VLIB_NODE_FUNCTION_MULTIARCH (tcp4_syn_sent_node, tcp4_syn_sent);
1452
1453/* *INDENT-OFF* */
1454VLIB_REGISTER_NODE (tcp6_syn_sent_node) =
1455{
1456 .function = tcp6_syn_sent_rcv,
1457 .name = "tcp6-syn-sent",
1458 /* Takes a vector of packets. */
1459 .vector_size = sizeof (u32),
1460 .n_errors = TCP_N_ERROR,
1461 .error_strings = tcp_error_strings,
1462 .n_next_nodes = TCP_SYN_SENT_N_NEXT,
1463 .next_nodes =
1464 {
1465#define _(s,n) [TCP_SYN_SENT_NEXT_##s] = n,
1466 foreach_tcp_state_next
1467#undef _
1468 }
1469,};
1470/* *INDENT-ON* */
1471
1472VLIB_NODE_FUNCTION_MULTIARCH (tcp6_syn_sent_node, tcp6_syn_sent_rcv);
1473/**
Florin Corasd79b41e2017-03-04 05:37:52 -08001474 * Handles reception for all states except LISTEN, SYN-SENT and ESTABLISHED
Dave Barach68b0fb02017-02-28 15:15:56 -05001475 * as per RFC793 p. 64
1476 */
1477always_inline uword
1478tcp46_rcv_process_inline (vlib_main_t * vm, vlib_node_runtime_t * node,
1479 vlib_frame_t * from_frame, int is_ip4)
1480{
1481 tcp_main_t *tm = vnet_get_tcp_main ();
1482 u32 n_left_from, next_index, *from, *to_next;
1483 u32 my_thread_index = vm->cpu_index, errors = 0;
1484
1485 from = vlib_frame_vector_args (from_frame);
1486 n_left_from = from_frame->n_vectors;
1487
1488 next_index = node->cached_next_index;
1489
1490 while (n_left_from > 0)
1491 {
1492 u32 n_left_to_next;
1493
1494 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
1495
1496 while (n_left_from > 0 && n_left_to_next > 0)
1497 {
1498 u32 bi0;
1499 vlib_buffer_t *b0;
1500 tcp_header_t *tcp0 = 0;
1501 tcp_connection_t *tc0;
1502 ip4_header_t *ip40;
1503 ip6_header_t *ip60;
1504 u32 n_advance_bytes0, n_data_bytes0;
1505 u32 next0 = TCP_RCV_PROCESS_NEXT_DROP, error0 = TCP_ERROR_ENQUEUED;
1506
1507 bi0 = from[0];
1508 to_next[0] = bi0;
1509 from += 1;
1510 to_next += 1;
1511 n_left_from -= 1;
1512 n_left_to_next -= 1;
1513
1514 b0 = vlib_get_buffer (vm, bi0);
1515 tc0 = tcp_connection_get (vnet_buffer (b0)->tcp.connection_index,
1516 my_thread_index);
Florin Corasd79b41e2017-03-04 05:37:52 -08001517 if (PREDICT_FALSE (tc0 == 0))
1518 {
1519 error0 = TCP_ERROR_INVALID_CONNECTION;
1520 goto drop;
1521 }
Dave Barach68b0fb02017-02-28 15:15:56 -05001522
1523 /* Checksum computed by ipx_local no need to compute again */
1524
1525 if (is_ip4)
1526 {
1527 ip40 = vlib_buffer_get_current (b0);
1528 tcp0 = ip4_next_header (ip40);
1529 n_advance_bytes0 = (ip4_header_bytes (ip40)
1530 + tcp_header_bytes (tcp0));
1531 n_data_bytes0 = clib_net_to_host_u16 (ip40->length)
1532 - n_advance_bytes0;
1533 }
1534 else
1535 {
1536 ip60 = vlib_buffer_get_current (b0);
1537 tcp0 = ip6_next_header (ip60);
1538 n_advance_bytes0 = tcp_header_bytes (tcp0);
1539 n_data_bytes0 = clib_net_to_host_u16 (ip60->payload_length)
1540 - n_advance_bytes0;
1541 n_advance_bytes0 += sizeof (ip60[0]);
1542 }
1543
1544 /* SYNs, FINs and data consume sequence numbers */
1545 vnet_buffer (b0)->tcp.seq_end = vnet_buffer (b0)->tcp.seq_number
1546 + tcp_is_syn (tcp0) + tcp_is_fin (tcp0) + n_data_bytes0;
1547
1548 /*
1549 * Special treatment for CLOSED
1550 */
1551 switch (tc0->state)
1552 {
1553 case TCP_STATE_CLOSED:
1554 goto drop;
1555 break;
1556 }
1557
1558 /*
1559 * For all other states (except LISTEN)
1560 */
1561
1562 /* 1-4: check SEQ, RST, SYN */
1563 if (PREDICT_FALSE
1564 (tcp_segment_validate (vm, tc0, b0, tcp0, &next0)))
1565 {
1566 error0 = TCP_ERROR_SEGMENT_INVALID;
1567 goto drop;
1568 }
1569
1570 /* 5: check the ACK field */
1571 switch (tc0->state)
1572 {
1573 case TCP_STATE_SYN_RCVD:
1574 /*
1575 * If the segment acknowledgment is not acceptable, form a
1576 * reset segment,
1577 * <SEQ=SEG.ACK><CTL=RST>
1578 * and send it.
1579 */
1580 if (!tcp_rcv_ack_is_acceptable (tc0, b0))
1581 {
1582 tcp_send_reset (b0, is_ip4);
1583 goto drop;
1584 }
1585 /* Switch state to ESTABLISHED */
1586 tc0->state = TCP_STATE_ESTABLISHED;
1587
1588 /* Initialize session variables */
1589 tc0->snd_una = vnet_buffer (b0)->tcp.ack_number;
Florin Corase04c2992017-03-01 08:17:34 -08001590 tc0->snd_wnd = clib_net_to_host_u16 (tcp0->window)
Dave Barach68b0fb02017-02-28 15:15:56 -05001591 << tc0->opt.wscale;
1592 tc0->snd_wl1 = vnet_buffer (b0)->tcp.seq_number;
1593 tc0->snd_wl2 = vnet_buffer (b0)->tcp.ack_number;
1594
1595 /* Shoulder tap the server */
1596 stream_session_accept_notify (&tc0->connection);
1597
Florin Corasd79b41e2017-03-04 05:37:52 -08001598 /* Reset SYN-ACK retransmit timer */
1599 tcp_timer_reset (tc0, TCP_TIMER_RETRANSMIT);
Dave Barach68b0fb02017-02-28 15:15:56 -05001600 break;
1601 case TCP_STATE_ESTABLISHED:
1602 /* We can get packets in established state here because they
1603 * were enqueued before state change */
1604 if (tcp_rcv_ack (tc0, b0, tcp0, &next0, &error0))
1605 goto drop;
1606
1607 break;
1608 case TCP_STATE_FIN_WAIT_1:
1609 /* In addition to the processing for the ESTABLISHED state, if
1610 * our FIN is now acknowledged then enter FIN-WAIT-2 and
1611 * continue processing in that state. */
1612 if (tcp_rcv_ack (tc0, b0, tcp0, &next0, &error0))
1613 goto drop;
Florin Corasd79b41e2017-03-04 05:37:52 -08001614
1615 /* If FIN is ACKed */
1616 if (tc0->snd_una == tc0->snd_una_max)
1617 {
1618 tc0->state = TCP_STATE_FIN_WAIT_2;
1619 /* Stop all timers, 2MSL will be set lower */
1620 tcp_connection_timers_reset (tc0);
1621 }
Dave Barach68b0fb02017-02-28 15:15:56 -05001622 break;
1623 case TCP_STATE_FIN_WAIT_2:
1624 /* In addition to the processing for the ESTABLISHED state, if
1625 * the retransmission queue is empty, the user's CLOSE can be
1626 * acknowledged ("ok") but do not delete the TCB. */
1627 if (tcp_rcv_ack (tc0, b0, tcp0, &next0, &error0))
1628 goto drop;
1629 /* check if rtx queue is empty and ack CLOSE TODO */
1630 break;
1631 case TCP_STATE_CLOSE_WAIT:
1632 /* Do the same processing as for the ESTABLISHED state. */
1633 if (tcp_rcv_ack (tc0, b0, tcp0, &next0, &error0))
1634 goto drop;
1635 break;
1636 case TCP_STATE_CLOSING:
1637 /* In addition to the processing for the ESTABLISHED state, if
1638 * the ACK acknowledges our FIN then enter the TIME-WAIT state,
1639 * otherwise ignore the segment. */
1640 if (tcp_rcv_ack (tc0, b0, tcp0, &next0, &error0))
1641 goto drop;
1642
1643 /* XXX test that send queue empty */
1644 tc0->state = TCP_STATE_TIME_WAIT;
1645 goto drop;
1646
1647 break;
1648 case TCP_STATE_LAST_ACK:
1649 /* The only thing that can arrive in this state is an
1650 * acknowledgment of our FIN. If our FIN is now acknowledged,
1651 * delete the TCB, enter the CLOSED state, and return. */
1652
1653 if (!tcp_rcv_ack_is_acceptable (tc0, b0))
1654 goto drop;
1655
Florin Corasd79b41e2017-03-04 05:37:52 -08001656 tc0->state = TCP_STATE_CLOSED;
1657
1658 /* Don't delete the connection/session yet. Instead, wait a
1659 * reasonable amount of time until the pipes are cleared. In
1660 * particular, this makes sure that we won't have dead sessions
1661 * when processing events on the tx path */
1662 tcp_timer_update (tc0, TCP_TIMER_WAITCLOSE, TCP_CLEANUP_TIME);
1663
1664 /* Stop retransmit */
1665 tcp_timer_reset (tc0, TCP_TIMER_RETRANSMIT);
1666
Dave Barach68b0fb02017-02-28 15:15:56 -05001667 goto drop;
1668
1669 break;
1670 case TCP_STATE_TIME_WAIT:
1671 /* The only thing that can arrive in this state is a
1672 * retransmission of the remote FIN. Acknowledge it, and restart
1673 * the 2 MSL timeout. */
1674
1675 /* TODO */
1676 goto drop;
1677 break;
1678 default:
1679 ASSERT (0);
1680 }
1681
1682 /* 6: check the URG bit TODO */
1683
1684 /* 7: process the segment text */
1685 switch (tc0->state)
1686 {
1687 case TCP_STATE_ESTABLISHED:
1688 case TCP_STATE_FIN_WAIT_1:
1689 case TCP_STATE_FIN_WAIT_2:
1690 error0 = tcp_segment_rcv (tm, tc0, b0, n_data_bytes0, &next0);
1691 break;
1692 case TCP_STATE_CLOSE_WAIT:
1693 case TCP_STATE_CLOSING:
1694 case TCP_STATE_LAST_ACK:
1695 case TCP_STATE_TIME_WAIT:
1696 /* This should not occur, since a FIN has been received from the
1697 * remote side. Ignore the segment text. */
1698 break;
1699 }
1700
1701 /* 8: check the FIN bit */
1702 if (!tcp_fin (tcp0))
1703 goto drop;
1704
1705 switch (tc0->state)
1706 {
1707 case TCP_STATE_ESTABLISHED:
1708 case TCP_STATE_SYN_RCVD:
1709 /* Send FIN-ACK notify app and enter CLOSE-WAIT */
1710 tcp_connection_timers_reset (tc0);
Florin Corasd79b41e2017-03-04 05:37:52 -08001711 tcp_make_fin (tc0, b0);
Dave Barach68b0fb02017-02-28 15:15:56 -05001712 next0 = tcp_next_output (tc0->c_is_ip4);
1713 stream_session_disconnect_notify (&tc0->connection);
1714 tc0->state = TCP_STATE_CLOSE_WAIT;
1715 break;
1716 case TCP_STATE_CLOSE_WAIT:
1717 case TCP_STATE_CLOSING:
1718 case TCP_STATE_LAST_ACK:
1719 /* move along .. */
1720 break;
1721 case TCP_STATE_FIN_WAIT_1:
1722 tc0->state = TCP_STATE_TIME_WAIT;
1723 tcp_connection_timers_reset (tc0);
Florin Corasd79b41e2017-03-04 05:37:52 -08001724 tcp_timer_set (tc0, TCP_TIMER_WAITCLOSE, TCP_2MSL_TIME);
Dave Barach68b0fb02017-02-28 15:15:56 -05001725 break;
1726 case TCP_STATE_FIN_WAIT_2:
1727 /* Got FIN, send ACK! */
1728 tc0->state = TCP_STATE_TIME_WAIT;
Florin Corasd79b41e2017-03-04 05:37:52 -08001729 tcp_timer_set (tc0, TCP_TIMER_WAITCLOSE, TCP_2MSL_TIME);
Dave Barach68b0fb02017-02-28 15:15:56 -05001730 tcp_make_ack (tc0, b0);
1731 next0 = tcp_next_output (is_ip4);
1732 break;
1733 case TCP_STATE_TIME_WAIT:
1734 /* Remain in the TIME-WAIT state. Restart the 2 MSL time-wait
1735 * timeout.
1736 */
Florin Corasd79b41e2017-03-04 05:37:52 -08001737 tcp_timer_update (tc0, TCP_TIMER_WAITCLOSE, TCP_2MSL_TIME);
Dave Barach68b0fb02017-02-28 15:15:56 -05001738 break;
1739 }
1740
1741 b0->error = error0 ? node->errors[error0] : 0;
1742
1743 drop:
1744 if (PREDICT_FALSE (b0->flags & VLIB_BUFFER_IS_TRACED))
1745 {
1746
1747 }
1748
1749 vlib_validate_buffer_enqueue_x1 (vm, node, next_index, to_next,
1750 n_left_to_next, bi0, next0);
1751 }
1752
1753 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
1754 }
1755
1756 errors = session_manager_flush_enqueue_events (my_thread_index);
1757 if (errors)
1758 {
1759 if (is_ip4)
1760 vlib_node_increment_counter (vm, tcp4_established_node.index,
1761 TCP_ERROR_EVENT_FIFO_FULL, errors);
1762 else
1763 vlib_node_increment_counter (vm, tcp6_established_node.index,
1764 TCP_ERROR_EVENT_FIFO_FULL, errors);
1765 }
1766
1767 return from_frame->n_vectors;
1768}
1769
1770static uword
1771tcp4_rcv_process (vlib_main_t * vm, vlib_node_runtime_t * node,
1772 vlib_frame_t * from_frame)
1773{
1774 return tcp46_rcv_process_inline (vm, node, from_frame, 1 /* is_ip4 */ );
1775}
1776
1777static uword
1778tcp6_rcv_process (vlib_main_t * vm, vlib_node_runtime_t * node,
1779 vlib_frame_t * from_frame)
1780{
1781 return tcp46_rcv_process_inline (vm, node, from_frame, 0 /* is_ip4 */ );
1782}
1783
1784/* *INDENT-OFF* */
1785VLIB_REGISTER_NODE (tcp4_rcv_process_node) =
1786{
1787 .function = tcp4_rcv_process,
1788 .name = "tcp4-rcv-process",
1789 /* Takes a vector of packets. */
1790 .vector_size = sizeof (u32),
1791 .n_errors = TCP_N_ERROR,
1792 .error_strings = tcp_error_strings,
1793 .n_next_nodes = TCP_RCV_PROCESS_N_NEXT,
1794 .next_nodes =
1795 {
1796#define _(s,n) [TCP_RCV_PROCESS_NEXT_##s] = n,
1797 foreach_tcp_state_next
1798#undef _
1799 },
1800};
1801/* *INDENT-ON* */
1802
1803VLIB_NODE_FUNCTION_MULTIARCH (tcp4_rcv_process_node, tcp4_rcv_process);
1804
1805/* *INDENT-OFF* */
1806VLIB_REGISTER_NODE (tcp6_rcv_process_node) =
1807{
1808 .function = tcp6_rcv_process,
1809 .name = "tcp6-rcv-process",
1810 /* Takes a vector of packets. */
1811 .vector_size = sizeof (u32),
1812 .n_errors = TCP_N_ERROR,
1813 .error_strings = tcp_error_strings,
1814 .n_next_nodes = TCP_RCV_PROCESS_N_NEXT,
1815 .next_nodes =
1816 {
1817#define _(s,n) [TCP_RCV_PROCESS_NEXT_##s] = n,
1818 foreach_tcp_state_next
1819#undef _
1820 },
1821};
1822/* *INDENT-ON* */
1823
1824VLIB_NODE_FUNCTION_MULTIARCH (tcp6_rcv_process_node, tcp6_rcv_process);
1825
1826vlib_node_registration_t tcp4_listen_node;
1827vlib_node_registration_t tcp6_listen_node;
1828
1829/**
1830 * LISTEN state processing as per RFC 793 p. 65
1831 */
1832always_inline uword
1833tcp46_listen_inline (vlib_main_t * vm, vlib_node_runtime_t * node,
1834 vlib_frame_t * from_frame, int is_ip4)
1835{
1836 u32 n_left_from, next_index, *from, *to_next;
1837 u32 my_thread_index = vm->cpu_index;
1838 tcp_main_t *tm = vnet_get_tcp_main ();
1839 u8 sst = is_ip4 ? SESSION_TYPE_IP4_TCP : SESSION_TYPE_IP6_TCP;
1840
1841 from = vlib_frame_vector_args (from_frame);
1842 n_left_from = from_frame->n_vectors;
1843
1844 next_index = node->cached_next_index;
1845
1846 while (n_left_from > 0)
1847 {
1848 u32 n_left_to_next;
1849
1850 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
1851
1852 while (n_left_from > 0 && n_left_to_next > 0)
1853 {
1854 u32 bi0;
1855 vlib_buffer_t *b0;
1856 tcp_header_t *th0 = 0;
1857 tcp_connection_t *lc0;
1858 ip4_header_t *ip40;
1859 ip6_header_t *ip60;
1860 tcp_connection_t *child0;
1861 u32 error0 = TCP_ERROR_SYNS_RCVD, next0 = TCP_LISTEN_NEXT_DROP;
1862
1863 bi0 = from[0];
1864 to_next[0] = bi0;
1865 from += 1;
1866 to_next += 1;
1867 n_left_from -= 1;
1868 n_left_to_next -= 1;
1869
1870 b0 = vlib_get_buffer (vm, bi0);
1871 lc0 = tcp_listener_get (vnet_buffer (b0)->tcp.connection_index);
1872
1873 if (is_ip4)
1874 {
1875 ip40 = vlib_buffer_get_current (b0);
1876 th0 = ip4_next_header (ip40);
1877 }
1878 else
1879 {
1880 ip60 = vlib_buffer_get_current (b0);
1881 th0 = ip6_next_header (ip60);
1882 }
1883
1884 /* Create child session. For syn-flood protection use filter */
1885
1886 /* 1. first check for an RST */
1887 if (tcp_rst (th0))
1888 goto drop;
1889
1890 /* 2. second check for an ACK */
1891 if (tcp_ack (th0))
1892 {
1893 tcp_send_reset (b0, is_ip4);
1894 goto drop;
1895 }
1896
1897 /* 3. check for a SYN (did that already) */
1898
1899 /* Create child session and send SYN-ACK */
1900 pool_get (tm->connections[my_thread_index], child0);
1901 memset (child0, 0, sizeof (*child0));
1902
1903 child0->c_c_index = child0 - tm->connections[my_thread_index];
1904 child0->c_lcl_port = lc0->c_lcl_port;
1905 child0->c_rmt_port = th0->src_port;
1906 child0->c_is_ip4 = is_ip4;
1907 child0->c_thread_index = my_thread_index;
1908
1909 if (is_ip4)
1910 {
1911 child0->c_lcl_ip4.as_u32 = ip40->dst_address.as_u32;
1912 child0->c_rmt_ip4.as_u32 = ip40->src_address.as_u32;
1913 }
1914 else
1915 {
1916 clib_memcpy (&child0->c_lcl_ip6, &ip60->dst_address,
1917 sizeof (ip6_address_t));
1918 clib_memcpy (&child0->c_rmt_ip6, &ip60->src_address,
1919 sizeof (ip6_address_t));
1920 }
1921
1922 if (stream_session_accept (&child0->connection, lc0->c_s_index, sst,
1923 0 /* notify */ ))
1924 {
1925 error0 = TCP_ERROR_CREATE_SESSION_FAIL;
1926 goto drop;
1927 }
1928
1929 tcp_options_parse (th0, &child0->opt);
Dave Barach68b0fb02017-02-28 15:15:56 -05001930
1931 child0->irs = vnet_buffer (b0)->tcp.seq_number;
1932 child0->rcv_nxt = vnet_buffer (b0)->tcp.seq_number + 1;
1933 child0->state = TCP_STATE_SYN_RCVD;
1934
1935 /* RFC1323: TSval timestamps sent on {SYN} and {SYN,ACK}
1936 * segments are used to initialize PAWS. */
1937 if (tcp_opts_tstamp (&child0->opt))
1938 {
1939 child0->tsval_recent = child0->opt.tsval;
1940 child0->tsval_recent_age = tcp_time_now ();
1941 }
1942
Florin Corase04c2992017-03-01 08:17:34 -08001943 if (tcp_opts_wscale (&child0->opt))
1944 child0->snd_wscale = child0->opt.wscale;
1945
1946 /* No scaling */
1947 child0->snd_wnd = clib_net_to_host_u16 (th0->window);
1948 child0->snd_wl1 = vnet_buffer (b0)->tcp.seq_number;
1949 child0->snd_wl2 = vnet_buffer (b0)->tcp.ack_number;
1950
1951 tcp_connection_init_vars (child0);
1952
Dave Barach68b0fb02017-02-28 15:15:56 -05001953 /* Reuse buffer to make syn-ack and send */
1954 tcp_make_synack (child0, b0);
1955 next0 = tcp_next_output (is_ip4);
1956
1957 drop:
1958 if (PREDICT_FALSE (b0->flags & VLIB_BUFFER_IS_TRACED))
1959 {
1960
1961 }
1962
Florin Corase04c2992017-03-01 08:17:34 -08001963 b0->error = node->errors[error0];
Dave Barach68b0fb02017-02-28 15:15:56 -05001964
1965 vlib_validate_buffer_enqueue_x1 (vm, node, next_index, to_next,
1966 n_left_to_next, bi0, next0);
1967 }
1968
1969 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
1970 }
1971 return from_frame->n_vectors;
1972}
1973
1974static uword
1975tcp4_listen (vlib_main_t * vm, vlib_node_runtime_t * node,
1976 vlib_frame_t * from_frame)
1977{
1978 return tcp46_listen_inline (vm, node, from_frame, 1 /* is_ip4 */ );
1979}
1980
1981static uword
1982tcp6_listen (vlib_main_t * vm, vlib_node_runtime_t * node,
1983 vlib_frame_t * from_frame)
1984{
1985 return tcp46_listen_inline (vm, node, from_frame, 0 /* is_ip4 */ );
1986}
1987
1988/* *INDENT-OFF* */
1989VLIB_REGISTER_NODE (tcp4_listen_node) =
1990{
1991 .function = tcp4_listen,
1992 .name = "tcp4-listen",
1993 /* Takes a vector of packets. */
1994 .vector_size = sizeof (u32),
1995 .n_errors = TCP_N_ERROR,
1996 .error_strings = tcp_error_strings,
1997 .n_next_nodes = TCP_LISTEN_N_NEXT,
1998 .next_nodes =
1999 {
2000#define _(s,n) [TCP_LISTEN_NEXT_##s] = n,
2001 foreach_tcp_state_next
2002#undef _
2003 },
2004};
2005/* *INDENT-ON* */
2006
2007VLIB_NODE_FUNCTION_MULTIARCH (tcp4_listen_node, tcp4_listen);
2008
2009/* *INDENT-OFF* */
2010VLIB_REGISTER_NODE (tcp6_listen_node) =
2011{
2012 .function = tcp6_listen,
2013 .name = "tcp6-listen",
2014 /* Takes a vector of packets. */
2015 .vector_size = sizeof (u32),
2016 .n_errors = TCP_N_ERROR,
2017 .error_strings = tcp_error_strings,
2018 .n_next_nodes = TCP_LISTEN_N_NEXT,
2019 .next_nodes =
2020 {
2021#define _(s,n) [TCP_LISTEN_NEXT_##s] = n,
2022 foreach_tcp_state_next
2023#undef _
2024 },
2025};
2026/* *INDENT-ON* */
2027
2028VLIB_NODE_FUNCTION_MULTIARCH (tcp6_listen_node, tcp6_listen);
2029
2030vlib_node_registration_t tcp4_input_node;
2031vlib_node_registration_t tcp6_input_node;
2032
2033typedef enum _tcp_input_next
2034{
2035 TCP_INPUT_NEXT_DROP,
2036 TCP_INPUT_NEXT_LISTEN,
2037 TCP_INPUT_NEXT_RCV_PROCESS,
2038 TCP_INPUT_NEXT_SYN_SENT,
2039 TCP_INPUT_NEXT_ESTABLISHED,
2040 TCP_INPUT_NEXT_RESET,
2041 TCP_INPUT_N_NEXT
2042} tcp_input_next_t;
2043
2044#define foreach_tcp4_input_next \
2045 _ (DROP, "error-drop") \
2046 _ (LISTEN, "tcp4-listen") \
2047 _ (RCV_PROCESS, "tcp4-rcv-process") \
2048 _ (SYN_SENT, "tcp4-syn-sent") \
2049 _ (ESTABLISHED, "tcp4-established") \
2050 _ (RESET, "tcp4-reset")
2051
2052#define foreach_tcp6_input_next \
2053 _ (DROP, "error-drop") \
2054 _ (LISTEN, "tcp6-listen") \
2055 _ (RCV_PROCESS, "tcp6-rcv-process") \
2056 _ (SYN_SENT, "tcp6-syn-sent") \
2057 _ (ESTABLISHED, "tcp6-established") \
2058 _ (RESET, "tcp6-reset")
2059
2060typedef struct
2061{
2062 u16 src_port;
2063 u16 dst_port;
2064 u8 state;
2065} tcp_rx_trace_t;
2066
2067const char *tcp_fsm_states[] = {
2068#define _(sym, str) str,
2069 foreach_tcp_fsm_state
2070#undef _
2071};
2072
2073u8 *
2074format_tcp_state (u8 * s, va_list * args)
2075{
2076 tcp_state_t *state = va_arg (*args, tcp_state_t *);
2077
2078 if (state[0] < TCP_N_STATES)
2079 s = format (s, "%s", tcp_fsm_states[state[0]]);
2080 else
2081 s = format (s, "UNKNOWN");
2082
2083 return s;
2084}
2085
2086u8 *
2087format_tcp_rx_trace (u8 * s, va_list * args)
2088{
2089 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
2090 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
2091 tcp_rx_trace_t *t = va_arg (*args, tcp_rx_trace_t *);
2092
2093 s = format (s, "TCP: src-port %d dst-port %U%s\n",
2094 clib_net_to_host_u16 (t->src_port),
2095 clib_net_to_host_u16 (t->dst_port), format_tcp_state, t->state);
2096
2097 return s;
2098}
2099
2100#define filter_flags (TCP_FLAG_SYN|TCP_FLAG_ACK|TCP_FLAG_RST|TCP_FLAG_FIN)
2101
2102always_inline uword
2103tcp46_input_inline (vlib_main_t * vm, vlib_node_runtime_t * node,
2104 vlib_frame_t * from_frame, int is_ip4)
2105{
2106 u32 n_left_from, next_index, *from, *to_next;
2107 u32 my_thread_index = vm->cpu_index;
2108 tcp_main_t *tm = vnet_get_tcp_main ();
Dave Barach68b0fb02017-02-28 15:15:56 -05002109
2110 from = vlib_frame_vector_args (from_frame);
2111 n_left_from = from_frame->n_vectors;
2112
2113 next_index = node->cached_next_index;
2114
2115 while (n_left_from > 0)
2116 {
2117 u32 n_left_to_next;
2118
2119 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
2120
2121 while (n_left_from > 0 && n_left_to_next > 0)
2122 {
2123 u32 bi0;
2124 vlib_buffer_t *b0;
2125 tcp_header_t *tcp0 = 0;
2126 tcp_connection_t *tc0;
2127 ip4_header_t *ip40;
2128 ip6_header_t *ip60;
2129 u32 error0 = TCP_ERROR_NO_LISTENER, next0 = TCP_INPUT_NEXT_DROP;
2130 u8 flags0;
2131
2132 bi0 = from[0];
2133 to_next[0] = bi0;
2134 from += 1;
2135 to_next += 1;
2136 n_left_from -= 1;
2137 n_left_to_next -= 1;
2138
2139 b0 = vlib_get_buffer (vm, bi0);
Florin Corasd79b41e2017-03-04 05:37:52 -08002140 vnet_buffer (b0)->tcp.flags = 0;
Dave Barach68b0fb02017-02-28 15:15:56 -05002141
2142 if (is_ip4)
2143 {
2144 ip40 = vlib_buffer_get_current (b0);
2145 tcp0 = ip4_next_header (ip40);
2146
2147 /* lookup session */
2148 tc0 =
Florin Corase04c2992017-03-01 08:17:34 -08002149 (tcp_connection_t *)
2150 stream_session_lookup_transport4 (&ip40->dst_address,
2151 &ip40->src_address,
2152 tcp0->dst_port,
2153 tcp0->src_port,
2154 SESSION_TYPE_IP4_TCP,
2155 my_thread_index);
Dave Barach68b0fb02017-02-28 15:15:56 -05002156 }
2157 else
2158 {
2159 ip60 = vlib_buffer_get_current (b0);
2160 tcp0 = ip6_next_header (ip60);
2161 tc0 =
Florin Corase04c2992017-03-01 08:17:34 -08002162 (tcp_connection_t *)
2163 stream_session_lookup_transport6 (&ip60->src_address,
2164 &ip60->dst_address,
2165 tcp0->src_port,
2166 tcp0->dst_port,
2167 SESSION_TYPE_IP6_TCP,
2168 my_thread_index);
Dave Barach68b0fb02017-02-28 15:15:56 -05002169 }
2170
2171 /* Session exists */
2172 if (PREDICT_TRUE (0 != tc0))
2173 {
2174 /* Save connection index */
2175 vnet_buffer (b0)->tcp.connection_index = tc0->c_c_index;
2176 vnet_buffer (b0)->tcp.seq_number =
2177 clib_net_to_host_u32 (tcp0->seq_number);
2178 vnet_buffer (b0)->tcp.ack_number =
2179 clib_net_to_host_u32 (tcp0->ack_number);
2180
2181 flags0 = tcp0->flags & filter_flags;
2182 next0 = tm->dispatch_table[tc0->state][flags0].next;
2183 error0 = tm->dispatch_table[tc0->state][flags0].error;
2184
2185 if (PREDICT_FALSE (error0 == TCP_ERROR_DISPATCH))
2186 {
2187 /* Overload tcp flags to store state */
2188 vnet_buffer (b0)->tcp.flags = tc0->state;
2189 }
2190 }
2191 else
2192 {
2193 /* Send reset */
2194 next0 = TCP_INPUT_NEXT_RESET;
2195 error0 = TCP_ERROR_NO_LISTENER;
Dave Barach68b0fb02017-02-28 15:15:56 -05002196 }
2197
2198 b0->error = error0 ? node->errors[error0] : 0;
2199
2200 if (PREDICT_FALSE (b0->flags & VLIB_BUFFER_IS_TRACED))
2201 {
2202
2203 }
2204
2205 vlib_validate_buffer_enqueue_x1 (vm, node, next_index, to_next,
2206 n_left_to_next, bi0, next0);
2207 }
2208
2209 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
2210 }
2211
2212 return from_frame->n_vectors;
2213}
2214
2215static uword
2216tcp4_input (vlib_main_t * vm, vlib_node_runtime_t * node,
2217 vlib_frame_t * from_frame)
2218{
2219 return tcp46_input_inline (vm, node, from_frame, 1 /* is_ip4 */ );
2220}
2221
2222static uword
2223tcp6_input (vlib_main_t * vm, vlib_node_runtime_t * node,
2224 vlib_frame_t * from_frame)
2225{
2226 return tcp46_input_inline (vm, node, from_frame, 0 /* is_ip4 */ );
2227}
2228
2229/* *INDENT-OFF* */
2230VLIB_REGISTER_NODE (tcp4_input_node) =
2231{
2232 .function = tcp4_input,
2233 .name = "tcp4-input",
2234 /* Takes a vector of packets. */
2235 .vector_size = sizeof (u32),
2236 .n_errors = TCP_N_ERROR,
2237 .error_strings = tcp_error_strings,
2238 .n_next_nodes = TCP_INPUT_N_NEXT,
2239 .next_nodes =
2240 {
2241#define _(s,n) [TCP_INPUT_NEXT_##s] = n,
2242 foreach_tcp4_input_next
2243#undef _
2244 },
2245 .format_buffer = format_tcp_header,
2246 .format_trace = format_tcp_rx_trace,
2247};
2248/* *INDENT-ON* */
2249
2250VLIB_NODE_FUNCTION_MULTIARCH (tcp4_input_node, tcp4_input);
2251
2252/* *INDENT-OFF* */
2253VLIB_REGISTER_NODE (tcp6_input_node) =
2254{
2255 .function = tcp6_input,
2256 .name = "tcp6-input",
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_INPUT_N_NEXT,
2262 .next_nodes =
2263 {
2264#define _(s,n) [TCP_INPUT_NEXT_##s] = n,
2265 foreach_tcp6_input_next
2266#undef _
2267 },
2268 .format_buffer = format_tcp_header,
2269 .format_trace = format_tcp_rx_trace,
2270};
2271/* *INDENT-ON* */
2272
2273VLIB_NODE_FUNCTION_MULTIARCH (tcp6_input_node, tcp6_input);
2274void
2275tcp_update_time (f64 now, u32 thread_index)
2276{
2277 tcp_main_t *tm = vnet_get_tcp_main ();
2278 tw_timer_expire_timers_16t_2w_512sl (&tm->timer_wheels[thread_index], now);
2279}
2280
2281static void
2282tcp_dispatch_table_init (tcp_main_t * tm)
2283{
2284 int i, j;
2285 for (i = 0; i < ARRAY_LEN (tm->dispatch_table); i++)
2286 for (j = 0; j < ARRAY_LEN (tm->dispatch_table[i]); j++)
2287 {
2288 tm->dispatch_table[i][j].next = TCP_INPUT_NEXT_DROP;
2289 tm->dispatch_table[i][j].error = TCP_ERROR_DISPATCH;
2290 }
2291
2292#define _(t,f,n,e) \
2293do { \
2294 tm->dispatch_table[TCP_STATE_##t][f].next = (n); \
2295 tm->dispatch_table[TCP_STATE_##t][f].error = (e); \
2296} while (0)
2297
2298 /* SYNs for new connections -> tcp-listen. */
2299 _(LISTEN, TCP_FLAG_SYN, TCP_INPUT_NEXT_LISTEN, TCP_ERROR_NONE);
2300 /* ACK for for a SYN-ACK -> tcp-rcv-process. */
2301 _(SYN_RCVD, TCP_FLAG_ACK, TCP_INPUT_NEXT_RCV_PROCESS, TCP_ERROR_NONE);
2302 /* SYN-ACK for a SYN */
2303 _(SYN_SENT, TCP_FLAG_SYN | TCP_FLAG_ACK, TCP_INPUT_NEXT_SYN_SENT,
2304 TCP_ERROR_NONE);
2305 _(SYN_SENT, TCP_FLAG_ACK, TCP_INPUT_NEXT_SYN_SENT, TCP_ERROR_NONE);
2306 _(SYN_SENT, TCP_FLAG_RST, TCP_INPUT_NEXT_SYN_SENT, TCP_ERROR_NONE);
2307 _(SYN_SENT, TCP_FLAG_RST | TCP_FLAG_ACK, TCP_INPUT_NEXT_SYN_SENT,
2308 TCP_ERROR_NONE);
2309 /* ACK for for established connection -> tcp-established. */
2310 _(ESTABLISHED, TCP_FLAG_ACK, TCP_INPUT_NEXT_ESTABLISHED, TCP_ERROR_NONE);
2311 /* FIN for for established connection -> tcp-established. */
2312 _(ESTABLISHED, TCP_FLAG_FIN, TCP_INPUT_NEXT_ESTABLISHED, TCP_ERROR_NONE);
2313 _(ESTABLISHED, TCP_FLAG_FIN | TCP_FLAG_ACK, TCP_INPUT_NEXT_ESTABLISHED,
2314 TCP_ERROR_NONE);
Florin Corasd79b41e2017-03-04 05:37:52 -08002315 _(ESTABLISHED, TCP_FLAG_RST, TCP_INPUT_NEXT_ESTABLISHED, TCP_ERROR_NONE);
Dave Barach68b0fb02017-02-28 15:15:56 -05002316 /* ACK or FIN-ACK to our FIN */
2317 _(FIN_WAIT_1, TCP_FLAG_ACK, TCP_INPUT_NEXT_RCV_PROCESS, TCP_ERROR_NONE);
2318 _(FIN_WAIT_1, TCP_FLAG_ACK | TCP_FLAG_FIN, TCP_INPUT_NEXT_RCV_PROCESS,
2319 TCP_ERROR_NONE);
2320 /* FIN in reply to our FIN from the other side */
2321 _(FIN_WAIT_1, TCP_FLAG_FIN, TCP_INPUT_NEXT_RCV_PROCESS, TCP_ERROR_NONE);
2322 /* FIN confirming that the peer (app) has closed */
2323 _(FIN_WAIT_2, TCP_FLAG_FIN, TCP_INPUT_NEXT_RCV_PROCESS, TCP_ERROR_NONE);
2324 _(FIN_WAIT_2, TCP_FLAG_FIN | TCP_FLAG_ACK, TCP_INPUT_NEXT_RCV_PROCESS,
2325 TCP_ERROR_NONE);
2326 _(LAST_ACK, TCP_FLAG_ACK, TCP_INPUT_NEXT_RCV_PROCESS, TCP_ERROR_NONE);
2327#undef _
2328}
2329
2330clib_error_t *
2331tcp_input_init (vlib_main_t * vm)
2332{
2333 clib_error_t *error = 0;
2334 tcp_main_t *tm = vnet_get_tcp_main ();
2335
2336 if ((error = vlib_call_init_function (vm, tcp_init)))
2337 return error;
2338
2339 /* Initialize dispatch table. */
2340 tcp_dispatch_table_init (tm);
2341
2342 return error;
2343}
2344
2345VLIB_INIT_FUNCTION (tcp_input_init);
2346
2347/*
2348 * fd.io coding-style-patch-verification: ON
2349 *
2350 * Local Variables:
2351 * eval: (c-set-style "gnu")
2352 * End:
2353 */