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