blob: 7e9fa47b290b81dbca28fb9783b10ae75bf5d043 [file] [log] [blame]
Dave Barach68b0fb02017-02-28 15:15:56 -05001/*
2 * Copyright (c) 2016 Cisco and/or its affiliates.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at:
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16#include <vppinfra/sparse_vec.h>
17#include <vnet/tcp/tcp_packet.h>
18#include <vnet/tcp/tcp.h>
19#include <vnet/session/session.h>
20#include <math.h>
21
22static char *tcp_error_strings[] = {
23#define tcp_error(n,s) s,
24#include <vnet/tcp/tcp_error.def>
25#undef tcp_error
26};
27
28/* All TCP nodes have the same outgoing arcs */
29#define foreach_tcp_state_next \
30 _ (DROP, "error-drop") \
31 _ (TCP4_OUTPUT, "tcp4-output") \
32 _ (TCP6_OUTPUT, "tcp6-output")
33
34typedef enum _tcp_established_next
35{
36#define _(s,n) TCP_ESTABLISHED_NEXT_##s,
37 foreach_tcp_state_next
38#undef _
39 TCP_ESTABLISHED_N_NEXT,
40} tcp_established_next_t;
41
42typedef enum _tcp_rcv_process_next
43{
44#define _(s,n) TCP_RCV_PROCESS_NEXT_##s,
45 foreach_tcp_state_next
46#undef _
47 TCP_RCV_PROCESS_N_NEXT,
48} tcp_rcv_process_next_t;
49
50typedef enum _tcp_syn_sent_next
51{
52#define _(s,n) TCP_SYN_SENT_NEXT_##s,
53 foreach_tcp_state_next
54#undef _
55 TCP_SYN_SENT_N_NEXT,
56} tcp_syn_sent_next_t;
57
58typedef enum _tcp_listen_next
59{
60#define _(s,n) TCP_LISTEN_NEXT_##s,
61 foreach_tcp_state_next
62#undef _
63 TCP_LISTEN_N_NEXT,
64} tcp_listen_next_t;
65
66/* Generic, state independent indices */
67typedef enum _tcp_state_next
68{
69#define _(s,n) TCP_NEXT_##s,
70 foreach_tcp_state_next
71#undef _
72 TCP_STATE_N_NEXT,
73} tcp_state_next_t;
74
75#define tcp_next_output(is_ip4) (is_ip4 ? TCP_NEXT_TCP4_OUTPUT \
76 : TCP_NEXT_TCP6_OUTPUT)
77
78vlib_node_registration_t tcp4_established_node;
79vlib_node_registration_t tcp6_established_node;
80
81/**
82 * Validate segment sequence number. As per RFC793:
83 *
84 * Segment Receive Test
85 * Length Window
86 * ------- ------- -------------------------------------------
87 * 0 0 SEG.SEQ = RCV.NXT
88 * 0 >0 RCV.NXT =< SEG.SEQ < RCV.NXT+RCV.WND
89 * >0 0 not acceptable
90 * >0 >0 RCV.NXT =< SEG.SEQ < RCV.NXT+RCV.WND
91 * or RCV.NXT =< SEG.SEQ+SEG.LEN-1 < RCV.NXT+RCV.WND
92 *
93 * This ultimately consists in checking if segment falls within the window.
94 * The one important difference compared to RFC793 is that we use rcv_las,
95 * or the rcv_nxt at last ack sent instead of rcv_nxt since that's the
96 * peer's reference when computing our receive window.
97 *
Florin Coras6792ec02017-03-13 03:49:51 -070098 * This:
99 * seq_leq (end_seq, tc->rcv_las + tc->rcv_wnd) && seq_geq (seq, tc->rcv_las)
100 * however, is too strict when we have retransmits. Instead we just check that
101 * the seq is not beyond the right edge and that the end of the segment is not
102 * less than the left edge.
103 *
104 * N.B. rcv_nxt and rcv_wnd are both updated in this node if acks are sent, so
105 * use rcv_nxt in the right edge window test instead of rcv_las.
106 *
Dave Barach68b0fb02017-02-28 15:15:56 -0500107 */
108always_inline u8
109tcp_segment_in_rcv_wnd (tcp_connection_t * tc, u32 seq, u32 end_seq)
110{
Florin Coras6792ec02017-03-13 03:49:51 -0700111 return (seq_geq (end_seq, tc->rcv_las)
112 && seq_leq (seq, tc->rcv_nxt + tc->rcv_wnd));
Dave Barach68b0fb02017-02-28 15:15:56 -0500113}
114
115void
116tcp_options_parse (tcp_header_t * th, tcp_options_t * to)
117{
118 const u8 *data;
119 u8 opt_len, opts_len, kind;
120 int j;
121 sack_block_t b;
122
123 opts_len = (tcp_doff (th) << 2) - sizeof (tcp_header_t);
124 data = (const u8 *) (th + 1);
125
126 /* Zero out all flags but those set in SYN */
127 to->flags &= (TCP_OPTS_FLAG_SACK_PERMITTED | TCP_OPTS_FLAG_WSCALE);
128
129 for (; opts_len > 0; opts_len -= opt_len, data += opt_len)
130 {
131 kind = data[0];
132
133 /* Get options length */
134 if (kind == TCP_OPTION_EOL)
135 break;
136 else if (kind == TCP_OPTION_NOOP)
137 opt_len = 1;
138 else
139 {
140 /* broken options */
141 if (opts_len < 2)
142 break;
143 opt_len = data[1];
144
145 /* weird option length */
146 if (opt_len < 2 || opt_len > opts_len)
147 break;
148 }
149
150 /* Parse options */
151 switch (kind)
152 {
153 case TCP_OPTION_MSS:
154 if ((opt_len == TCP_OPTION_LEN_MSS) && tcp_syn (th))
155 {
156 to->flags |= TCP_OPTS_FLAG_MSS;
157 to->mss = clib_net_to_host_u16 (*(u16 *) (data + 2));
158 }
159 break;
160 case TCP_OPTION_WINDOW_SCALE:
161 if ((opt_len == TCP_OPTION_LEN_WINDOW_SCALE) && tcp_syn (th))
162 {
163 to->flags |= TCP_OPTS_FLAG_WSCALE;
164 to->wscale = data[2];
165 if (to->wscale > TCP_MAX_WND_SCALE)
166 {
167 clib_warning ("Illegal window scaling value: %d",
168 to->wscale);
169 to->wscale = TCP_MAX_WND_SCALE;
170 }
171 }
172 break;
173 case TCP_OPTION_TIMESTAMP:
174 if (opt_len == TCP_OPTION_LEN_TIMESTAMP)
175 {
176 to->flags |= TCP_OPTS_FLAG_TSTAMP;
177 to->tsval = clib_net_to_host_u32 (*(u32 *) (data + 2));
178 to->tsecr = clib_net_to_host_u32 (*(u32 *) (data + 6));
179 }
180 break;
181 case TCP_OPTION_SACK_PERMITTED:
182 if (opt_len == TCP_OPTION_LEN_SACK_PERMITTED && tcp_syn (th))
183 to->flags |= TCP_OPTS_FLAG_SACK_PERMITTED;
184 break;
185 case TCP_OPTION_SACK_BLOCK:
186 /* If SACK permitted was not advertised or a SYN, break */
187 if ((to->flags & TCP_OPTS_FLAG_SACK_PERMITTED) == 0 || tcp_syn (th))
188 break;
189
190 /* If too short or not correctly formatted, break */
191 if (opt_len < 10 || ((opt_len - 2) % TCP_OPTION_LEN_SACK_BLOCK))
192 break;
193
194 to->flags |= TCP_OPTS_FLAG_SACK;
195 to->n_sack_blocks = (opt_len - 2) / TCP_OPTION_LEN_SACK_BLOCK;
196 vec_reset_length (to->sacks);
197 for (j = 0; j < to->n_sack_blocks; j++)
198 {
199 b.start = clib_net_to_host_u32 (*(u32 *) (data + 2 + 4 * j));
200 b.end = clib_net_to_host_u32 (*(u32 *) (data + 6 + 4 * j));
201 vec_add1 (to->sacks, b);
202 }
203 break;
204 default:
205 /* Nothing to see here */
206 continue;
207 }
208 }
209}
210
211always_inline int
212tcp_segment_check_paws (tcp_connection_t * tc)
213{
214 /* XXX normally test for timestamp should be lt instead of leq, but for
215 * local testing this is not enough */
216 return tcp_opts_tstamp (&tc->opt) && tc->tsval_recent
217 && timestamp_lt (tc->opt.tsval, tc->tsval_recent);
218}
219
220/**
221 * Validate incoming segment as per RFC793 p. 69 and RFC1323 p. 19
222 *
223 * It first verifies if segment has a wrapped sequence number (PAWS) and then
224 * does the processing associated to the first four steps (ignoring security
225 * and precedence): sequence number, rst bit and syn bit checks.
226 *
227 * @return 0 if segments passes validation.
228 */
229static int
230tcp_segment_validate (vlib_main_t * vm, tcp_connection_t * tc0,
231 vlib_buffer_t * b0, tcp_header_t * th0, u32 * next0)
232{
233 u8 paws_failed;
234
235 if (PREDICT_FALSE (!tcp_ack (th0) && !tcp_rst (th0) && !tcp_syn (th0)))
236 return -1;
237
238 tcp_options_parse (th0, &tc0->opt);
239
240 /* RFC1323: Check against wrapped sequence numbers (PAWS). If we have
241 * timestamp to echo and it's less than tsval_recent, drop segment
242 * but still send an ACK in order to retain TCP's mechanism for detecting
243 * and recovering from half-open connections */
244 paws_failed = tcp_segment_check_paws (tc0);
245 if (paws_failed)
246 {
247 clib_warning ("paws failed");
248
249 /* If it just so happens that a segment updates tsval_recent for a
250 * segment over 24 days old, invalidate tsval_recent. */
251 if (timestamp_lt (tc0->tsval_recent_age + TCP_PAWS_IDLE,
252 tcp_time_now ()))
253 {
254 /* Age isn't reset until we get a valid tsval (bsd inspired) */
255 tc0->tsval_recent = 0;
256 }
257 else
258 {
259 /* Drop after ack if not rst */
260 if (!tcp_rst (th0))
261 {
262 tcp_make_ack (tc0, b0);
263 *next0 = tcp_next_output (tc0->c_is_ip4);
Florin Coras6792ec02017-03-13 03:49:51 -0700264 TCP_EVT_DBG (TCP_EVT_DUPACK_SENT, tc0);
Dave Barach68b0fb02017-02-28 15:15:56 -0500265 return -1;
266 }
267 }
268 }
269
270 /* 1st: check sequence number */
271 if (!tcp_segment_in_rcv_wnd (tc0, vnet_buffer (b0)->tcp.seq_number,
272 vnet_buffer (b0)->tcp.seq_end))
273 {
Florin Coras6792ec02017-03-13 03:49:51 -0700274 /* If our window is 0 and the packet is in sequence, let it pass
275 * through for ack processing. It should be dropped later.*/
276 if (tc0->rcv_wnd == 0
277 && tc0->rcv_nxt == vnet_buffer (b0)->tcp.seq_number)
Dave Barach68b0fb02017-02-28 15:15:56 -0500278 {
Florin Coras3e350af2017-03-30 02:54:28 -0700279 /* TODO Should segment be tagged? */
Dave Barach68b0fb02017-02-28 15:15:56 -0500280 }
Florin Coras6792ec02017-03-13 03:49:51 -0700281 else
282 {
283 /* If not RST, send dup ack */
284 if (!tcp_rst (th0))
285 {
286 tcp_make_ack (tc0, b0);
287 *next0 = tcp_next_output (tc0->c_is_ip4);
288 TCP_EVT_DBG (TCP_EVT_DUPACK_SENT, tc0);
289 }
290 return -1;
291 }
Dave Barach68b0fb02017-02-28 15:15:56 -0500292 }
293
294 /* 2nd: check the RST bit */
295 if (tcp_rst (th0))
296 {
Florin Corasd79b41e2017-03-04 05:37:52 -0800297 tcp_connection_reset (tc0);
Dave Barach68b0fb02017-02-28 15:15:56 -0500298 return -1;
299 }
300
301 /* 3rd: check security and precedence (skip) */
302
303 /* 4th: check the SYN bit */
304 if (tcp_syn (th0))
305 {
306 tcp_send_reset (b0, tc0->c_is_ip4);
307 return -1;
308 }
309
310 /* If PAWS passed and segment in window, save timestamp */
311 if (!paws_failed)
312 {
313 tc0->tsval_recent = tc0->opt.tsval;
314 tc0->tsval_recent_age = tcp_time_now ();
315 }
316
317 return 0;
318}
319
320always_inline int
321tcp_rcv_ack_is_acceptable (tcp_connection_t * tc0, vlib_buffer_t * tb0)
322{
323 /* SND.UNA =< SEG.ACK =< SND.NXT */
324 return (seq_leq (tc0->snd_una, vnet_buffer (tb0)->tcp.ack_number)
325 && seq_leq (vnet_buffer (tb0)->tcp.ack_number, tc0->snd_nxt));
326}
327
328/**
329 * Compute smoothed RTT as per VJ's '88 SIGCOMM and RFC6298
330 *
331 * Note that although the original article, srtt and rttvar are scaled
332 * to minimize round-off errors, here we don't. Instead, we rely on
333 * better precision time measurements.
334 *
335 * TODO support us rtt resolution
336 */
337static void
338tcp_estimate_rtt (tcp_connection_t * tc, u32 mrtt)
339{
340 int err;
341
342 if (tc->srtt != 0)
343 {
344 err = mrtt - tc->srtt;
345 tc->srtt += err >> 3;
346
347 /* XXX Drop in RTT results in RTTVAR increase and bigger RTO.
348 * The increase should be bound */
Florin Coras6792ec02017-03-13 03:49:51 -0700349 tc->rttvar += ((int) clib_abs (err) - (int) tc->rttvar) >> 2;
Dave Barach68b0fb02017-02-28 15:15:56 -0500350 }
351 else
352 {
353 /* First measurement. */
354 tc->srtt = mrtt;
Florin Coras6792ec02017-03-13 03:49:51 -0700355 tc->rttvar = mrtt >> 1;
Dave Barach68b0fb02017-02-28 15:15:56 -0500356 }
357}
358
359/** Update RTT estimate and RTO timer
360 *
361 * Measure RTT: We have two sources of RTT measurements: TSOPT and ACK
362 * timing. Middle boxes are known to fiddle with TCP options so we
363 * should give higher priority to ACK timing.
364 *
365 * return 1 if valid rtt 0 otherwise
366 */
367static int
368tcp_update_rtt (tcp_connection_t * tc, u32 ack)
369{
370 u32 mrtt = 0;
371
372 /* Karn's rule, part 1. Don't use retransmitted segments to estimate
373 * RTT because they're ambiguous. */
374 if (tc->rtt_seq && seq_gt (ack, tc->rtt_seq) && !tc->rto_boff)
375 {
376 mrtt = tcp_time_now () - tc->rtt_ts;
Dave Barach68b0fb02017-02-28 15:15:56 -0500377 }
378
379 /* As per RFC7323 TSecr can be used for RTTM only if the segment advances
380 * snd_una, i.e., the left side of the send window:
381 * seq_lt (tc->snd_una, ack). Note: last condition could be dropped, we don't
382 * try to update rtt for dupacks */
383 else if (tcp_opts_tstamp (&tc->opt) && tc->opt.tsecr && tc->bytes_acked)
384 {
385 mrtt = tcp_time_now () - tc->opt.tsecr;
386 }
387
388 /* Ignore dubious measurements */
389 if (mrtt == 0 || mrtt > TCP_RTT_MAX)
390 return 0;
391
392 tcp_estimate_rtt (tc, mrtt);
393
394 tc->rto = clib_min (tc->srtt + (tc->rttvar << 2), TCP_RTO_MAX);
395
Florin Coras3e350af2017-03-30 02:54:28 -0700396 /* Allow measuring of RTT and make sure boff is 0 */
397 tc->rtt_seq = 0;
398 tc->rto_boff = 0;
399
Dave Barach68b0fb02017-02-28 15:15:56 -0500400 return 1;
401}
402
403/**
404 * Dequeue bytes that have been acked and while at it update RTT estimates.
405 */
406static void
407tcp_dequeue_acked (tcp_connection_t * tc, u32 ack)
408{
409 /* Dequeue the newly ACKed bytes */
410 stream_session_dequeue_drop (&tc->connection, tc->bytes_acked);
411
412 /* Update rtt and rto */
Florin Coras3e350af2017-03-30 02:54:28 -0700413 tcp_update_rtt (tc, ack);
Dave Barach68b0fb02017-02-28 15:15:56 -0500414}
415
Florin Coras6792ec02017-03-13 03:49:51 -0700416/**
417 * Check if dupack as per RFC5681 Sec. 2
418 *
419 * This works only if called before updating snd_wnd.
420 * */
Dave Barach68b0fb02017-02-28 15:15:56 -0500421always_inline u8
422tcp_ack_is_dupack (tcp_connection_t * tc, vlib_buffer_t * b, u32 new_snd_wnd)
423{
424 return ((vnet_buffer (b)->tcp.ack_number == tc->snd_una)
425 && seq_gt (tc->snd_una_max, tc->snd_una)
426 && (vnet_buffer (b)->tcp.seq_end == vnet_buffer (b)->tcp.seq_number)
427 && (new_snd_wnd == tc->snd_wnd));
428}
429
430void
431scoreboard_remove_hole (sack_scoreboard_t * sb, sack_scoreboard_hole_t * hole)
432{
433 sack_scoreboard_hole_t *next, *prev;
434
435 if (hole->next != TCP_INVALID_SACK_HOLE_INDEX)
436 {
437 next = pool_elt_at_index (sb->holes, hole->next);
438 next->prev = hole->prev;
439 }
440
441 if (hole->prev != TCP_INVALID_SACK_HOLE_INDEX)
442 {
443 prev = pool_elt_at_index (sb->holes, hole->prev);
444 prev->next = hole->next;
445 }
446 else
447 {
448 sb->head = hole->next;
449 }
450
451 pool_put (sb->holes, hole);
452}
453
454sack_scoreboard_hole_t *
Florin Coras6792ec02017-03-13 03:49:51 -0700455scoreboard_insert_hole (sack_scoreboard_t * sb, u32 prev_index,
Dave Barach68b0fb02017-02-28 15:15:56 -0500456 u32 start, u32 end)
457{
Florin Coras6792ec02017-03-13 03:49:51 -0700458 sack_scoreboard_hole_t *hole, *next, *prev;
Dave Barach68b0fb02017-02-28 15:15:56 -0500459 u32 hole_index;
460
461 pool_get (sb->holes, hole);
462 memset (hole, 0, sizeof (*hole));
463
464 hole->start = start;
465 hole->end = end;
466 hole_index = hole - sb->holes;
467
Florin Coras6792ec02017-03-13 03:49:51 -0700468 prev = scoreboard_get_hole (sb, prev_index);
Dave Barach68b0fb02017-02-28 15:15:56 -0500469 if (prev)
470 {
471 hole->prev = prev - sb->holes;
472 hole->next = prev->next;
473
474 if ((next = scoreboard_next_hole (sb, hole)))
475 next->prev = hole_index;
476
477 prev->next = hole_index;
478 }
479 else
480 {
481 sb->head = hole_index;
482 hole->prev = TCP_INVALID_SACK_HOLE_INDEX;
483 hole->next = TCP_INVALID_SACK_HOLE_INDEX;
484 }
485
486 return hole;
487}
488
Florin Coras6792ec02017-03-13 03:49:51 -0700489void
Dave Barach68b0fb02017-02-28 15:15:56 -0500490tcp_rcv_sacks (tcp_connection_t * tc, u32 ack)
491{
492 sack_scoreboard_t *sb = &tc->sack_sb;
493 sack_block_t *blk, tmp;
Florin Coras6792ec02017-03-13 03:49:51 -0700494 sack_scoreboard_hole_t *hole, *next_hole, *last_hole, *new_hole;
495 u32 blk_index = 0, old_sacked_bytes, hole_index;
Dave Barach68b0fb02017-02-28 15:15:56 -0500496 int i, j;
497
Florin Coras6792ec02017-03-13 03:49:51 -0700498 sb->last_sacked_bytes = 0;
499 sb->snd_una_adv = 0;
500 old_sacked_bytes = sb->sacked_bytes;
501
502 if (!tcp_opts_sack (&tc->opt) && sb->head == TCP_INVALID_SACK_HOLE_INDEX)
Dave Barach68b0fb02017-02-28 15:15:56 -0500503 return;
504
505 /* Remove invalid blocks */
Florin Coras6792ec02017-03-13 03:49:51 -0700506 blk = tc->opt.sacks;
507 while (blk < vec_end (tc->opt.sacks))
508 {
509 if (seq_lt (blk->start, blk->end)
510 && seq_gt (blk->start, tc->snd_una)
511 && seq_gt (blk->start, ack) && seq_leq (blk->end, tc->snd_nxt))
512 {
513 blk++;
514 continue;
515 }
516 vec_del1 (tc->opt.sacks, blk - tc->opt.sacks);
517 }
Dave Barach68b0fb02017-02-28 15:15:56 -0500518
519 /* Add block for cumulative ack */
520 if (seq_gt (ack, tc->snd_una))
521 {
522 tmp.start = tc->snd_una;
523 tmp.end = ack;
524 vec_add1 (tc->opt.sacks, tmp);
525 }
526
527 if (vec_len (tc->opt.sacks) == 0)
528 return;
529
530 /* Make sure blocks are ordered */
531 for (i = 0; i < vec_len (tc->opt.sacks); i++)
Florin Coras6792ec02017-03-13 03:49:51 -0700532 for (j = i + 1; j < vec_len (tc->opt.sacks); j++)
Dave Barach68b0fb02017-02-28 15:15:56 -0500533 if (seq_lt (tc->opt.sacks[j].start, tc->opt.sacks[i].start))
534 {
535 tmp = tc->opt.sacks[i];
536 tc->opt.sacks[i] = tc->opt.sacks[j];
537 tc->opt.sacks[j] = tmp;
538 }
539
Dave Barach68b0fb02017-02-28 15:15:56 -0500540 if (sb->head == TCP_INVALID_SACK_HOLE_INDEX)
541 {
Florin Coras6792ec02017-03-13 03:49:51 -0700542 /* If no holes, insert the first that covers all outstanding bytes */
543 last_hole = scoreboard_insert_hole (sb, TCP_INVALID_SACK_HOLE_INDEX,
544 tc->snd_una, tc->snd_una_max);
545 sb->tail = scoreboard_hole_index (sb, last_hole);
546 }
547 else
548 {
549 /* If we have holes but snd_una_max is beyond the last hole, update
550 * last hole end */
551 tmp = tc->opt.sacks[vec_len (tc->opt.sacks) - 1];
552 last_hole = scoreboard_last_hole (sb);
553 if (seq_gt (tc->snd_una_max, sb->max_byte_sacked)
554 && seq_gt (tc->snd_una_max, last_hole->end))
555 last_hole->end = tc->snd_una_max;
Dave Barach68b0fb02017-02-28 15:15:56 -0500556 }
557
558 /* Walk the holes with the SACK blocks */
559 hole = pool_elt_at_index (sb->holes, sb->head);
560 while (hole && blk_index < vec_len (tc->opt.sacks))
561 {
562 blk = &tc->opt.sacks[blk_index];
563
564 if (seq_leq (blk->start, hole->start))
565 {
566 /* Block covers hole. Remove hole */
567 if (seq_geq (blk->end, hole->end))
568 {
569 next_hole = scoreboard_next_hole (sb, hole);
570
571 /* Byte accounting */
Florin Coras6792ec02017-03-13 03:49:51 -0700572 if (seq_leq (hole->end, ack))
Dave Barach68b0fb02017-02-28 15:15:56 -0500573 {
Florin Coras6792ec02017-03-13 03:49:51 -0700574 /* Bytes lost because snd_wnd left edge advances */
575 if (next_hole && seq_leq (next_hole->start, ack))
Dave Barach68b0fb02017-02-28 15:15:56 -0500576 sb->sacked_bytes -= next_hole->start - hole->end;
577 else
578 sb->sacked_bytes -= ack - hole->end;
579 }
580 else
581 {
582 sb->sacked_bytes += scoreboard_hole_bytes (hole);
583 }
584
Florin Coras6792ec02017-03-13 03:49:51 -0700585 /* snd_una needs to be advanced */
586 if (seq_geq (ack, hole->end))
587 {
588 if (next_hole && seq_lt (ack, next_hole->start))
589 sb->snd_una_adv = next_hole->start - ack;
590 else
591 sb->snd_una_adv = sb->max_byte_sacked - ack;
592
593 /* all these can be delivered */
594 sb->sacked_bytes -= sb->snd_una_adv;
595 }
596
597 /* About to remove last hole */
598 if (hole == last_hole)
599 {
600 sb->tail = hole->prev;
601 last_hole = scoreboard_last_hole (sb);
602 /* keep track of max byte sacked in case the last hole
603 * is acked */
604 if (seq_gt (hole->end, sb->max_byte_sacked))
605 sb->max_byte_sacked = hole->end;
606 }
Dave Barach68b0fb02017-02-28 15:15:56 -0500607 scoreboard_remove_hole (sb, hole);
608 hole = next_hole;
609 }
Florin Coras6792ec02017-03-13 03:49:51 -0700610 /* Partial 'head' overlap */
Dave Barach68b0fb02017-02-28 15:15:56 -0500611 else
612 {
Florin Coras6792ec02017-03-13 03:49:51 -0700613 if (seq_gt (blk->end, hole->start))
614 {
615 sb->sacked_bytes += blk->end - hole->start;
616 hole->start = blk->end;
617 }
Dave Barach68b0fb02017-02-28 15:15:56 -0500618 blk_index++;
619 }
620 }
621 else
622 {
623 /* Hole must be split */
Florin Coras6792ec02017-03-13 03:49:51 -0700624 if (seq_lt (blk->end, hole->end))
Dave Barach68b0fb02017-02-28 15:15:56 -0500625 {
626 sb->sacked_bytes += blk->end - blk->start;
Florin Coras6792ec02017-03-13 03:49:51 -0700627 hole_index = scoreboard_hole_index (sb, hole);
628 new_hole = scoreboard_insert_hole (sb, hole_index, blk->end,
629 hole->end);
630
631 /* Pool might've moved */
632 hole = scoreboard_get_hole (sb, hole_index);
633 hole->end = blk->start;
634
635 /* New or split of tail */
636 if ((last_hole->end == new_hole->end)
637 || seq_lt (last_hole->end, new_hole->start))
638 {
639 last_hole = new_hole;
640 sb->tail = scoreboard_hole_index (sb, new_hole);
641 }
642
Dave Barach68b0fb02017-02-28 15:15:56 -0500643 blk_index++;
Florin Coras6792ec02017-03-13 03:49:51 -0700644 hole = scoreboard_next_hole (sb, hole);
Dave Barach68b0fb02017-02-28 15:15:56 -0500645 }
646 else
647 {
Florin Coras6792ec02017-03-13 03:49:51 -0700648 sb->sacked_bytes += hole->end - blk->start;
649 hole->end = blk->start;
Dave Barach68b0fb02017-02-28 15:15:56 -0500650 hole = scoreboard_next_hole (sb, hole);
651 }
652 }
653 }
Florin Coras6792ec02017-03-13 03:49:51 -0700654
655 sb->last_sacked_bytes = sb->sacked_bytes + sb->snd_una_adv
656 - old_sacked_bytes;
Dave Barach68b0fb02017-02-28 15:15:56 -0500657}
658
659/** Update snd_wnd
660 *
661 * If (SND.WL1 < SEG.SEQ or (SND.WL1 = SEG.SEQ and SND.WL2 =< SEG.ACK)), set
662 * SND.WND <- SEG.WND, set SND.WL1 <- SEG.SEQ, and set SND.WL2 <- SEG.ACK */
663static void
664tcp_update_snd_wnd (tcp_connection_t * tc, u32 seq, u32 ack, u32 snd_wnd)
665{
Florin Coras6792ec02017-03-13 03:49:51 -0700666 if (seq_lt (tc->snd_wl1, seq)
667 || (tc->snd_wl1 == seq && seq_leq (tc->snd_wl2, ack)))
Dave Barach68b0fb02017-02-28 15:15:56 -0500668 {
669 tc->snd_wnd = snd_wnd;
670 tc->snd_wl1 = seq;
671 tc->snd_wl2 = ack;
Florin Coras6792ec02017-03-13 03:49:51 -0700672 TCP_EVT_DBG (TCP_EVT_SND_WND, tc);
Florin Coras3e350af2017-03-30 02:54:28 -0700673
674 /* Set probe timer if we just got 0 wnd */
675 if (tc->snd_wnd < tc->snd_mss
676 && !tcp_timer_is_active (tc, TCP_TIMER_PERSIST))
677 tcp_persist_timer_set (tc);
678 else
679 tcp_persist_timer_reset (tc);
Dave Barach68b0fb02017-02-28 15:15:56 -0500680 }
681}
682
Florin Coras6792ec02017-03-13 03:49:51 -0700683void
Dave Barach68b0fb02017-02-28 15:15:56 -0500684tcp_cc_congestion (tcp_connection_t * tc)
685{
Florin Coras6792ec02017-03-13 03:49:51 -0700686 tc->snd_congestion = tc->snd_nxt;
Dave Barach68b0fb02017-02-28 15:15:56 -0500687 tc->cc_algo->congestion (tc);
Florin Coras6792ec02017-03-13 03:49:51 -0700688 TCP_EVT_DBG (TCP_EVT_CC_EVT, tc, 4);
Dave Barach68b0fb02017-02-28 15:15:56 -0500689}
690
Florin Coras6792ec02017-03-13 03:49:51 -0700691void
Dave Barach68b0fb02017-02-28 15:15:56 -0500692tcp_cc_recover (tcp_connection_t * tc)
693{
Florin Coras3e350af2017-03-30 02:54:28 -0700694 /* TODO: check if time to recover was small. It might be that RTO popped
695 * too soon.
696 */
697
Florin Coras6792ec02017-03-13 03:49:51 -0700698 tc->cc_algo->recovered (tc);
699
700 tc->rtx_bytes = 0;
701 tc->rcv_dupacks = 0;
702 tc->snd_nxt = tc->snd_una;
703
704 tc->cc_algo->rcv_ack (tc);
705 tc->tsecr_last_ack = tc->opt.tsecr;
706
Florin Coras3e350af2017-03-30 02:54:28 -0700707 tcp_cong_recovery_off (tc);
Florin Coras6792ec02017-03-13 03:49:51 -0700708
709 TCP_EVT_DBG (TCP_EVT_CC_EVT, tc, 3);
Dave Barach68b0fb02017-02-28 15:15:56 -0500710}
711
712static void
Florin Coras6792ec02017-03-13 03:49:51 -0700713tcp_cc_rcv_ack (tcp_connection_t * tc, vlib_buffer_t * b)
Dave Barach68b0fb02017-02-28 15:15:56 -0500714{
715 u8 partial_ack;
716
Florin Coras3e350af2017-03-30 02:54:28 -0700717 if (tcp_in_cong_recovery (tc))
Dave Barach68b0fb02017-02-28 15:15:56 -0500718 {
Florin Coras6792ec02017-03-13 03:49:51 -0700719 partial_ack = seq_lt (tc->snd_una, tc->snd_congestion);
Dave Barach68b0fb02017-02-28 15:15:56 -0500720 if (!partial_ack)
721 {
722 /* Clear retransmitted bytes. */
Dave Barach68b0fb02017-02-28 15:15:56 -0500723 tcp_cc_recover (tc);
724 }
725 else
726 {
Florin Coras6792ec02017-03-13 03:49:51 -0700727 TCP_EVT_DBG (TCP_EVT_CC_PACK, tc);
728
Dave Barach68b0fb02017-02-28 15:15:56 -0500729 /* Clear retransmitted bytes. XXX should we clear all? */
730 tc->rtx_bytes = 0;
731 tc->cc_algo->rcv_cong_ack (tc, TCP_CC_PARTIALACK);
732
Florin Coras6792ec02017-03-13 03:49:51 -0700733 /* In case snd_nxt is still in the past and output tries to
734 * shove some new bytes */
Florin Coras3e350af2017-03-30 02:54:28 -0700735 tc->snd_nxt = tc->snd_una_max;
Florin Coras6792ec02017-03-13 03:49:51 -0700736
737 /* XXX need proper RFC6675 support */
Florin Coras3e350af2017-03-30 02:54:28 -0700738 if (tc->sack_sb.last_sacked_bytes && !tcp_in_recovery (tc))
Florin Coras6792ec02017-03-13 03:49:51 -0700739 {
740 tcp_fast_retransmit (tc);
741 }
742 else
743 {
744 /* Retransmit first unacked segment */
745 tcp_retransmit_first_unacked (tc);
Florin Coras6792ec02017-03-13 03:49:51 -0700746 }
Dave Barach68b0fb02017-02-28 15:15:56 -0500747 }
748 }
749 else
750 {
751 tc->cc_algo->rcv_ack (tc);
Florin Coras6792ec02017-03-13 03:49:51 -0700752 tc->tsecr_last_ack = tc->opt.tsecr;
753 tc->rcv_dupacks = 0;
Dave Barach68b0fb02017-02-28 15:15:56 -0500754 }
Dave Barach68b0fb02017-02-28 15:15:56 -0500755}
756
757static void
758tcp_cc_rcv_dupack (tcp_connection_t * tc, u32 ack)
759{
Florin Coras6792ec02017-03-13 03:49:51 -0700760// ASSERT (seq_geq(tc->snd_una, ack));
Dave Barach68b0fb02017-02-28 15:15:56 -0500761
762 tc->rcv_dupacks++;
763 if (tc->rcv_dupacks == TCP_DUPACK_THRESHOLD)
764 {
765 /* RFC6582 NewReno heuristic to avoid multiple fast retransmits */
766 if (tc->opt.tsecr != tc->tsecr_last_ack)
767 {
768 tc->rcv_dupacks = 0;
769 return;
770 }
771
772 tcp_fastrecovery_on (tc);
773
774 /* Handle congestion and dupack */
775 tcp_cc_congestion (tc);
776 tc->cc_algo->rcv_cong_ack (tc, TCP_CC_DUPACK);
777
778 tcp_fast_retransmit (tc);
779
780 /* Post retransmit update cwnd to ssthresh and account for the
781 * three segments that have left the network and should've been
782 * buffered at the receiver */
783 tc->cwnd = tc->ssthresh + TCP_DUPACK_THRESHOLD * tc->snd_mss;
784 }
785 else if (tc->rcv_dupacks > TCP_DUPACK_THRESHOLD)
786 {
787 ASSERT (tcp_in_fastrecovery (tc));
788
789 tc->cc_algo->rcv_cong_ack (tc, TCP_CC_DUPACK);
790 }
791}
792
793void
794tcp_cc_init (tcp_connection_t * tc)
795{
796 tc->cc_algo = tcp_cc_algo_get (TCP_CC_NEWRENO);
797 tc->cc_algo->init (tc);
798}
799
800static int
801tcp_rcv_ack (tcp_connection_t * tc, vlib_buffer_t * b,
802 tcp_header_t * th, u32 * next, u32 * error)
803{
804 u32 new_snd_wnd;
805
Florin Coras6792ec02017-03-13 03:49:51 -0700806 /* If the ACK acks something not yet sent (SEG.ACK > SND.NXT) */
Dave Barach68b0fb02017-02-28 15:15:56 -0500807 if (seq_gt (vnet_buffer (b)->tcp.ack_number, tc->snd_nxt))
808 {
Florin Coras6792ec02017-03-13 03:49:51 -0700809 /* If we have outstanding data and this is within the window, accept it,
810 * probably retransmit has timed out. Otherwise ACK segment and then
811 * drop it */
812 if (seq_gt (vnet_buffer (b)->tcp.ack_number, tc->snd_una_max))
813 {
814 tcp_make_ack (tc, b);
815 *next = tcp_next_output (tc->c_is_ip4);
816 *error = TCP_ERROR_ACK_INVALID;
817 TCP_EVT_DBG (TCP_EVT_ACK_RCV_ERR, tc, 0,
818 vnet_buffer (b)->tcp.ack_number);
819 return -1;
820 }
821
Florin Coras6792ec02017-03-13 03:49:51 -0700822 TCP_EVT_DBG (TCP_EVT_ACK_RCV_ERR, tc, 2,
823 vnet_buffer (b)->tcp.ack_number);
Florin Coras3e350af2017-03-30 02:54:28 -0700824
825 tc->snd_nxt = vnet_buffer (b)->tcp.ack_number;
826 *error = TCP_ERROR_ACK_FUTURE;
Dave Barach68b0fb02017-02-28 15:15:56 -0500827 }
828
Florin Coras6792ec02017-03-13 03:49:51 -0700829 /* If old ACK, probably it's an old dupack */
Dave Barach68b0fb02017-02-28 15:15:56 -0500830 if (seq_lt (vnet_buffer (b)->tcp.ack_number, tc->snd_una))
831 {
832 *error = TCP_ERROR_ACK_OLD;
Florin Coras6792ec02017-03-13 03:49:51 -0700833 TCP_EVT_DBG (TCP_EVT_ACK_RCV_ERR, tc, 1,
834 vnet_buffer (b)->tcp.ack_number);
835 if (tcp_in_fastrecovery (tc) && tc->rcv_dupacks == TCP_DUPACK_THRESHOLD)
836 {
837 TCP_EVT_DBG (TCP_EVT_DUPACK_RCVD, tc);
838 tcp_cc_rcv_dupack (tc, vnet_buffer (b)->tcp.ack_number);
839 }
Dave Barach68b0fb02017-02-28 15:15:56 -0500840 return -1;
841 }
842
843 if (tcp_opts_sack_permitted (&tc->opt))
844 tcp_rcv_sacks (tc, vnet_buffer (b)->tcp.ack_number);
845
Florin Corase04c2992017-03-01 08:17:34 -0800846 new_snd_wnd = clib_net_to_host_u16 (th->window) << tc->snd_wscale;
Dave Barach68b0fb02017-02-28 15:15:56 -0500847
848 if (tcp_ack_is_dupack (tc, b, new_snd_wnd))
849 {
Florin Coras6792ec02017-03-13 03:49:51 -0700850 TCP_EVT_DBG (TCP_EVT_DUPACK_RCVD, tc, 1);
Dave Barach68b0fb02017-02-28 15:15:56 -0500851 tcp_cc_rcv_dupack (tc, vnet_buffer (b)->tcp.ack_number);
852 *error = TCP_ERROR_ACK_DUP;
853 return -1;
854 }
855
Florin Coras6792ec02017-03-13 03:49:51 -0700856 /*
857 * Valid ACK
858 */
859
Dave Barach68b0fb02017-02-28 15:15:56 -0500860 tc->bytes_acked = vnet_buffer (b)->tcp.ack_number - tc->snd_una;
Florin Coras6792ec02017-03-13 03:49:51 -0700861 tc->snd_una = vnet_buffer (b)->tcp.ack_number + tc->sack_sb.snd_una_adv;
Dave Barach68b0fb02017-02-28 15:15:56 -0500862
Florin Coras6792ec02017-03-13 03:49:51 -0700863 /* Dequeue ACKed data and update RTT */
Dave Barach68b0fb02017-02-28 15:15:56 -0500864 tcp_dequeue_acked (tc, vnet_buffer (b)->tcp.ack_number);
Dave Barach68b0fb02017-02-28 15:15:56 -0500865 tcp_update_snd_wnd (tc, vnet_buffer (b)->tcp.seq_number,
866 vnet_buffer (b)->tcp.ack_number, new_snd_wnd);
867
Florin Coras6792ec02017-03-13 03:49:51 -0700868 /* If some of our sent bytes have been acked, update cc and retransmit
869 * timer. */
870 if (tc->bytes_acked)
871 {
Florin Coras3e350af2017-03-30 02:54:28 -0700872 TCP_EVT_DBG (TCP_EVT_ACK_RCVD, tc);
Dave Barach68b0fb02017-02-28 15:15:56 -0500873
Florin Coras6792ec02017-03-13 03:49:51 -0700874 /* Updates congestion control (slow start/congestion avoidance) */
875 tcp_cc_rcv_ack (tc, b);
Florin Corase69f4952017-03-07 10:06:24 -0800876
Florin Coras6792ec02017-03-13 03:49:51 -0700877 /* If everything has been acked, stop retransmit timer
878 * otherwise update */
879 if (tc->snd_una == tc->snd_una_max)
880 tcp_retransmit_timer_reset (tc);
881 else
882 tcp_retransmit_timer_update (tc);
883 }
Dave Barach68b0fb02017-02-28 15:15:56 -0500884
885 return 0;
886}
887
888/**
889 * Build SACK list as per RFC2018.
890 *
891 * Makes sure the first block contains the segment that generated the current
892 * ACK and the following ones are the ones most recently reported in SACK
893 * blocks.
894 *
895 * @param tc TCP connection for which the SACK list is updated
896 * @param start Start sequence number of the newest SACK block
897 * @param end End sequence of the newest SACK block
898 */
899static void
900tcp_update_sack_list (tcp_connection_t * tc, u32 start, u32 end)
901{
902 sack_block_t *new_list = 0, block;
Dave Barach68b0fb02017-02-28 15:15:56 -0500903 int i;
Dave Barach68b0fb02017-02-28 15:15:56 -0500904
905 /* If the first segment is ooo add it to the list. Last write might've moved
906 * rcv_nxt over the first segment. */
907 if (seq_lt (tc->rcv_nxt, start))
908 {
909 block.start = start;
910 block.end = end;
911 vec_add1 (new_list, block);
Dave Barach68b0fb02017-02-28 15:15:56 -0500912 }
913
914 /* Find the blocks still worth keeping. */
915 for (i = 0; i < vec_len (tc->snd_sacks); i++)
916 {
917 /* Discard if:
918 * 1) rcv_nxt advanced beyond current block OR
919 * 2) Segment overlapped by the first segment, i.e., it has been merged
920 * into it.*/
921 if (seq_leq (tc->snd_sacks[i].start, tc->rcv_nxt)
922 || seq_leq (tc->snd_sacks[i].start, end))
923 continue;
924
Florin Coras6792ec02017-03-13 03:49:51 -0700925 /* Save to new SACK list. */
926 vec_add1 (new_list, tc->snd_sacks[i]);
Dave Barach68b0fb02017-02-28 15:15:56 -0500927 }
928
Florin Coras6792ec02017-03-13 03:49:51 -0700929 ASSERT (vec_len (new_list) < TCP_MAX_SACK_BLOCKS);
930
Dave Barach68b0fb02017-02-28 15:15:56 -0500931 /* Replace old vector with new one */
932 vec_free (tc->snd_sacks);
933 tc->snd_sacks = new_list;
934}
935
936/** Enqueue data for delivery to application */
Florin Coras6792ec02017-03-13 03:49:51 -0700937always_inline int
Dave Barach68b0fb02017-02-28 15:15:56 -0500938tcp_session_enqueue_data (tcp_connection_t * tc, vlib_buffer_t * b,
939 u16 data_len)
940{
941 int written;
942
943 /* Pure ACK. Update rcv_nxt and be done. */
944 if (PREDICT_FALSE (data_len == 0))
945 {
946 tc->rcv_nxt = vnet_buffer (b)->tcp.seq_end;
947 return TCP_ERROR_PURE_ACK;
948 }
949
950 written = stream_session_enqueue_data (&tc->connection,
951 vlib_buffer_get_current (b),
952 data_len, 1 /* queue event */ );
953
Florin Coras6792ec02017-03-13 03:49:51 -0700954 TCP_EVT_DBG (TCP_EVT_INPUT, tc, 0, data_len, written);
955
Dave Barach68b0fb02017-02-28 15:15:56 -0500956 /* Update rcv_nxt */
957 if (PREDICT_TRUE (written == data_len))
958 {
959 tc->rcv_nxt = vnet_buffer (b)->tcp.seq_end;
960 }
961 /* If more data written than expected, account for out-of-order bytes. */
962 else if (written > data_len)
963 {
964 tc->rcv_nxt = vnet_buffer (b)->tcp.seq_end + written - data_len;
965
966 /* Send ACK confirming the update */
967 tc->flags |= TCP_CONN_SNDACK;
Florin Coras6792ec02017-03-13 03:49:51 -0700968 }
969 else if (written > 0)
970 {
971 /* We've written something but FIFO is probably full now */
972 tc->rcv_nxt += written;
Dave Barach68b0fb02017-02-28 15:15:56 -0500973
Florin Coras6792ec02017-03-13 03:49:51 -0700974 /* Depending on how fast the app is, all remaining buffers in burst will
Florin Coras3e350af2017-03-30 02:54:28 -0700975 * not be enqueued. Inform peer */
976 tc->flags |= TCP_CONN_SNDACK;
977
Florin Coras6792ec02017-03-13 03:49:51 -0700978 return TCP_ERROR_PARTIALLY_ENQUEUED;
Dave Barach68b0fb02017-02-28 15:15:56 -0500979 }
980 else
981 {
Florin Coras3e350af2017-03-30 02:54:28 -0700982 tc->flags |= TCP_CONN_SNDACK;
Dave Barach68b0fb02017-02-28 15:15:56 -0500983 return TCP_ERROR_FIFO_FULL;
984 }
985
Florin Coras6792ec02017-03-13 03:49:51 -0700986 /* Update SACK list if need be */
987 if (tcp_opts_sack_permitted (&tc->opt))
988 {
989 /* Remove SACK blocks that have been delivered */
990 tcp_update_sack_list (tc, tc->rcv_nxt, tc->rcv_nxt);
991 }
992
Dave Barach68b0fb02017-02-28 15:15:56 -0500993 return TCP_ERROR_ENQUEUED;
994}
995
996/** Enqueue out-of-order data */
Florin Coras6792ec02017-03-13 03:49:51 -0700997always_inline int
Dave Barach68b0fb02017-02-28 15:15:56 -0500998tcp_session_enqueue_ooo (tcp_connection_t * tc, vlib_buffer_t * b,
999 u16 data_len)
1000{
1001 stream_session_t *s0;
1002 u32 offset, seq;
Florin Coras6792ec02017-03-13 03:49:51 -07001003 int rv;
1004
1005 /* Pure ACK. Do nothing */
1006 if (PREDICT_FALSE (data_len == 0))
1007 {
1008 return TCP_ERROR_PURE_ACK;
1009 }
Dave Barach68b0fb02017-02-28 15:15:56 -05001010
1011 s0 = stream_session_get (tc->c_s_index, tc->c_thread_index);
1012 seq = vnet_buffer (b)->tcp.seq_number;
1013 offset = seq - tc->rcv_nxt;
1014
Florin Coras6792ec02017-03-13 03:49:51 -07001015 rv = svm_fifo_enqueue_with_offset (s0->server_rx_fifo, s0->pid, offset,
1016 data_len, vlib_buffer_get_current (b));
1017
1018 /* Nothing written */
1019 if (rv)
1020 {
1021 TCP_EVT_DBG (TCP_EVT_INPUT, tc, 1, data_len, 0);
1022 return TCP_ERROR_FIFO_FULL;
1023 }
1024
1025 TCP_EVT_DBG (TCP_EVT_INPUT, tc, 1, data_len, data_len);
Dave Barach68b0fb02017-02-28 15:15:56 -05001026
1027 /* Update SACK list if in use */
1028 if (tcp_opts_sack_permitted (&tc->opt))
1029 {
1030 ooo_segment_t *newest;
1031 u32 start, end;
1032
1033 /* Get the newest segment from the fifo */
1034 newest = svm_fifo_newest_ooo_segment (s0->server_rx_fifo);
1035 start = tc->rcv_nxt + ooo_segment_offset (s0->server_rx_fifo, newest);
1036 end = tc->rcv_nxt + ooo_segment_end_offset (s0->server_rx_fifo, newest);
1037
1038 tcp_update_sack_list (tc, start, end);
1039 }
1040
1041 return TCP_ERROR_ENQUEUED;
1042}
1043
1044/**
Florin Coras6792ec02017-03-13 03:49:51 -07001045 * Check if ACK could be delayed. If ack can be delayed, it should return
1046 * true for a full frame. If we're always acking return 0.
Dave Barach68b0fb02017-02-28 15:15:56 -05001047 */
1048always_inline int
1049tcp_can_delack (tcp_connection_t * tc)
1050{
Florin Coras6792ec02017-03-13 03:49:51 -07001051 /* Send ack if ... */
1052 if (TCP_ALWAYS_ACK
1053 /* just sent a rcv wnd 0 */
1054 || (tc->flags & TCP_CONN_SENT_RCV_WND0) != 0
1055 /* constrained to send ack */
1056 || (tc->flags & TCP_CONN_SNDACK) != 0
1057 /* we're almost out of tx wnd */
1058 || tcp_available_snd_space (tc) < 2 * tc->snd_mss)
1059 return 0;
Dave Barach68b0fb02017-02-28 15:15:56 -05001060
Florin Coras6792ec02017-03-13 03:49:51 -07001061 return 1;
Dave Barach68b0fb02017-02-28 15:15:56 -05001062}
1063
1064static int
1065tcp_segment_rcv (tcp_main_t * tm, tcp_connection_t * tc, vlib_buffer_t * b,
1066 u16 n_data_bytes, u32 * next0)
1067{
1068 u32 error = 0;
1069
1070 /* Handle out-of-order data */
1071 if (PREDICT_FALSE (vnet_buffer (b)->tcp.seq_number != tc->rcv_nxt))
1072 {
Florin Coras6792ec02017-03-13 03:49:51 -07001073 /* Old sequence numbers allowed through because they overlapped
1074 * the rx window */
1075 if (seq_lt (vnet_buffer (b)->tcp.seq_number, tc->rcv_nxt))
Dave Barach68b0fb02017-02-28 15:15:56 -05001076 {
Florin Coras6792ec02017-03-13 03:49:51 -07001077 error = TCP_ERROR_SEGMENT_OLD;
1078 *next0 = TCP_NEXT_DROP;
1079 goto done;
Dave Barach68b0fb02017-02-28 15:15:56 -05001080 }
1081
Florin Coras6792ec02017-03-13 03:49:51 -07001082 error = tcp_session_enqueue_ooo (tc, b, n_data_bytes);
1083
1084 /* N.B. Should not filter burst of dupacks. Two issues 1) dupacks open
1085 * cwnd on remote peer when congested 2) acks leaving should have the
1086 * latest rcv_wnd since the burst may eaten up all of it, so only the
1087 * old ones could be filtered.
1088 */
1089
1090 /* RFC2581: Send DUPACK for fast retransmit */
1091 tcp_make_ack (tc, b);
1092 *next0 = tcp_next_output (tc->c_is_ip4);
1093
1094 /* Mark as DUPACK. We may filter these in output if
1095 * the burst fills the holes. */
1096 if (n_data_bytes)
1097 vnet_buffer (b)->tcp.flags = TCP_BUF_FLAG_DUPACK;
1098
1099 TCP_EVT_DBG (TCP_EVT_DUPACK_SENT, tc);
Dave Barach68b0fb02017-02-28 15:15:56 -05001100 goto done;
1101 }
1102
1103 /* In order data, enqueue. Fifo figures out by itself if any out-of-order
1104 * segments can be enqueued after fifo tail offset changes. */
1105 error = tcp_session_enqueue_data (tc, b, n_data_bytes);
1106
Florin Coras6792ec02017-03-13 03:49:51 -07001107 if (n_data_bytes == 0)
1108 {
1109 *next0 = TCP_NEXT_DROP;
1110 goto done;
1111 }
1112
Dave Barach68b0fb02017-02-28 15:15:56 -05001113 /* Check if ACK can be delayed */
Florin Coras3e350af2017-03-30 02:54:28 -07001114 if (tcp_can_delack (tc))
Dave Barach68b0fb02017-02-28 15:15:56 -05001115 {
Florin Coras6792ec02017-03-13 03:49:51 -07001116 if (!tcp_timer_is_active (tc, TCP_TIMER_DELACK))
1117 tcp_timer_set (tc, TCP_TIMER_DELACK, TCP_DELACK_TIME);
Florin Coras3e350af2017-03-30 02:54:28 -07001118 goto done;
Dave Barach68b0fb02017-02-28 15:15:56 -05001119 }
1120
Florin Coras3e350af2017-03-30 02:54:28 -07001121 *next0 = tcp_next_output (tc->c_is_ip4);
1122 tcp_make_ack (tc, b);
1123
Dave Barach68b0fb02017-02-28 15:15:56 -05001124done:
1125 return error;
1126}
1127
Florin Coras6792ec02017-03-13 03:49:51 -07001128always_inline void
1129tcp_established_inc_counter (vlib_main_t * vm, u8 is_ip4, u8 evt, u8 val)
Dave Barach68b0fb02017-02-28 15:15:56 -05001130{
Florin Coras6792ec02017-03-13 03:49:51 -07001131 if (PREDICT_TRUE (!val))
1132 return;
Dave Barach68b0fb02017-02-28 15:15:56 -05001133
Florin Coras6792ec02017-03-13 03:49:51 -07001134 if (is_ip4)
1135 vlib_node_increment_counter (vm, tcp4_established_node.index, evt, val);
1136 else
1137 vlib_node_increment_counter (vm, tcp6_established_node.index, evt, val);
Dave Barach68b0fb02017-02-28 15:15:56 -05001138}
1139
1140always_inline uword
1141tcp46_established_inline (vlib_main_t * vm, vlib_node_runtime_t * node,
1142 vlib_frame_t * from_frame, int is_ip4)
1143{
1144 u32 n_left_from, next_index, *from, *to_next;
Damjan Marion586afd72017-04-05 19:18:20 +02001145 u32 my_thread_index = vm->thread_index, errors = 0;
Dave Barach68b0fb02017-02-28 15:15:56 -05001146 tcp_main_t *tm = vnet_get_tcp_main ();
1147
1148 from = vlib_frame_vector_args (from_frame);
1149 n_left_from = from_frame->n_vectors;
1150
1151 next_index = node->cached_next_index;
1152
1153 while (n_left_from > 0)
1154 {
1155 u32 n_left_to_next;
1156
1157 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
1158
1159 while (n_left_from > 0 && n_left_to_next > 0)
1160 {
1161 u32 bi0;
1162 vlib_buffer_t *b0;
1163 tcp_header_t *th0 = 0;
1164 tcp_connection_t *tc0;
1165 ip4_header_t *ip40;
1166 ip6_header_t *ip60;
1167 u32 n_advance_bytes0, n_data_bytes0;
1168 u32 next0 = TCP_ESTABLISHED_NEXT_DROP, error0 = TCP_ERROR_ENQUEUED;
1169
1170 bi0 = from[0];
1171 to_next[0] = bi0;
1172 from += 1;
1173 to_next += 1;
1174 n_left_from -= 1;
1175 n_left_to_next -= 1;
1176
1177 b0 = vlib_get_buffer (vm, bi0);
1178 tc0 = tcp_connection_get (vnet_buffer (b0)->tcp.connection_index,
1179 my_thread_index);
1180
Florin Corasd79b41e2017-03-04 05:37:52 -08001181 if (PREDICT_FALSE (tc0 == 0))
1182 {
1183 error0 = TCP_ERROR_INVALID_CONNECTION;
Florin Coras6792ec02017-03-13 03:49:51 -07001184 goto done;
Florin Corasd79b41e2017-03-04 05:37:52 -08001185 }
1186
Dave Barach68b0fb02017-02-28 15:15:56 -05001187 /* Checksum computed by ipx_local no need to compute again */
1188
1189 if (is_ip4)
1190 {
1191 ip40 = vlib_buffer_get_current (b0);
1192 th0 = ip4_next_header (ip40);
1193 n_advance_bytes0 = (ip4_header_bytes (ip40)
1194 + tcp_header_bytes (th0));
1195 n_data_bytes0 = clib_net_to_host_u16 (ip40->length)
1196 - n_advance_bytes0;
1197 }
1198 else
1199 {
1200 ip60 = vlib_buffer_get_current (b0);
1201 th0 = ip6_next_header (ip60);
1202 n_advance_bytes0 = tcp_header_bytes (th0);
1203 n_data_bytes0 = clib_net_to_host_u16 (ip60->payload_length)
1204 - n_advance_bytes0;
1205 n_advance_bytes0 += sizeof (ip60[0]);
1206 }
1207
1208 /* SYNs, FINs and data consume sequence numbers */
1209 vnet_buffer (b0)->tcp.seq_end = vnet_buffer (b0)->tcp.seq_number
1210 + tcp_is_syn (th0) + tcp_is_fin (th0) + n_data_bytes0;
1211
1212 /* TODO header prediction fast path */
1213
1214 /* 1-4: check SEQ, RST, SYN */
1215 if (PREDICT_FALSE (tcp_segment_validate (vm, tc0, b0, th0, &next0)))
1216 {
1217 error0 = TCP_ERROR_SEGMENT_INVALID;
Florin Coras6792ec02017-03-13 03:49:51 -07001218 TCP_EVT_DBG (TCP_EVT_SEG_INVALID, tc0,
1219 vnet_buffer (b0)->tcp.seq_number,
1220 vnet_buffer (b0)->tcp.seq_end);
1221 goto done;
Dave Barach68b0fb02017-02-28 15:15:56 -05001222 }
1223
1224 /* 5: check the ACK field */
1225 if (tcp_rcv_ack (tc0, b0, th0, &next0, &error0))
1226 {
Florin Coras6792ec02017-03-13 03:49:51 -07001227 goto done;
Dave Barach68b0fb02017-02-28 15:15:56 -05001228 }
1229
1230 /* 6: check the URG bit TODO */
1231
1232 /* 7: process the segment text */
Florin Coras6792ec02017-03-13 03:49:51 -07001233
Dave Barach68b0fb02017-02-28 15:15:56 -05001234 vlib_buffer_advance (b0, n_advance_bytes0);
1235 error0 = tcp_segment_rcv (tm, tc0, b0, n_data_bytes0, &next0);
1236
1237 /* 8: check the FIN bit */
1238 if (tcp_fin (th0))
1239 {
Florin Corasd79b41e2017-03-04 05:37:52 -08001240 /* Enter CLOSE-WAIT and notify session. Don't send ACK, instead
1241 * wait for session to call close. To avoid lingering
1242 * in CLOSE-WAIT, set timer (reuse WAITCLOSE). */
Dave Barach68b0fb02017-02-28 15:15:56 -05001243 tc0->state = TCP_STATE_CLOSE_WAIT;
Florin Corase69f4952017-03-07 10:06:24 -08001244 TCP_EVT_DBG (TCP_EVT_FIN_RCVD, tc0);
Dave Barach68b0fb02017-02-28 15:15:56 -05001245 stream_session_disconnect_notify (&tc0->connection);
Florin Corasd79b41e2017-03-04 05:37:52 -08001246 tcp_timer_set (tc0, TCP_TIMER_WAITCLOSE, TCP_CLOSEWAIT_TIME);
Dave Barach68b0fb02017-02-28 15:15:56 -05001247 }
1248
Florin Coras6792ec02017-03-13 03:49:51 -07001249 done:
Dave Barach68b0fb02017-02-28 15:15:56 -05001250 b0->error = node->errors[error0];
1251 if (PREDICT_FALSE (b0->flags & VLIB_BUFFER_IS_TRACED))
1252 {
1253
1254 }
1255
1256 vlib_validate_buffer_enqueue_x1 (vm, node, next_index, to_next,
1257 n_left_to_next, bi0, next0);
1258 }
1259
1260 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
1261 }
1262
1263 errors = session_manager_flush_enqueue_events (my_thread_index);
Florin Coras6792ec02017-03-13 03:49:51 -07001264 tcp_established_inc_counter (vm, is_ip4, TCP_ERROR_EVENT_FIFO_FULL, errors);
Dave Barach68b0fb02017-02-28 15:15:56 -05001265
1266 return from_frame->n_vectors;
1267}
1268
1269static uword
1270tcp4_established (vlib_main_t * vm, vlib_node_runtime_t * node,
1271 vlib_frame_t * from_frame)
1272{
1273 return tcp46_established_inline (vm, node, from_frame, 1 /* is_ip4 */ );
1274}
1275
1276static uword
1277tcp6_established (vlib_main_t * vm, vlib_node_runtime_t * node,
1278 vlib_frame_t * from_frame)
1279{
1280 return tcp46_established_inline (vm, node, from_frame, 0 /* is_ip4 */ );
1281}
1282
1283/* *INDENT-OFF* */
1284VLIB_REGISTER_NODE (tcp4_established_node) =
1285{
1286 .function = tcp4_established,
1287 .name = "tcp4-established",
1288 /* Takes a vector of packets. */
1289 .vector_size = sizeof (u32),
Florin Corase69f4952017-03-07 10:06:24 -08001290 .n_errors = TCP_N_ERROR,
1291 .error_strings = tcp_error_strings,
Dave Barach68b0fb02017-02-28 15:15:56 -05001292 .n_next_nodes = TCP_ESTABLISHED_N_NEXT,
1293 .next_nodes =
1294 {
1295#define _(s,n) [TCP_ESTABLISHED_NEXT_##s] = n,
1296 foreach_tcp_state_next
1297#undef _
1298 },
1299};
1300/* *INDENT-ON* */
1301
1302VLIB_NODE_FUNCTION_MULTIARCH (tcp4_established_node, tcp4_established);
1303
1304/* *INDENT-OFF* */
1305VLIB_REGISTER_NODE (tcp6_established_node) =
1306{
1307 .function = tcp6_established,
1308 .name = "tcp6-established",
1309 /* Takes a vector of packets. */
1310 .vector_size = sizeof (u32),
1311 .n_errors = TCP_N_ERROR,
1312 .error_strings = tcp_error_strings,
1313 .n_next_nodes = TCP_ESTABLISHED_N_NEXT,
1314 .next_nodes =
1315 {
1316#define _(s,n) [TCP_ESTABLISHED_NEXT_##s] = n,
1317 foreach_tcp_state_next
1318#undef _
1319 },
1320};
1321/* *INDENT-ON* */
1322
1323
1324VLIB_NODE_FUNCTION_MULTIARCH (tcp6_established_node, tcp6_established);
1325
1326vlib_node_registration_t tcp4_syn_sent_node;
1327vlib_node_registration_t tcp6_syn_sent_node;
1328
1329always_inline uword
1330tcp46_syn_sent_inline (vlib_main_t * vm, vlib_node_runtime_t * node,
1331 vlib_frame_t * from_frame, int is_ip4)
1332{
1333 tcp_main_t *tm = vnet_get_tcp_main ();
1334 u32 n_left_from, next_index, *from, *to_next;
Damjan Marion586afd72017-04-05 19:18:20 +02001335 u32 my_thread_index = vm->thread_index, errors = 0;
Dave Barach68b0fb02017-02-28 15:15:56 -05001336 u8 sst = is_ip4 ? SESSION_TYPE_IP4_TCP : SESSION_TYPE_IP6_TCP;
1337
1338 from = vlib_frame_vector_args (from_frame);
1339 n_left_from = from_frame->n_vectors;
1340
1341 next_index = node->cached_next_index;
1342
1343 while (n_left_from > 0)
1344 {
1345 u32 n_left_to_next;
1346
1347 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
1348
1349 while (n_left_from > 0 && n_left_to_next > 0)
1350 {
1351 u32 bi0, ack0, seq0;
1352 vlib_buffer_t *b0;
1353 tcp_header_t *tcp0 = 0;
1354 tcp_connection_t *tc0;
1355 ip4_header_t *ip40;
1356 ip6_header_t *ip60;
1357 u32 n_advance_bytes0, n_data_bytes0;
1358 tcp_connection_t *new_tc0;
1359 u32 next0 = TCP_SYN_SENT_NEXT_DROP, error0 = TCP_ERROR_ENQUEUED;
1360
1361 bi0 = from[0];
1362 to_next[0] = bi0;
1363 from += 1;
1364 to_next += 1;
1365 n_left_from -= 1;
1366 n_left_to_next -= 1;
1367
1368 b0 = vlib_get_buffer (vm, bi0);
1369 tc0 =
1370 tcp_half_open_connection_get (vnet_buffer (b0)->
1371 tcp.connection_index);
1372
1373 ack0 = vnet_buffer (b0)->tcp.ack_number;
1374 seq0 = vnet_buffer (b0)->tcp.seq_number;
1375
1376 /* Checksum computed by ipx_local no need to compute again */
1377
1378 if (is_ip4)
1379 {
1380 ip40 = vlib_buffer_get_current (b0);
1381 tcp0 = ip4_next_header (ip40);
1382 n_advance_bytes0 = (ip4_header_bytes (ip40)
1383 + tcp_header_bytes (tcp0));
1384 n_data_bytes0 = clib_net_to_host_u16 (ip40->length)
1385 - n_advance_bytes0;
1386 }
1387 else
1388 {
1389 ip60 = vlib_buffer_get_current (b0);
1390 tcp0 = ip6_next_header (ip60);
1391 n_advance_bytes0 = tcp_header_bytes (tcp0);
1392 n_data_bytes0 = clib_net_to_host_u16 (ip60->payload_length)
1393 - n_advance_bytes0;
1394 n_advance_bytes0 += sizeof (ip60[0]);
1395 }
1396
1397 if (PREDICT_FALSE
1398 (!tcp_ack (tcp0) && !tcp_rst (tcp0) && !tcp_syn (tcp0)))
1399 goto drop;
1400
1401 /* SYNs, FINs and data consume sequence numbers */
1402 vnet_buffer (b0)->tcp.seq_end = seq0 + tcp_is_syn (tcp0)
1403 + tcp_is_fin (tcp0) + n_data_bytes0;
1404
1405 /*
1406 * 1. check the ACK bit
1407 */
1408
1409 /*
1410 * If the ACK bit is set
1411 * If SEG.ACK =< ISS, or SEG.ACK > SND.NXT, send a reset (unless
1412 * the RST bit is set, if so drop the segment and return)
1413 * <SEQ=SEG.ACK><CTL=RST>
1414 * and discard the segment. Return.
1415 * If SND.UNA =< SEG.ACK =< SND.NXT then the ACK is acceptable.
1416 */
1417 if (tcp_ack (tcp0))
1418 {
1419 if (ack0 <= tc0->iss || ack0 > tc0->snd_nxt)
1420 {
1421 if (!tcp_rst (tcp0))
1422 tcp_send_reset (b0, is_ip4);
1423
1424 goto drop;
1425 }
1426
1427 /* Make sure ACK is valid */
1428 if (tc0->snd_una > ack0)
1429 goto drop;
1430 }
1431
1432 /*
1433 * 2. check the RST bit
1434 */
1435
1436 if (tcp_rst (tcp0))
1437 {
1438 /* If ACK is acceptable, signal client that peer is not
1439 * willing to accept connection and drop connection*/
1440 if (tcp_ack (tcp0))
1441 {
1442 stream_session_connect_notify (&tc0->connection, sst,
1443 1 /* fail */ );
1444 tcp_connection_cleanup (tc0);
1445 }
1446 goto drop;
1447 }
1448
1449 /*
1450 * 3. check the security and precedence (skipped)
1451 */
1452
1453 /*
1454 * 4. check the SYN bit
1455 */
1456
1457 /* No SYN flag. Drop. */
1458 if (!tcp_syn (tcp0))
1459 goto drop;
1460
1461 /* Stop connection establishment and retransmit timers */
1462 tcp_timer_reset (tc0, TCP_TIMER_ESTABLISH);
1463 tcp_timer_reset (tc0, TCP_TIMER_RETRANSMIT_SYN);
1464
1465 /* Valid SYN or SYN-ACK. Move connection from half-open pool to
1466 * current thread pool. */
1467 pool_get (tm->connections[my_thread_index], new_tc0);
1468 clib_memcpy (new_tc0, tc0, sizeof (*new_tc0));
1469
1470 new_tc0->c_thread_index = my_thread_index;
1471
1472 /* Cleanup half-open connection XXX lock */
1473 pool_put (tm->half_open_connections, tc0);
1474
1475 new_tc0->rcv_nxt = vnet_buffer (b0)->tcp.seq_end;
1476 new_tc0->irs = seq0;
1477
1478 /* Parse options */
1479 tcp_options_parse (tcp0, &new_tc0->opt);
Dave Barach68b0fb02017-02-28 15:15:56 -05001480
1481 if (tcp_opts_tstamp (&new_tc0->opt))
1482 {
1483 new_tc0->tsval_recent = new_tc0->opt.tsval;
1484 new_tc0->tsval_recent_age = tcp_time_now ();
1485 }
1486
1487 if (tcp_opts_wscale (&new_tc0->opt))
1488 new_tc0->snd_wscale = new_tc0->opt.wscale;
1489
Florin Corase04c2992017-03-01 08:17:34 -08001490 /* No scaling */
1491 new_tc0->snd_wnd = clib_net_to_host_u16 (tcp0->window);
Dave Barach68b0fb02017-02-28 15:15:56 -05001492 new_tc0->snd_wl1 = seq0;
1493 new_tc0->snd_wl2 = ack0;
1494
Florin Corase04c2992017-03-01 08:17:34 -08001495 tcp_connection_init_vars (new_tc0);
1496
Dave Barach68b0fb02017-02-28 15:15:56 -05001497 /* SYN-ACK: See if we can switch to ESTABLISHED state */
1498 if (tcp_ack (tcp0))
1499 {
1500 /* Our SYN is ACKed: we have iss < ack = snd_una */
1501
1502 /* TODO Dequeue acknowledged segments if we support Fast Open */
1503 new_tc0->snd_una = ack0;
1504 new_tc0->state = TCP_STATE_ESTABLISHED;
1505
Florin Corase04c2992017-03-01 08:17:34 -08001506 /* Make sure las is initialized for the wnd computation */
1507 new_tc0->rcv_las = new_tc0->rcv_nxt;
1508
Dave Barach68b0fb02017-02-28 15:15:56 -05001509 /* Notify app that we have connection */
1510 stream_session_connect_notify (&new_tc0->connection, sst, 0);
1511
1512 /* Make sure after data segment processing ACK is sent */
1513 new_tc0->flags |= TCP_CONN_SNDACK;
1514 }
1515 /* SYN: Simultaneous open. Change state to SYN-RCVD and send SYN-ACK */
1516 else
1517 {
1518 new_tc0->state = TCP_STATE_SYN_RCVD;
1519
Florin Corase69f4952017-03-07 10:06:24 -08001520 /* Notify app that we have connection */
Dave Barach68b0fb02017-02-28 15:15:56 -05001521 stream_session_connect_notify (&new_tc0->connection, sst, 0);
1522
1523 tcp_make_synack (new_tc0, b0);
1524 next0 = tcp_next_output (is_ip4);
1525
1526 goto drop;
1527 }
1528
1529 /* Read data, if any */
1530 if (n_data_bytes0)
1531 {
1532 error0 =
1533 tcp_segment_rcv (tm, new_tc0, b0, n_data_bytes0, &next0);
1534 if (error0 == TCP_ERROR_PURE_ACK)
1535 error0 = TCP_ERROR_SYN_ACKS_RCVD;
1536 }
1537 else
1538 {
1539 tcp_make_ack (new_tc0, b0);
1540 next0 = tcp_next_output (new_tc0->c_is_ip4);
1541 }
1542
1543 drop:
1544
1545 b0->error = error0 ? node->errors[error0] : 0;
1546 if (PREDICT_FALSE (b0->flags & VLIB_BUFFER_IS_TRACED))
1547 {
1548
1549 }
1550
1551 vlib_validate_buffer_enqueue_x1 (vm, node, next_index, to_next,
1552 n_left_to_next, bi0, next0);
1553 }
1554
1555 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
1556 }
1557
1558 errors = session_manager_flush_enqueue_events (my_thread_index);
1559 if (errors)
1560 {
1561 if (is_ip4)
1562 vlib_node_increment_counter (vm, tcp4_established_node.index,
1563 TCP_ERROR_EVENT_FIFO_FULL, errors);
1564 else
1565 vlib_node_increment_counter (vm, tcp6_established_node.index,
1566 TCP_ERROR_EVENT_FIFO_FULL, errors);
1567 }
1568
1569 return from_frame->n_vectors;
1570}
1571
1572static uword
1573tcp4_syn_sent (vlib_main_t * vm, vlib_node_runtime_t * node,
1574 vlib_frame_t * from_frame)
1575{
1576 return tcp46_syn_sent_inline (vm, node, from_frame, 1 /* is_ip4 */ );
1577}
1578
1579static uword
1580tcp6_syn_sent_rcv (vlib_main_t * vm, vlib_node_runtime_t * node,
1581 vlib_frame_t * from_frame)
1582{
1583 return tcp46_syn_sent_inline (vm, node, from_frame, 0 /* is_ip4 */ );
1584}
1585
1586/* *INDENT-OFF* */
1587VLIB_REGISTER_NODE (tcp4_syn_sent_node) =
1588{
1589 .function = tcp4_syn_sent,
1590 .name = "tcp4-syn-sent",
1591 /* Takes a vector of packets. */
1592 .vector_size = sizeof (u32),
1593 .n_errors = TCP_N_ERROR,
1594 .error_strings = tcp_error_strings,
1595 .n_next_nodes = TCP_SYN_SENT_N_NEXT,
1596 .next_nodes =
1597 {
1598#define _(s,n) [TCP_SYN_SENT_NEXT_##s] = n,
1599 foreach_tcp_state_next
1600#undef _
1601 },
1602};
1603/* *INDENT-ON* */
1604
1605VLIB_NODE_FUNCTION_MULTIARCH (tcp4_syn_sent_node, tcp4_syn_sent);
1606
1607/* *INDENT-OFF* */
1608VLIB_REGISTER_NODE (tcp6_syn_sent_node) =
1609{
1610 .function = tcp6_syn_sent_rcv,
1611 .name = "tcp6-syn-sent",
1612 /* Takes a vector of packets. */
1613 .vector_size = sizeof (u32),
1614 .n_errors = TCP_N_ERROR,
1615 .error_strings = tcp_error_strings,
1616 .n_next_nodes = TCP_SYN_SENT_N_NEXT,
1617 .next_nodes =
1618 {
1619#define _(s,n) [TCP_SYN_SENT_NEXT_##s] = n,
1620 foreach_tcp_state_next
1621#undef _
1622 }
1623,};
1624/* *INDENT-ON* */
1625
1626VLIB_NODE_FUNCTION_MULTIARCH (tcp6_syn_sent_node, tcp6_syn_sent_rcv);
1627/**
Florin Corasd79b41e2017-03-04 05:37:52 -08001628 * Handles reception for all states except LISTEN, SYN-SENT and ESTABLISHED
Dave Barach68b0fb02017-02-28 15:15:56 -05001629 * as per RFC793 p. 64
1630 */
1631always_inline uword
1632tcp46_rcv_process_inline (vlib_main_t * vm, vlib_node_runtime_t * node,
1633 vlib_frame_t * from_frame, int is_ip4)
1634{
1635 tcp_main_t *tm = vnet_get_tcp_main ();
1636 u32 n_left_from, next_index, *from, *to_next;
Damjan Marion586afd72017-04-05 19:18:20 +02001637 u32 my_thread_index = vm->thread_index, errors = 0;
Dave Barach68b0fb02017-02-28 15:15:56 -05001638
1639 from = vlib_frame_vector_args (from_frame);
1640 n_left_from = from_frame->n_vectors;
1641
1642 next_index = node->cached_next_index;
1643
1644 while (n_left_from > 0)
1645 {
1646 u32 n_left_to_next;
1647
1648 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
1649
1650 while (n_left_from > 0 && n_left_to_next > 0)
1651 {
1652 u32 bi0;
1653 vlib_buffer_t *b0;
1654 tcp_header_t *tcp0 = 0;
1655 tcp_connection_t *tc0;
1656 ip4_header_t *ip40;
1657 ip6_header_t *ip60;
1658 u32 n_advance_bytes0, n_data_bytes0;
1659 u32 next0 = TCP_RCV_PROCESS_NEXT_DROP, error0 = TCP_ERROR_ENQUEUED;
1660
1661 bi0 = from[0];
1662 to_next[0] = bi0;
1663 from += 1;
1664 to_next += 1;
1665 n_left_from -= 1;
1666 n_left_to_next -= 1;
1667
1668 b0 = vlib_get_buffer (vm, bi0);
1669 tc0 = tcp_connection_get (vnet_buffer (b0)->tcp.connection_index,
1670 my_thread_index);
Florin Corasd79b41e2017-03-04 05:37:52 -08001671 if (PREDICT_FALSE (tc0 == 0))
1672 {
1673 error0 = TCP_ERROR_INVALID_CONNECTION;
1674 goto drop;
1675 }
Dave Barach68b0fb02017-02-28 15:15:56 -05001676
1677 /* Checksum computed by ipx_local no need to compute again */
1678
1679 if (is_ip4)
1680 {
1681 ip40 = vlib_buffer_get_current (b0);
1682 tcp0 = ip4_next_header (ip40);
1683 n_advance_bytes0 = (ip4_header_bytes (ip40)
1684 + tcp_header_bytes (tcp0));
1685 n_data_bytes0 = clib_net_to_host_u16 (ip40->length)
1686 - n_advance_bytes0;
1687 }
1688 else
1689 {
1690 ip60 = vlib_buffer_get_current (b0);
1691 tcp0 = ip6_next_header (ip60);
1692 n_advance_bytes0 = tcp_header_bytes (tcp0);
1693 n_data_bytes0 = clib_net_to_host_u16 (ip60->payload_length)
1694 - n_advance_bytes0;
1695 n_advance_bytes0 += sizeof (ip60[0]);
1696 }
1697
1698 /* SYNs, FINs and data consume sequence numbers */
1699 vnet_buffer (b0)->tcp.seq_end = vnet_buffer (b0)->tcp.seq_number
1700 + tcp_is_syn (tcp0) + tcp_is_fin (tcp0) + n_data_bytes0;
1701
1702 /*
1703 * Special treatment for CLOSED
1704 */
1705 switch (tc0->state)
1706 {
1707 case TCP_STATE_CLOSED:
1708 goto drop;
1709 break;
1710 }
1711
1712 /*
1713 * For all other states (except LISTEN)
1714 */
1715
1716 /* 1-4: check SEQ, RST, SYN */
1717 if (PREDICT_FALSE
1718 (tcp_segment_validate (vm, tc0, b0, tcp0, &next0)))
1719 {
1720 error0 = TCP_ERROR_SEGMENT_INVALID;
1721 goto drop;
1722 }
1723
1724 /* 5: check the ACK field */
1725 switch (tc0->state)
1726 {
1727 case TCP_STATE_SYN_RCVD:
1728 /*
1729 * If the segment acknowledgment is not acceptable, form a
1730 * reset segment,
1731 * <SEQ=SEG.ACK><CTL=RST>
1732 * and send it.
1733 */
1734 if (!tcp_rcv_ack_is_acceptable (tc0, b0))
1735 {
1736 tcp_send_reset (b0, is_ip4);
1737 goto drop;
1738 }
1739 /* Switch state to ESTABLISHED */
1740 tc0->state = TCP_STATE_ESTABLISHED;
1741
1742 /* Initialize session variables */
1743 tc0->snd_una = vnet_buffer (b0)->tcp.ack_number;
Florin Corase04c2992017-03-01 08:17:34 -08001744 tc0->snd_wnd = clib_net_to_host_u16 (tcp0->window)
Dave Barach68b0fb02017-02-28 15:15:56 -05001745 << tc0->opt.wscale;
1746 tc0->snd_wl1 = vnet_buffer (b0)->tcp.seq_number;
1747 tc0->snd_wl2 = vnet_buffer (b0)->tcp.ack_number;
1748
1749 /* Shoulder tap the server */
1750 stream_session_accept_notify (&tc0->connection);
1751
Florin Corasd79b41e2017-03-04 05:37:52 -08001752 /* Reset SYN-ACK retransmit timer */
Florin Coras6792ec02017-03-13 03:49:51 -07001753 tcp_retransmit_timer_reset (tc0);
Dave Barach68b0fb02017-02-28 15:15:56 -05001754 break;
1755 case TCP_STATE_ESTABLISHED:
1756 /* We can get packets in established state here because they
1757 * were enqueued before state change */
1758 if (tcp_rcv_ack (tc0, b0, tcp0, &next0, &error0))
1759 goto drop;
1760
1761 break;
1762 case TCP_STATE_FIN_WAIT_1:
1763 /* In addition to the processing for the ESTABLISHED state, if
1764 * our FIN is now acknowledged then enter FIN-WAIT-2 and
1765 * continue processing in that state. */
1766 if (tcp_rcv_ack (tc0, b0, tcp0, &next0, &error0))
1767 goto drop;
Florin Corasd79b41e2017-03-04 05:37:52 -08001768
1769 /* If FIN is ACKed */
1770 if (tc0->snd_una == tc0->snd_una_max)
1771 {
1772 tc0->state = TCP_STATE_FIN_WAIT_2;
1773 /* Stop all timers, 2MSL will be set lower */
1774 tcp_connection_timers_reset (tc0);
1775 }
Dave Barach68b0fb02017-02-28 15:15:56 -05001776 break;
1777 case TCP_STATE_FIN_WAIT_2:
1778 /* In addition to the processing for the ESTABLISHED state, if
1779 * the retransmission queue is empty, the user's CLOSE can be
1780 * acknowledged ("ok") but do not delete the TCB. */
1781 if (tcp_rcv_ack (tc0, b0, tcp0, &next0, &error0))
1782 goto drop;
1783 /* check if rtx queue is empty and ack CLOSE TODO */
1784 break;
1785 case TCP_STATE_CLOSE_WAIT:
1786 /* Do the same processing as for the ESTABLISHED state. */
1787 if (tcp_rcv_ack (tc0, b0, tcp0, &next0, &error0))
1788 goto drop;
1789 break;
1790 case TCP_STATE_CLOSING:
1791 /* In addition to the processing for the ESTABLISHED state, if
1792 * the ACK acknowledges our FIN then enter the TIME-WAIT state,
1793 * otherwise ignore the segment. */
1794 if (tcp_rcv_ack (tc0, b0, tcp0, &next0, &error0))
1795 goto drop;
1796
1797 /* XXX test that send queue empty */
1798 tc0->state = TCP_STATE_TIME_WAIT;
1799 goto drop;
1800
1801 break;
1802 case TCP_STATE_LAST_ACK:
1803 /* The only thing that can arrive in this state is an
1804 * acknowledgment of our FIN. If our FIN is now acknowledged,
1805 * delete the TCB, enter the CLOSED state, and return. */
1806
1807 if (!tcp_rcv_ack_is_acceptable (tc0, b0))
1808 goto drop;
1809
Florin Corasd79b41e2017-03-04 05:37:52 -08001810 tc0->state = TCP_STATE_CLOSED;
1811
1812 /* Don't delete the connection/session yet. Instead, wait a
1813 * reasonable amount of time until the pipes are cleared. In
1814 * particular, this makes sure that we won't have dead sessions
1815 * when processing events on the tx path */
1816 tcp_timer_update (tc0, TCP_TIMER_WAITCLOSE, TCP_CLEANUP_TIME);
1817
1818 /* Stop retransmit */
Florin Coras6792ec02017-03-13 03:49:51 -07001819 tcp_retransmit_timer_reset (tc0);
Florin Corasd79b41e2017-03-04 05:37:52 -08001820
Dave Barach68b0fb02017-02-28 15:15:56 -05001821 goto drop;
1822
1823 break;
1824 case TCP_STATE_TIME_WAIT:
1825 /* The only thing that can arrive in this state is a
1826 * retransmission of the remote FIN. Acknowledge it, and restart
1827 * the 2 MSL timeout. */
1828
1829 /* TODO */
1830 goto drop;
1831 break;
1832 default:
1833 ASSERT (0);
1834 }
1835
1836 /* 6: check the URG bit TODO */
1837
1838 /* 7: process the segment text */
1839 switch (tc0->state)
1840 {
1841 case TCP_STATE_ESTABLISHED:
1842 case TCP_STATE_FIN_WAIT_1:
1843 case TCP_STATE_FIN_WAIT_2:
1844 error0 = tcp_segment_rcv (tm, tc0, b0, n_data_bytes0, &next0);
1845 break;
1846 case TCP_STATE_CLOSE_WAIT:
1847 case TCP_STATE_CLOSING:
1848 case TCP_STATE_LAST_ACK:
1849 case TCP_STATE_TIME_WAIT:
1850 /* This should not occur, since a FIN has been received from the
1851 * remote side. Ignore the segment text. */
1852 break;
1853 }
1854
1855 /* 8: check the FIN bit */
1856 if (!tcp_fin (tcp0))
1857 goto drop;
1858
1859 switch (tc0->state)
1860 {
1861 case TCP_STATE_ESTABLISHED:
1862 case TCP_STATE_SYN_RCVD:
1863 /* Send FIN-ACK notify app and enter CLOSE-WAIT */
1864 tcp_connection_timers_reset (tc0);
Florin Corasd79b41e2017-03-04 05:37:52 -08001865 tcp_make_fin (tc0, b0);
Dave Barach68b0fb02017-02-28 15:15:56 -05001866 next0 = tcp_next_output (tc0->c_is_ip4);
1867 stream_session_disconnect_notify (&tc0->connection);
1868 tc0->state = TCP_STATE_CLOSE_WAIT;
1869 break;
1870 case TCP_STATE_CLOSE_WAIT:
1871 case TCP_STATE_CLOSING:
1872 case TCP_STATE_LAST_ACK:
1873 /* move along .. */
1874 break;
1875 case TCP_STATE_FIN_WAIT_1:
1876 tc0->state = TCP_STATE_TIME_WAIT;
1877 tcp_connection_timers_reset (tc0);
Florin Corasd79b41e2017-03-04 05:37:52 -08001878 tcp_timer_set (tc0, TCP_TIMER_WAITCLOSE, TCP_2MSL_TIME);
Dave Barach68b0fb02017-02-28 15:15:56 -05001879 break;
1880 case TCP_STATE_FIN_WAIT_2:
1881 /* Got FIN, send ACK! */
1882 tc0->state = TCP_STATE_TIME_WAIT;
Florin Corase69f4952017-03-07 10:06:24 -08001883 tcp_timer_set (tc0, TCP_TIMER_WAITCLOSE, TCP_CLOSEWAIT_TIME);
Dave Barach68b0fb02017-02-28 15:15:56 -05001884 tcp_make_ack (tc0, b0);
1885 next0 = tcp_next_output (is_ip4);
1886 break;
1887 case TCP_STATE_TIME_WAIT:
1888 /* Remain in the TIME-WAIT state. Restart the 2 MSL time-wait
1889 * timeout.
1890 */
Florin Corasd79b41e2017-03-04 05:37:52 -08001891 tcp_timer_update (tc0, TCP_TIMER_WAITCLOSE, TCP_2MSL_TIME);
Dave Barach68b0fb02017-02-28 15:15:56 -05001892 break;
1893 }
Florin Corase69f4952017-03-07 10:06:24 -08001894 TCP_EVT_DBG (TCP_EVT_FIN_RCVD, tc0);
Dave Barach68b0fb02017-02-28 15:15:56 -05001895
1896 b0->error = error0 ? node->errors[error0] : 0;
1897
1898 drop:
1899 if (PREDICT_FALSE (b0->flags & VLIB_BUFFER_IS_TRACED))
1900 {
1901
1902 }
1903
1904 vlib_validate_buffer_enqueue_x1 (vm, node, next_index, to_next,
1905 n_left_to_next, bi0, next0);
1906 }
1907
1908 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
1909 }
1910
1911 errors = session_manager_flush_enqueue_events (my_thread_index);
1912 if (errors)
1913 {
1914 if (is_ip4)
1915 vlib_node_increment_counter (vm, tcp4_established_node.index,
1916 TCP_ERROR_EVENT_FIFO_FULL, errors);
1917 else
1918 vlib_node_increment_counter (vm, tcp6_established_node.index,
1919 TCP_ERROR_EVENT_FIFO_FULL, errors);
1920 }
1921
1922 return from_frame->n_vectors;
1923}
1924
1925static uword
1926tcp4_rcv_process (vlib_main_t * vm, vlib_node_runtime_t * node,
1927 vlib_frame_t * from_frame)
1928{
1929 return tcp46_rcv_process_inline (vm, node, from_frame, 1 /* is_ip4 */ );
1930}
1931
1932static uword
1933tcp6_rcv_process (vlib_main_t * vm, vlib_node_runtime_t * node,
1934 vlib_frame_t * from_frame)
1935{
1936 return tcp46_rcv_process_inline (vm, node, from_frame, 0 /* is_ip4 */ );
1937}
1938
1939/* *INDENT-OFF* */
1940VLIB_REGISTER_NODE (tcp4_rcv_process_node) =
1941{
1942 .function = tcp4_rcv_process,
1943 .name = "tcp4-rcv-process",
1944 /* Takes a vector of packets. */
1945 .vector_size = sizeof (u32),
1946 .n_errors = TCP_N_ERROR,
1947 .error_strings = tcp_error_strings,
1948 .n_next_nodes = TCP_RCV_PROCESS_N_NEXT,
1949 .next_nodes =
1950 {
1951#define _(s,n) [TCP_RCV_PROCESS_NEXT_##s] = n,
1952 foreach_tcp_state_next
1953#undef _
1954 },
1955};
1956/* *INDENT-ON* */
1957
1958VLIB_NODE_FUNCTION_MULTIARCH (tcp4_rcv_process_node, tcp4_rcv_process);
1959
1960/* *INDENT-OFF* */
1961VLIB_REGISTER_NODE (tcp6_rcv_process_node) =
1962{
1963 .function = tcp6_rcv_process,
1964 .name = "tcp6-rcv-process",
1965 /* Takes a vector of packets. */
1966 .vector_size = sizeof (u32),
1967 .n_errors = TCP_N_ERROR,
1968 .error_strings = tcp_error_strings,
1969 .n_next_nodes = TCP_RCV_PROCESS_N_NEXT,
1970 .next_nodes =
1971 {
1972#define _(s,n) [TCP_RCV_PROCESS_NEXT_##s] = n,
1973 foreach_tcp_state_next
1974#undef _
1975 },
1976};
1977/* *INDENT-ON* */
1978
1979VLIB_NODE_FUNCTION_MULTIARCH (tcp6_rcv_process_node, tcp6_rcv_process);
1980
1981vlib_node_registration_t tcp4_listen_node;
1982vlib_node_registration_t tcp6_listen_node;
1983
1984/**
1985 * LISTEN state processing as per RFC 793 p. 65
1986 */
1987always_inline uword
1988tcp46_listen_inline (vlib_main_t * vm, vlib_node_runtime_t * node,
1989 vlib_frame_t * from_frame, int is_ip4)
1990{
1991 u32 n_left_from, next_index, *from, *to_next;
Damjan Marion586afd72017-04-05 19:18:20 +02001992 u32 my_thread_index = vm->thread_index;
Dave Barach68b0fb02017-02-28 15:15:56 -05001993 tcp_main_t *tm = vnet_get_tcp_main ();
1994 u8 sst = is_ip4 ? SESSION_TYPE_IP4_TCP : SESSION_TYPE_IP6_TCP;
1995
1996 from = vlib_frame_vector_args (from_frame);
1997 n_left_from = from_frame->n_vectors;
1998
1999 next_index = node->cached_next_index;
2000
2001 while (n_left_from > 0)
2002 {
2003 u32 n_left_to_next;
2004
2005 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
2006
2007 while (n_left_from > 0 && n_left_to_next > 0)
2008 {
2009 u32 bi0;
2010 vlib_buffer_t *b0;
2011 tcp_header_t *th0 = 0;
2012 tcp_connection_t *lc0;
2013 ip4_header_t *ip40;
2014 ip6_header_t *ip60;
2015 tcp_connection_t *child0;
2016 u32 error0 = TCP_ERROR_SYNS_RCVD, next0 = TCP_LISTEN_NEXT_DROP;
2017
2018 bi0 = from[0];
2019 to_next[0] = bi0;
2020 from += 1;
2021 to_next += 1;
2022 n_left_from -= 1;
2023 n_left_to_next -= 1;
2024
2025 b0 = vlib_get_buffer (vm, bi0);
2026 lc0 = tcp_listener_get (vnet_buffer (b0)->tcp.connection_index);
2027
2028 if (is_ip4)
2029 {
2030 ip40 = vlib_buffer_get_current (b0);
2031 th0 = ip4_next_header (ip40);
2032 }
2033 else
2034 {
2035 ip60 = vlib_buffer_get_current (b0);
2036 th0 = ip6_next_header (ip60);
2037 }
2038
2039 /* Create child session. For syn-flood protection use filter */
2040
2041 /* 1. first check for an RST */
2042 if (tcp_rst (th0))
2043 goto drop;
2044
2045 /* 2. second check for an ACK */
2046 if (tcp_ack (th0))
2047 {
2048 tcp_send_reset (b0, is_ip4);
2049 goto drop;
2050 }
2051
2052 /* 3. check for a SYN (did that already) */
2053
2054 /* Create child session and send SYN-ACK */
2055 pool_get (tm->connections[my_thread_index], child0);
2056 memset (child0, 0, sizeof (*child0));
2057
2058 child0->c_c_index = child0 - tm->connections[my_thread_index];
2059 child0->c_lcl_port = lc0->c_lcl_port;
2060 child0->c_rmt_port = th0->src_port;
2061 child0->c_is_ip4 = is_ip4;
2062 child0->c_thread_index = my_thread_index;
2063
2064 if (is_ip4)
2065 {
2066 child0->c_lcl_ip4.as_u32 = ip40->dst_address.as_u32;
2067 child0->c_rmt_ip4.as_u32 = ip40->src_address.as_u32;
2068 }
2069 else
2070 {
2071 clib_memcpy (&child0->c_lcl_ip6, &ip60->dst_address,
2072 sizeof (ip6_address_t));
2073 clib_memcpy (&child0->c_rmt_ip6, &ip60->src_address,
2074 sizeof (ip6_address_t));
2075 }
2076
2077 if (stream_session_accept (&child0->connection, lc0->c_s_index, sst,
2078 0 /* notify */ ))
2079 {
2080 error0 = TCP_ERROR_CREATE_SESSION_FAIL;
2081 goto drop;
2082 }
2083
2084 tcp_options_parse (th0, &child0->opt);
Dave Barach68b0fb02017-02-28 15:15:56 -05002085
2086 child0->irs = vnet_buffer (b0)->tcp.seq_number;
2087 child0->rcv_nxt = vnet_buffer (b0)->tcp.seq_number + 1;
Florin Coras3e350af2017-03-30 02:54:28 -07002088 child0->rcv_las = child0->rcv_nxt;
Dave Barach68b0fb02017-02-28 15:15:56 -05002089 child0->state = TCP_STATE_SYN_RCVD;
2090
2091 /* RFC1323: TSval timestamps sent on {SYN} and {SYN,ACK}
2092 * segments are used to initialize PAWS. */
2093 if (tcp_opts_tstamp (&child0->opt))
2094 {
2095 child0->tsval_recent = child0->opt.tsval;
2096 child0->tsval_recent_age = tcp_time_now ();
2097 }
2098
Florin Corase04c2992017-03-01 08:17:34 -08002099 if (tcp_opts_wscale (&child0->opt))
2100 child0->snd_wscale = child0->opt.wscale;
2101
2102 /* No scaling */
2103 child0->snd_wnd = clib_net_to_host_u16 (th0->window);
2104 child0->snd_wl1 = vnet_buffer (b0)->tcp.seq_number;
2105 child0->snd_wl2 = vnet_buffer (b0)->tcp.ack_number;
2106
2107 tcp_connection_init_vars (child0);
2108
Florin Corase69f4952017-03-07 10:06:24 -08002109 TCP_EVT_DBG (TCP_EVT_SYN_RCVD, child0);
2110
Dave Barach68b0fb02017-02-28 15:15:56 -05002111 /* Reuse buffer to make syn-ack and send */
2112 tcp_make_synack (child0, b0);
2113 next0 = tcp_next_output (is_ip4);
2114
2115 drop:
2116 if (PREDICT_FALSE (b0->flags & VLIB_BUFFER_IS_TRACED))
2117 {
2118
2119 }
2120
Florin Corase04c2992017-03-01 08:17:34 -08002121 b0->error = node->errors[error0];
Dave Barach68b0fb02017-02-28 15:15:56 -05002122
2123 vlib_validate_buffer_enqueue_x1 (vm, node, next_index, to_next,
2124 n_left_to_next, bi0, next0);
2125 }
2126
2127 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
2128 }
2129 return from_frame->n_vectors;
2130}
2131
2132static uword
2133tcp4_listen (vlib_main_t * vm, vlib_node_runtime_t * node,
2134 vlib_frame_t * from_frame)
2135{
2136 return tcp46_listen_inline (vm, node, from_frame, 1 /* is_ip4 */ );
2137}
2138
2139static uword
2140tcp6_listen (vlib_main_t * vm, vlib_node_runtime_t * node,
2141 vlib_frame_t * from_frame)
2142{
2143 return tcp46_listen_inline (vm, node, from_frame, 0 /* is_ip4 */ );
2144}
2145
2146/* *INDENT-OFF* */
2147VLIB_REGISTER_NODE (tcp4_listen_node) =
2148{
2149 .function = tcp4_listen,
2150 .name = "tcp4-listen",
2151 /* Takes a vector of packets. */
2152 .vector_size = sizeof (u32),
2153 .n_errors = TCP_N_ERROR,
2154 .error_strings = tcp_error_strings,
2155 .n_next_nodes = TCP_LISTEN_N_NEXT,
2156 .next_nodes =
2157 {
2158#define _(s,n) [TCP_LISTEN_NEXT_##s] = n,
2159 foreach_tcp_state_next
2160#undef _
2161 },
2162};
2163/* *INDENT-ON* */
2164
2165VLIB_NODE_FUNCTION_MULTIARCH (tcp4_listen_node, tcp4_listen);
2166
2167/* *INDENT-OFF* */
2168VLIB_REGISTER_NODE (tcp6_listen_node) =
2169{
2170 .function = tcp6_listen,
2171 .name = "tcp6-listen",
2172 /* Takes a vector of packets. */
2173 .vector_size = sizeof (u32),
2174 .n_errors = TCP_N_ERROR,
2175 .error_strings = tcp_error_strings,
2176 .n_next_nodes = TCP_LISTEN_N_NEXT,
2177 .next_nodes =
2178 {
2179#define _(s,n) [TCP_LISTEN_NEXT_##s] = n,
2180 foreach_tcp_state_next
2181#undef _
2182 },
2183};
2184/* *INDENT-ON* */
2185
2186VLIB_NODE_FUNCTION_MULTIARCH (tcp6_listen_node, tcp6_listen);
2187
2188vlib_node_registration_t tcp4_input_node;
2189vlib_node_registration_t tcp6_input_node;
2190
2191typedef enum _tcp_input_next
2192{
2193 TCP_INPUT_NEXT_DROP,
2194 TCP_INPUT_NEXT_LISTEN,
2195 TCP_INPUT_NEXT_RCV_PROCESS,
2196 TCP_INPUT_NEXT_SYN_SENT,
2197 TCP_INPUT_NEXT_ESTABLISHED,
2198 TCP_INPUT_NEXT_RESET,
2199 TCP_INPUT_N_NEXT
2200} tcp_input_next_t;
2201
2202#define foreach_tcp4_input_next \
2203 _ (DROP, "error-drop") \
2204 _ (LISTEN, "tcp4-listen") \
2205 _ (RCV_PROCESS, "tcp4-rcv-process") \
2206 _ (SYN_SENT, "tcp4-syn-sent") \
2207 _ (ESTABLISHED, "tcp4-established") \
2208 _ (RESET, "tcp4-reset")
2209
2210#define foreach_tcp6_input_next \
2211 _ (DROP, "error-drop") \
2212 _ (LISTEN, "tcp6-listen") \
2213 _ (RCV_PROCESS, "tcp6-rcv-process") \
2214 _ (SYN_SENT, "tcp6-syn-sent") \
2215 _ (ESTABLISHED, "tcp6-established") \
2216 _ (RESET, "tcp6-reset")
2217
2218typedef struct
2219{
2220 u16 src_port;
2221 u16 dst_port;
2222 u8 state;
2223} tcp_rx_trace_t;
2224
Dave Barach68b0fb02017-02-28 15:15:56 -05002225u8 *
2226format_tcp_rx_trace (u8 * s, va_list * args)
2227{
2228 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
2229 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
2230 tcp_rx_trace_t *t = va_arg (*args, tcp_rx_trace_t *);
2231
2232 s = format (s, "TCP: src-port %d dst-port %U%s\n",
2233 clib_net_to_host_u16 (t->src_port),
2234 clib_net_to_host_u16 (t->dst_port), format_tcp_state, t->state);
2235
2236 return s;
2237}
2238
2239#define filter_flags (TCP_FLAG_SYN|TCP_FLAG_ACK|TCP_FLAG_RST|TCP_FLAG_FIN)
2240
2241always_inline uword
2242tcp46_input_inline (vlib_main_t * vm, vlib_node_runtime_t * node,
2243 vlib_frame_t * from_frame, int is_ip4)
2244{
2245 u32 n_left_from, next_index, *from, *to_next;
Damjan Marion586afd72017-04-05 19:18:20 +02002246 u32 my_thread_index = vm->thread_index;
Dave Barach68b0fb02017-02-28 15:15:56 -05002247 tcp_main_t *tm = vnet_get_tcp_main ();
Dave Barach68b0fb02017-02-28 15:15:56 -05002248
2249 from = vlib_frame_vector_args (from_frame);
2250 n_left_from = from_frame->n_vectors;
2251
2252 next_index = node->cached_next_index;
2253
2254 while (n_left_from > 0)
2255 {
2256 u32 n_left_to_next;
2257
2258 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
2259
2260 while (n_left_from > 0 && n_left_to_next > 0)
2261 {
2262 u32 bi0;
2263 vlib_buffer_t *b0;
2264 tcp_header_t *tcp0 = 0;
2265 tcp_connection_t *tc0;
2266 ip4_header_t *ip40;
2267 ip6_header_t *ip60;
2268 u32 error0 = TCP_ERROR_NO_LISTENER, next0 = TCP_INPUT_NEXT_DROP;
2269 u8 flags0;
2270
2271 bi0 = from[0];
2272 to_next[0] = bi0;
2273 from += 1;
2274 to_next += 1;
2275 n_left_from -= 1;
2276 n_left_to_next -= 1;
2277
2278 b0 = vlib_get_buffer (vm, bi0);
Florin Corasd79b41e2017-03-04 05:37:52 -08002279 vnet_buffer (b0)->tcp.flags = 0;
Dave Barach68b0fb02017-02-28 15:15:56 -05002280
2281 if (is_ip4)
2282 {
2283 ip40 = vlib_buffer_get_current (b0);
2284 tcp0 = ip4_next_header (ip40);
2285
2286 /* lookup session */
2287 tc0 =
Florin Corase04c2992017-03-01 08:17:34 -08002288 (tcp_connection_t *)
2289 stream_session_lookup_transport4 (&ip40->dst_address,
2290 &ip40->src_address,
2291 tcp0->dst_port,
2292 tcp0->src_port,
2293 SESSION_TYPE_IP4_TCP,
2294 my_thread_index);
Dave Barach68b0fb02017-02-28 15:15:56 -05002295 }
2296 else
2297 {
2298 ip60 = vlib_buffer_get_current (b0);
2299 tcp0 = ip6_next_header (ip60);
2300 tc0 =
Florin Corase04c2992017-03-01 08:17:34 -08002301 (tcp_connection_t *)
2302 stream_session_lookup_transport6 (&ip60->src_address,
2303 &ip60->dst_address,
2304 tcp0->src_port,
2305 tcp0->dst_port,
2306 SESSION_TYPE_IP6_TCP,
2307 my_thread_index);
Dave Barach68b0fb02017-02-28 15:15:56 -05002308 }
2309
2310 /* Session exists */
2311 if (PREDICT_TRUE (0 != tc0))
2312 {
2313 /* Save connection index */
2314 vnet_buffer (b0)->tcp.connection_index = tc0->c_c_index;
2315 vnet_buffer (b0)->tcp.seq_number =
2316 clib_net_to_host_u32 (tcp0->seq_number);
2317 vnet_buffer (b0)->tcp.ack_number =
2318 clib_net_to_host_u32 (tcp0->ack_number);
2319
2320 flags0 = tcp0->flags & filter_flags;
2321 next0 = tm->dispatch_table[tc0->state][flags0].next;
2322 error0 = tm->dispatch_table[tc0->state][flags0].error;
2323
2324 if (PREDICT_FALSE (error0 == TCP_ERROR_DISPATCH))
2325 {
2326 /* Overload tcp flags to store state */
2327 vnet_buffer (b0)->tcp.flags = tc0->state;
2328 }
2329 }
2330 else
2331 {
2332 /* Send reset */
2333 next0 = TCP_INPUT_NEXT_RESET;
2334 error0 = TCP_ERROR_NO_LISTENER;
Dave Barach68b0fb02017-02-28 15:15:56 -05002335 }
2336
2337 b0->error = error0 ? node->errors[error0] : 0;
2338
2339 if (PREDICT_FALSE (b0->flags & VLIB_BUFFER_IS_TRACED))
2340 {
2341
2342 }
2343
2344 vlib_validate_buffer_enqueue_x1 (vm, node, next_index, to_next,
2345 n_left_to_next, bi0, next0);
2346 }
2347
2348 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
2349 }
2350
2351 return from_frame->n_vectors;
2352}
2353
2354static uword
2355tcp4_input (vlib_main_t * vm, vlib_node_runtime_t * node,
2356 vlib_frame_t * from_frame)
2357{
2358 return tcp46_input_inline (vm, node, from_frame, 1 /* is_ip4 */ );
2359}
2360
2361static uword
2362tcp6_input (vlib_main_t * vm, vlib_node_runtime_t * node,
2363 vlib_frame_t * from_frame)
2364{
2365 return tcp46_input_inline (vm, node, from_frame, 0 /* is_ip4 */ );
2366}
2367
2368/* *INDENT-OFF* */
2369VLIB_REGISTER_NODE (tcp4_input_node) =
2370{
2371 .function = tcp4_input,
2372 .name = "tcp4-input",
2373 /* Takes a vector of packets. */
2374 .vector_size = sizeof (u32),
2375 .n_errors = TCP_N_ERROR,
2376 .error_strings = tcp_error_strings,
2377 .n_next_nodes = TCP_INPUT_N_NEXT,
2378 .next_nodes =
2379 {
2380#define _(s,n) [TCP_INPUT_NEXT_##s] = n,
2381 foreach_tcp4_input_next
2382#undef _
2383 },
2384 .format_buffer = format_tcp_header,
2385 .format_trace = format_tcp_rx_trace,
2386};
2387/* *INDENT-ON* */
2388
2389VLIB_NODE_FUNCTION_MULTIARCH (tcp4_input_node, tcp4_input);
2390
2391/* *INDENT-OFF* */
2392VLIB_REGISTER_NODE (tcp6_input_node) =
2393{
2394 .function = tcp6_input,
2395 .name = "tcp6-input",
2396 /* Takes a vector of packets. */
2397 .vector_size = sizeof (u32),
2398 .n_errors = TCP_N_ERROR,
2399 .error_strings = tcp_error_strings,
2400 .n_next_nodes = TCP_INPUT_N_NEXT,
2401 .next_nodes =
2402 {
2403#define _(s,n) [TCP_INPUT_NEXT_##s] = n,
2404 foreach_tcp6_input_next
2405#undef _
2406 },
2407 .format_buffer = format_tcp_header,
2408 .format_trace = format_tcp_rx_trace,
2409};
2410/* *INDENT-ON* */
2411
2412VLIB_NODE_FUNCTION_MULTIARCH (tcp6_input_node, tcp6_input);
2413void
2414tcp_update_time (f64 now, u32 thread_index)
2415{
2416 tcp_main_t *tm = vnet_get_tcp_main ();
2417 tw_timer_expire_timers_16t_2w_512sl (&tm->timer_wheels[thread_index], now);
2418}
2419
2420static void
2421tcp_dispatch_table_init (tcp_main_t * tm)
2422{
2423 int i, j;
2424 for (i = 0; i < ARRAY_LEN (tm->dispatch_table); i++)
2425 for (j = 0; j < ARRAY_LEN (tm->dispatch_table[i]); j++)
2426 {
2427 tm->dispatch_table[i][j].next = TCP_INPUT_NEXT_DROP;
2428 tm->dispatch_table[i][j].error = TCP_ERROR_DISPATCH;
2429 }
2430
2431#define _(t,f,n,e) \
2432do { \
2433 tm->dispatch_table[TCP_STATE_##t][f].next = (n); \
2434 tm->dispatch_table[TCP_STATE_##t][f].error = (e); \
2435} while (0)
2436
2437 /* SYNs for new connections -> tcp-listen. */
2438 _(LISTEN, TCP_FLAG_SYN, TCP_INPUT_NEXT_LISTEN, TCP_ERROR_NONE);
2439 /* ACK for for a SYN-ACK -> tcp-rcv-process. */
2440 _(SYN_RCVD, TCP_FLAG_ACK, TCP_INPUT_NEXT_RCV_PROCESS, TCP_ERROR_NONE);
2441 /* SYN-ACK for a SYN */
2442 _(SYN_SENT, TCP_FLAG_SYN | TCP_FLAG_ACK, TCP_INPUT_NEXT_SYN_SENT,
2443 TCP_ERROR_NONE);
2444 _(SYN_SENT, TCP_FLAG_ACK, TCP_INPUT_NEXT_SYN_SENT, TCP_ERROR_NONE);
2445 _(SYN_SENT, TCP_FLAG_RST, TCP_INPUT_NEXT_SYN_SENT, TCP_ERROR_NONE);
2446 _(SYN_SENT, TCP_FLAG_RST | TCP_FLAG_ACK, TCP_INPUT_NEXT_SYN_SENT,
2447 TCP_ERROR_NONE);
2448 /* ACK for for established connection -> tcp-established. */
2449 _(ESTABLISHED, TCP_FLAG_ACK, TCP_INPUT_NEXT_ESTABLISHED, TCP_ERROR_NONE);
2450 /* FIN for for established connection -> tcp-established. */
2451 _(ESTABLISHED, TCP_FLAG_FIN, TCP_INPUT_NEXT_ESTABLISHED, TCP_ERROR_NONE);
2452 _(ESTABLISHED, TCP_FLAG_FIN | TCP_FLAG_ACK, TCP_INPUT_NEXT_ESTABLISHED,
2453 TCP_ERROR_NONE);
Florin Corasd79b41e2017-03-04 05:37:52 -08002454 _(ESTABLISHED, TCP_FLAG_RST, TCP_INPUT_NEXT_ESTABLISHED, TCP_ERROR_NONE);
Dave Barach68b0fb02017-02-28 15:15:56 -05002455 /* ACK or FIN-ACK to our FIN */
2456 _(FIN_WAIT_1, TCP_FLAG_ACK, TCP_INPUT_NEXT_RCV_PROCESS, TCP_ERROR_NONE);
2457 _(FIN_WAIT_1, TCP_FLAG_ACK | TCP_FLAG_FIN, TCP_INPUT_NEXT_RCV_PROCESS,
2458 TCP_ERROR_NONE);
2459 /* FIN in reply to our FIN from the other side */
2460 _(FIN_WAIT_1, TCP_FLAG_FIN, TCP_INPUT_NEXT_RCV_PROCESS, TCP_ERROR_NONE);
2461 /* FIN confirming that the peer (app) has closed */
2462 _(FIN_WAIT_2, TCP_FLAG_FIN, TCP_INPUT_NEXT_RCV_PROCESS, TCP_ERROR_NONE);
2463 _(FIN_WAIT_2, TCP_FLAG_FIN | TCP_FLAG_ACK, TCP_INPUT_NEXT_RCV_PROCESS,
2464 TCP_ERROR_NONE);
2465 _(LAST_ACK, TCP_FLAG_ACK, TCP_INPUT_NEXT_RCV_PROCESS, TCP_ERROR_NONE);
2466#undef _
2467}
2468
2469clib_error_t *
2470tcp_input_init (vlib_main_t * vm)
2471{
2472 clib_error_t *error = 0;
2473 tcp_main_t *tm = vnet_get_tcp_main ();
2474
2475 if ((error = vlib_call_init_function (vm, tcp_init)))
2476 return error;
2477
2478 /* Initialize dispatch table. */
2479 tcp_dispatch_table_init (tm);
2480
2481 return error;
2482}
2483
2484VLIB_INIT_FUNCTION (tcp_input_init);
2485
2486/*
2487 * fd.io coding-style-patch-verification: ON
2488 *
2489 * Local Variables:
2490 * eval: (c-set-style "gnu")
2491 * End:
2492 */