blob: 1bcdd4cf9bdb19d9afe150c1fe675e9a4fe83edb [file] [log] [blame]
Dave Barach68b0fb02017-02-28 15:15:56 -05001/*
Florin Coras830fe732019-02-15 18:20:58 -08002 * Copyright (c) 2016-2019 Cisco and/or its affiliates.
Dave Barach68b0fb02017-02-28 15:15:56 -05003 * 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>
Simon Zhang1146ff42019-09-02 22:54:00 +080017#include <vnet/fib/ip4_fib.h>
18#include <vnet/fib/ip6_fib.h>
Dave Barach68b0fb02017-02-28 15:15:56 -050019#include <vnet/tcp/tcp.h>
Florin Coras999840c2020-03-18 20:31:34 +000020#include <vnet/tcp/tcp_inlines.h>
Dave Barach68b0fb02017-02-28 15:15:56 -050021#include <vnet/session/session.h>
22#include <math.h>
23
Filip Tehlar0c4931c2021-10-06 09:47:41 +000024static vlib_error_desc_t tcp_input_error_counters[] = {
25#define tcp_error(f, n, s, d) { #n, d, VL_COUNTER_SEVERITY_##s },
Dave Barach68b0fb02017-02-28 15:15:56 -050026#include <vnet/tcp/tcp_error.def>
27#undef tcp_error
28};
29
30/* All TCP nodes have the same outgoing arcs */
31#define foreach_tcp_state_next \
Vijayabhaskar Katamreddyce074122017-11-15 13:50:26 -080032 _ (DROP4, "ip4-drop") \
33 _ (DROP6, "ip6-drop") \
Dave Barach68b0fb02017-02-28 15:15:56 -050034 _ (TCP4_OUTPUT, "tcp4-output") \
35 _ (TCP6_OUTPUT, "tcp6-output")
36
37typedef enum _tcp_established_next
38{
39#define _(s,n) TCP_ESTABLISHED_NEXT_##s,
40 foreach_tcp_state_next
41#undef _
42 TCP_ESTABLISHED_N_NEXT,
43} tcp_established_next_t;
44
45typedef enum _tcp_rcv_process_next
46{
47#define _(s,n) TCP_RCV_PROCESS_NEXT_##s,
48 foreach_tcp_state_next
49#undef _
50 TCP_RCV_PROCESS_N_NEXT,
51} tcp_rcv_process_next_t;
52
53typedef enum _tcp_syn_sent_next
54{
55#define _(s,n) TCP_SYN_SENT_NEXT_##s,
56 foreach_tcp_state_next
57#undef _
58 TCP_SYN_SENT_N_NEXT,
59} tcp_syn_sent_next_t;
60
61typedef enum _tcp_listen_next
62{
63#define _(s,n) TCP_LISTEN_NEXT_##s,
64 foreach_tcp_state_next
65#undef _
66 TCP_LISTEN_N_NEXT,
67} tcp_listen_next_t;
68
69/* Generic, state independent indices */
70typedef enum _tcp_state_next
71{
72#define _(s,n) TCP_NEXT_##s,
73 foreach_tcp_state_next
74#undef _
75 TCP_STATE_N_NEXT,
76} tcp_state_next_t;
77
Filip Tehlarbc4dc162023-04-17 12:22:12 +020078typedef enum _tcp_input_next
79{
80 TCP_INPUT_NEXT_DROP,
81 TCP_INPUT_NEXT_LISTEN,
82 TCP_INPUT_NEXT_RCV_PROCESS,
83 TCP_INPUT_NEXT_SYN_SENT,
84 TCP_INPUT_NEXT_ESTABLISHED,
85 TCP_INPUT_NEXT_RESET,
86 TCP_INPUT_NEXT_PUNT,
87 TCP_INPUT_N_NEXT
88} tcp_input_next_t;
89
Dave Barach68b0fb02017-02-28 15:15:56 -050090#define tcp_next_output(is_ip4) (is_ip4 ? TCP_NEXT_TCP4_OUTPUT \
91 : TCP_NEXT_TCP6_OUTPUT)
92
Vijayabhaskar Katamreddyce074122017-11-15 13:50:26 -080093#define tcp_next_drop(is_ip4) (is_ip4 ? TCP_NEXT_DROP4 \
94 : TCP_NEXT_DROP6)
95
Dave Barach68b0fb02017-02-28 15:15:56 -050096/**
97 * Validate segment sequence number. As per RFC793:
98 *
99 * Segment Receive Test
100 * Length Window
101 * ------- ------- -------------------------------------------
102 * 0 0 SEG.SEQ = RCV.NXT
103 * 0 >0 RCV.NXT =< SEG.SEQ < RCV.NXT+RCV.WND
104 * >0 0 not acceptable
105 * >0 >0 RCV.NXT =< SEG.SEQ < RCV.NXT+RCV.WND
106 * or RCV.NXT =< SEG.SEQ+SEG.LEN-1 < RCV.NXT+RCV.WND
107 *
108 * This ultimately consists in checking if segment falls within the window.
109 * The one important difference compared to RFC793 is that we use rcv_las,
110 * or the rcv_nxt at last ack sent instead of rcv_nxt since that's the
111 * peer's reference when computing our receive window.
112 *
Florin Coras6792ec02017-03-13 03:49:51 -0700113 * This:
114 * seq_leq (end_seq, tc->rcv_las + tc->rcv_wnd) && seq_geq (seq, tc->rcv_las)
115 * however, is too strict when we have retransmits. Instead we just check that
116 * the seq is not beyond the right edge and that the end of the segment is not
117 * less than the left edge.
118 *
119 * N.B. rcv_nxt and rcv_wnd are both updated in this node if acks are sent, so
120 * use rcv_nxt in the right edge window test instead of rcv_las.
121 *
Dave Barach68b0fb02017-02-28 15:15:56 -0500122 */
123always_inline u8
124tcp_segment_in_rcv_wnd (tcp_connection_t * tc, u32 seq, u32 end_seq)
125{
Florin Coras6792ec02017-03-13 03:49:51 -0700126 return (seq_geq (end_seq, tc->rcv_las)
127 && seq_leq (seq, tc->rcv_nxt + tc->rcv_wnd));
Dave Barach68b0fb02017-02-28 15:15:56 -0500128}
129
Florin Corasdb84e572017-05-09 18:54:52 -0700130/**
Florin Corasc28764f2017-04-26 00:08:42 -0700131 * RFC1323: Check against wrapped sequence numbers (PAWS). If we have
132 * timestamp to echo and it's less than tsval_recent, drop segment
133 * but still send an ACK in order to retain TCP's mechanism for detecting
134 * and recovering from half-open connections
135 *
136 * Or at least that's what the theory says. It seems that this might not work
137 * very well with packet reordering and fast retransmit. XXX
138 */
Dave Barach68b0fb02017-02-28 15:15:56 -0500139always_inline int
140tcp_segment_check_paws (tcp_connection_t * tc)
141{
Florin Corasea41aac2018-12-06 17:24:59 -0800142 return tcp_opts_tstamp (&tc->rcv_opts)
Florin Coras93992a92017-05-24 18:03:56 -0700143 && timestamp_lt (tc->rcv_opts.tsval, tc->tsval_recent);
Dave Barach68b0fb02017-02-28 15:15:56 -0500144}
145
146/**
Florin Corasc28764f2017-04-26 00:08:42 -0700147 * Update tsval recent
148 */
149always_inline void
150tcp_update_timestamp (tcp_connection_t * tc, u32 seq, u32 seq_end)
151{
152 /*
153 * RFC1323: If Last.ACK.sent falls within the range of sequence numbers
154 * of an incoming segment:
155 * SEG.SEQ <= Last.ACK.sent < SEG.SEQ + SEG.LEN
156 * then the TSval from the segment is copied to TS.Recent;
157 * otherwise, the TSval is ignored.
158 */
Florin Corasf1762d62017-09-24 19:43:08 -0400159 if (tcp_opts_tstamp (&tc->rcv_opts) && seq_leq (seq, tc->rcv_las)
160 && seq_leq (tc->rcv_las, seq_end))
Florin Corasc28764f2017-04-26 00:08:42 -0700161 {
Dave Barach2c25a622017-06-26 11:35:07 -0400162 ASSERT (timestamp_leq (tc->tsval_recent, tc->rcv_opts.tsval));
Florin Coras93992a92017-05-24 18:03:56 -0700163 tc->tsval_recent = tc->rcv_opts.tsval;
Florin Coras8f10b902021-04-02 18:32:00 -0700164 tc->tsval_recent_age = tcp_time_tstamp (tc->c_thread_index);
Florin Corasc28764f2017-04-26 00:08:42 -0700165 }
166}
167
Florin Coras6939d5e2020-02-10 23:22:34 +0000168static void
169tcp_handle_rst (tcp_connection_t * tc)
170{
171 switch (tc->rst_state)
172 {
173 case TCP_STATE_SYN_RCVD:
174 /* Cleanup everything. App wasn't notified yet */
175 session_transport_delete_notify (&tc->connection);
176 tcp_connection_cleanup (tc);
177 break;
178 case TCP_STATE_SYN_SENT:
Florin Coras67fe7782020-09-02 10:51:10 -0700179 session_stream_connect_notify (&tc->connection, SESSION_E_REFUSED);
Florin Coras6939d5e2020-02-10 23:22:34 +0000180 tcp_connection_cleanup (tc);
181 break;
182 case TCP_STATE_ESTABLISHED:
183 session_transport_reset_notify (&tc->connection);
184 session_transport_closed_notify (&tc->connection);
185 break;
186 case TCP_STATE_CLOSE_WAIT:
187 case TCP_STATE_FIN_WAIT_1:
188 case TCP_STATE_FIN_WAIT_2:
189 case TCP_STATE_CLOSING:
190 case TCP_STATE_LAST_ACK:
191 session_transport_closed_notify (&tc->connection);
192 break;
193 case TCP_STATE_CLOSED:
194 case TCP_STATE_TIME_WAIT:
195 break;
196 default:
197 TCP_DBG ("reset state: %u", tc->state);
198 }
199}
200
201static void
202tcp_program_reset_ntf (tcp_worker_ctx_t * wrk, tcp_connection_t * tc)
203{
204 if (!tcp_disconnect_pending (tc))
205 {
206 tc->rst_state = tc->state;
207 vec_add1 (wrk->pending_resets, tc->c_c_index);
208 tcp_disconnect_pending_on (tc);
209 }
210}
211
212/**
213 * Handle reset packet
214 *
215 * Programs disconnect/reset notification that should be sent
216 * later by calling @ref tcp_handle_disconnects
217 */
218static void
219tcp_rcv_rst (tcp_worker_ctx_t * wrk, tcp_connection_t * tc)
220{
221 TCP_EVT (TCP_EVT_RST_RCVD, tc);
222 switch (tc->state)
223 {
224 case TCP_STATE_SYN_RCVD:
225 tcp_program_reset_ntf (wrk, tc);
226 tcp_connection_set_state (tc, TCP_STATE_CLOSED);
227 break;
228 case TCP_STATE_SYN_SENT:
Florin Coras96acc9b2020-02-18 22:51:26 +0000229 /* Do not program ntf because the connection is half-open */
Ivan Shvedunove52eafd2020-07-29 19:15:41 +0300230 tc->rst_state = tc->state;
Florin Coras96acc9b2020-02-18 22:51:26 +0000231 tcp_handle_rst (tc);
Florin Coras6939d5e2020-02-10 23:22:34 +0000232 break;
233 case TCP_STATE_ESTABLISHED:
234 tcp_connection_timers_reset (tc);
Florin Coras6939d5e2020-02-10 23:22:34 +0000235 tcp_cong_recovery_off (tc);
236 tcp_program_reset_ntf (wrk, tc);
237 tcp_connection_set_state (tc, TCP_STATE_CLOSED);
Florin Coras7a3a8662020-02-22 02:27:21 +0000238 tcp_program_cleanup (wrk, tc);
Florin Coras6939d5e2020-02-10 23:22:34 +0000239 break;
240 case TCP_STATE_CLOSE_WAIT:
241 case TCP_STATE_FIN_WAIT_1:
242 case TCP_STATE_FIN_WAIT_2:
243 case TCP_STATE_CLOSING:
244 case TCP_STATE_LAST_ACK:
245 tcp_connection_timers_reset (tc);
Florin Coras6939d5e2020-02-10 23:22:34 +0000246 tcp_cong_recovery_off (tc);
247 tcp_program_reset_ntf (wrk, tc);
248 /* Make sure we mark the session as closed. In some states we may
249 * be still trying to send data */
250 tcp_connection_set_state (tc, TCP_STATE_CLOSED);
Florin Coras7a3a8662020-02-22 02:27:21 +0000251 tcp_program_cleanup (wrk, tc);
Florin Coras6939d5e2020-02-10 23:22:34 +0000252 break;
253 case TCP_STATE_CLOSED:
254 case TCP_STATE_TIME_WAIT:
255 break;
256 default:
257 TCP_DBG ("reset state: %u", tc->state);
258 }
259}
260
Florin Corasc28764f2017-04-26 00:08:42 -0700261/**
Dave Barach68b0fb02017-02-28 15:15:56 -0500262 * Validate incoming segment as per RFC793 p. 69 and RFC1323 p. 19
263 *
264 * It first verifies if segment has a wrapped sequence number (PAWS) and then
265 * does the processing associated to the first four steps (ignoring security
266 * and precedence): sequence number, rst bit and syn bit checks.
267 *
268 * @return 0 if segments passes validation.
269 */
270static int
Florin Coras7ac053b2018-11-05 15:57:21 -0800271tcp_segment_validate (tcp_worker_ctx_t * wrk, tcp_connection_t * tc0,
272 vlib_buffer_t * b0, tcp_header_t * th0, u32 * error0)
Dave Barach68b0fb02017-02-28 15:15:56 -0500273{
Florin Corasca1c8f32018-05-23 21:01:30 -0700274 /* We could get a burst of RSTs interleaved with acks */
275 if (PREDICT_FALSE (tc0->state == TCP_STATE_CLOSED))
276 {
277 tcp_send_reset (tc0);
278 *error0 = TCP_ERROR_CONNECTION_CLOSED;
Florin Coras7ac053b2018-11-05 15:57:21 -0800279 goto error;
Florin Corasca1c8f32018-05-23 21:01:30 -0700280 }
281
Dave Barach68b0fb02017-02-28 15:15:56 -0500282 if (PREDICT_FALSE (!tcp_ack (th0) && !tcp_rst (th0) && !tcp_syn (th0)))
Florin Coras00cd22d2018-04-18 13:20:18 -0700283 {
284 *error0 = TCP_ERROR_SEGMENT_INVALID;
Florin Coras7ac053b2018-11-05 15:57:21 -0800285 goto error;
Florin Coras00cd22d2018-04-18 13:20:18 -0700286 }
Dave Barach68b0fb02017-02-28 15:15:56 -0500287
Florin Coras80231112018-12-05 15:59:31 -0800288 if (PREDICT_FALSE (tcp_options_parse (th0, &tc0->rcv_opts, 0)))
Florin Corasdb84e572017-05-09 18:54:52 -0700289 {
Florin Coras00cd22d2018-04-18 13:20:18 -0700290 *error0 = TCP_ERROR_OPTIONS;
Florin Coras7ac053b2018-11-05 15:57:21 -0800291 goto error;
Florin Corasdb84e572017-05-09 18:54:52 -0700292 }
Dave Barach68b0fb02017-02-28 15:15:56 -0500293
Florin Coras00cd22d2018-04-18 13:20:18 -0700294 if (PREDICT_FALSE (tcp_segment_check_paws (tc0)))
Dave Barach68b0fb02017-02-28 15:15:56 -0500295 {
Florin Coras00cd22d2018-04-18 13:20:18 -0700296 *error0 = TCP_ERROR_PAWS;
Florin Corasa436a422019-08-20 07:09:31 -0700297 TCP_EVT (TCP_EVT_PAWS_FAIL, tc0, vnet_buffer (b0)->tcp.seq_number,
298 vnet_buffer (b0)->tcp.seq_end);
Dave Barach68b0fb02017-02-28 15:15:56 -0500299
300 /* If it just so happens that a segment updates tsval_recent for a
301 * segment over 24 days old, invalidate tsval_recent. */
302 if (timestamp_lt (tc0->tsval_recent_age + TCP_PAWS_IDLE,
Florin Coras8f10b902021-04-02 18:32:00 -0700303 tcp_time_tstamp (tc0->c_thread_index)))
Dave Barach68b0fb02017-02-28 15:15:56 -0500304 {
Florin Corasea41aac2018-12-06 17:24:59 -0800305 tc0->tsval_recent = tc0->rcv_opts.tsval;
Florin Coras91236ce2018-12-16 11:41:45 -0800306 clib_warning ("paws failed: 24-day old segment");
Dave Barach68b0fb02017-02-28 15:15:56 -0500307 }
Florin Coras91236ce2018-12-16 11:41:45 -0800308 /* Drop after ack if not rst. Resets can fail paws check as per
309 * RFC 7323 sec. 5.2: When an <RST> segment is received, it MUST NOT
310 * be subjected to the PAWS check by verifying an acceptable value in
311 * SEG.TSval */
312 else if (!tcp_rst (th0))
Dave Barach68b0fb02017-02-28 15:15:56 -0500313 {
Florin Coras26dd6de2019-07-23 23:54:47 -0700314 tcp_program_ack (tc0);
Florin Corasa436a422019-08-20 07:09:31 -0700315 TCP_EVT (TCP_EVT_DUPACK_SENT, tc0, vnet_buffer (b0)->tcp);
Florin Coras91236ce2018-12-16 11:41:45 -0800316 goto error;
Dave Barach68b0fb02017-02-28 15:15:56 -0500317 }
318 }
319
320 /* 1st: check sequence number */
321 if (!tcp_segment_in_rcv_wnd (tc0, vnet_buffer (b0)->tcp.seq_number,
322 vnet_buffer (b0)->tcp.seq_end))
323 {
Florin Coras830fe732019-02-15 18:20:58 -0800324 /* SYN/SYN-ACK retransmit */
325 if (tcp_syn (th0)
326 && vnet_buffer (b0)->tcp.seq_number == tc0->rcv_nxt - 1)
327 {
328 tcp_options_parse (th0, &tc0->rcv_opts, 1);
329 if (tc0->state == TCP_STATE_SYN_RCVD)
330 {
331 tcp_send_synack (tc0);
Florin Corasa436a422019-08-20 07:09:31 -0700332 TCP_EVT (TCP_EVT_SYN_RCVD, tc0, 0);
Florin Coras830fe732019-02-15 18:20:58 -0800333 *error0 = TCP_ERROR_SYNS_RCVD;
334 }
335 else
336 {
Florin Coras26dd6de2019-07-23 23:54:47 -0700337 tcp_program_ack (tc0);
Florin Corasa436a422019-08-20 07:09:31 -0700338 TCP_EVT (TCP_EVT_SYNACK_RCVD, tc0);
Florin Coras830fe732019-02-15 18:20:58 -0800339 *error0 = TCP_ERROR_SYN_ACKS_RCVD;
340 }
341 goto error;
342 }
343
Florin Coras6792ec02017-03-13 03:49:51 -0700344 /* If our window is 0 and the packet is in sequence, let it pass
Florin Coras00cd22d2018-04-18 13:20:18 -0700345 * through for ack processing. It should be dropped later. */
Florin Coras7e74bf32019-03-06 16:51:58 -0800346 if (tc0->rcv_wnd < tc0->snd_mss
Florin Coras222e1f412019-02-16 20:47:32 -0800347 && tc0->rcv_nxt == vnet_buffer (b0)->tcp.seq_number)
348 goto check_reset;
349
350 /* If we entered recovery and peer did so as well, there's a chance that
351 * dup acks won't be acceptable on either end because seq_end may be less
352 * than rcv_las. This can happen if acks are lost in both directions. */
353 if (tcp_in_recovery (tc0)
354 && seq_geq (vnet_buffer (b0)->tcp.seq_number,
355 tc0->rcv_las - tc0->rcv_wnd)
356 && seq_leq (vnet_buffer (b0)->tcp.seq_end,
357 tc0->rcv_nxt + tc0->rcv_wnd))
358 goto check_reset;
359
360 *error0 = TCP_ERROR_RCV_WND;
361
Florin Corasa495a3e2019-08-29 18:33:24 -0700362 /* If we advertised a zero rcv_wnd and the segment is in the past or the
363 * next one that we expect, it is probably a window probe */
364 if ((tc0->flags & TCP_CONN_ZERO_RWND_SENT)
365 && seq_lt (vnet_buffer (b0)->tcp.seq_end,
366 tc0->rcv_las + tc0->rcv_opts.mss))
367 *error0 = TCP_ERROR_ZERO_RWND;
368
Florin Corasedfe0ee2019-07-29 18:13:25 -0700369 tc0->errors.below_data_wnd += seq_lt (vnet_buffer (b0)->tcp.seq_end,
370 tc0->rcv_las);
371
Florin Coras222e1f412019-02-16 20:47:32 -0800372 /* If not RST, send dup ack */
373 if (!tcp_rst (th0))
Florin Coras6792ec02017-03-13 03:49:51 -0700374 {
Florin Coras26dd6de2019-07-23 23:54:47 -0700375 tcp_program_dupack (tc0);
Florin Corasa436a422019-08-20 07:09:31 -0700376 TCP_EVT (TCP_EVT_DUPACK_SENT, tc0, vnet_buffer (b0)->tcp);
Florin Coras6792ec02017-03-13 03:49:51 -0700377 }
Florin Coras222e1f412019-02-16 20:47:32 -0800378 goto error;
379
380 check_reset:
381 ;
Dave Barach68b0fb02017-02-28 15:15:56 -0500382 }
383
384 /* 2nd: check the RST bit */
Florin Coras00cd22d2018-04-18 13:20:18 -0700385 if (PREDICT_FALSE (tcp_rst (th0)))
Dave Barach68b0fb02017-02-28 15:15:56 -0500386 {
Florin Coras6939d5e2020-02-10 23:22:34 +0000387 tcp_rcv_rst (wrk, tc0);
Florin Coras00cd22d2018-04-18 13:20:18 -0700388 *error0 = TCP_ERROR_RST_RCVD;
Florin Coras7ac053b2018-11-05 15:57:21 -0800389 goto error;
Dave Barach68b0fb02017-02-28 15:15:56 -0500390 }
391
392 /* 3rd: check security and precedence (skip) */
393
Florin Coras830fe732019-02-15 18:20:58 -0800394 /* 4th: check the SYN bit (in window) */
Florin Coras00cd22d2018-04-18 13:20:18 -0700395 if (PREDICT_FALSE (tcp_syn (th0)))
Dave Barach68b0fb02017-02-28 15:15:56 -0500396 {
Florin Corasd567a8d2019-06-07 12:38:55 -0700397 /* As per RFC5961 send challenge ack instead of reset */
Florin Coras26dd6de2019-07-23 23:54:47 -0700398 tcp_program_ack (tc0);
Florin Coras830fe732019-02-15 18:20:58 -0800399 *error0 = TCP_ERROR_SPURIOUS_SYN;
Florin Coras00cd22d2018-04-18 13:20:18 -0700400 goto error;
Dave Barach68b0fb02017-02-28 15:15:56 -0500401 }
402
Florin Corasc28764f2017-04-26 00:08:42 -0700403 /* If segment in window, save timestamp */
404 tcp_update_timestamp (tc0, vnet_buffer (b0)->tcp.seq_number,
405 vnet_buffer (b0)->tcp.seq_end);
Dave Barach68b0fb02017-02-28 15:15:56 -0500406 return 0;
Florin Coras00cd22d2018-04-18 13:20:18 -0700407
Florin Coras00cd22d2018-04-18 13:20:18 -0700408error:
Florin Coras00cd22d2018-04-18 13:20:18 -0700409 return -1;
Dave Barach68b0fb02017-02-28 15:15:56 -0500410}
411
412always_inline int
Florin Corasf65074e2019-03-31 17:17:11 -0700413tcp_rcv_ack_no_cc (tcp_connection_t * tc, vlib_buffer_t * b, u32 * error)
Dave Barach68b0fb02017-02-28 15:15:56 -0500414{
415 /* SND.UNA =< SEG.ACK =< SND.NXT */
Florin Corasf65074e2019-03-31 17:17:11 -0700416 if (!(seq_leq (tc->snd_una, vnet_buffer (b)->tcp.ack_number)
417 && seq_leq (vnet_buffer (b)->tcp.ack_number, tc->snd_nxt)))
418 {
Florin Coras55e556c2020-10-23 10:45:48 -0700419 if (seq_leq (vnet_buffer (b)->tcp.ack_number, tc->snd_nxt)
Florin Coras282a3cb2019-04-02 21:43:38 -0700420 && seq_gt (vnet_buffer (b)->tcp.ack_number, tc->snd_una))
Florin Corasf65074e2019-03-31 17:17:11 -0700421 {
422 tc->snd_nxt = vnet_buffer (b)->tcp.ack_number;
423 goto acceptable;
424 }
425 *error = TCP_ERROR_ACK_INVALID;
426 return -1;
427 }
428
429acceptable:
430 tc->bytes_acked = vnet_buffer (b)->tcp.ack_number - tc->snd_una;
431 tc->snd_una = vnet_buffer (b)->tcp.ack_number;
432 *error = TCP_ERROR_ACK_OK;
433 return 0;
Dave Barach68b0fb02017-02-28 15:15:56 -0500434}
435
436/**
437 * Compute smoothed RTT as per VJ's '88 SIGCOMM and RFC6298
438 *
Florin Coraseedc74b2020-07-31 12:32:40 -0700439 * Note that although in the original article srtt and rttvar are scaled
Dave Barach68b0fb02017-02-28 15:15:56 -0500440 * to minimize round-off errors, here we don't. Instead, we rely on
441 * better precision time measurements.
Florin Coraseedc74b2020-07-31 12:32:40 -0700442 *
443 * A known limitation of the algorithm is that a drop in rtt results in a
444 * rttvar increase and bigger RTO.
445 *
446 * mrtt must be provided in @ref TCP_TICK multiples, i.e., in us. Note that
447 * timestamps are measured as ms ticks so they must be converted before
448 * calling this function.
Dave Barach68b0fb02017-02-28 15:15:56 -0500449 */
450static void
451tcp_estimate_rtt (tcp_connection_t * tc, u32 mrtt)
452{
Florin Corasf03a59a2017-06-09 21:07:32 -0700453 int err, diff;
Dave Barach68b0fb02017-02-28 15:15:56 -0500454
Florin Coraseedc74b2020-07-31 12:32:40 -0700455 err = mrtt - tc->srtt;
456 tc->srtt = clib_max ((int) tc->srtt + (err >> 3), 1);
457 diff = (clib_abs (err) - (int) tc->rttvar) >> 2;
458 tc->rttvar = clib_max ((int) tc->rttvar + diff, 1);
Dave Barach68b0fb02017-02-28 15:15:56 -0500459}
460
Florin Coras8ddd19b2020-06-07 20:06:56 +0000461static inline void
462tcp_estimate_rtt_us (tcp_connection_t * tc, f64 mrtt)
463{
464 tc->mrtt_us = tc->mrtt_us + (mrtt - tc->mrtt_us) * 0.125;
465}
466
Florin Corasf1762d62017-09-24 19:43:08 -0400467/**
Florin Coras8ddd19b2020-06-07 20:06:56 +0000468 * Update rtt estimate
Dave Barach68b0fb02017-02-28 15:15:56 -0500469 *
Florin Coras8ddd19b2020-06-07 20:06:56 +0000470 * We have potentially three sources of rtt measurements:
Dave Barach68b0fb02017-02-28 15:15:56 -0500471 *
Florin Coras8ddd19b2020-06-07 20:06:56 +0000472 * TSOPT difference between current and echoed timestamp. It has ms
473 * precision and can be computed per ack
474 * ACK timing one sequence number is tracked per rtt with us (micro second)
475 * precision.
476 * rate sample if enabled, all outstanding bytes are tracked with us
477 * precision. Every ack and sack are a rtt sample
Florin Corasf1762d62017-09-24 19:43:08 -0400478 *
Florin Coras8ddd19b2020-06-07 20:06:56 +0000479 * Middle boxes are known to fiddle with TCP options so we give higher
480 * priority to ACK timing.
481 *
482 * For now, rate sample rtts are only used under congestion.
Dave Barach68b0fb02017-02-28 15:15:56 -0500483 */
484static int
Florin Coras1dbda642019-09-11 13:42:57 -0700485tcp_update_rtt (tcp_connection_t * tc, tcp_rate_sample_t * rs, u32 ack)
Dave Barach68b0fb02017-02-28 15:15:56 -0500486{
487 u32 mrtt = 0;
488
489 /* Karn's rule, part 1. Don't use retransmitted segments to estimate
490 * RTT because they're ambiguous. */
Florin Coras1dbda642019-09-11 13:42:57 -0700491 if (tcp_in_cong_recovery (tc))
Florin Coras3ec66b02018-08-23 16:27:05 -0700492 {
Florin Coras1dbda642019-09-11 13:42:57 -0700493 /* Accept rtt estimates for samples that have not been retransmitted */
Florin Coras8ddd19b2020-06-07 20:06:56 +0000494 if (!(tc->cfg_flags & TCP_CFG_F_RATE_SAMPLE)
495 || (rs->flags & TCP_BTS_IS_RXT))
496 goto done;
497 if (rs->rtt_time)
498 tcp_estimate_rtt_us (tc, rs->rtt_time);
499 mrtt = rs->rtt_time * THZ;
500 goto estimate_rtt;
Florin Coras3ec66b02018-08-23 16:27:05 -0700501 }
Florin Corasf1762d62017-09-24 19:43:08 -0400502
503 if (tc->rtt_ts && seq_geq (ack, tc->rtt_seq))
Dave Barach68b0fb02017-02-28 15:15:56 -0500504 {
Florin Corasefefc6b2018-11-07 12:49:19 -0800505 f64 sample = tcp_time_now_us (tc->c_thread_index) - tc->rtt_ts;
Florin Coras8ddd19b2020-06-07 20:06:56 +0000506 tcp_estimate_rtt_us (tc, sample);
Florin Corasefefc6b2018-11-07 12:49:19 -0800507 mrtt = clib_max ((u32) (sample * THZ), 1);
508 /* Allow measuring of a new RTT */
509 tc->rtt_ts = 0;
Dave Barach68b0fb02017-02-28 15:15:56 -0500510 }
Dave Barach68b0fb02017-02-28 15:15:56 -0500511 /* As per RFC7323 TSecr can be used for RTTM only if the segment advances
512 * snd_una, i.e., the left side of the send window:
Florin Corasf1762d62017-09-24 19:43:08 -0400513 * seq_lt (tc->snd_una, ack). This is a condition for calling update_rtt */
514 else if (tcp_opts_tstamp (&tc->rcv_opts) && tc->rcv_opts.tsecr)
Dave Barach68b0fb02017-02-28 15:15:56 -0500515 {
Florin Coraseedc74b2020-07-31 12:32:40 -0700516 mrtt = clib_max (tcp_tstamp (tc) - tc->rcv_opts.tsecr, 1);
517 mrtt *= TCP_TSTP_TO_HZ;
Dave Barach68b0fb02017-02-28 15:15:56 -0500518 }
519
Florin Coras1dbda642019-09-11 13:42:57 -0700520estimate_rtt:
521
Florin Corasf1762d62017-09-24 19:43:08 -0400522 /* Ignore dubious measurements */
523 if (mrtt == 0 || mrtt > TCP_RTT_MAX)
524 goto done;
525
526 tcp_estimate_rtt (tc, mrtt);
527
528done:
529
Florin Corasf1762d62017-09-24 19:43:08 -0400530 /* If we got here something must've been ACKed so make sure boff is 0,
Florin Coras3ec66b02018-08-23 16:27:05 -0700531 * even if mrtt is not valid since we update the rto lower */
Florin Corasf1762d62017-09-24 19:43:08 -0400532 tc->rto_boff = 0;
Florin Coras93992a92017-05-24 18:03:56 -0700533 tcp_update_rto (tc);
Dave Barach68b0fb02017-02-28 15:15:56 -0500534
Florin Coras3af90fc2017-05-03 21:09:42 -0700535 return 0;
Dave Barach68b0fb02017-02-28 15:15:56 -0500536}
537
Florin Corasefefc6b2018-11-07 12:49:19 -0800538static void
539tcp_estimate_initial_rtt (tcp_connection_t * tc)
540{
541 u8 thread_index = vlib_num_workers ()? 1 : 0;
542 int mrtt;
543
544 if (tc->rtt_ts)
545 {
546 tc->mrtt_us = tcp_time_now_us (thread_index) - tc->rtt_ts;
Florin Coras222e1f412019-02-16 20:47:32 -0800547 tc->mrtt_us = clib_max (tc->mrtt_us, 0.0001);
Florin Corasefefc6b2018-11-07 12:49:19 -0800548 mrtt = clib_max ((u32) (tc->mrtt_us * THZ), 1);
549 tc->rtt_ts = 0;
550 }
551 else
552 {
Florin Coraseedc74b2020-07-31 12:32:40 -0700553 mrtt = tcp_tstamp (tc) - tc->rcv_opts.tsecr;
554 mrtt = clib_max (mrtt, 1) * TCP_TSTP_TO_HZ;
Florin Coras222e1f412019-02-16 20:47:32 -0800555 /* Due to retransmits we don't know the initial mrtt */
556 if (tc->rto_boff && mrtt > 1 * THZ)
557 mrtt = 1 * THZ;
Florin Corasefefc6b2018-11-07 12:49:19 -0800558 tc->mrtt_us = (f64) mrtt *TCP_TICK;
Florin Corasefefc6b2018-11-07 12:49:19 -0800559 }
560
561 if (mrtt > 0 && mrtt < TCP_RTT_MAX)
Florin Coraseedc74b2020-07-31 12:32:40 -0700562 {
563 /* First measurement as per RFC 6298 */
564 tc->srtt = mrtt;
565 tc->rttvar = mrtt >> 1;
566 }
Florin Coras54ddf432018-12-21 13:54:09 -0800567 tcp_update_rto (tc);
Florin Corasefefc6b2018-11-07 12:49:19 -0800568}
569
Dave Barach68b0fb02017-02-28 15:15:56 -0500570/**
Florin Coras9ece3c02018-11-05 11:06:53 -0800571 * Dequeue bytes for connections that have received acks in last burst
Dave Barach68b0fb02017-02-28 15:15:56 -0500572 */
573static void
Florin Coras9ece3c02018-11-05 11:06:53 -0800574tcp_handle_postponed_dequeues (tcp_worker_ctx_t * wrk)
Dave Barach68b0fb02017-02-28 15:15:56 -0500575{
Florin Coras9ece3c02018-11-05 11:06:53 -0800576 u32 thread_index = wrk->vm->thread_index;
577 u32 *pending_deq_acked;
578 tcp_connection_t *tc;
579 int i;
Florin Coras93992a92017-05-24 18:03:56 -0700580
Florin Coras9ece3c02018-11-05 11:06:53 -0800581 if (!vec_len (wrk->pending_deq_acked))
582 return;
Dave Barach68b0fb02017-02-28 15:15:56 -0500583
Florin Coras9ece3c02018-11-05 11:06:53 -0800584 pending_deq_acked = wrk->pending_deq_acked;
585 for (i = 0; i < vec_len (pending_deq_acked); i++)
586 {
587 tc = tcp_connection_get (pending_deq_acked[i], thread_index);
588 tc->flags &= ~TCP_CONN_DEQ_PENDING;
Florin Coras93992a92017-05-24 18:03:56 -0700589
Florin Coras11e9e352019-11-13 19:09:47 -0800590 if (PREDICT_FALSE (!tc->burst_acked))
591 continue;
592
593 /* Dequeue the newly ACKed bytes */
594 session_tx_fifo_dequeue_drop (&tc->connection, tc->burst_acked);
Florin Coras55e556c2020-10-23 10:45:48 -0700595 tcp_validate_txf_size (tc, tc->snd_nxt - tc->snd_una);
Florin Coras11e9e352019-11-13 19:09:47 -0800596
Florin Coras6080e0d2020-03-13 20:39:43 +0000597 if (tcp_is_descheduled (tc))
598 tcp_reschedule (tc);
599
Florin Coras11e9e352019-11-13 19:09:47 -0800600 /* If everything has been acked, stop retransmit timer
601 * otherwise update. */
Florin Coras0765d972020-03-18 21:26:41 +0000602 tcp_retransmit_timer_update (&wrk->timer_wheel, tc);
Florin Corasb3dce892019-10-30 09:22:14 -0700603
Florin Coras11e9e352019-11-13 19:09:47 -0800604 /* Update pacer based on our new cwnd estimate */
605 tcp_connection_tx_pacer_update (tc);
606
Florin Corasb3dce892019-10-30 09:22:14 -0700607 tc->burst_acked = 0;
Florin Coras9ece3c02018-11-05 11:06:53 -0800608 }
Damjan Marion8bea5892022-04-04 22:40:45 +0200609 vec_set_len (wrk->pending_deq_acked, 0);
Florin Coras9ece3c02018-11-05 11:06:53 -0800610}
611
612static void
613tcp_program_dequeue (tcp_worker_ctx_t * wrk, tcp_connection_t * tc)
614{
615 if (!(tc->flags & TCP_CONN_DEQ_PENDING))
616 {
617 vec_add1 (wrk->pending_deq_acked, tc->c_c_index);
618 tc->flags |= TCP_CONN_DEQ_PENDING;
619 }
Florin Coras558e3e02019-09-06 12:56:58 -0700620 tc->burst_acked += tc->bytes_acked;
Dave Barach68b0fb02017-02-28 15:15:56 -0500621}
622
Florin Coras93992a92017-05-24 18:03:56 -0700623/**
624 * Try to update snd_wnd based on feedback received from peer.
Dave Barach68b0fb02017-02-28 15:15:56 -0500625 *
Florin Coras93992a92017-05-24 18:03:56 -0700626 * If successful, and new window is 'effectively' 0, activate persist
627 * timer.
628 */
Dave Barach68b0fb02017-02-28 15:15:56 -0500629static void
630tcp_update_snd_wnd (tcp_connection_t * tc, u32 seq, u32 ack, u32 snd_wnd)
631{
Florin Coras93992a92017-05-24 18:03:56 -0700632 /* If (SND.WL1 < SEG.SEQ or (SND.WL1 = SEG.SEQ and SND.WL2 =< SEG.ACK)), set
633 * SND.WND <- SEG.WND, set SND.WL1 <- SEG.SEQ, and set SND.WL2 <- SEG.ACK */
Florin Coras6792ec02017-03-13 03:49:51 -0700634 if (seq_lt (tc->snd_wl1, seq)
635 || (tc->snd_wl1 == seq && seq_leq (tc->snd_wl2, ack)))
Dave Barach68b0fb02017-02-28 15:15:56 -0500636 {
637 tc->snd_wnd = snd_wnd;
638 tc->snd_wl1 = seq;
639 tc->snd_wl2 = ack;
Florin Corasa436a422019-08-20 07:09:31 -0700640 TCP_EVT (TCP_EVT_SND_WND, tc);
Florin Coras3e350af2017-03-30 02:54:28 -0700641
Florin Coras9ece3c02018-11-05 11:06:53 -0800642 if (PREDICT_FALSE (tc->snd_wnd < tc->snd_mss))
Florin Corasbb292f42017-05-19 09:49:19 -0700643 {
Florin Corasc6a2f1f2022-03-14 14:23:39 -0700644 if (!tcp_timer_is_active (tc, TCP_TIMER_RETRANSMIT))
Florin Coras0765d972020-03-18 21:26:41 +0000645 {
646 tcp_worker_ctx_t *wrk = tcp_get_worker (tc->c_thread_index);
Florin Corasc6a2f1f2022-03-14 14:23:39 -0700647
648 /* Set persist timer if we just got 0 wnd. If already set,
649 * update it because some data sent with snd_wnd < snd_mss was
650 * acked. */
651 if (tcp_timer_is_active (tc, TCP_TIMER_PERSIST))
652 tcp_persist_timer_reset (&wrk->timer_wheel, tc);
Florin Coras0765d972020-03-18 21:26:41 +0000653 tcp_persist_timer_set (&wrk->timer_wheel, tc);
654 }
Florin Corasbb292f42017-05-19 09:49:19 -0700655 }
Florin Coras3e350af2017-03-30 02:54:28 -0700656 else
Florin Coras93992a92017-05-24 18:03:56 -0700657 {
Florin Coras70f879d2020-03-13 17:54:42 +0000658 if (PREDICT_FALSE (tcp_timer_is_active (tc, TCP_TIMER_PERSIST)))
Florin Coras0765d972020-03-18 21:26:41 +0000659 {
660 tcp_worker_ctx_t *wrk = tcp_get_worker (tc->c_thread_index);
661 tcp_persist_timer_reset (&wrk->timer_wheel, tc);
662 }
Florin Coras70f879d2020-03-13 17:54:42 +0000663
Florin Coras6080e0d2020-03-13 20:39:43 +0000664 if (PREDICT_FALSE (tcp_is_descheduled (tc)))
665 tcp_reschedule (tc);
666
Florin Coras9ece3c02018-11-05 11:06:53 -0800667 if (PREDICT_FALSE (!tcp_in_recovery (tc) && tc->rto_boff > 0))
Florin Coras93992a92017-05-24 18:03:56 -0700668 {
669 tc->rto_boff = 0;
670 tcp_update_rto (tc);
671 }
672 }
Dave Barach68b0fb02017-02-28 15:15:56 -0500673 }
674}
675
Florin Corasd2aab832018-05-22 11:39:59 -0700676/**
677 * Init loss recovery/fast recovery.
678 *
679 * Triggered by dup acks as opposed to timer timeout. Note that cwnd is
680 * updated in @ref tcp_cc_handle_event after fast retransmit
681 */
Florin Coras36ebcff2019-09-12 18:36:44 -0700682static void
Florin Coras93992a92017-05-24 18:03:56 -0700683tcp_cc_init_congestion (tcp_connection_t * tc)
Dave Barach68b0fb02017-02-28 15:15:56 -0500684{
Florin Coras93992a92017-05-24 18:03:56 -0700685 tcp_fastrecovery_on (tc);
Florin Coras47596832019-03-12 18:58:54 -0700686 tc->snd_congestion = tc->snd_nxt;
Florin Coras62166002018-04-18 16:40:55 -0700687 tc->cwnd_acc_bytes = 0;
Florin Corasbf4d5ce2018-10-19 16:26:24 -0700688 tc->snd_rxt_bytes = 0;
Florin Coras36ebcff2019-09-12 18:36:44 -0700689 tc->rxt_delivered = 0;
690 tc->prr_delivered = 0;
Florin Corasbe237bf2019-09-27 08:16:40 -0700691 tc->prr_start = tc->snd_una;
Florin Corasbf4d5ce2018-10-19 16:26:24 -0700692 tc->prev_ssthresh = tc->ssthresh;
693 tc->prev_cwnd = tc->cwnd;
Florin Coras36ebcff2019-09-12 18:36:44 -0700694
Florin Corasbe237bf2019-09-27 08:16:40 -0700695 tc->snd_rxt_ts = tcp_tstamp (tc);
Florin Coras36ebcff2019-09-12 18:36:44 -0700696 tcp_cc_congestion (tc);
697
698 /* Post retransmit update cwnd to ssthresh and account for the
699 * three segments that have left the network and should've been
700 * buffered at the receiver XXX */
701 if (!tcp_opts_sack_permitted (&tc->rcv_opts))
Florin Corascc4d6d02020-07-29 23:03:39 -0700702 tc->cwnd += TCP_DUPACK_THRESHOLD * tc->snd_mss;
Florin Coras36ebcff2019-09-12 18:36:44 -0700703
Florin Corasedfe0ee2019-07-29 18:13:25 -0700704 tc->fr_occurences += 1;
Florin Corasa436a422019-08-20 07:09:31 -0700705 TCP_EVT (TCP_EVT_CC_EVT, tc, 4);
Dave Barach68b0fb02017-02-28 15:15:56 -0500706}
Dave Barach68b0fb02017-02-28 15:15:56 -0500707
708static void
Florin Coras93992a92017-05-24 18:03:56 -0700709tcp_cc_congestion_undo (tcp_connection_t * tc)
Dave Barach68b0fb02017-02-28 15:15:56 -0500710{
Florin Coras93992a92017-05-24 18:03:56 -0700711 tc->cwnd = tc->prev_cwnd;
712 tc->ssthresh = tc->prev_ssthresh;
Florin Coraseff6b822019-07-03 19:51:02 -0700713 tcp_cc_undo_recovery (tc);
Florin Coras93992a92017-05-24 18:03:56 -0700714 ASSERT (tc->rto_boff == 0);
Florin Corasa436a422019-08-20 07:09:31 -0700715 TCP_EVT (TCP_EVT_CC_EVT, tc, 5);
Florin Coras93992a92017-05-24 18:03:56 -0700716}
Dave Barach68b0fb02017-02-28 15:15:56 -0500717
Florin Corasbf4d5ce2018-10-19 16:26:24 -0700718static inline u8
719tcp_cc_is_spurious_timeout_rxt (tcp_connection_t * tc)
Florin Coras93992a92017-05-24 18:03:56 -0700720{
Florin Corasf1762d62017-09-24 19:43:08 -0400721 return (tcp_in_recovery (tc) && tc->rto_boff == 1
Dave Barach2c25a622017-06-26 11:35:07 -0400722 && tc->snd_rxt_ts
Florin Coras93992a92017-05-24 18:03:56 -0700723 && tcp_opts_tstamp (&tc->rcv_opts)
724 && timestamp_lt (tc->rcv_opts.tsecr, tc->snd_rxt_ts));
725}
726
Florin Corasbf4d5ce2018-10-19 16:26:24 -0700727static inline u8
Florin Corasbf4d5ce2018-10-19 16:26:24 -0700728tcp_cc_is_spurious_retransmit (tcp_connection_t * tc)
729{
Florin Coras36ebcff2019-09-12 18:36:44 -0700730 return (tcp_cc_is_spurious_timeout_rxt (tc));
731}
732
733static inline u8
Florin Corasbe237bf2019-09-27 08:16:40 -0700734tcp_should_fastrecover (tcp_connection_t * tc, u8 has_sack)
Florin Coras36ebcff2019-09-12 18:36:44 -0700735{
Florin Corasbe237bf2019-09-27 08:16:40 -0700736 if (!has_sack)
737 {
738 /* If of of the two conditions lower hold, reset dupacks because
739 * we're probably after timeout (RFC6582 heuristics).
740 * If Cumulative ack does not cover more than congestion threshold,
741 * and:
742 * 1) The following doesn't hold: The congestion window is greater
743 * than SMSS bytes and the difference between highest_ack
744 * and prev_highest_ack is at most 4*SMSS bytes
745 * 2) Echoed timestamp in the last non-dup ack does not equal the
746 * stored timestamp
747 */
748 if (seq_leq (tc->snd_una, tc->snd_congestion)
749 && ((!(tc->cwnd > tc->snd_mss
750 && tc->bytes_acked <= 4 * tc->snd_mss))
751 || (tc->rcv_opts.tsecr != tc->tsecr_last_ack)))
752 {
753 tc->rcv_dupacks = 0;
754 return 0;
755 }
756 }
Florin Corascc4d6d02020-07-29 23:03:39 -0700757 return tc->sack_sb.lost_bytes || tc->rcv_dupacks >= tc->sack_sb.reorder;
Florin Corasbf4d5ce2018-10-19 16:26:24 -0700758}
759
Florin Coras0dbd5172018-06-25 16:19:34 -0700760static int
Florin Corasb56f24b2022-01-05 12:55:35 -0800761tcp_cc_try_recover (tcp_connection_t *tc)
Florin Coras93992a92017-05-24 18:03:56 -0700762{
Florin Coras321cfa52019-09-12 18:49:44 -0700763 sack_scoreboard_hole_t *hole;
Florin Coras36ebcff2019-09-12 18:36:44 -0700764 u8 is_spurious = 0;
Florin Coras321cfa52019-09-12 18:49:44 -0700765
Florin Coras93992a92017-05-24 18:03:56 -0700766 ASSERT (tcp_in_cong_recovery (tc));
Florin Coras321cfa52019-09-12 18:49:44 -0700767
Florin Coras36ebcff2019-09-12 18:36:44 -0700768 if (tcp_cc_is_spurious_retransmit (tc))
769 {
770 tcp_cc_congestion_undo (tc);
771 is_spurious = 1;
772 }
773
Florin Corasc31dc312019-10-06 14:06:14 -0700774 tcp_connection_tx_pacer_reset (tc, tc->cwnd, 0 /* start bucket */ );
Florin Corasb3dce892019-10-30 09:22:14 -0700775 tc->rcv_dupacks = 0;
Florin Coras8e76dfa2022-01-05 19:54:45 -0800776 tcp_recovery_off (tc);
Florin Corasc31dc312019-10-06 14:06:14 -0700777
Florin Coras36ebcff2019-09-12 18:36:44 -0700778 /* Previous recovery left us congested. Continue sending as part
779 * of the current recovery event with an updated snd_congestion */
Florin Coras8e76dfa2022-01-05 19:54:45 -0800780 if (tc->sack_sb.sacked_bytes && tcp_in_fastrecovery (tc))
Florin Coras36ebcff2019-09-12 18:36:44 -0700781 {
782 tc->snd_congestion = tc->snd_nxt;
Florin Corasb56f24b2022-01-05 12:55:35 -0800783 return -1;
Florin Coras36ebcff2019-09-12 18:36:44 -0700784 }
785
Florin Corasb3dce892019-10-30 09:22:14 -0700786 tc->rxt_delivered = 0;
787 tc->snd_rxt_bytes = 0;
788 tc->snd_rxt_ts = 0;
789 tc->prr_delivered = 0;
790 tc->rtt_ts = 0;
791 tc->flags &= ~TCP_CONN_RXT_PENDING;
792
Florin Coras321cfa52019-09-12 18:49:44 -0700793 hole = scoreboard_first_hole (&tc->sack_sb);
794 if (hole && hole->start == tc->snd_una && hole->end == tc->snd_nxt)
795 scoreboard_clear (&tc->sack_sb);
796
Florin Coras8e76dfa2022-01-05 19:54:45 -0800797 if (tcp_in_fastrecovery (tc) && !is_spurious)
Florin Coras36ebcff2019-09-12 18:36:44 -0700798 tcp_cc_recovered (tc);
Florin Coras93992a92017-05-24 18:03:56 -0700799
Florin Coras36ebcff2019-09-12 18:36:44 -0700800 tcp_fastrecovery_off (tc);
801 tcp_fastrecovery_first_off (tc);
Florin Coras36ebcff2019-09-12 18:36:44 -0700802 TCP_EVT (TCP_EVT_CC_EVT, tc, 3);
Florin Coras93992a92017-05-24 18:03:56 -0700803
804 ASSERT (tc->rto_boff == 0);
805 ASSERT (!tcp_in_cong_recovery (tc));
Florin Coras3eb50622017-07-13 01:24:57 -0400806 ASSERT (tcp_scoreboard_is_sane_post_recovery (tc));
Florin Coras6080e0d2020-03-13 20:39:43 +0000807
Florin Corasb56f24b2022-01-05 12:55:35 -0800808 return 0;
Florin Coras93992a92017-05-24 18:03:56 -0700809}
810
811static void
Florin Coras52814732019-06-12 15:38:19 -0700812tcp_cc_update (tcp_connection_t * tc, tcp_rate_sample_t * rs)
Florin Coras93992a92017-05-24 18:03:56 -0700813{
Florin Coras3eb50622017-07-13 01:24:57 -0400814 ASSERT (!tcp_in_cong_recovery (tc) || tcp_is_lost_fin (tc));
Florin Coras93992a92017-05-24 18:03:56 -0700815
816 /* Congestion avoidance */
Florin Coras52814732019-06-12 15:38:19 -0700817 tcp_cc_rcv_ack (tc, rs);
Florin Coras93992a92017-05-24 18:03:56 -0700818
819 /* If a cumulative ack, make sure dupacks is 0 */
820 tc->rcv_dupacks = 0;
Florin Coras93992a92017-05-24 18:03:56 -0700821}
822
Florin Corasf03a59a2017-06-09 21:07:32 -0700823/**
824 * One function to rule them all ... and in the darkness bind them
825 */
Florin Coras93992a92017-05-24 18:03:56 -0700826static void
Florin Coras52814732019-06-12 15:38:19 -0700827tcp_cc_handle_event (tcp_connection_t * tc, tcp_rate_sample_t * rs,
828 u32 is_dack)
Florin Coras93992a92017-05-24 18:03:56 -0700829{
Florin Corasafef8bf2019-09-11 23:22:29 -0700830 u8 has_sack = tcp_opts_sack_permitted (&tc->rcv_opts);
Florin Corasf03a59a2017-06-09 21:07:32 -0700831
Florin Coras1de71672019-12-22 09:20:26 -0800832 /* If reneging, wait for timer based retransmits */
833 if (PREDICT_FALSE (tcp_is_lost_fin (tc) || tc->sack_sb.is_reneging))
834 return;
835
Florin Corasafef8bf2019-09-11 23:22:29 -0700836 /*
Florin Corasbe237bf2019-09-27 08:16:40 -0700837 * If not in recovery, figure out if we should enter
Florin Corasafef8bf2019-09-11 23:22:29 -0700838 */
Florin Corasbe237bf2019-09-27 08:16:40 -0700839 if (!tcp_in_cong_recovery (tc))
Florin Corasca1c8f32018-05-23 21:01:30 -0700840 {
Florin Corasbe237bf2019-09-27 08:16:40 -0700841 ASSERT (is_dack);
Florin Coras36ebcff2019-09-12 18:36:44 -0700842
Florin Coras93992a92017-05-24 18:03:56 -0700843 tc->rcv_dupacks++;
Florin Corasbe237bf2019-09-27 08:16:40 -0700844 TCP_EVT (TCP_EVT_DUPACK_RCVD, tc, 1);
845 tcp_cc_rcv_cong_ack (tc, TCP_CC_DUPACK, rs);
Florin Coras93992a92017-05-24 18:03:56 -0700846
Florin Corasbe237bf2019-09-27 08:16:40 -0700847 if (tcp_should_fastrecover (tc, has_sack))
Dave Barach68b0fb02017-02-28 15:15:56 -0500848 {
Florin Coras93992a92017-05-24 18:03:56 -0700849 tcp_cc_init_congestion (tc);
Florin Coras93992a92017-05-24 18:03:56 -0700850
Florin Corasafef8bf2019-09-11 23:22:29 -0700851 if (has_sack)
Florin Corasbe237bf2019-09-27 08:16:40 -0700852 scoreboard_init_rxt (&tc->sack_sb, tc->snd_una);
Florin Corasbf4d5ce2018-10-19 16:26:24 -0700853
Florin Coras36ebcff2019-09-12 18:36:44 -0700854 tcp_connection_tx_pacer_reset (tc, tc->cwnd, 0 /* start bucket */ );
855 tcp_program_retransmit (tc);
Dave Barach68b0fb02017-02-28 15:15:56 -0500856 }
Florin Coras93992a92017-05-24 18:03:56 -0700857
Florin Corasbe237bf2019-09-27 08:16:40 -0700858 return;
859 }
Florin Corasd2aab832018-05-22 11:39:59 -0700860
Florin Coras93992a92017-05-24 18:03:56 -0700861 /*
Florin Corasb3dce892019-10-30 09:22:14 -0700862 * Already in recovery
Florin Coras93992a92017-05-24 18:03:56 -0700863 */
Florin Coras93992a92017-05-24 18:03:56 -0700864
Florin Coras93992a92017-05-24 18:03:56 -0700865 /*
Florin Corasb56f24b2022-01-05 12:55:35 -0800866 * See if we can exit and stop retransmitting
867 */
868 if (seq_geq (tc->snd_una, tc->snd_congestion))
869 {
870 /* If successfully recovered, treat ack as congestion avoidance ack
871 * and return. Otherwise, we're still congested so process feedback */
872 if (!tcp_cc_try_recover (tc))
873 {
874 tcp_cc_rcv_ack (tc, rs);
875 return;
876 }
877 }
878
879 /*
Florin Corasbe237bf2019-09-27 08:16:40 -0700880 * Process (re)transmit feedback. Output path uses this to decide how much
881 * more data to release into the network
882 */
883 if (has_sack)
884 {
Florin Corasb3dce892019-10-30 09:22:14 -0700885 if (!tc->bytes_acked && tc->sack_sb.rxt_sacked)
886 tcp_fastrecovery_first_on (tc);
887
Florin Corasbe237bf2019-09-27 08:16:40 -0700888 tc->rxt_delivered += tc->sack_sb.rxt_sacked;
Florin Coras0f8b91f2022-01-05 08:47:11 -0800889 tc->prr_delivered += rs->delivered;
Florin Corasbe237bf2019-09-27 08:16:40 -0700890 }
891 else
892 {
893 if (is_dack)
894 {
895 tc->rcv_dupacks += 1;
896 TCP_EVT (TCP_EVT_DUPACK_RCVD, tc, 1);
897 }
Florin Coras56cef052020-01-15 20:18:35 +0000898 tc->rxt_delivered = clib_min (tc->rxt_delivered + tc->bytes_acked,
Florin Corasbe237bf2019-09-27 08:16:40 -0700899 tc->snd_rxt_bytes);
900 if (is_dack)
Florin Corasbf1f8b72019-11-04 14:39:33 -0800901 tc->prr_delivered += clib_min (tc->snd_mss,
902 tc->snd_nxt - tc->snd_una);
Florin Corasbe237bf2019-09-27 08:16:40 -0700903 else
Florin Corasbf1f8b72019-11-04 14:39:33 -0800904 tc->prr_delivered += tc->bytes_acked - clib_min (tc->bytes_acked,
905 tc->snd_mss *
906 tc->rcv_dupacks);
Florin Corasbe237bf2019-09-27 08:16:40 -0700907
908 /* If partial ack, assume that the first un-acked segment was lost */
909 if (tc->bytes_acked || tc->rcv_dupacks == TCP_DUPACK_THRESHOLD)
910 tcp_fastrecovery_first_on (tc);
Florin Corasbe237bf2019-09-27 08:16:40 -0700911 }
912
Florin Coras34177e82020-03-20 20:25:37 +0000913 tcp_program_retransmit (tc);
914
Florin Corasb3dce892019-10-30 09:22:14 -0700915 /*
Florin Corasbe237bf2019-09-27 08:16:40 -0700916 * Notify cc of the event
Florin Coras93992a92017-05-24 18:03:56 -0700917 */
Florin Coras93992a92017-05-24 18:03:56 -0700918
Florin Corasbe237bf2019-09-27 08:16:40 -0700919 if (!tc->bytes_acked)
920 {
921 tcp_cc_rcv_cong_ack (tc, TCP_CC_DUPACK, rs);
922 return;
923 }
924
925 /* RFC6675: If the incoming ACK is a cumulative acknowledgment,
926 * reset dupacks to 0. Also needed if in congestion recovery */
927 tc->rcv_dupacks = 0;
Florin Corasbf4d5ce2018-10-19 16:26:24 -0700928
Florin Coras36ebcff2019-09-12 18:36:44 -0700929 if (tcp_in_recovery (tc))
930 tcp_cc_rcv_ack (tc, rs);
Dave Barach68b0fb02017-02-28 15:15:56 -0500931 else
Florin Coras36ebcff2019-09-12 18:36:44 -0700932 tcp_cc_rcv_cong_ack (tc, TCP_CC_PARTIALACK, rs);
Florin Corasbe237bf2019-09-27 08:16:40 -0700933}
Dave Barach68b0fb02017-02-28 15:15:56 -0500934
Florin Coras2f04cb92019-12-23 10:03:27 -0800935static void
Florin Coras067f8f92020-01-04 00:53:04 +0000936tcp_handle_old_ack (tcp_connection_t * tc, tcp_rate_sample_t * rs)
Florin Coras2f04cb92019-12-23 10:03:27 -0800937{
938 if (!tcp_in_cong_recovery (tc))
939 return;
940
941 if (tcp_opts_sack_permitted (&tc->rcv_opts))
Florin Coras067f8f92020-01-04 00:53:04 +0000942 tcp_rcv_sacks (tc, tc->snd_una);
Florin Coras2f04cb92019-12-23 10:03:27 -0800943
944 tc->bytes_acked = 0;
945
946 if (tc->cfg_flags & TCP_CFG_F_RATE_SAMPLE)
947 tcp_bt_sample_delivery_rate (tc, rs);
948
949 tcp_cc_handle_event (tc, rs, 1);
950}
951
Florin Corasbe237bf2019-09-27 08:16:40 -0700952/**
953 * Check if duplicate ack as per RFC5681 Sec. 2
954 */
955always_inline u8
956tcp_ack_is_dupack (tcp_connection_t * tc, vlib_buffer_t * b, u32 prev_snd_wnd,
957 u32 prev_snd_una)
958{
959 return ((vnet_buffer (b)->tcp.ack_number == prev_snd_una)
960 && seq_gt (tc->snd_nxt, tc->snd_una)
961 && (vnet_buffer (b)->tcp.seq_end == vnet_buffer (b)->tcp.seq_number)
962 && (prev_snd_wnd == tc->snd_wnd));
963}
964
965/**
966 * Checks if ack is a congestion control event.
967 */
968static u8
969tcp_ack_is_cc_event (tcp_connection_t * tc, vlib_buffer_t * b,
970 u32 prev_snd_wnd, u32 prev_snd_una, u8 * is_dack)
971{
972 /* Check if ack is duplicate. Per RFC 6675, ACKs that SACK new data are
973 * defined to be 'duplicate' as well */
974 *is_dack = tc->sack_sb.last_sacked_bytes
975 || tcp_ack_is_dupack (tc, b, prev_snd_wnd, prev_snd_una);
976
Florin Corasbe237bf2019-09-27 08:16:40 -0700977 return (*is_dack || tcp_in_cong_recovery (tc));
Dave Barach68b0fb02017-02-28 15:15:56 -0500978}
979
Florin Coras93992a92017-05-24 18:03:56 -0700980/**
981 * Process incoming ACK
982 */
Dave Barach68b0fb02017-02-28 15:15:56 -0500983static int
Florin Coras9ece3c02018-11-05 11:06:53 -0800984tcp_rcv_ack (tcp_worker_ctx_t * wrk, tcp_connection_t * tc, vlib_buffer_t * b,
Florin Coras7ac053b2018-11-05 15:57:21 -0800985 tcp_header_t * th, u32 * error)
Dave Barach68b0fb02017-02-28 15:15:56 -0500986{
Florin Coras93992a92017-05-24 18:03:56 -0700987 u32 prev_snd_wnd, prev_snd_una;
Florin Coras52814732019-06-12 15:38:19 -0700988 tcp_rate_sample_t rs = { 0 };
Florin Coras93992a92017-05-24 18:03:56 -0700989 u8 is_dack;
Dave Barach68b0fb02017-02-28 15:15:56 -0500990
Florin Corasa436a422019-08-20 07:09:31 -0700991 TCP_EVT (TCP_EVT_CC_STAT, tc);
Florin Corasf03a59a2017-06-09 21:07:32 -0700992
Florin Coras6792ec02017-03-13 03:49:51 -0700993 /* If the ACK acks something not yet sent (SEG.ACK > SND.NXT) */
Florin Coras93992a92017-05-24 18:03:56 -0700994 if (PREDICT_FALSE (seq_gt (vnet_buffer (b)->tcp.ack_number, tc->snd_nxt)))
Dave Barach68b0fb02017-02-28 15:15:56 -0500995 {
Florin Coras47596832019-03-12 18:58:54 -0700996 /* We've probably entered recovery and the peer still has some
997 * of the data we've sent. Update snd_nxt and accept the ack */
Florin Coras55e556c2020-10-23 10:45:48 -0700998 if (seq_leq (vnet_buffer (b)->tcp.ack_number, tc->snd_nxt)
Florin Coras282a3cb2019-04-02 21:43:38 -0700999 && seq_gt (vnet_buffer (b)->tcp.ack_number, tc->snd_una))
Florin Corasca1c8f32018-05-23 21:01:30 -07001000 {
Florin Coras3ec66b02018-08-23 16:27:05 -07001001 tc->snd_nxt = vnet_buffer (b)->tcp.ack_number;
Florin Corasca1c8f32018-05-23 21:01:30 -07001002 goto process_ack;
1003 }
1004
Florin Corasedfe0ee2019-07-29 18:13:25 -07001005 tc->errors.above_ack_wnd += 1;
Florin Coras47596832019-03-12 18:58:54 -07001006 *error = TCP_ERROR_ACK_FUTURE;
Florin Corasa436a422019-08-20 07:09:31 -07001007 TCP_EVT (TCP_EVT_ACK_RCV_ERR, tc, 0, vnet_buffer (b)->tcp.ack_number);
Florin Coras47596832019-03-12 18:58:54 -07001008 return -1;
Dave Barach68b0fb02017-02-28 15:15:56 -05001009 }
1010
Florin Coras6792ec02017-03-13 03:49:51 -07001011 /* If old ACK, probably it's an old dupack */
Florin Coras93992a92017-05-24 18:03:56 -07001012 if (PREDICT_FALSE (seq_lt (vnet_buffer (b)->tcp.ack_number, tc->snd_una)))
Dave Barach68b0fb02017-02-28 15:15:56 -05001013 {
Florin Corasedfe0ee2019-07-29 18:13:25 -07001014 tc->errors.below_ack_wnd += 1;
Dave Barach68b0fb02017-02-28 15:15:56 -05001015 *error = TCP_ERROR_ACK_OLD;
Florin Corasa436a422019-08-20 07:09:31 -07001016 TCP_EVT (TCP_EVT_ACK_RCV_ERR, tc, 1, vnet_buffer (b)->tcp.ack_number);
Florin Coras2f04cb92019-12-23 10:03:27 -08001017
1018 if (seq_lt (vnet_buffer (b)->tcp.ack_number, tc->snd_una - tc->rcv_wnd))
1019 return -1;
1020
Florin Coras067f8f92020-01-04 00:53:04 +00001021 tcp_handle_old_ack (tc, &rs);
Florin Coras2f04cb92019-12-23 10:03:27 -08001022
Florin Corasc28764f2017-04-26 00:08:42 -07001023 /* Don't drop yet */
1024 return 0;
Dave Barach68b0fb02017-02-28 15:15:56 -05001025 }
1026
Florin Coras47596832019-03-12 18:58:54 -07001027process_ack:
1028
Florin Coras93992a92017-05-24 18:03:56 -07001029 /*
1030 * Looks okay, process feedback
1031 */
Florin Corasedfe0ee2019-07-29 18:13:25 -07001032
Florin Coras93992a92017-05-24 18:03:56 -07001033 if (tcp_opts_sack_permitted (&tc->rcv_opts))
Dave Barach68b0fb02017-02-28 15:15:56 -05001034 tcp_rcv_sacks (tc, vnet_buffer (b)->tcp.ack_number);
1035
Florin Coras93992a92017-05-24 18:03:56 -07001036 prev_snd_wnd = tc->snd_wnd;
1037 prev_snd_una = tc->snd_una;
1038 tcp_update_snd_wnd (tc, vnet_buffer (b)->tcp.seq_number,
1039 vnet_buffer (b)->tcp.ack_number,
1040 clib_net_to_host_u16 (th->window) << tc->snd_wscale);
1041 tc->bytes_acked = vnet_buffer (b)->tcp.ack_number - tc->snd_una;
Florin Coras558e3e02019-09-06 12:56:58 -07001042 tc->snd_una = vnet_buffer (b)->tcp.ack_number;
Florin Coras93992a92017-05-24 18:03:56 -07001043 tcp_validate_txf_size (tc, tc->bytes_acked);
Dave Barach68b0fb02017-02-28 15:15:56 -05001044
Florin Corasbbcfaac2019-10-10 13:52:04 -07001045 if (tc->cfg_flags & TCP_CFG_F_RATE_SAMPLE)
Florin Coras1dbda642019-09-11 13:42:57 -07001046 tcp_bt_sample_delivery_rate (tc, &rs);
Florin Coras0f8b91f2022-01-05 08:47:11 -08001047 else
1048 rs.delivered = tc->bytes_acked + tc->sack_sb.last_sacked_bytes -
1049 tc->sack_sb.last_bytes_delivered;
Florin Coras1dbda642019-09-11 13:42:57 -07001050
Florin Coras8ddd19b2020-06-07 20:06:56 +00001051 if (tc->bytes_acked + tc->sack_sb.last_sacked_bytes)
Florin Coras11e9e352019-11-13 19:09:47 -08001052 {
Florin Coras11e9e352019-11-13 19:09:47 -08001053 tcp_update_rtt (tc, &rs, vnet_buffer (b)->tcp.ack_number);
Florin Coras8ddd19b2020-06-07 20:06:56 +00001054 if (tc->bytes_acked)
1055 tcp_program_dequeue (wrk, tc);
Florin Coras11e9e352019-11-13 19:09:47 -08001056 }
Florin Coras93992a92017-05-24 18:03:56 -07001057
Florin Corasa436a422019-08-20 07:09:31 -07001058 TCP_EVT (TCP_EVT_ACK_RCVD, tc);
Florin Coras6534b7a2017-07-18 05:38:03 -04001059
Florin Coras93992a92017-05-24 18:03:56 -07001060 /*
1061 * Check if we have congestion event
1062 */
1063
1064 if (tcp_ack_is_cc_event (tc, b, prev_snd_wnd, prev_snd_una, &is_dack))
Dave Barach68b0fb02017-02-28 15:15:56 -05001065 {
Florin Coras52814732019-06-12 15:38:19 -07001066 tcp_cc_handle_event (tc, &rs, is_dack);
Florin Corasedfe0ee2019-07-29 18:13:25 -07001067 tc->dupacks_in += is_dack;
Florin Corasb2215d62017-08-01 16:56:58 -07001068 if (!tcp_in_cong_recovery (tc))
Florin Corasa9d5bea2018-12-17 08:24:19 -08001069 {
1070 *error = TCP_ERROR_ACK_OK;
1071 return 0;
1072 }
Dave Barach68b0fb02017-02-28 15:15:56 -05001073 *error = TCP_ERROR_ACK_DUP;
Florin Corasca09d072018-10-01 18:31:02 -07001074 if (vnet_buffer (b)->tcp.data_len || tcp_is_fin (th))
1075 return 0;
1076 return -1;
Dave Barach68b0fb02017-02-28 15:15:56 -05001077 }
1078
Florin Coras6792ec02017-03-13 03:49:51 -07001079 /*
Florin Coras93992a92017-05-24 18:03:56 -07001080 * Update congestion control (slow start/congestion avoidance)
Florin Coras6792ec02017-03-13 03:49:51 -07001081 */
Florin Coras52814732019-06-12 15:38:19 -07001082 tcp_cc_update (tc, &rs);
Florin Coras00cd22d2018-04-18 13:20:18 -07001083 *error = TCP_ERROR_ACK_OK;
Dave Barach68b0fb02017-02-28 15:15:56 -05001084 return 0;
1085}
1086
Florin Corasb11175d2018-11-09 14:34:08 -08001087static void
1088tcp_program_disconnect (tcp_worker_ctx_t * wrk, tcp_connection_t * tc)
1089{
1090 if (!tcp_disconnect_pending (tc))
1091 {
1092 vec_add1 (wrk->pending_disconnects, tc->c_c_index);
1093 tcp_disconnect_pending_on (tc);
1094 }
1095}
1096
1097static void
1098tcp_handle_disconnects (tcp_worker_ctx_t * wrk)
1099{
Florin Coras6939d5e2020-02-10 23:22:34 +00001100 u32 thread_index, *pending_disconnects, *pending_resets;
Florin Corasb11175d2018-11-09 14:34:08 -08001101 tcp_connection_t *tc;
1102 int i;
1103
Florin Coras6939d5e2020-02-10 23:22:34 +00001104 if (vec_len (wrk->pending_disconnects))
Florin Corasb11175d2018-11-09 14:34:08 -08001105 {
Florin Coras6939d5e2020-02-10 23:22:34 +00001106 thread_index = wrk->vm->thread_index;
1107 pending_disconnects = wrk->pending_disconnects;
1108 for (i = 0; i < vec_len (pending_disconnects); i++)
1109 {
1110 tc = tcp_connection_get (pending_disconnects[i], thread_index);
1111 tcp_disconnect_pending_off (tc);
1112 session_transport_closing_notify (&tc->connection);
1113 }
Damjan Marion8bea5892022-04-04 22:40:45 +02001114 vec_set_len (wrk->pending_disconnects, 0);
Florin Corasb11175d2018-11-09 14:34:08 -08001115 }
Florin Coras6939d5e2020-02-10 23:22:34 +00001116
1117 if (vec_len (wrk->pending_resets))
1118 {
1119 thread_index = wrk->vm->thread_index;
1120 pending_resets = wrk->pending_resets;
1121 for (i = 0; i < vec_len (pending_resets); i++)
1122 {
1123 tc = tcp_connection_get (pending_resets[i], thread_index);
1124 tcp_disconnect_pending_off (tc);
1125 tcp_handle_rst (tc);
1126 }
Damjan Marion8bea5892022-04-04 22:40:45 +02001127 vec_set_len (wrk->pending_resets, 0);
Florin Coras6939d5e2020-02-10 23:22:34 +00001128 }
Florin Corasb11175d2018-11-09 14:34:08 -08001129}
1130
1131static void
1132tcp_rcv_fin (tcp_worker_ctx_t * wrk, tcp_connection_t * tc, vlib_buffer_t * b,
1133 u32 * error)
1134{
Florin Corasf73d4c22019-06-28 09:18:48 -07001135 /* Reject out-of-order fins */
1136 if (vnet_buffer (b)->tcp.seq_end != tc->rcv_nxt)
1137 return;
1138
Florin Coras3c514d52018-12-22 11:39:33 -08001139 /* Account for the FIN and send ack */
1140 tc->rcv_nxt += 1;
Florin Corascb711a42019-10-16 19:28:17 -07001141 tc->flags |= TCP_CONN_FINRCVD;
Florin Coras26dd6de2019-07-23 23:54:47 -07001142 tcp_program_ack (tc);
Florin Corasb11175d2018-11-09 14:34:08 -08001143 /* Enter CLOSE-WAIT and notify session. To avoid lingering
1144 * in CLOSE-WAIT, set timer (reuse WAITCLOSE). */
Florin Coras3c514d52018-12-22 11:39:33 -08001145 tcp_connection_set_state (tc, TCP_STATE_CLOSE_WAIT);
Florin Corasb11175d2018-11-09 14:34:08 -08001146 tcp_program_disconnect (wrk, tc);
Florin Coras0765d972020-03-18 21:26:41 +00001147 tcp_timer_update (&wrk->timer_wheel, tc, TCP_TIMER_WAITCLOSE,
1148 tcp_cfg.closewait_time);
Florin Corasa436a422019-08-20 07:09:31 -07001149 TCP_EVT (TCP_EVT_FIN_RCVD, tc);
Florin Corasb11175d2018-11-09 14:34:08 -08001150 *error = TCP_ERROR_FIN_RCVD;
1151}
1152
Dave Barach68b0fb02017-02-28 15:15:56 -05001153/** Enqueue data for delivery to application */
Florin Coras0dbd5172018-06-25 16:19:34 -07001154static int
Dave Barach68b0fb02017-02-28 15:15:56 -05001155tcp_session_enqueue_data (tcp_connection_t * tc, vlib_buffer_t * b,
1156 u16 data_len)
1157{
Florin Coras1f152cd2017-08-18 19:28:03 -07001158 int written, error = TCP_ERROR_ENQUEUED;
Dave Barach68b0fb02017-02-28 15:15:56 -05001159
Dave Barach2c25a622017-06-26 11:35:07 -04001160 ASSERT (seq_geq (vnet_buffer (b)->tcp.seq_number, tc->rcv_nxt));
Florin Coras00cd22d2018-04-18 13:20:18 -07001161 ASSERT (data_len);
Florin Coras3cbc04b2017-10-02 00:18:51 -07001162 written = session_enqueue_stream_connection (&tc->connection, b, 0,
1163 1 /* queue event */ , 1);
Dave Barach68b0fb02017-02-28 15:15:56 -05001164
Florin Corasa436a422019-08-20 07:09:31 -07001165 TCP_EVT (TCP_EVT_INPUT, tc, 0, data_len, written);
Florin Coras6792ec02017-03-13 03:49:51 -07001166
Dave Barach68b0fb02017-02-28 15:15:56 -05001167 /* Update rcv_nxt */
1168 if (PREDICT_TRUE (written == data_len))
1169 {
Florin Coras1f152cd2017-08-18 19:28:03 -07001170 tc->rcv_nxt += written;
Florin Corasc37ce792022-08-29 11:35:53 -07001171 tc->bytes_in += written;
Dave Barach68b0fb02017-02-28 15:15:56 -05001172 }
1173 /* If more data written than expected, account for out-of-order bytes. */
1174 else if (written > data_len)
1175 {
Florin Coras1f152cd2017-08-18 19:28:03 -07001176 tc->rcv_nxt += written;
Florin Corasc37ce792022-08-29 11:35:53 -07001177 tc->bytes_in += data_len;
Florin Corasa436a422019-08-20 07:09:31 -07001178 TCP_EVT (TCP_EVT_CC_INPUT, tc, data_len, written);
Florin Coras6792ec02017-03-13 03:49:51 -07001179 }
1180 else if (written > 0)
1181 {
1182 /* We've written something but FIFO is probably full now */
1183 tc->rcv_nxt += written;
Florin Corasc37ce792022-08-29 11:35:53 -07001184 tc->bytes_in += written;
Florin Coras1f152cd2017-08-18 19:28:03 -07001185 error = TCP_ERROR_PARTIALLY_ENQUEUED;
Dave Barach68b0fb02017-02-28 15:15:56 -05001186 }
1187 else
1188 {
Florin Corasdfd980f2020-03-26 02:45:39 +00001189 /* Packet made it through for ack processing */
1190 if (tc->rcv_wnd < tc->snd_mss)
1191 return TCP_ERROR_ZERO_RWND;
1192
Dave Barach68b0fb02017-02-28 15:15:56 -05001193 return TCP_ERROR_FIFO_FULL;
1194 }
1195
Florin Coras6792ec02017-03-13 03:49:51 -07001196 /* Update SACK list if need be */
Florin Coras607ece32021-04-22 21:10:02 -07001197 if (tcp_opts_sack_permitted (&tc->rcv_opts) && vec_len (tc->snd_sacks))
Florin Coras6792ec02017-03-13 03:49:51 -07001198 {
1199 /* Remove SACK blocks that have been delivered */
1200 tcp_update_sack_list (tc, tc->rcv_nxt, tc->rcv_nxt);
1201 }
1202
Florin Coras1f152cd2017-08-18 19:28:03 -07001203 return error;
Dave Barach68b0fb02017-02-28 15:15:56 -05001204}
1205
1206/** Enqueue out-of-order data */
Florin Coras0dbd5172018-06-25 16:19:34 -07001207static int
Dave Barach68b0fb02017-02-28 15:15:56 -05001208tcp_session_enqueue_ooo (tcp_connection_t * tc, vlib_buffer_t * b,
1209 u16 data_len)
1210{
Florin Coras288eaab2019-02-03 15:26:14 -08001211 session_t *s0;
Florin Coras3eb50622017-07-13 01:24:57 -04001212 int rv, offset;
Florin Coras6792ec02017-03-13 03:49:51 -07001213
Florin Corasf03a59a2017-06-09 21:07:32 -07001214 ASSERT (seq_gt (vnet_buffer (b)->tcp.seq_number, tc->rcv_nxt));
Florin Coras00cd22d2018-04-18 13:20:18 -07001215 ASSERT (data_len);
Dave Barach68b0fb02017-02-28 15:15:56 -05001216
Florin Corasf03a59a2017-06-09 21:07:32 -07001217 /* Enqueue out-of-order data with relative offset */
Florin Coras3cbc04b2017-10-02 00:18:51 -07001218 rv = session_enqueue_stream_connection (&tc->connection, b,
1219 vnet_buffer (b)->tcp.seq_number -
1220 tc->rcv_nxt, 0 /* queue event */ ,
1221 0);
Florin Coras6792ec02017-03-13 03:49:51 -07001222
1223 /* Nothing written */
1224 if (rv)
1225 {
Florin Corasa436a422019-08-20 07:09:31 -07001226 TCP_EVT (TCP_EVT_INPUT, tc, 1, data_len, 0);
Florin Coras6792ec02017-03-13 03:49:51 -07001227 return TCP_ERROR_FIFO_FULL;
1228 }
1229
Florin Corasa436a422019-08-20 07:09:31 -07001230 TCP_EVT (TCP_EVT_INPUT, tc, 1, data_len, data_len);
Florin Corasedfe0ee2019-07-29 18:13:25 -07001231 tc->bytes_in += data_len;
Dave Barach68b0fb02017-02-28 15:15:56 -05001232
1233 /* Update SACK list if in use */
Florin Coras93992a92017-05-24 18:03:56 -07001234 if (tcp_opts_sack_permitted (&tc->rcv_opts))
Dave Barach68b0fb02017-02-28 15:15:56 -05001235 {
1236 ooo_segment_t *newest;
1237 u32 start, end;
1238
Florin Corascea194d2017-10-02 00:18:51 -07001239 s0 = session_get (tc->c_s_index, tc->c_thread_index);
Florin Corasf6d68ed2017-05-07 19:12:02 -07001240
Dave Barach68b0fb02017-02-28 15:15:56 -05001241 /* Get the newest segment from the fifo */
Florin Coras288eaab2019-02-03 15:26:14 -08001242 newest = svm_fifo_newest_ooo_segment (s0->rx_fifo);
Florin Corasf03a59a2017-06-09 21:07:32 -07001243 if (newest)
1244 {
Sirshak Das28aa5392019-02-05 01:33:33 -06001245 offset = ooo_segment_offset_prod (s0->rx_fifo, newest);
Florin Coras3eb50622017-07-13 01:24:57 -04001246 ASSERT (offset <= vnet_buffer (b)->tcp.seq_number - tc->rcv_nxt);
1247 start = tc->rcv_nxt + offset;
Florin Coras288eaab2019-02-03 15:26:14 -08001248 end = start + ooo_segment_length (s0->rx_fifo, newest);
Florin Corasf03a59a2017-06-09 21:07:32 -07001249 tcp_update_sack_list (tc, start, end);
Florin Coras288eaab2019-02-03 15:26:14 -08001250 svm_fifo_newest_ooo_segment_reset (s0->rx_fifo);
Florin Corasa436a422019-08-20 07:09:31 -07001251 TCP_EVT (TCP_EVT_CC_SACKS, tc);
Florin Corasf03a59a2017-06-09 21:07:32 -07001252 }
Dave Barach68b0fb02017-02-28 15:15:56 -05001253 }
1254
Florin Coras00cd22d2018-04-18 13:20:18 -07001255 return TCP_ERROR_ENQUEUED_OOO;
Dave Barach68b0fb02017-02-28 15:15:56 -05001256}
1257
Dave Barach68b0fb02017-02-28 15:15:56 -05001258static int
Florin Corasb2215d62017-08-01 16:56:58 -07001259tcp_buffer_discard_bytes (vlib_buffer_t * b, u32 n_bytes_to_drop)
1260{
Florin Coras1f152cd2017-08-18 19:28:03 -07001261 u32 discard, first = b->current_length;
Florin Corasb2215d62017-08-01 16:56:58 -07001262 vlib_main_t *vm = vlib_get_main ();
1263
Florin Coras1f152cd2017-08-18 19:28:03 -07001264 /* Handle multi-buffer segments */
Florin Corasb2215d62017-08-01 16:56:58 -07001265 if (n_bytes_to_drop > b->current_length)
1266 {
1267 if (!(b->flags & VLIB_BUFFER_NEXT_PRESENT))
1268 return -1;
1269 do
1270 {
1271 discard = clib_min (n_bytes_to_drop, b->current_length);
1272 vlib_buffer_advance (b, discard);
1273 b = vlib_get_buffer (vm, b->next_buffer);
1274 n_bytes_to_drop -= discard;
1275 }
1276 while (n_bytes_to_drop);
Florin Coras1f152cd2017-08-18 19:28:03 -07001277 if (n_bytes_to_drop > first)
1278 b->total_length_not_including_first_buffer -= n_bytes_to_drop - first;
Florin Corasb2215d62017-08-01 16:56:58 -07001279 }
Florin Coras1f152cd2017-08-18 19:28:03 -07001280 else
1281 vlib_buffer_advance (b, n_bytes_to_drop);
1282 vnet_buffer (b)->tcp.data_len -= n_bytes_to_drop;
Florin Corasb2215d62017-08-01 16:56:58 -07001283 return 0;
1284}
1285
Florin Coras00cd22d2018-04-18 13:20:18 -07001286/**
1287 * Receive buffer for connection and handle acks
1288 *
1289 * It handles both in order or out-of-order data.
1290 */
Florin Corasb2215d62017-08-01 16:56:58 -07001291static int
Florin Coras7ac053b2018-11-05 15:57:21 -08001292tcp_segment_rcv (tcp_worker_ctx_t * wrk, tcp_connection_t * tc,
1293 vlib_buffer_t * b)
Dave Barach68b0fb02017-02-28 15:15:56 -05001294{
Florin Coras00cd22d2018-04-18 13:20:18 -07001295 u32 error, n_bytes_to_drop, n_data_bytes;
Florin Coras6534b7a2017-07-18 05:38:03 -04001296
1297 vlib_buffer_advance (b, vnet_buffer (b)->tcp.data_offset);
1298 n_data_bytes = vnet_buffer (b)->tcp.data_len;
1299 ASSERT (n_data_bytes);
Florin Corasedfe0ee2019-07-29 18:13:25 -07001300 tc->data_segs_in += 1;
Dave Barach68b0fb02017-02-28 15:15:56 -05001301
Florin Coras1f85dad2020-11-05 13:39:48 -08001302 /* Make sure we don't consume trailing bytes */
Florin Corasab0a87f2020-11-09 19:03:09 -08001303 if (PREDICT_FALSE (b->current_length > n_data_bytes))
Florin Coras1f85dad2020-11-05 13:39:48 -08001304 b->current_length = n_data_bytes;
1305
Dave Barach68b0fb02017-02-28 15:15:56 -05001306 /* Handle out-of-order data */
1307 if (PREDICT_FALSE (vnet_buffer (b)->tcp.seq_number != tc->rcv_nxt))
1308 {
Florin Coras6792ec02017-03-13 03:49:51 -07001309 /* Old sequence numbers allowed through because they overlapped
1310 * the rx window */
1311 if (seq_lt (vnet_buffer (b)->tcp.seq_number, tc->rcv_nxt))
Dave Barach68b0fb02017-02-28 15:15:56 -05001312 {
Florin Coras00cd22d2018-04-18 13:20:18 -07001313 /* Completely in the past (possible retransmit). Ack
1314 * retransmissions since we may not have any data to send */
Florin Corasf03a59a2017-06-09 21:07:32 -07001315 if (seq_leq (vnet_buffer (b)->tcp.seq_end, tc->rcv_nxt))
Florin Coras6534b7a2017-07-18 05:38:03 -04001316 {
Florin Coras7fd59cc2020-03-12 15:50:57 +00001317 tcp_program_dupack (tc);
1318 tc->errors.below_data_wnd++;
Florin Coras00cd22d2018-04-18 13:20:18 -07001319 error = TCP_ERROR_SEGMENT_OLD;
Florin Coras6534b7a2017-07-18 05:38:03 -04001320 goto done;
1321 }
Dave Barach259cdae2017-05-15 16:27:05 -04001322
Florin Coras00cd22d2018-04-18 13:20:18 -07001323 /* Chop off the bytes in the past and see if what is left
1324 * can be enqueued in order */
Florin Corasdb84e572017-05-09 18:54:52 -07001325 n_bytes_to_drop = tc->rcv_nxt - vnet_buffer (b)->tcp.seq_number;
1326 n_data_bytes -= n_bytes_to_drop;
Dave Barach2c25a622017-06-26 11:35:07 -04001327 vnet_buffer (b)->tcp.seq_number = tc->rcv_nxt;
Florin Corasb2215d62017-08-01 16:56:58 -07001328 if (tcp_buffer_discard_bytes (b, n_bytes_to_drop))
Florin Coras00cd22d2018-04-18 13:20:18 -07001329 {
1330 error = TCP_ERROR_SEGMENT_OLD;
Florin Coras00cd22d2018-04-18 13:20:18 -07001331 goto done;
1332 }
Florin Corasdb84e572017-05-09 18:54:52 -07001333 goto in_order;
Dave Barach68b0fb02017-02-28 15:15:56 -05001334 }
1335
Florin Coras00cd22d2018-04-18 13:20:18 -07001336 /* RFC2581: Enqueue and send DUPACK for fast retransmit */
Florin Coras6792ec02017-03-13 03:49:51 -07001337 error = tcp_session_enqueue_ooo (tc, b, n_data_bytes);
Florin Coras26dd6de2019-07-23 23:54:47 -07001338 tcp_program_dupack (tc);
Florin Corasa436a422019-08-20 07:09:31 -07001339 TCP_EVT (TCP_EVT_DUPACK_SENT, tc, vnet_buffer (b)->tcp);
Florin Corasedfe0ee2019-07-29 18:13:25 -07001340 tc->errors.above_data_wnd += seq_gt (vnet_buffer (b)->tcp.seq_end,
1341 tc->rcv_las + tc->rcv_wnd);
Dave Barach68b0fb02017-02-28 15:15:56 -05001342 goto done;
1343 }
1344
Florin Corasdb84e572017-05-09 18:54:52 -07001345in_order:
1346
Dave Barach68b0fb02017-02-28 15:15:56 -05001347 /* In order data, enqueue. Fifo figures out by itself if any out-of-order
1348 * segments can be enqueued after fifo tail offset changes. */
1349 error = tcp_session_enqueue_data (tc, b, n_data_bytes);
Florin Coras26dd6de2019-07-23 23:54:47 -07001350 tcp_program_ack (tc);
Florin Coras3e350af2017-03-30 02:54:28 -07001351
Dave Barach68b0fb02017-02-28 15:15:56 -05001352done:
1353 return error;
1354}
1355
Clement Durand6cf260c2017-04-13 13:27:04 +02001356typedef struct
1357{
1358 tcp_header_t tcp_header;
1359 tcp_connection_t tcp_connection;
1360} tcp_rx_trace_t;
1361
Florin Coras0dbd5172018-06-25 16:19:34 -07001362static u8 *
Clement Durand6cf260c2017-04-13 13:27:04 +02001363format_tcp_rx_trace (u8 * s, va_list * args)
1364{
1365 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
1366 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
1367 tcp_rx_trace_t *t = va_arg (*args, tcp_rx_trace_t *);
Florin Coras30928f82020-01-27 19:21:28 -08001368 tcp_connection_t *tc = &t->tcp_connection;
Christophe Fontained3c008d2017-10-02 18:10:54 +02001369 u32 indent = format_get_indent (s);
Clement Durand6cf260c2017-04-13 13:27:04 +02001370
Filip Tehlarbc4dc162023-04-17 12:22:12 +02001371 if (!tc->c_lcl_port)
1372 s = format (s, "no tcp connection\n%U%U", format_white_space, indent,
1373 format_tcp_header, &t->tcp_header, 128);
1374 else
1375 s = format (s, "%U state %U\n%U%U", format_tcp_connection_id, tc,
1376 format_tcp_state, tc->state, format_white_space, indent,
1377 format_tcp_header, &t->tcp_header, 128);
Clement Durand6cf260c2017-04-13 13:27:04 +02001378
1379 return s;
1380}
1381
Florin Coras0dbd5172018-06-25 16:19:34 -07001382static u8 *
Clement Durand6cf260c2017-04-13 13:27:04 +02001383format_tcp_rx_trace_short (u8 * s, va_list * args)
1384{
1385 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
1386 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
1387 tcp_rx_trace_t *t = va_arg (*args, tcp_rx_trace_t *);
1388
1389 s = format (s, "%d -> %d (%U)",
Florin Coras4df38712018-06-20 12:44:16 -07001390 clib_net_to_host_u16 (t->tcp_header.dst_port),
1391 clib_net_to_host_u16 (t->tcp_header.src_port), format_tcp_state,
Florin Corasbb292f42017-05-19 09:49:19 -07001392 t->tcp_connection.state);
Clement Durand6cf260c2017-04-13 13:27:04 +02001393
1394 return s;
1395}
1396
Florin Coras4df38712018-06-20 12:44:16 -07001397static void
Florin Coras82b13a82017-04-25 11:58:06 -07001398tcp_set_rx_trace_data (tcp_rx_trace_t * t0, tcp_connection_t * tc0,
1399 tcp_header_t * th0, vlib_buffer_t * b0, u8 is_ip4)
1400{
1401 if (tc0)
1402 {
Dave Barach178cf492018-11-13 16:34:13 -05001403 clib_memcpy_fast (&t0->tcp_connection, tc0,
1404 sizeof (t0->tcp_connection));
Florin Coras82b13a82017-04-25 11:58:06 -07001405 }
1406 else
1407 {
1408 th0 = tcp_buffer_hdr (b0);
1409 }
Dave Barach178cf492018-11-13 16:34:13 -05001410 clib_memcpy_fast (&t0->tcp_header, th0, sizeof (t0->tcp_header));
Florin Coras82b13a82017-04-25 11:58:06 -07001411}
1412
Florin Coras4df38712018-06-20 12:44:16 -07001413static void
1414tcp_established_trace_frame (vlib_main_t * vm, vlib_node_runtime_t * node,
1415 vlib_frame_t * frame, u8 is_ip4)
1416{
1417 u32 *from, n_left;
1418
1419 n_left = frame->n_vectors;
1420 from = vlib_frame_vector_args (frame);
1421
1422 while (n_left >= 1)
1423 {
1424 tcp_connection_t *tc0;
1425 tcp_rx_trace_t *t0;
1426 tcp_header_t *th0;
1427 vlib_buffer_t *b0;
1428 u32 bi0;
1429
1430 bi0 = from[0];
1431 b0 = vlib_get_buffer (vm, bi0);
1432
1433 if (b0->flags & VLIB_BUFFER_IS_TRACED)
1434 {
1435 t0 = vlib_add_trace (vm, node, b0, sizeof (*t0));
1436 tc0 = tcp_connection_get (vnet_buffer (b0)->tcp.connection_index,
1437 vm->thread_index);
1438 th0 = tcp_buffer_hdr (b0);
1439 tcp_set_rx_trace_data (t0, tc0, th0, b0, is_ip4);
1440 }
1441
1442 from += 1;
1443 n_left -= 1;
1444 }
1445}
1446
Dave Barach68b0fb02017-02-28 15:15:56 -05001447always_inline uword
1448tcp46_established_inline (vlib_main_t * vm, vlib_node_runtime_t * node,
Florin Coras4df38712018-06-20 12:44:16 -07001449 vlib_frame_t * frame, int is_ip4)
Dave Barach68b0fb02017-02-28 15:15:56 -05001450{
Florin Coras4df38712018-06-20 12:44:16 -07001451 u32 thread_index = vm->thread_index, errors = 0;
Florin Coras9ece3c02018-11-05 11:06:53 -08001452 tcp_worker_ctx_t *wrk = tcp_get_worker (thread_index);
Florin Coras974b4682021-04-22 18:53:30 -07001453 vlib_buffer_t *bufs[VLIB_FRAME_SIZE], **b;
Florin Coras00cd22d2018-04-18 13:20:18 -07001454 u16 err_counters[TCP_N_ERROR] = { 0 };
Florin Coras974b4682021-04-22 18:53:30 -07001455 u32 n_left_from, *from;
Dave Barach68b0fb02017-02-28 15:15:56 -05001456
Florin Coras4df38712018-06-20 12:44:16 -07001457 if (node->flags & VLIB_NODE_FLAG_TRACE)
1458 tcp_established_trace_frame (vm, node, frame, is_ip4);
1459
Florin Coras974b4682021-04-22 18:53:30 -07001460 from = vlib_frame_vector_args (frame);
Florin Coras4df38712018-06-20 12:44:16 -07001461 n_left_from = frame->n_vectors;
Dave Barach68b0fb02017-02-28 15:15:56 -05001462
Florin Coras974b4682021-04-22 18:53:30 -07001463 vlib_get_buffers (vm, from, bufs, n_left_from);
1464 b = bufs;
1465
Dave Barach68b0fb02017-02-28 15:15:56 -05001466 while (n_left_from > 0)
1467 {
Florin Coras974b4682021-04-22 18:53:30 -07001468 u32 error = TCP_ERROR_ACK_OK;
1469 tcp_connection_t *tc;
1470 tcp_header_t *th;
Dave Barach68b0fb02017-02-28 15:15:56 -05001471
Florin Coras7ac053b2018-11-05 15:57:21 -08001472 if (n_left_from > 1)
Dave Barach68b0fb02017-02-28 15:15:56 -05001473 {
Florin Coras974b4682021-04-22 18:53:30 -07001474 vlib_prefetch_buffer_header (b[1], LOAD);
1475 CLIB_PREFETCH (b[1]->data, 2 * CLIB_CACHE_LINE_BYTES, LOAD);
Dave Barach68b0fb02017-02-28 15:15:56 -05001476 }
1477
Florin Coras974b4682021-04-22 18:53:30 -07001478 tc = tcp_connection_get (vnet_buffer (b[0])->tcp.connection_index,
1479 thread_index);
Florin Coras7ac053b2018-11-05 15:57:21 -08001480
Florin Coras974b4682021-04-22 18:53:30 -07001481 if (PREDICT_FALSE (tc == 0))
Florin Coras7ac053b2018-11-05 15:57:21 -08001482 {
Florin Coras974b4682021-04-22 18:53:30 -07001483 error = TCP_ERROR_INVALID_CONNECTION;
Florin Coras7ac053b2018-11-05 15:57:21 -08001484 goto done;
1485 }
1486
Florin Coras974b4682021-04-22 18:53:30 -07001487 th = tcp_buffer_hdr (b[0]);
Florin Coras7ac053b2018-11-05 15:57:21 -08001488
1489 /* TODO header prediction fast path */
1490
1491 /* 1-4: check SEQ, RST, SYN */
Florin Coras974b4682021-04-22 18:53:30 -07001492 if (PREDICT_FALSE (tcp_segment_validate (wrk, tc, b[0], th, &error)))
Florin Coras7ac053b2018-11-05 15:57:21 -08001493 {
Florin Coras974b4682021-04-22 18:53:30 -07001494 TCP_EVT (TCP_EVT_SEG_INVALID, tc, vnet_buffer (b[0])->tcp);
Florin Coras7ac053b2018-11-05 15:57:21 -08001495 goto done;
1496 }
1497
1498 /* 5: check the ACK field */
Florin Coras974b4682021-04-22 18:53:30 -07001499 if (PREDICT_FALSE (tcp_rcv_ack (wrk, tc, b[0], th, &error)))
Florin Coras7ac053b2018-11-05 15:57:21 -08001500 goto done;
1501
1502 /* 6: check the URG bit TODO */
1503
1504 /* 7: process the segment text */
Florin Coras974b4682021-04-22 18:53:30 -07001505 if (vnet_buffer (b[0])->tcp.data_len)
1506 error = tcp_segment_rcv (wrk, tc, b[0]);
Florin Coras7ac053b2018-11-05 15:57:21 -08001507
1508 /* 8: check the FIN bit */
Florin Coras974b4682021-04-22 18:53:30 -07001509 if (PREDICT_FALSE (tcp_is_fin (th)))
1510 tcp_rcv_fin (wrk, tc, b[0], &error);
Florin Coras7ac053b2018-11-05 15:57:21 -08001511
1512 done:
Florin Coras974b4682021-04-22 18:53:30 -07001513 tcp_inc_err_counter (err_counters, error, 1);
1514
1515 n_left_from -= 1;
1516 b += 1;
Dave Barach68b0fb02017-02-28 15:15:56 -05001517 }
1518
Florin Coras31c99552019-03-01 13:00:58 -08001519 errors = session_main_flush_enqueue_events (TRANSPORT_PROTO_TCP,
1520 thread_index);
Florin Corasa9d5bea2018-12-17 08:24:19 -08001521 err_counters[TCP_ERROR_MSG_QUEUE_FULL] = errors;
Florin Coras00cd22d2018-04-18 13:20:18 -07001522 tcp_store_err_counters (established, err_counters);
Florin Coras9ece3c02018-11-05 11:06:53 -08001523 tcp_handle_postponed_dequeues (wrk);
Florin Corasb11175d2018-11-09 14:34:08 -08001524 tcp_handle_disconnects (wrk);
Florin Coras974b4682021-04-22 18:53:30 -07001525 vlib_buffer_free (vm, from, frame->n_vectors);
Florin Coras4df38712018-06-20 12:44:16 -07001526
1527 return frame->n_vectors;
Dave Barach68b0fb02017-02-28 15:15:56 -05001528}
1529
Filip Tehlare275bed2019-03-06 00:06:56 -08001530VLIB_NODE_FN (tcp4_established_node) (vlib_main_t * vm,
1531 vlib_node_runtime_t * node,
1532 vlib_frame_t * from_frame)
Dave Barach68b0fb02017-02-28 15:15:56 -05001533{
1534 return tcp46_established_inline (vm, node, from_frame, 1 /* is_ip4 */ );
1535}
1536
Filip Tehlare275bed2019-03-06 00:06:56 -08001537VLIB_NODE_FN (tcp6_established_node) (vlib_main_t * vm,
1538 vlib_node_runtime_t * node,
1539 vlib_frame_t * from_frame)
Dave Barach68b0fb02017-02-28 15:15:56 -05001540{
1541 return tcp46_established_inline (vm, node, from_frame, 0 /* is_ip4 */ );
1542}
1543
1544/* *INDENT-OFF* */
1545VLIB_REGISTER_NODE (tcp4_established_node) =
1546{
Dave Barach68b0fb02017-02-28 15:15:56 -05001547 .name = "tcp4-established",
1548 /* Takes a vector of packets. */
1549 .vector_size = sizeof (u32),
Florin Corase69f4952017-03-07 10:06:24 -08001550 .n_errors = TCP_N_ERROR,
Filip Tehlar0c4931c2021-10-06 09:47:41 +00001551 .error_counters = tcp_input_error_counters,
Dave Barach68b0fb02017-02-28 15:15:56 -05001552 .n_next_nodes = TCP_ESTABLISHED_N_NEXT,
1553 .next_nodes =
1554 {
1555#define _(s,n) [TCP_ESTABLISHED_NEXT_##s] = n,
1556 foreach_tcp_state_next
1557#undef _
1558 },
Clement Durand6cf260c2017-04-13 13:27:04 +02001559 .format_trace = format_tcp_rx_trace_short,
Dave Barach68b0fb02017-02-28 15:15:56 -05001560};
1561/* *INDENT-ON* */
1562
Dave Barach68b0fb02017-02-28 15:15:56 -05001563/* *INDENT-OFF* */
1564VLIB_REGISTER_NODE (tcp6_established_node) =
1565{
Dave Barach68b0fb02017-02-28 15:15:56 -05001566 .name = "tcp6-established",
1567 /* Takes a vector of packets. */
1568 .vector_size = sizeof (u32),
1569 .n_errors = TCP_N_ERROR,
Filip Tehlar0c4931c2021-10-06 09:47:41 +00001570 .error_counters = tcp_input_error_counters,
Dave Barach68b0fb02017-02-28 15:15:56 -05001571 .n_next_nodes = TCP_ESTABLISHED_N_NEXT,
1572 .next_nodes =
1573 {
1574#define _(s,n) [TCP_ESTABLISHED_NEXT_##s] = n,
1575 foreach_tcp_state_next
1576#undef _
1577 },
Clement Durand6cf260c2017-04-13 13:27:04 +02001578 .format_trace = format_tcp_rx_trace_short,
Dave Barach68b0fb02017-02-28 15:15:56 -05001579};
1580/* *INDENT-ON* */
1581
1582
Florin Coras4eeeaaf2017-09-05 14:03:37 -04001583static u8
Srikanth Akulacf4c2102019-11-06 18:53:13 -08001584tcp_lookup_is_valid (tcp_connection_t * tc, vlib_buffer_t * b,
1585 tcp_header_t * hdr)
Florin Coras4eeeaaf2017-09-05 14:03:37 -04001586{
Florin Corascea194d2017-10-02 00:18:51 -07001587 transport_connection_t *tmp = 0;
1588 u64 handle;
1589
Florin Coras4eeeaaf2017-09-05 14:03:37 -04001590 if (!tc)
1591 return 1;
1592
Florin Coras70131132017-11-27 02:43:30 -08001593 /* Proxy case */
1594 if (tc->c_lcl_port == 0 && tc->state == TCP_STATE_LISTEN)
1595 return 1;
1596
Florin Coras07df7912019-11-07 08:26:06 -08001597 u8 is_ip_valid = 0, val_l, val_r;
Srikanth Akulacf4c2102019-11-06 18:53:13 -08001598
Srikanth Akulacf4c2102019-11-06 18:53:13 -08001599 if (tc->connection.is_ip4)
1600 {
1601 ip4_header_t *ip4_hdr = (ip4_header_t *) vlib_buffer_get_current (b);
Florin Coras07df7912019-11-07 08:26:06 -08001602
1603 val_l = !ip4_address_compare (&ip4_hdr->dst_address,
1604 &tc->connection.lcl_ip.ip4);
1605 val_l = val_l || ip_is_zero (&tc->connection.lcl_ip, 1);
1606 val_r = !ip4_address_compare (&ip4_hdr->src_address,
1607 &tc->connection.rmt_ip.ip4);
1608 val_r = val_r || tc->state == TCP_STATE_LISTEN;
1609 is_ip_valid = val_l && val_r;
Srikanth Akulacf4c2102019-11-06 18:53:13 -08001610 }
1611 else
1612 {
1613 ip6_header_t *ip6_hdr = (ip6_header_t *) vlib_buffer_get_current (b);
Florin Coras07df7912019-11-07 08:26:06 -08001614
1615 val_l = !ip6_address_compare (&ip6_hdr->dst_address,
1616 &tc->connection.lcl_ip.ip6);
1617 val_l = val_l || ip_is_zero (&tc->connection.lcl_ip, 0);
1618 val_r = !ip6_address_compare (&ip6_hdr->src_address,
1619 &tc->connection.rmt_ip.ip6);
1620 val_r = val_r || tc->state == TCP_STATE_LISTEN;
1621 is_ip_valid = val_l && val_r;
Srikanth Akulacf4c2102019-11-06 18:53:13 -08001622 }
1623
Florin Coras4eeeaaf2017-09-05 14:03:37 -04001624 u8 is_valid = (tc->c_lcl_port == hdr->dst_port
1625 && (tc->state == TCP_STATE_LISTEN
Srikanth Akulacf4c2102019-11-06 18:53:13 -08001626 || tc->c_rmt_port == hdr->src_port) && is_ip_valid);
Florin Coras4eeeaaf2017-09-05 14:03:37 -04001627
1628 if (!is_valid)
1629 {
Florin Corascea194d2017-10-02 00:18:51 -07001630 handle = session_lookup_half_open_handle (&tc->connection);
1631 tmp = session_lookup_half_open_connection (handle & 0xFFFFFFFF,
Florin Coras3cbc04b2017-10-02 00:18:51 -07001632 tc->c_proto, tc->c_is_ip4);
Florin Corascea194d2017-10-02 00:18:51 -07001633
1634 if (tmp)
Florin Coras4eeeaaf2017-09-05 14:03:37 -04001635 {
1636 if (tmp->lcl_port == hdr->dst_port
1637 && tmp->rmt_port == hdr->src_port)
1638 {
Florin Corascea194d2017-10-02 00:18:51 -07001639 TCP_DBG ("half-open is valid!");
Ryujiro Shibuya3ea17d52019-11-05 07:24:32 +00001640 is_valid = 1;
Florin Coras4eeeaaf2017-09-05 14:03:37 -04001641 }
1642 }
1643 }
1644 return is_valid;
1645}
1646
1647/**
1648 * Lookup transport connection
1649 */
1650static tcp_connection_t *
Florin Corascea194d2017-10-02 00:18:51 -07001651tcp_lookup_connection (u32 fib_index, vlib_buffer_t * b, u8 thread_index,
1652 u8 is_ip4)
Florin Coras4eeeaaf2017-09-05 14:03:37 -04001653{
1654 tcp_header_t *tcp;
1655 transport_connection_t *tconn;
1656 tcp_connection_t *tc;
Florin Corasdff48db2017-11-19 18:06:58 -08001657 u8 is_filtered = 0;
Florin Coras4eeeaaf2017-09-05 14:03:37 -04001658 if (is_ip4)
1659 {
1660 ip4_header_t *ip4;
1661 ip4 = vlib_buffer_get_current (b);
1662 tcp = ip4_next_header (ip4);
Florin Corascea194d2017-10-02 00:18:51 -07001663 tconn = session_lookup_connection_wt4 (fib_index,
1664 &ip4->dst_address,
1665 &ip4->src_address,
1666 tcp->dst_port,
1667 tcp->src_port,
1668 TRANSPORT_PROTO_TCP,
Florin Corasdff48db2017-11-19 18:06:58 -08001669 thread_index, &is_filtered);
Florin Coras4eeeaaf2017-09-05 14:03:37 -04001670 tc = tcp_get_connection_from_transport (tconn);
Srikanth Akulacf4c2102019-11-06 18:53:13 -08001671 ASSERT (tcp_lookup_is_valid (tc, b, tcp));
Florin Coras4eeeaaf2017-09-05 14:03:37 -04001672 }
1673 else
1674 {
1675 ip6_header_t *ip6;
1676 ip6 = vlib_buffer_get_current (b);
1677 tcp = ip6_next_header (ip6);
Florin Corascea194d2017-10-02 00:18:51 -07001678 tconn = session_lookup_connection_wt6 (fib_index,
1679 &ip6->dst_address,
1680 &ip6->src_address,
1681 tcp->dst_port,
1682 tcp->src_port,
1683 TRANSPORT_PROTO_TCP,
Florin Corasdff48db2017-11-19 18:06:58 -08001684 thread_index, &is_filtered);
Florin Coras4eeeaaf2017-09-05 14:03:37 -04001685 tc = tcp_get_connection_from_transport (tconn);
Srikanth Akulacf4c2102019-11-06 18:53:13 -08001686 ASSERT (tcp_lookup_is_valid (tc, b, tcp));
Florin Coras4eeeaaf2017-09-05 14:03:37 -04001687 }
1688 return tc;
1689}
1690
Yu Pingb092b772019-12-27 04:04:33 +08001691static tcp_connection_t *
1692tcp_lookup_listener (vlib_buffer_t * b, u32 fib_index, int is_ip4)
1693{
1694 session_t *s;
1695
1696 if (is_ip4)
1697 {
1698 ip4_header_t *ip4 = vlib_buffer_get_current (b);
1699 tcp_header_t *tcp = tcp_buffer_hdr (b);
1700 s = session_lookup_listener4 (fib_index,
1701 &ip4->dst_address,
1702 tcp->dst_port, TRANSPORT_PROTO_TCP, 1);
1703 }
1704 else
1705 {
1706 ip6_header_t *ip6 = vlib_buffer_get_current (b);
1707 tcp_header_t *tcp = tcp_buffer_hdr (b);
1708 s = session_lookup_listener6 (fib_index,
1709 &ip6->dst_address,
1710 tcp->dst_port, TRANSPORT_PROTO_TCP, 1);
1711
1712 }
1713 if (PREDICT_TRUE (s != 0))
1714 return tcp_get_connection_from_transport (transport_get_listener
1715 (TRANSPORT_PROTO_TCP,
1716 s->connection_index));
1717 else
1718 return 0;
1719}
1720
Florin Corasb636abe2021-05-04 17:08:49 -07001721static void
1722tcp46_syn_sent_trace_frame (vlib_main_t *vm, vlib_node_runtime_t *node,
1723 u32 *from, u32 n_bufs)
1724{
1725 tcp_connection_t *tc = 0;
1726 tcp_rx_trace_t *t;
1727 vlib_buffer_t *b;
1728 int i;
1729
1730 for (i = 0; i < n_bufs; i++)
1731 {
1732 b = vlib_get_buffer (vm, from[i]);
1733 if (!(b->flags & VLIB_BUFFER_IS_TRACED))
1734 continue;
1735 tc =
1736 tcp_half_open_connection_get (vnet_buffer (b)->tcp.connection_index);
1737 t = vlib_add_trace (vm, node, b, sizeof (*t));
1738 tcp_set_rx_trace_data (t, tc, tcp_buffer_hdr (b), b, 1);
1739 }
1740}
1741
Simon Zhang1146ff42019-09-02 22:54:00 +08001742always_inline void
1743tcp_check_tx_offload (tcp_connection_t * tc, int is_ipv4)
1744{
1745 vnet_main_t *vnm = vnet_get_main ();
1746 const dpo_id_t *dpo;
1747 const load_balance_t *lb;
1748 vnet_hw_interface_t *hw_if;
1749 u32 sw_if_idx, lb_idx;
1750
1751 if (is_ipv4)
1752 {
1753 ip4_address_t *dst_addr = &(tc->c_rmt_ip.ip4);
1754 lb_idx = ip4_fib_forwarding_lookup (tc->c_fib_index, dst_addr);
1755 }
1756 else
1757 {
1758 ip6_address_t *dst_addr = &(tc->c_rmt_ip.ip6);
1759 lb_idx = ip6_fib_table_fwding_lookup (tc->c_fib_index, dst_addr);
1760 }
1761
1762 lb = load_balance_get (lb_idx);
Simon Zhang79bfb9e2019-12-24 20:02:20 +08001763 if (PREDICT_FALSE (lb->lb_n_buckets > 1))
1764 return;
Simon Zhang1146ff42019-09-02 22:54:00 +08001765 dpo = load_balance_get_bucket_i (lb, 0);
1766
Simon Zhang79bfb9e2019-12-24 20:02:20 +08001767 sw_if_idx = dpo_get_urpf (dpo);
1768 if (PREDICT_FALSE (sw_if_idx == ~0))
1769 return;
Simon Zhang1146ff42019-09-02 22:54:00 +08001770
Simon Zhang79bfb9e2019-12-24 20:02:20 +08001771 hw_if = vnet_get_sup_hw_interface (vnm, sw_if_idx);
Damjan Mariond4f88cc2022-01-05 01:52:38 +01001772 if (hw_if->caps & VNET_HW_IF_CAP_TCP_GSO)
Florin Corasbbcfaac2019-10-10 13:52:04 -07001773 tc->cfg_flags |= TCP_CFG_F_TSO;
Simon Zhang1146ff42019-09-02 22:54:00 +08001774}
1775
Filip Tehlarbc4dc162023-04-17 12:22:12 +02001776static void
1777tcp_input_trace_frame (vlib_main_t *vm, vlib_node_runtime_t *node,
1778 vlib_buffer_t **bs, u16 *nexts, u32 n_bufs, u8 is_ip4)
1779{
1780 tcp_connection_t *tc;
1781 tcp_header_t *tcp;
1782 tcp_rx_trace_t *t;
1783 u8 flags;
1784 int i;
1785
1786 for (i = 0; i < n_bufs; i++)
1787 {
1788 if (!(bs[i]->flags & VLIB_BUFFER_IS_TRACED))
1789 continue;
1790
1791 t = vlib_add_trace (vm, node, bs[i], sizeof (*t));
1792 if (nexts[i] == TCP_INPUT_NEXT_DROP || nexts[i] == TCP_INPUT_NEXT_PUNT ||
1793 nexts[i] == TCP_INPUT_NEXT_RESET)
1794 {
1795 tc = 0;
1796 }
1797 else
1798 {
1799 flags = vnet_buffer (bs[i])->tcp.flags;
1800
1801 if (flags == TCP_STATE_LISTEN)
1802 tc = tcp_listener_get (vnet_buffer (bs[i])->tcp.connection_index);
1803 else if (flags == TCP_STATE_SYN_SENT)
1804 tc = tcp_half_open_connection_get (
1805 vnet_buffer (bs[i])->tcp.connection_index);
1806 else
1807 tc = tcp_connection_get (vnet_buffer (bs[i])->tcp.connection_index,
1808 vm->thread_index);
1809 }
1810 tcp = tcp_buffer_hdr (bs[i]);
1811 tcp_set_rx_trace_data (t, tc, tcp, bs[i], is_ip4);
1812 }
1813}
1814
Dave Barach68b0fb02017-02-28 15:15:56 -05001815always_inline uword
Florin Corasb636abe2021-05-04 17:08:49 -07001816tcp46_syn_sent_inline (vlib_main_t *vm, vlib_node_runtime_t *node,
1817 vlib_frame_t *frame, int is_ip4)
Dave Barach68b0fb02017-02-28 15:15:56 -05001818{
Florin Corasb636abe2021-05-04 17:08:49 -07001819 u32 n_left_from, *from, thread_index = vm->thread_index, errors = 0;
1820 tcp_worker_ctx_t *wrk = tcp_get_worker (thread_index);
1821 vlib_buffer_t *bufs[VLIB_FRAME_SIZE], **b;
Dave Barach68b0fb02017-02-28 15:15:56 -05001822
Florin Corasb636abe2021-05-04 17:08:49 -07001823 from = vlib_frame_vector_args (frame);
1824 n_left_from = frame->n_vectors;
1825
1826 if (PREDICT_FALSE (node->flags & VLIB_NODE_FLAG_TRACE))
1827 tcp46_syn_sent_trace_frame (vm, node, from, n_left_from);
1828
1829 vlib_get_buffers (vm, from, bufs, n_left_from);
1830 b = bufs;
Dave Barach68b0fb02017-02-28 15:15:56 -05001831
Dave Barach68b0fb02017-02-28 15:15:56 -05001832 while (n_left_from > 0)
1833 {
Florin Corasb636abe2021-05-04 17:08:49 -07001834 u32 ack, seq, error = TCP_ERROR_NONE;
1835 tcp_connection_t *tc, *new_tc;
1836 tcp_header_t *tcp;
Dave Barach68b0fb02017-02-28 15:15:56 -05001837
Florin Corasb636abe2021-05-04 17:08:49 -07001838 tc = tcp_half_open_connection_get (
1839 vnet_buffer (b[0])->tcp.connection_index);
1840 if (PREDICT_FALSE (tc == 0))
Dave Barach68b0fb02017-02-28 15:15:56 -05001841 {
Florin Corasb636abe2021-05-04 17:08:49 -07001842 error = TCP_ERROR_INVALID_CONNECTION;
Florin Coras7ac053b2018-11-05 15:57:21 -08001843 goto drop;
Dave Barach68b0fb02017-02-28 15:15:56 -05001844 }
1845
Ivan Shvedunov4beb1c62021-01-16 03:43:01 +03001846 /* Half-open completed or cancelled recently but the connection
1847 * was't removed yet by the owning thread */
Florin Corasb636abe2021-05-04 17:08:49 -07001848 if (PREDICT_FALSE (tc->flags & TCP_CONN_HALF_OPEN_DONE))
Florin Coras7ac053b2018-11-05 15:57:21 -08001849 {
Florin Corasb636abe2021-05-04 17:08:49 -07001850 error = TCP_ERROR_SPURIOUS_SYN_ACK;
Florin Coras7ac053b2018-11-05 15:57:21 -08001851 goto drop;
1852 }
1853
Florin Corasb636abe2021-05-04 17:08:49 -07001854 ack = vnet_buffer (b[0])->tcp.ack_number;
1855 seq = vnet_buffer (b[0])->tcp.seq_number;
1856 tcp = tcp_buffer_hdr (b[0]);
Florin Coras7ac053b2018-11-05 15:57:21 -08001857
1858 /* Crude check to see if the connection handle does not match
1859 * the packet. Probably connection just switched to established */
Florin Corasb636abe2021-05-04 17:08:49 -07001860 if (PREDICT_FALSE (tcp->dst_port != tc->c_lcl_port ||
1861 tcp->src_port != tc->c_rmt_port))
Florin Coras7ac053b2018-11-05 15:57:21 -08001862 {
Florin Corasb636abe2021-05-04 17:08:49 -07001863 error = TCP_ERROR_INVALID_CONNECTION;
Florin Coras7ac053b2018-11-05 15:57:21 -08001864 goto drop;
1865 }
1866
Florin Corasb636abe2021-05-04 17:08:49 -07001867 if (PREDICT_FALSE (!tcp_ack (tcp) && !tcp_rst (tcp) && !tcp_syn (tcp)))
Florin Coras7ac053b2018-11-05 15:57:21 -08001868 {
Florin Corasb636abe2021-05-04 17:08:49 -07001869 error = TCP_ERROR_SEGMENT_INVALID;
Florin Coras7ac053b2018-11-05 15:57:21 -08001870 goto drop;
1871 }
1872
Florin Coras3c514d52018-12-22 11:39:33 -08001873 /* SYNs consume sequence numbers */
Florin Corasb636abe2021-05-04 17:08:49 -07001874 vnet_buffer (b[0])->tcp.seq_end += tcp_is_syn (tcp);
Florin Coras7ac053b2018-11-05 15:57:21 -08001875
1876 /*
1877 * 1. check the ACK bit
1878 */
1879
1880 /*
1881 * If the ACK bit is set
1882 * If SEG.ACK =< ISS, or SEG.ACK > SND.NXT, send a reset (unless
1883 * the RST bit is set, if so drop the segment and return)
1884 * <SEQ=SEG.ACK><CTL=RST>
1885 * and discard the segment. Return.
1886 * If SND.UNA =< SEG.ACK =< SND.NXT then the ACK is acceptable.
1887 */
Florin Corasb636abe2021-05-04 17:08:49 -07001888 if (tcp_ack (tcp))
Florin Coras7ac053b2018-11-05 15:57:21 -08001889 {
Florin Corasb636abe2021-05-04 17:08:49 -07001890 if (seq_leq (ack, tc->iss) || seq_gt (ack, tc->snd_nxt))
Florin Coras7ac053b2018-11-05 15:57:21 -08001891 {
Florin Corasb636abe2021-05-04 17:08:49 -07001892 if (!tcp_rst (tcp))
1893 tcp_send_reset_w_pkt (tc, b[0], thread_index, is_ip4);
1894 error = TCP_ERROR_RCV_WND;
Florin Coras7ac053b2018-11-05 15:57:21 -08001895 goto drop;
1896 }
1897
1898 /* Make sure ACK is valid */
Florin Corasb636abe2021-05-04 17:08:49 -07001899 if (seq_gt (tc->snd_una, ack))
Florin Coras7ac053b2018-11-05 15:57:21 -08001900 {
Florin Corasb636abe2021-05-04 17:08:49 -07001901 error = TCP_ERROR_ACK_INVALID;
Florin Coras7ac053b2018-11-05 15:57:21 -08001902 goto drop;
1903 }
1904 }
1905
1906 /*
1907 * 2. check the RST bit
1908 */
1909
Florin Corasb636abe2021-05-04 17:08:49 -07001910 if (tcp_rst (tcp))
Florin Coras7ac053b2018-11-05 15:57:21 -08001911 {
1912 /* If ACK is acceptable, signal client that peer is not
1913 * willing to accept connection and drop connection*/
Florin Corasb636abe2021-05-04 17:08:49 -07001914 if (tcp_ack (tcp))
1915 tcp_rcv_rst (wrk, tc);
1916 error = TCP_ERROR_RST_RCVD;
Florin Coras7ac053b2018-11-05 15:57:21 -08001917 goto drop;
1918 }
1919
1920 /*
1921 * 3. check the security and precedence (skipped)
1922 */
1923
1924 /*
1925 * 4. check the SYN bit
1926 */
1927
1928 /* No SYN flag. Drop. */
Florin Corasb636abe2021-05-04 17:08:49 -07001929 if (!tcp_syn (tcp))
Florin Coras7ac053b2018-11-05 15:57:21 -08001930 {
Florin Corasb636abe2021-05-04 17:08:49 -07001931 error = TCP_ERROR_SEGMENT_INVALID;
Florin Coras7ac053b2018-11-05 15:57:21 -08001932 goto drop;
1933 }
1934
1935 /* Parse options */
Florin Corasb636abe2021-05-04 17:08:49 -07001936 if (tcp_options_parse (tcp, &tc->rcv_opts, 1))
Florin Coras7ac053b2018-11-05 15:57:21 -08001937 {
Florin Corasb636abe2021-05-04 17:08:49 -07001938 error = TCP_ERROR_OPTIONS;
Florin Coras7ac053b2018-11-05 15:57:21 -08001939 goto drop;
1940 }
1941
1942 /* Valid SYN or SYN-ACK. Move connection from half-open pool to
1943 * current thread pool. */
Florin Coras57b2e4a2021-07-06 08:25:36 -07001944 new_tc = tcp_connection_alloc_w_base (thread_index, &tc);
Florin Corasb636abe2021-05-04 17:08:49 -07001945 new_tc->rcv_nxt = vnet_buffer (b[0])->tcp.seq_end;
1946 new_tc->irs = seq;
1947 new_tc->timers[TCP_TIMER_RETRANSMIT_SYN] = TCP_TIMER_HANDLE_INVALID;
Florin Coras7ac053b2018-11-05 15:57:21 -08001948
Florin Corasb636abe2021-05-04 17:08:49 -07001949 if (tcp_opts_tstamp (&new_tc->rcv_opts))
Florin Coras7ac053b2018-11-05 15:57:21 -08001950 {
Florin Corasb636abe2021-05-04 17:08:49 -07001951 new_tc->tsval_recent = new_tc->rcv_opts.tsval;
1952 new_tc->tsval_recent_age = tcp_time_tstamp (thread_index);
Florin Coras7ac053b2018-11-05 15:57:21 -08001953 }
1954
Florin Corasb636abe2021-05-04 17:08:49 -07001955 if (tcp_opts_wscale (&new_tc->rcv_opts))
1956 new_tc->snd_wscale = new_tc->rcv_opts.wscale;
Florin Corase80b5912018-12-12 19:25:43 -08001957 else
Florin Corasb636abe2021-05-04 17:08:49 -07001958 new_tc->rcv_wscale = 0;
Florin Coras7ac053b2018-11-05 15:57:21 -08001959
Florin Corasb636abe2021-05-04 17:08:49 -07001960 new_tc->snd_wnd = clib_net_to_host_u16 (tcp->window)
1961 << new_tc->snd_wscale;
1962 new_tc->snd_wl1 = seq;
1963 new_tc->snd_wl2 = ack;
Florin Coras7ac053b2018-11-05 15:57:21 -08001964
Florin Corasb636abe2021-05-04 17:08:49 -07001965 tcp_connection_init_vars (new_tc);
Florin Coras7ac053b2018-11-05 15:57:21 -08001966
1967 /* SYN-ACK: See if we can switch to ESTABLISHED state */
Florin Corasb636abe2021-05-04 17:08:49 -07001968 if (PREDICT_TRUE (tcp_ack (tcp)))
Florin Coras7ac053b2018-11-05 15:57:21 -08001969 {
1970 /* Our SYN is ACKed: we have iss < ack = snd_una */
1971
1972 /* TODO Dequeue acknowledged segments if we support Fast Open */
Florin Corasb636abe2021-05-04 17:08:49 -07001973 new_tc->snd_una = ack;
1974 new_tc->state = TCP_STATE_ESTABLISHED;
Florin Coras7ac053b2018-11-05 15:57:21 -08001975
1976 /* Make sure las is initialized for the wnd computation */
Florin Corasb636abe2021-05-04 17:08:49 -07001977 new_tc->rcv_las = new_tc->rcv_nxt;
Florin Coras7ac053b2018-11-05 15:57:21 -08001978
1979 /* Notify app that we have connection. If session layer can't
1980 * allocate session send reset */
Florin Corasb636abe2021-05-04 17:08:49 -07001981 if (session_stream_connect_notify (&new_tc->connection,
Florin Coras00e01d32019-10-21 16:07:46 -07001982 SESSION_E_NONE))
Florin Coras7ac053b2018-11-05 15:57:21 -08001983 {
Florin Corasb636abe2021-05-04 17:08:49 -07001984 tcp_send_reset_w_pkt (new_tc, b[0], thread_index, is_ip4);
1985 tcp_connection_cleanup (new_tc);
1986 error = TCP_ERROR_CREATE_SESSION_FAIL;
Florin Corasd50ff7f2020-04-16 04:30:22 +00001987 goto cleanup_ho;
Florin Coras7ac053b2018-11-05 15:57:21 -08001988 }
1989
Florin Corasb636abe2021-05-04 17:08:49 -07001990 transport_fifos_init_ooo (&new_tc->connection);
1991 new_tc->tx_fifo_size = transport_tx_fifo_size (&new_tc->connection);
Florin Coras7ac053b2018-11-05 15:57:21 -08001992 /* Update rtt with the syn-ack sample */
Florin Corasb636abe2021-05-04 17:08:49 -07001993 tcp_estimate_initial_rtt (new_tc);
1994 TCP_EVT (TCP_EVT_SYNACK_RCVD, new_tc);
1995 error = TCP_ERROR_SYN_ACKS_RCVD;
Florin Coras7ac053b2018-11-05 15:57:21 -08001996 }
1997 /* SYN: Simultaneous open. Change state to SYN-RCVD and send SYN-ACK */
1998 else
1999 {
Florin Corasb636abe2021-05-04 17:08:49 -07002000 new_tc->state = TCP_STATE_SYN_RCVD;
Florin Coras7ac053b2018-11-05 15:57:21 -08002001
2002 /* Notify app that we have connection */
Florin Corasb636abe2021-05-04 17:08:49 -07002003 if (session_stream_connect_notify (&new_tc->connection,
Florin Coras00e01d32019-10-21 16:07:46 -07002004 SESSION_E_NONE))
Florin Coras7ac053b2018-11-05 15:57:21 -08002005 {
Florin Corasb636abe2021-05-04 17:08:49 -07002006 tcp_connection_cleanup (new_tc);
2007 tcp_send_reset_w_pkt (tc, b[0], thread_index, is_ip4);
2008 TCP_EVT (TCP_EVT_RST_SENT, tc);
2009 error = TCP_ERROR_CREATE_SESSION_FAIL;
Florin Corasd50ff7f2020-04-16 04:30:22 +00002010 goto cleanup_ho;
Florin Coras7ac053b2018-11-05 15:57:21 -08002011 }
2012
Florin Corasb636abe2021-05-04 17:08:49 -07002013 transport_fifos_init_ooo (&new_tc->connection);
2014 new_tc->tx_fifo_size = transport_tx_fifo_size (&new_tc->connection);
2015 new_tc->rtt_ts = 0;
2016 tcp_init_snd_vars (new_tc);
2017 tcp_send_synack (new_tc);
2018 error = TCP_ERROR_SYNS_RCVD;
Florin Corasd50ff7f2020-04-16 04:30:22 +00002019 goto cleanup_ho;
Florin Coras7ac053b2018-11-05 15:57:21 -08002020 }
2021
Florin Corasb636abe2021-05-04 17:08:49 -07002022 if (!(new_tc->cfg_flags & TCP_CFG_F_NO_TSO))
2023 tcp_check_tx_offload (new_tc, is_ip4);
Simon Zhang1146ff42019-09-02 22:54:00 +08002024
Florin Coras7ac053b2018-11-05 15:57:21 -08002025 /* Read data, if any */
Florin Corasb636abe2021-05-04 17:08:49 -07002026 if (PREDICT_FALSE (vnet_buffer (b[0])->tcp.data_len))
Florin Coras7ac053b2018-11-05 15:57:21 -08002027 {
2028 clib_warning ("rcvd data in syn-sent");
Florin Corasb636abe2021-05-04 17:08:49 -07002029 error = tcp_segment_rcv (wrk, new_tc, b[0]);
2030 if (error == TCP_ERROR_ACK_OK)
2031 error = TCP_ERROR_SYN_ACKS_RCVD;
Florin Coras7ac053b2018-11-05 15:57:21 -08002032 }
2033 else
2034 {
Florin Corascb711a42019-10-16 19:28:17 -07002035 /* Send ack now instead of programming it because connection was
2036 * just established and it's not optional. */
Florin Corasb636abe2021-05-04 17:08:49 -07002037 tcp_send_ack (new_tc);
Florin Coras7ac053b2018-11-05 15:57:21 -08002038 }
2039
Florin Corasd50ff7f2020-04-16 04:30:22 +00002040 cleanup_ho:
2041
2042 /* If this is not the owning thread, wait for syn retransmit to
2043 * expire and cleanup then */
Florin Corasb636abe2021-05-04 17:08:49 -07002044 if (tcp_half_open_connection_cleanup (tc))
2045 tc->flags |= TCP_CONN_HALF_OPEN_DONE;
Florin Corasd50ff7f2020-04-16 04:30:22 +00002046
Florin Coras7ac053b2018-11-05 15:57:21 -08002047 drop:
2048
Florin Corasb636abe2021-05-04 17:08:49 -07002049 b += 1;
2050 n_left_from -= 1;
2051 tcp_inc_counter (syn_sent, error, 1);
Dave Barach68b0fb02017-02-28 15:15:56 -05002052 }
2053
Florin Corasb636abe2021-05-04 17:08:49 -07002054 errors =
2055 session_main_flush_enqueue_events (TRANSPORT_PROTO_TCP, thread_index);
Florin Corasa9d5bea2018-12-17 08:24:19 -08002056 tcp_inc_counter (syn_sent, TCP_ERROR_MSG_QUEUE_FULL, errors);
Florin Corasb636abe2021-05-04 17:08:49 -07002057 vlib_buffer_free (vm, from, frame->n_vectors);
Florin Coras6939d5e2020-02-10 23:22:34 +00002058 tcp_handle_disconnects (wrk);
Florin Coras7ac053b2018-11-05 15:57:21 -08002059
Florin Corasb636abe2021-05-04 17:08:49 -07002060 return frame->n_vectors;
Dave Barach68b0fb02017-02-28 15:15:56 -05002061}
2062
Filip Tehlare275bed2019-03-06 00:06:56 -08002063VLIB_NODE_FN (tcp4_syn_sent_node) (vlib_main_t * vm,
2064 vlib_node_runtime_t * node,
2065 vlib_frame_t * from_frame)
Dave Barach68b0fb02017-02-28 15:15:56 -05002066{
2067 return tcp46_syn_sent_inline (vm, node, from_frame, 1 /* is_ip4 */ );
2068}
2069
Filip Tehlare275bed2019-03-06 00:06:56 -08002070VLIB_NODE_FN (tcp6_syn_sent_node) (vlib_main_t * vm,
2071 vlib_node_runtime_t * node,
2072 vlib_frame_t * from_frame)
Dave Barach68b0fb02017-02-28 15:15:56 -05002073{
2074 return tcp46_syn_sent_inline (vm, node, from_frame, 0 /* is_ip4 */ );
2075}
2076
2077/* *INDENT-OFF* */
2078VLIB_REGISTER_NODE (tcp4_syn_sent_node) =
2079{
Dave Barach68b0fb02017-02-28 15:15:56 -05002080 .name = "tcp4-syn-sent",
2081 /* Takes a vector of packets. */
2082 .vector_size = sizeof (u32),
2083 .n_errors = TCP_N_ERROR,
Filip Tehlar0c4931c2021-10-06 09:47:41 +00002084 .error_counters = tcp_input_error_counters,
Dave Barach68b0fb02017-02-28 15:15:56 -05002085 .n_next_nodes = TCP_SYN_SENT_N_NEXT,
2086 .next_nodes =
2087 {
2088#define _(s,n) [TCP_SYN_SENT_NEXT_##s] = n,
2089 foreach_tcp_state_next
2090#undef _
2091 },
Clement Durand6cf260c2017-04-13 13:27:04 +02002092 .format_trace = format_tcp_rx_trace_short,
Dave Barach68b0fb02017-02-28 15:15:56 -05002093};
2094/* *INDENT-ON* */
2095
Dave Barach68b0fb02017-02-28 15:15:56 -05002096/* *INDENT-OFF* */
2097VLIB_REGISTER_NODE (tcp6_syn_sent_node) =
2098{
Dave Barach68b0fb02017-02-28 15:15:56 -05002099 .name = "tcp6-syn-sent",
2100 /* Takes a vector of packets. */
2101 .vector_size = sizeof (u32),
2102 .n_errors = TCP_N_ERROR,
Filip Tehlar0c4931c2021-10-06 09:47:41 +00002103 .error_counters = tcp_input_error_counters,
Dave Barach68b0fb02017-02-28 15:15:56 -05002104 .n_next_nodes = TCP_SYN_SENT_N_NEXT,
2105 .next_nodes =
2106 {
2107#define _(s,n) [TCP_SYN_SENT_NEXT_##s] = n,
2108 foreach_tcp_state_next
2109#undef _
Clement Durand6cf260c2017-04-13 13:27:04 +02002110 },
2111 .format_trace = format_tcp_rx_trace_short,
2112};
Dave Barach68b0fb02017-02-28 15:15:56 -05002113/* *INDENT-ON* */
2114
Florin Coras95929092021-05-04 19:30:58 -07002115static void
2116tcp46_rcv_process_trace_frame (vlib_main_t *vm, vlib_node_runtime_t *node,
2117 u32 *from, u32 n_bufs)
2118{
2119 u32 thread_index = vm->thread_index;
2120 tcp_connection_t *tc = 0;
2121 tcp_rx_trace_t *t;
2122 vlib_buffer_t *b;
2123 int i;
2124
2125 for (i = 0; i < n_bufs; i++)
2126 {
2127 b = vlib_get_buffer (vm, from[i]);
2128 if (!(b->flags & VLIB_BUFFER_IS_TRACED))
2129 continue;
2130 tc = tcp_connection_get (vnet_buffer (b)->tcp.connection_index,
2131 thread_index);
2132 t = vlib_add_trace (vm, node, b, sizeof (*t));
2133 tcp_set_rx_trace_data (t, tc, tcp_buffer_hdr (b), b, 1);
2134 }
2135}
2136
Dave Barach68b0fb02017-02-28 15:15:56 -05002137/**
Florin Corasd79b41e2017-03-04 05:37:52 -08002138 * Handles reception for all states except LISTEN, SYN-SENT and ESTABLISHED
Dave Barach68b0fb02017-02-28 15:15:56 -05002139 * as per RFC793 p. 64
2140 */
2141always_inline uword
Florin Coras95929092021-05-04 19:30:58 -07002142tcp46_rcv_process_inline (vlib_main_t *vm, vlib_node_runtime_t *node,
2143 vlib_frame_t *frame, int is_ip4)
Dave Barach68b0fb02017-02-28 15:15:56 -05002144{
Florin Coras95929092021-05-04 19:30:58 -07002145 u32 thread_index = vm->thread_index, errors, n_left_from, *from, max_deq;
Florin Coras7ac053b2018-11-05 15:57:21 -08002146 tcp_worker_ctx_t *wrk = tcp_get_worker (thread_index);
Florin Coras95929092021-05-04 19:30:58 -07002147 vlib_buffer_t *bufs[VLIB_FRAME_SIZE], **b;
Dave Barach68b0fb02017-02-28 15:15:56 -05002148
Florin Coras95929092021-05-04 19:30:58 -07002149 from = vlib_frame_vector_args (frame);
2150 n_left_from = frame->n_vectors;
2151
2152 if (PREDICT_FALSE (node->flags & VLIB_NODE_FLAG_TRACE))
2153 tcp46_rcv_process_trace_frame (vm, node, from, n_left_from);
2154
2155 vlib_get_buffers (vm, from, bufs, n_left_from);
2156 b = bufs;
Dave Barach68b0fb02017-02-28 15:15:56 -05002157
2158 while (n_left_from > 0)
2159 {
Florin Coras95929092021-05-04 19:30:58 -07002160 u32 error = TCP_ERROR_NONE;
2161 tcp_header_t *tcp = 0;
2162 tcp_connection_t *tc;
2163 u8 is_fin;
Dave Barach68b0fb02017-02-28 15:15:56 -05002164
Florin Coras95929092021-05-04 19:30:58 -07002165 tc = tcp_connection_get (vnet_buffer (b[0])->tcp.connection_index,
2166 thread_index);
2167 if (PREDICT_FALSE (tc == 0))
Dave Barach68b0fb02017-02-28 15:15:56 -05002168 {
Florin Coras95929092021-05-04 19:30:58 -07002169 error = TCP_ERROR_INVALID_CONNECTION;
Florin Coras7ac053b2018-11-05 15:57:21 -08002170 goto drop;
Dave Barach68b0fb02017-02-28 15:15:56 -05002171 }
2172
Florin Coras95929092021-05-04 19:30:58 -07002173 tcp = tcp_buffer_hdr (b[0]);
2174 is_fin = tcp_is_fin (tcp);
Florin Coras7ac053b2018-11-05 15:57:21 -08002175
Florin Coras7ac053b2018-11-05 15:57:21 -08002176 if (CLIB_DEBUG)
2177 {
Florin Coras95929092021-05-04 19:30:58 -07002178 if (!(tc->connection.flags & TRANSPORT_CONNECTION_F_NO_LOOKUP))
Florin Coras7ac053b2018-11-05 15:57:21 -08002179 {
Ryujiro Shibuyac8be8512019-10-28 00:32:12 +00002180 tcp_connection_t *tmp;
Florin Coras95929092021-05-04 19:30:58 -07002181 tmp = tcp_lookup_connection (tc->c_fib_index, b[0], thread_index,
Ryujiro Shibuyac8be8512019-10-28 00:32:12 +00002182 is_ip4);
Florin Coras95929092021-05-04 19:30:58 -07002183 if (tmp->state != tc->state)
Ryujiro Shibuyac8be8512019-10-28 00:32:12 +00002184 {
Florin Coras95929092021-05-04 19:30:58 -07002185 if (tc->state != TCP_STATE_CLOSED)
Ryujiro Shibuyac8be8512019-10-28 00:32:12 +00002186 clib_warning ("state changed");
2187 goto drop;
2188 }
Florin Coras7ac053b2018-11-05 15:57:21 -08002189 }
2190 }
2191
2192 /*
2193 * Special treatment for CLOSED
2194 */
Florin Coras95929092021-05-04 19:30:58 -07002195 if (PREDICT_FALSE (tc->state == TCP_STATE_CLOSED))
Florin Coras7ac053b2018-11-05 15:57:21 -08002196 {
Florin Coras95929092021-05-04 19:30:58 -07002197 error = TCP_ERROR_CONNECTION_CLOSED;
Florin Coras7ac053b2018-11-05 15:57:21 -08002198 goto drop;
2199 }
2200
2201 /*
2202 * For all other states (except LISTEN)
2203 */
2204
2205 /* 1-4: check SEQ, RST, SYN */
Florin Coras95929092021-05-04 19:30:58 -07002206 if (PREDICT_FALSE (tcp_segment_validate (wrk, tc, b[0], tcp, &error)))
Florin Coras7ac053b2018-11-05 15:57:21 -08002207 goto drop;
2208
2209 /* 5: check the ACK field */
Florin Coras95929092021-05-04 19:30:58 -07002210 switch (tc->state)
Florin Coras7ac053b2018-11-05 15:57:21 -08002211 {
2212 case TCP_STATE_SYN_RCVD:
Florin Corasf65074e2019-03-31 17:17:11 -07002213
2214 /* Make sure the segment is exactly right */
Florin Coras95929092021-05-04 19:30:58 -07002215 if (tc->rcv_nxt != vnet_buffer (b[0])->tcp.seq_number || is_fin)
Florin Corasf65074e2019-03-31 17:17:11 -07002216 {
Florin Coras95929092021-05-04 19:30:58 -07002217 tcp_send_reset_w_pkt (tc, b[0], thread_index, is_ip4);
2218 error = TCP_ERROR_SEGMENT_INVALID;
Florin Corasf65074e2019-03-31 17:17:11 -07002219 goto drop;
2220 }
2221
Florin Coras7ac053b2018-11-05 15:57:21 -08002222 /*
2223 * If the segment acknowledgment is not acceptable, form a
2224 * reset segment,
2225 * <SEQ=SEG.ACK><CTL=RST>
2226 * and send it.
2227 */
Florin Coras95929092021-05-04 19:30:58 -07002228 if (tcp_rcv_ack_no_cc (tc, b[0], &error))
Florin Coras7ac053b2018-11-05 15:57:21 -08002229 {
Florin Coras95929092021-05-04 19:30:58 -07002230 tcp_send_reset_w_pkt (tc, b[0], thread_index, is_ip4);
2231 error = TCP_ERROR_SEGMENT_INVALID;
Florin Coras4850e3e2018-12-12 14:34:38 -08002232 goto drop;
2233 }
2234
Florin Coras7ac053b2018-11-05 15:57:21 -08002235 /* Update rtt and rto */
Florin Coras95929092021-05-04 19:30:58 -07002236 tcp_estimate_initial_rtt (tc);
2237 tcp_connection_tx_pacer_update (tc);
Florin Coras7ac053b2018-11-05 15:57:21 -08002238
2239 /* Switch state to ESTABLISHED */
Florin Coras95929092021-05-04 19:30:58 -07002240 tc->state = TCP_STATE_ESTABLISHED;
2241 TCP_EVT (TCP_EVT_STATE_CHANGE, tc);
Florin Coras7ac053b2018-11-05 15:57:21 -08002242
Florin Coras95929092021-05-04 19:30:58 -07002243 if (!(tc->cfg_flags & TCP_CFG_F_NO_TSO))
2244 tcp_check_tx_offload (tc, is_ip4);
Simon Zhang1146ff42019-09-02 22:54:00 +08002245
Florin Coras7ac053b2018-11-05 15:57:21 -08002246 /* Initialize session variables */
Florin Coras95929092021-05-04 19:30:58 -07002247 tc->snd_una = vnet_buffer (b[0])->tcp.ack_number;
2248 tc->snd_wnd = clib_net_to_host_u16 (tcp->window)
2249 << tc->rcv_opts.wscale;
2250 tc->snd_wl1 = vnet_buffer (b[0])->tcp.seq_number;
2251 tc->snd_wl2 = vnet_buffer (b[0])->tcp.ack_number;
Florin Coras7ac053b2018-11-05 15:57:21 -08002252
2253 /* Reset SYN-ACK retransmit and SYN_RCV establish timers */
Florin Coras95929092021-05-04 19:30:58 -07002254 tcp_retransmit_timer_reset (&wrk->timer_wheel, tc);
2255 if (session_stream_accept_notify (&tc->connection))
Florin Corasa9d5bea2018-12-17 08:24:19 -08002256 {
Florin Coras95929092021-05-04 19:30:58 -07002257 error = TCP_ERROR_MSG_QUEUE_FULL;
2258 tcp_send_reset (tc);
2259 session_transport_delete_notify (&tc->connection);
2260 tcp_connection_cleanup (tc);
Florin Corasa9d5bea2018-12-17 08:24:19 -08002261 goto drop;
2262 }
Florin Coras95929092021-05-04 19:30:58 -07002263 error = TCP_ERROR_ACK_OK;
Florin Coras7ac053b2018-11-05 15:57:21 -08002264 break;
2265 case TCP_STATE_ESTABLISHED:
2266 /* We can get packets in established state here because they
2267 * were enqueued before state change */
Florin Coras95929092021-05-04 19:30:58 -07002268 if (tcp_rcv_ack (wrk, tc, b[0], tcp, &error))
Florin Coras7ac053b2018-11-05 15:57:21 -08002269 goto drop;
2270
2271 break;
2272 case TCP_STATE_FIN_WAIT_1:
2273 /* In addition to the processing for the ESTABLISHED state, if
2274 * our FIN is now acknowledged then enter FIN-WAIT-2 and
2275 * continue processing in that state. */
Florin Coras95929092021-05-04 19:30:58 -07002276 if (tcp_rcv_ack (wrk, tc, b[0], tcp, &error))
Florin Coras7ac053b2018-11-05 15:57:21 -08002277 goto drop;
2278
2279 /* Still have to send the FIN */
Florin Coras95929092021-05-04 19:30:58 -07002280 if (tc->flags & TCP_CONN_FINPNDG)
Florin Coras7ac053b2018-11-05 15:57:21 -08002281 {
2282 /* TX fifo finally drained */
Florin Coras95929092021-05-04 19:30:58 -07002283 max_deq = transport_max_tx_dequeue (&tc->connection);
2284 if (max_deq <= tc->burst_acked)
2285 tcp_send_fin (tc);
Florin Coras070fd4b2019-04-02 19:03:23 -07002286 /* If a fin was received and data was acked extend wait */
Florin Coras95929092021-05-04 19:30:58 -07002287 else if ((tc->flags & TCP_CONN_FINRCVD) && tc->bytes_acked)
2288 tcp_timer_update (&wrk->timer_wheel, tc, TCP_TIMER_WAITCLOSE,
Florin Coras9094b5c2019-08-12 14:17:47 -07002289 tcp_cfg.closewait_time);
Florin Coras7ac053b2018-11-05 15:57:21 -08002290 }
2291 /* If FIN is ACKed */
Florin Coras95929092021-05-04 19:30:58 -07002292 else if (tc->snd_una == tc->snd_nxt)
Florin Coras7ac053b2018-11-05 15:57:21 -08002293 {
Florin Coras7ac053b2018-11-05 15:57:21 -08002294 /* Stop all retransmit timers because we have nothing more
Florin Corasf65074e2019-03-31 17:17:11 -07002295 * to send. */
Florin Coras95929092021-05-04 19:30:58 -07002296 tcp_connection_timers_reset (tc);
Florin Corasf65074e2019-03-31 17:17:11 -07002297
2298 /* We already have a FIN but didn't transition to CLOSING
2299 * because of outstanding tx data. Close the connection. */
Florin Coras95929092021-05-04 19:30:58 -07002300 if (tc->flags & TCP_CONN_FINRCVD)
Florin Corasf65074e2019-03-31 17:17:11 -07002301 {
Florin Coras95929092021-05-04 19:30:58 -07002302 tcp_connection_set_state (tc, TCP_STATE_CLOSED);
2303 session_transport_closed_notify (&tc->connection);
2304 tcp_program_cleanup (wrk, tc);
Florin Corasf65074e2019-03-31 17:17:11 -07002305 goto drop;
2306 }
2307
Florin Coras95929092021-05-04 19:30:58 -07002308 tcp_connection_set_state (tc, TCP_STATE_FIN_WAIT_2);
Florin Corasf65074e2019-03-31 17:17:11 -07002309 /* Enable waitclose because we're willing to wait for peer's
2310 * FIN but not indefinitely. */
Florin Coras95929092021-05-04 19:30:58 -07002311 tcp_timer_set (&wrk->timer_wheel, tc, TCP_TIMER_WAITCLOSE,
Florin Coras0765d972020-03-18 21:26:41 +00002312 tcp_cfg.finwait2_time);
Florin Corasefefc6b2018-11-07 12:49:19 -08002313
2314 /* Don't try to deq the FIN acked */
Florin Coras95929092021-05-04 19:30:58 -07002315 if (tc->burst_acked > 1)
2316 session_tx_fifo_dequeue_drop (&tc->connection,
2317 tc->burst_acked - 1);
2318 tc->burst_acked = 0;
Florin Coras7ac053b2018-11-05 15:57:21 -08002319 }
2320 break;
2321 case TCP_STATE_FIN_WAIT_2:
2322 /* In addition to the processing for the ESTABLISHED state, if
2323 * the retransmission queue is empty, the user's CLOSE can be
2324 * acknowledged ("ok") but do not delete the TCB. */
Florin Coras95929092021-05-04 19:30:58 -07002325 if (tcp_rcv_ack_no_cc (tc, b[0], &error))
Florin Coras7ac053b2018-11-05 15:57:21 -08002326 goto drop;
Florin Coras95929092021-05-04 19:30:58 -07002327 tc->burst_acked = 0;
Florin Coras7ac053b2018-11-05 15:57:21 -08002328 break;
2329 case TCP_STATE_CLOSE_WAIT:
2330 /* Do the same processing as for the ESTABLISHED state. */
Florin Coras95929092021-05-04 19:30:58 -07002331 if (tcp_rcv_ack (wrk, tc, b[0], tcp, &error))
Florin Coras7ac053b2018-11-05 15:57:21 -08002332 goto drop;
2333
Florin Coras95929092021-05-04 19:30:58 -07002334 if (!(tc->flags & TCP_CONN_FINPNDG))
Florin Corasf65074e2019-03-31 17:17:11 -07002335 break;
2336
2337 /* Still have outstanding tx data */
Florin Coras95929092021-05-04 19:30:58 -07002338 max_deq = transport_max_tx_dequeue (&tc->connection);
2339 if (max_deq > tc->burst_acked)
Florin Corasf65074e2019-03-31 17:17:11 -07002340 break;
2341
Florin Coras95929092021-05-04 19:30:58 -07002342 tcp_send_fin (tc);
2343 tcp_connection_timers_reset (tc);
2344 tcp_connection_set_state (tc, TCP_STATE_LAST_ACK);
2345 tcp_timer_set (&wrk->timer_wheel, tc, TCP_TIMER_WAITCLOSE,
Florin Coras0765d972020-03-18 21:26:41 +00002346 tcp_cfg.lastack_time);
Florin Coras7ac053b2018-11-05 15:57:21 -08002347 break;
2348 case TCP_STATE_CLOSING:
2349 /* In addition to the processing for the ESTABLISHED state, if
2350 * the ACK acknowledges our FIN then enter the TIME-WAIT state,
2351 * otherwise ignore the segment. */
Florin Coras95929092021-05-04 19:30:58 -07002352 if (tcp_rcv_ack_no_cc (tc, b[0], &error))
Florin Corasf65074e2019-03-31 17:17:11 -07002353 goto drop;
Florin Coras47596832019-03-12 18:58:54 -07002354
Florin Coras95929092021-05-04 19:30:58 -07002355 if (tc->snd_una != tc->snd_nxt)
Florin Corasf65074e2019-03-31 17:17:11 -07002356 goto drop;
Florin Coras7ac053b2018-11-05 15:57:21 -08002357
Florin Coras95929092021-05-04 19:30:58 -07002358 tcp_connection_timers_reset (tc);
2359 tcp_connection_set_state (tc, TCP_STATE_TIME_WAIT);
2360 tcp_timer_set (&wrk->timer_wheel, tc, TCP_TIMER_WAITCLOSE,
Florin Coras0765d972020-03-18 21:26:41 +00002361 tcp_cfg.timewait_time);
Florin Coras95929092021-05-04 19:30:58 -07002362 session_transport_closed_notify (&tc->connection);
Florin Coras7ac053b2018-11-05 15:57:21 -08002363 goto drop;
2364
2365 break;
2366 case TCP_STATE_LAST_ACK:
2367 /* The only thing that [should] arrive in this state is an
2368 * acknowledgment of our FIN. If our FIN is now acknowledged,
2369 * delete the TCB, enter the CLOSED state, and return. */
2370
Florin Coras95929092021-05-04 19:30:58 -07002371 if (tcp_rcv_ack_no_cc (tc, b[0], &error))
Florin Corasf65074e2019-03-31 17:17:11 -07002372 goto drop;
2373
Florin Coras7ac053b2018-11-05 15:57:21 -08002374 /* Apparently our ACK for the peer's FIN was lost */
Florin Coras95929092021-05-04 19:30:58 -07002375 if (is_fin && tc->snd_una != tc->snd_nxt)
Florin Coras7ac053b2018-11-05 15:57:21 -08002376 {
Florin Coras95929092021-05-04 19:30:58 -07002377 tcp_send_fin (tc);
Florin Coras7ac053b2018-11-05 15:57:21 -08002378 goto drop;
2379 }
2380
Florin Coras95929092021-05-04 19:30:58 -07002381 tcp_connection_set_state (tc, TCP_STATE_CLOSED);
2382 session_transport_closed_notify (&tc->connection);
Florin Coras7ac053b2018-11-05 15:57:21 -08002383
2384 /* Don't free the connection from the data path since
2385 * we can't ensure that we have no packets already enqueued
2386 * to output. Rely instead on the waitclose timer */
Florin Coras95929092021-05-04 19:30:58 -07002387 tcp_connection_timers_reset (tc);
2388 tcp_program_cleanup (tcp_get_worker (tc->c_thread_index), tc);
Florin Coras7ac053b2018-11-05 15:57:21 -08002389
2390 goto drop;
2391
2392 break;
2393 case TCP_STATE_TIME_WAIT:
2394 /* The only thing that can arrive in this state is a
2395 * retransmission of the remote FIN. Acknowledge it, and restart
2396 * the 2 MSL timeout. */
2397
Florin Coras95929092021-05-04 19:30:58 -07002398 if (tcp_rcv_ack_no_cc (tc, b[0], &error))
Florin Coras7ac053b2018-11-05 15:57:21 -08002399 goto drop;
2400
Florin Coras95929092021-05-04 19:30:58 -07002401 if (!is_fin)
Florin Coras37db4302019-03-13 13:25:57 -07002402 goto drop;
2403
Florin Coras95929092021-05-04 19:30:58 -07002404 tcp_program_ack (tc);
2405 tcp_timer_update (&wrk->timer_wheel, tc, TCP_TIMER_WAITCLOSE,
Florin Coras0765d972020-03-18 21:26:41 +00002406 tcp_cfg.timewait_time);
Florin Coras7ac053b2018-11-05 15:57:21 -08002407 goto drop;
2408
2409 break;
2410 default:
2411 ASSERT (0);
2412 }
2413
2414 /* 6: check the URG bit TODO */
2415
2416 /* 7: process the segment text */
Florin Coras95929092021-05-04 19:30:58 -07002417 switch (tc->state)
Florin Coras7ac053b2018-11-05 15:57:21 -08002418 {
2419 case TCP_STATE_ESTABLISHED:
2420 case TCP_STATE_FIN_WAIT_1:
2421 case TCP_STATE_FIN_WAIT_2:
Florin Coras95929092021-05-04 19:30:58 -07002422 if (vnet_buffer (b[0])->tcp.data_len)
2423 error = tcp_segment_rcv (wrk, tc, b[0]);
Florin Corasc67724a2020-11-06 14:21:26 -08002424 /* Don't accept out of order fins lower */
Florin Coras95929092021-05-04 19:30:58 -07002425 if (vnet_buffer (b[0])->tcp.seq_end != tc->rcv_nxt)
Florin Corasc67724a2020-11-06 14:21:26 -08002426 goto drop;
Florin Coras7ac053b2018-11-05 15:57:21 -08002427 break;
2428 case TCP_STATE_CLOSE_WAIT:
2429 case TCP_STATE_CLOSING:
2430 case TCP_STATE_LAST_ACK:
2431 case TCP_STATE_TIME_WAIT:
2432 /* This should not occur, since a FIN has been received from the
2433 * remote side. Ignore the segment text. */
2434 break;
2435 }
2436
2437 /* 8: check the FIN bit */
Florin Coras95929092021-05-04 19:30:58 -07002438 if (!is_fin)
Florin Coras7ac053b2018-11-05 15:57:21 -08002439 goto drop;
2440
Florin Coras95929092021-05-04 19:30:58 -07002441 TCP_EVT (TCP_EVT_FIN_RCVD, tc);
Florin Coras3c514d52018-12-22 11:39:33 -08002442
Florin Coras95929092021-05-04 19:30:58 -07002443 switch (tc->state)
Florin Coras7ac053b2018-11-05 15:57:21 -08002444 {
2445 case TCP_STATE_ESTABLISHED:
Florin Coras3c514d52018-12-22 11:39:33 -08002446 /* Account for the FIN and send ack */
Florin Coras95929092021-05-04 19:30:58 -07002447 tc->rcv_nxt += 1;
2448 tcp_program_ack (tc);
2449 tcp_connection_set_state (tc, TCP_STATE_CLOSE_WAIT);
2450 tcp_program_disconnect (wrk, tc);
2451 tcp_timer_update (&wrk->timer_wheel, tc, TCP_TIMER_WAITCLOSE,
Florin Coras0765d972020-03-18 21:26:41 +00002452 tcp_cfg.closewait_time);
Florin Coras5c0f1662018-12-19 01:38:57 -08002453 break;
Florin Coras7ac053b2018-11-05 15:57:21 -08002454 case TCP_STATE_SYN_RCVD:
Florin Coras5c0f1662018-12-19 01:38:57 -08002455 /* Send FIN-ACK, enter LAST-ACK and because the app was not
2456 * notified yet, set a cleanup timer instead of relying on
2457 * disconnect notify and the implicit close call. */
Florin Coras95929092021-05-04 19:30:58 -07002458 tcp_connection_timers_reset (tc);
2459 tc->rcv_nxt += 1;
2460 tcp_send_fin (tc);
2461 tcp_connection_set_state (tc, TCP_STATE_LAST_ACK);
2462 tcp_timer_set (&wrk->timer_wheel, tc, TCP_TIMER_WAITCLOSE,
Florin Coras0765d972020-03-18 21:26:41 +00002463 tcp_cfg.lastack_time);
Florin Coras7ac053b2018-11-05 15:57:21 -08002464 break;
2465 case TCP_STATE_CLOSE_WAIT:
2466 case TCP_STATE_CLOSING:
2467 case TCP_STATE_LAST_ACK:
2468 /* move along .. */
2469 break;
2470 case TCP_STATE_FIN_WAIT_1:
Florin Coras95929092021-05-04 19:30:58 -07002471 tc->rcv_nxt += 1;
Florin Coras070fd4b2019-04-02 19:03:23 -07002472
Florin Coras95929092021-05-04 19:30:58 -07002473 if (tc->flags & TCP_CONN_FINPNDG)
Florin Coras565115e2019-02-20 19:48:31 -08002474 {
Florin Coras070fd4b2019-04-02 19:03:23 -07002475 /* If data is outstanding, stay in FIN_WAIT_1 and try to finish
2476 * sending it. Since we already received a fin, do not wait
2477 * for too long. */
Florin Coras95929092021-05-04 19:30:58 -07002478 tc->flags |= TCP_CONN_FINRCVD;
2479 tcp_timer_update (&wrk->timer_wheel, tc, TCP_TIMER_WAITCLOSE,
Florin Coras9094b5c2019-08-12 14:17:47 -07002480 tcp_cfg.closewait_time);
Florin Coras565115e2019-02-20 19:48:31 -08002481 }
2482 else
Florin Corasf65074e2019-03-31 17:17:11 -07002483 {
Florin Coras95929092021-05-04 19:30:58 -07002484 tcp_connection_set_state (tc, TCP_STATE_CLOSING);
2485 tcp_program_ack (tc);
Florin Coras070fd4b2019-04-02 19:03:23 -07002486 /* Wait for ACK for our FIN but not forever */
Florin Coras95929092021-05-04 19:30:58 -07002487 tcp_timer_update (&wrk->timer_wheel, tc, TCP_TIMER_WAITCLOSE,
Florin Coras9094b5c2019-08-12 14:17:47 -07002488 tcp_cfg.closing_time);
Florin Corasf65074e2019-03-31 17:17:11 -07002489 }
Florin Coras7ac053b2018-11-05 15:57:21 -08002490 break;
2491 case TCP_STATE_FIN_WAIT_2:
2492 /* Got FIN, send ACK! Be more aggressive with resource cleanup */
Florin Coras95929092021-05-04 19:30:58 -07002493 tc->rcv_nxt += 1;
2494 tcp_connection_set_state (tc, TCP_STATE_TIME_WAIT);
2495 tcp_connection_timers_reset (tc);
2496 tcp_timer_set (&wrk->timer_wheel, tc, TCP_TIMER_WAITCLOSE,
Florin Coras0765d972020-03-18 21:26:41 +00002497 tcp_cfg.timewait_time);
Florin Coras95929092021-05-04 19:30:58 -07002498 tcp_program_ack (tc);
2499 session_transport_closed_notify (&tc->connection);
Florin Coras7ac053b2018-11-05 15:57:21 -08002500 break;
2501 case TCP_STATE_TIME_WAIT:
2502 /* Remain in the TIME-WAIT state. Restart the time-wait
2503 * timeout.
2504 */
Florin Coras95929092021-05-04 19:30:58 -07002505 tcp_timer_update (&wrk->timer_wheel, tc, TCP_TIMER_WAITCLOSE,
Florin Coras0765d972020-03-18 21:26:41 +00002506 tcp_cfg.timewait_time);
Florin Coras7ac053b2018-11-05 15:57:21 -08002507 break;
2508 }
Florin Coras95929092021-05-04 19:30:58 -07002509 error = TCP_ERROR_FIN_RCVD;
Florin Coras7ac053b2018-11-05 15:57:21 -08002510
2511 drop:
2512
Florin Coras95929092021-05-04 19:30:58 -07002513 b += 1;
2514 n_left_from -= 1;
2515 tcp_inc_counter (rcv_process, error, 1);
Dave Barach68b0fb02017-02-28 15:15:56 -05002516 }
2517
Florin Coras31c99552019-03-01 13:00:58 -08002518 errors = session_main_flush_enqueue_events (TRANSPORT_PROTO_TCP,
2519 thread_index);
Florin Corasa9d5bea2018-12-17 08:24:19 -08002520 tcp_inc_counter (rcv_process, TCP_ERROR_MSG_QUEUE_FULL, errors);
Florin Coras9ece3c02018-11-05 11:06:53 -08002521 tcp_handle_postponed_dequeues (wrk);
Florin Coras79fdfd62019-05-23 06:19:09 -07002522 tcp_handle_disconnects (wrk);
Florin Coras95929092021-05-04 19:30:58 -07002523 vlib_buffer_free (vm, from, frame->n_vectors);
Florin Coras7ac053b2018-11-05 15:57:21 -08002524
Florin Coras95929092021-05-04 19:30:58 -07002525 return frame->n_vectors;
Dave Barach68b0fb02017-02-28 15:15:56 -05002526}
2527
Filip Tehlare275bed2019-03-06 00:06:56 -08002528VLIB_NODE_FN (tcp4_rcv_process_node) (vlib_main_t * vm,
2529 vlib_node_runtime_t * node,
2530 vlib_frame_t * from_frame)
Dave Barach68b0fb02017-02-28 15:15:56 -05002531{
2532 return tcp46_rcv_process_inline (vm, node, from_frame, 1 /* is_ip4 */ );
2533}
2534
Filip Tehlare275bed2019-03-06 00:06:56 -08002535VLIB_NODE_FN (tcp6_rcv_process_node) (vlib_main_t * vm,
2536 vlib_node_runtime_t * node,
2537 vlib_frame_t * from_frame)
Dave Barach68b0fb02017-02-28 15:15:56 -05002538{
2539 return tcp46_rcv_process_inline (vm, node, from_frame, 0 /* is_ip4 */ );
2540}
2541
2542/* *INDENT-OFF* */
2543VLIB_REGISTER_NODE (tcp4_rcv_process_node) =
2544{
Dave Barach68b0fb02017-02-28 15:15:56 -05002545 .name = "tcp4-rcv-process",
2546 /* Takes a vector of packets. */
2547 .vector_size = sizeof (u32),
2548 .n_errors = TCP_N_ERROR,
Filip Tehlar0c4931c2021-10-06 09:47:41 +00002549 .error_counters = tcp_input_error_counters,
Dave Barach68b0fb02017-02-28 15:15:56 -05002550 .n_next_nodes = TCP_RCV_PROCESS_N_NEXT,
2551 .next_nodes =
2552 {
2553#define _(s,n) [TCP_RCV_PROCESS_NEXT_##s] = n,
2554 foreach_tcp_state_next
2555#undef _
2556 },
Clement Durand6cf260c2017-04-13 13:27:04 +02002557 .format_trace = format_tcp_rx_trace_short,
Dave Barach68b0fb02017-02-28 15:15:56 -05002558};
2559/* *INDENT-ON* */
2560
Dave Barach68b0fb02017-02-28 15:15:56 -05002561/* *INDENT-OFF* */
2562VLIB_REGISTER_NODE (tcp6_rcv_process_node) =
2563{
Dave Barach68b0fb02017-02-28 15:15:56 -05002564 .name = "tcp6-rcv-process",
2565 /* Takes a vector of packets. */
2566 .vector_size = sizeof (u32),
2567 .n_errors = TCP_N_ERROR,
Filip Tehlar0c4931c2021-10-06 09:47:41 +00002568 .error_counters = tcp_input_error_counters,
Dave Barach68b0fb02017-02-28 15:15:56 -05002569 .n_next_nodes = TCP_RCV_PROCESS_N_NEXT,
2570 .next_nodes =
2571 {
2572#define _(s,n) [TCP_RCV_PROCESS_NEXT_##s] = n,
2573 foreach_tcp_state_next
2574#undef _
2575 },
Clement Durand6cf260c2017-04-13 13:27:04 +02002576 .format_trace = format_tcp_rx_trace_short,
Dave Barach68b0fb02017-02-28 15:15:56 -05002577};
2578/* *INDENT-ON* */
2579
Florin Coras7f969c72021-05-04 14:53:25 -07002580static void
2581tcp46_listen_trace_frame (vlib_main_t *vm, vlib_node_runtime_t *node,
2582 u32 *to_next, u32 n_bufs)
2583{
2584 tcp_connection_t *tc = 0;
2585 tcp_rx_trace_t *t;
2586 vlib_buffer_t *b;
2587 int i;
2588
2589 for (i = 0; i < n_bufs; i++)
2590 {
2591 b = vlib_get_buffer (vm, to_next[i]);
2592 if (!(b->flags & VLIB_BUFFER_IS_TRACED))
2593 continue;
2594 if (vnet_buffer (b)->tcp.flags == TCP_STATE_LISTEN)
2595 tc = tcp_listener_get (vnet_buffer (b)->tcp.connection_index);
2596 t = vlib_add_trace (vm, node, b, sizeof (*t));
2597 tcp_set_rx_trace_data (t, tc, tcp_buffer_hdr (b), b, 1);
2598 }
2599}
2600
Dave Barach68b0fb02017-02-28 15:15:56 -05002601/**
Michal Kalderon3effadc2021-08-08 04:30:39 -07002602 * SYN received in TIME-WAIT state.
2603 *
2604 * RFC 1122:
2605 * "When a connection is [...] on TIME-WAIT state [...]
2606 * [a TCP] MAY accept a new SYN from the remote TCP to
2607 * reopen the connection directly, if it:
2608 *
2609 * (1) assigns its initial sequence number for the new
2610 * connection to be larger than the largest sequence
2611 * number it used on the previous connection incarnation,
2612 * and
2613 *
2614 * (2) returns to TIME-WAIT state if the SYN turns out
2615 * to be an old duplicate".
2616 *
2617 * The function returns true if the syn can be accepted during
2618 * connection time-wait (port reuse). In this case the function
2619 * also calculates what the iss should be for the new connection.
2620 */
2621always_inline int
2622syn_during_timewait (tcp_connection_t *tc, vlib_buffer_t *b, u32 *iss)
2623{
2624 int paws_reject = tcp_segment_check_paws (tc);
2625 u32 tw_iss;
2626
2627 *iss = 0;
2628 /* Check that the SYN arrived out of window. We accept it */
2629 if (!paws_reject &&
2630 (seq_geq (vnet_buffer (b)->tcp.seq_number, tc->rcv_nxt) ||
2631 (tcp_opts_tstamp (&tc->rcv_opts) &&
2632 timestamp_lt (tc->tsval_recent, tc->rcv_opts.tsval))))
2633 {
2634 /* Set the iss of the new connection to be the largest sequence number
2635 * the old peer would have accepted and add some random number
2636 */
2637 tw_iss = tc->snd_nxt + tcp_available_snd_wnd (tc) +
2638 (uword) (tcp_time_now_us (tc->c_thread_index) * 1e6) % 65535;
2639 if (tw_iss == 0)
2640 tw_iss++;
2641 *iss = tw_iss;
2642
2643 return 1;
2644 }
2645 else
2646 {
2647 TCP_DBG (
2648 "ERROR not accepting SYN in timewait,paws_reject=%d, seq_num =%ld, "
2649 "rcv_nxt=%ld, tstamp_present=%d, tsval_recent = %d, tsval = %d\n",
2650 paws_reject, vnet_buffer (b)->tcp.seq_number, tc->rcv_nxt,
2651 tcp_opts_tstamp (&tc->rcv_opts), tc->tsval_recent, tc->rcv_opts.tsval);
2652 return 0;
2653 }
2654}
2655
2656/**
Dave Barach68b0fb02017-02-28 15:15:56 -05002657 * LISTEN state processing as per RFC 793 p. 65
2658 */
2659always_inline uword
Florin Coras7f969c72021-05-04 14:53:25 -07002660tcp46_listen_inline (vlib_main_t *vm, vlib_node_runtime_t *node,
2661 vlib_frame_t *frame, int is_ip4)
Dave Barach68b0fb02017-02-28 15:15:56 -05002662{
Florin Coras7f969c72021-05-04 14:53:25 -07002663 u32 n_left_from, *from, n_syns = 0;
2664 vlib_buffer_t *bufs[VLIB_FRAME_SIZE], **b;
Florin Coras27bb99e2020-03-17 17:09:12 +00002665 u32 thread_index = vm->thread_index;
Michal Kalderon3effadc2021-08-08 04:30:39 -07002666 u32 tw_iss = 0;
Dave Barach68b0fb02017-02-28 15:15:56 -05002667
Florin Coras7f969c72021-05-04 14:53:25 -07002668 from = vlib_frame_vector_args (frame);
2669 n_left_from = frame->n_vectors;
2670
2671 if (PREDICT_FALSE (node->flags & VLIB_NODE_FLAG_TRACE))
2672 tcp46_listen_trace_frame (vm, node, from, n_left_from);
2673
2674 vlib_get_buffers (vm, from, bufs, n_left_from);
2675 b = bufs;
Dave Barach68b0fb02017-02-28 15:15:56 -05002676
Dave Barach68b0fb02017-02-28 15:15:56 -05002677 while (n_left_from > 0)
2678 {
Florin Coras27bb99e2020-03-17 17:09:12 +00002679 tcp_connection_t *lc, *child;
Florin Corasde7fcac2020-01-16 04:04:54 +00002680
Florin Corasee1cb462020-12-29 09:42:58 -08002681 /* Flags initialized with connection state after lookup */
Florin Coras7f969c72021-05-04 14:53:25 -07002682 if (vnet_buffer (b[0])->tcp.flags == TCP_STATE_LISTEN)
Florin Corasee1cb462020-12-29 09:42:58 -08002683 {
Florin Coras7f969c72021-05-04 14:53:25 -07002684 lc = tcp_listener_get (vnet_buffer (b[0])->tcp.connection_index);
Florin Corasee1cb462020-12-29 09:42:58 -08002685 }
Florin Coras3ffc77d2023-02-16 18:59:38 -08002686 /* Probably we are in time-wait or closed state */
2687 else
Florin Corasde7fcac2020-01-16 04:04:54 +00002688 {
Florin Coras27bb99e2020-03-17 17:09:12 +00002689 tcp_connection_t *tc;
Florin Coras7f969c72021-05-04 14:53:25 -07002690 tc = tcp_connection_get (vnet_buffer (b[0])->tcp.connection_index,
Florin Coras27bb99e2020-03-17 17:09:12 +00002691 thread_index);
2692 if (tc->state != TCP_STATE_TIME_WAIT)
Florin Corasb7f035f2019-12-27 09:27:52 -08002693 {
Florin Corase8d67712022-03-15 21:46:34 -07002694 tcp_inc_counter (listen, TCP_ERROR_CREATE_EXISTS, 1);
Florin Coras27bb99e2020-03-17 17:09:12 +00002695 goto done;
Florin Corasb7f035f2019-12-27 09:27:52 -08002696 }
Michal Kalderon3effadc2021-08-08 04:30:39 -07002697
2698 if (PREDICT_FALSE (!syn_during_timewait (tc, b[0], &tw_iss)))
2699 {
2700 /* This SYN can't be accepted */
Florin Corase8d67712022-03-15 21:46:34 -07002701 tcp_inc_counter (listen, TCP_ERROR_CREATE_EXISTS, 1);
Michal Kalderon3effadc2021-08-08 04:30:39 -07002702 goto done;
2703 }
2704
Florin Coras7f969c72021-05-04 14:53:25 -07002705 lc = tcp_lookup_listener (b[0], tc->c_fib_index, is_ip4);
Florin Corasb7f035f2019-12-27 09:27:52 -08002706 /* clean up the old session */
Florin Coras27bb99e2020-03-17 17:09:12 +00002707 tcp_connection_del (tc);
Florin Coras355791c2020-10-11 11:20:56 -07002708 /* listener was cleaned up */
2709 if (!lc)
2710 {
Florin Corase8d67712022-03-15 21:46:34 -07002711 tcp_inc_counter (listen, TCP_ERROR_NO_LISTENER, 1);
Florin Coras355791c2020-10-11 11:20:56 -07002712 goto done;
2713 }
Florin Coras27bb99e2020-03-17 17:09:12 +00002714 }
2715
2716 /* Make sure connection wasn't just created */
Florin Coras7f969c72021-05-04 14:53:25 -07002717 child =
2718 tcp_lookup_connection (lc->c_fib_index, b[0], thread_index, is_ip4);
Florin Coras27bb99e2020-03-17 17:09:12 +00002719 if (PREDICT_FALSE (child->state != TCP_STATE_LISTEN))
2720 {
Florin Corase8d67712022-03-15 21:46:34 -07002721 tcp_inc_counter (listen, TCP_ERROR_CREATE_EXISTS, 1);
Florin Coras27bb99e2020-03-17 17:09:12 +00002722 goto done;
Yu Pingb092b772019-12-27 04:04:33 +08002723 }
Florin Coras7ac053b2018-11-05 15:57:21 -08002724
Florin Coras7ac053b2018-11-05 15:57:21 -08002725 /* Create child session. For syn-flood protection use filter */
2726
Florin Coras7f969c72021-05-04 14:53:25 -07002727 /* 1. first check for an RST: handled by input dispatch */
Florin Coras7ac053b2018-11-05 15:57:21 -08002728
Florin Coras7f969c72021-05-04 14:53:25 -07002729 /* 2. second check for an ACK: handled by input dispatch */
Florin Coras7ac053b2018-11-05 15:57:21 -08002730
2731 /* 3. check for a SYN (did that already) */
2732
Florin Coras7ac053b2018-11-05 15:57:21 -08002733 /* Create child session and send SYN-ACK */
Florin Coras27bb99e2020-03-17 17:09:12 +00002734 child = tcp_connection_alloc (thread_index);
Florin Coras7ac053b2018-11-05 15:57:21 -08002735
Florin Coras7f969c72021-05-04 14:53:25 -07002736 if (tcp_options_parse (tcp_buffer_hdr (b[0]), &child->rcv_opts, 1))
Florin Coras7ac053b2018-11-05 15:57:21 -08002737 {
Florin Corase8d67712022-03-15 21:46:34 -07002738 tcp_inc_counter (listen, TCP_ERROR_OPTIONS, 1);
Florin Coras27bb99e2020-03-17 17:09:12 +00002739 tcp_connection_free (child);
2740 goto done;
Florin Coras7ac053b2018-11-05 15:57:21 -08002741 }
2742
Florin Coras7f969c72021-05-04 14:53:25 -07002743 tcp_init_w_buffer (child, b[0], is_ip4);
Florin Coras27bb99e2020-03-17 17:09:12 +00002744
2745 child->state = TCP_STATE_SYN_RCVD;
2746 child->c_fib_index = lc->c_fib_index;
2747 child->cc_algo = lc->cc_algo;
Michal Kalderon3effadc2021-08-08 04:30:39 -07002748
2749 /* In the regular case, the tw_iss will be zero, but
2750 * in the special case of syn arriving in time_wait state, the value
2751 * will be set according to rfc 1122
2752 */
2753 child->iss = tw_iss;
Florin Coras27bb99e2020-03-17 17:09:12 +00002754 tcp_connection_init_vars (child);
2755 child->rto = TCP_RTO_MIN;
2756
Ivan Shvedunov9fefa892020-07-27 19:59:38 +03002757 /*
2758 * This initializes elog track, must be done before synack.
2759 * We also do it before possible tcp_connection_cleanup() as it
2760 * generates TCP_EVT_DELETE event.
2761 */
2762 TCP_EVT (TCP_EVT_SYN_RCVD, child, 1);
2763
Florin Coras27bb99e2020-03-17 17:09:12 +00002764 if (session_stream_accept (&child->connection, lc->c_s_index,
2765 lc->c_thread_index, 0 /* notify */ ))
Florin Coras7ac053b2018-11-05 15:57:21 -08002766 {
Florin Coras27bb99e2020-03-17 17:09:12 +00002767 tcp_connection_cleanup (child);
Florin Corase8d67712022-03-15 21:46:34 -07002768 tcp_inc_counter (listen, TCP_ERROR_CREATE_SESSION_FAIL, 1);
Florin Coras27bb99e2020-03-17 17:09:12 +00002769 goto done;
Florin Coras7ac053b2018-11-05 15:57:21 -08002770 }
2771
Florin Coras8c4fa012020-11-06 16:59:08 -08002772 transport_fifos_init_ooo (&child->connection);
Florin Coras27bb99e2020-03-17 17:09:12 +00002773 child->tx_fifo_size = transport_tx_fifo_size (&child->connection);
Florin Coras7ac053b2018-11-05 15:57:21 -08002774
Florin Corasd2f51742020-07-24 09:49:46 -07002775 tcp_send_synack (child);
Florin Corase8d67712022-03-15 21:46:34 -07002776 n_syns += 1;
Florin Corasd2f51742020-07-24 09:49:46 -07002777
Florin Coras27bb99e2020-03-17 17:09:12 +00002778 done:
Florin Coras7f969c72021-05-04 14:53:25 -07002779 b += 1;
2780 n_left_from -= 1;
Dave Barach68b0fb02017-02-28 15:15:56 -05002781 }
Florin Coras00cd22d2018-04-18 13:20:18 -07002782
2783 tcp_inc_counter (listen, TCP_ERROR_SYNS_RCVD, n_syns);
Florin Coras7f969c72021-05-04 14:53:25 -07002784 vlib_buffer_free (vm, from, frame->n_vectors);
Florin Coras7ac053b2018-11-05 15:57:21 -08002785
Florin Coras7f969c72021-05-04 14:53:25 -07002786 return frame->n_vectors;
Dave Barach68b0fb02017-02-28 15:15:56 -05002787}
2788
Filip Tehlare275bed2019-03-06 00:06:56 -08002789VLIB_NODE_FN (tcp4_listen_node) (vlib_main_t * vm, vlib_node_runtime_t * node,
2790 vlib_frame_t * from_frame)
Dave Barach68b0fb02017-02-28 15:15:56 -05002791{
2792 return tcp46_listen_inline (vm, node, from_frame, 1 /* is_ip4 */ );
2793}
2794
Filip Tehlare275bed2019-03-06 00:06:56 -08002795VLIB_NODE_FN (tcp6_listen_node) (vlib_main_t * vm, vlib_node_runtime_t * node,
2796 vlib_frame_t * from_frame)
Dave Barach68b0fb02017-02-28 15:15:56 -05002797{
2798 return tcp46_listen_inline (vm, node, from_frame, 0 /* is_ip4 */ );
2799}
2800
2801/* *INDENT-OFF* */
2802VLIB_REGISTER_NODE (tcp4_listen_node) =
2803{
Dave Barach68b0fb02017-02-28 15:15:56 -05002804 .name = "tcp4-listen",
2805 /* Takes a vector of packets. */
2806 .vector_size = sizeof (u32),
2807 .n_errors = TCP_N_ERROR,
Filip Tehlar0c4931c2021-10-06 09:47:41 +00002808 .error_counters = tcp_input_error_counters,
Dave Barach68b0fb02017-02-28 15:15:56 -05002809 .n_next_nodes = TCP_LISTEN_N_NEXT,
2810 .next_nodes =
2811 {
2812#define _(s,n) [TCP_LISTEN_NEXT_##s] = n,
2813 foreach_tcp_state_next
2814#undef _
2815 },
Clement Durand6cf260c2017-04-13 13:27:04 +02002816 .format_trace = format_tcp_rx_trace_short,
Dave Barach68b0fb02017-02-28 15:15:56 -05002817};
2818/* *INDENT-ON* */
2819
Dave Barach68b0fb02017-02-28 15:15:56 -05002820/* *INDENT-OFF* */
2821VLIB_REGISTER_NODE (tcp6_listen_node) =
2822{
Dave Barach68b0fb02017-02-28 15:15:56 -05002823 .name = "tcp6-listen",
2824 /* Takes a vector of packets. */
2825 .vector_size = sizeof (u32),
2826 .n_errors = TCP_N_ERROR,
Filip Tehlar0c4931c2021-10-06 09:47:41 +00002827 .error_counters = tcp_input_error_counters,
Dave Barach68b0fb02017-02-28 15:15:56 -05002828 .n_next_nodes = TCP_LISTEN_N_NEXT,
2829 .next_nodes =
2830 {
2831#define _(s,n) [TCP_LISTEN_NEXT_##s] = n,
2832 foreach_tcp_state_next
2833#undef _
2834 },
Clement Durand6cf260c2017-04-13 13:27:04 +02002835 .format_trace = format_tcp_rx_trace_short,
Dave Barach68b0fb02017-02-28 15:15:56 -05002836};
2837/* *INDENT-ON* */
2838
Dave Barach68b0fb02017-02-28 15:15:56 -05002839#define foreach_tcp4_input_next \
Vijayabhaskar Katamreddyce074122017-11-15 13:50:26 -08002840 _ (DROP, "ip4-drop") \
Dave Barach68b0fb02017-02-28 15:15:56 -05002841 _ (LISTEN, "tcp4-listen") \
2842 _ (RCV_PROCESS, "tcp4-rcv-process") \
2843 _ (SYN_SENT, "tcp4-syn-sent") \
2844 _ (ESTABLISHED, "tcp4-established") \
Pierre Pfister7fe51f32017-09-20 08:48:36 +02002845 _ (RESET, "tcp4-reset") \
Vijayabhaskar Katamreddyce074122017-11-15 13:50:26 -08002846 _ (PUNT, "ip4-punt")
Dave Barach68b0fb02017-02-28 15:15:56 -05002847
2848#define foreach_tcp6_input_next \
Vijayabhaskar Katamreddyce074122017-11-15 13:50:26 -08002849 _ (DROP, "ip6-drop") \
Dave Barach68b0fb02017-02-28 15:15:56 -05002850 _ (LISTEN, "tcp6-listen") \
2851 _ (RCV_PROCESS, "tcp6-rcv-process") \
2852 _ (SYN_SENT, "tcp6-syn-sent") \
2853 _ (ESTABLISHED, "tcp6-established") \
Pierre Pfister7fe51f32017-09-20 08:48:36 +02002854 _ (RESET, "tcp6-reset") \
Vijayabhaskar Katamreddyce074122017-11-15 13:50:26 -08002855 _ (PUNT, "ip6-punt")
Dave Barach68b0fb02017-02-28 15:15:56 -05002856
Dave Barach68b0fb02017-02-28 15:15:56 -05002857#define filter_flags (TCP_FLAG_SYN|TCP_FLAG_ACK|TCP_FLAG_RST|TCP_FLAG_FIN)
2858
Florin Coras0c8a3bc2018-06-14 17:11:56 -07002859static void
Florin Coras0c8a3bc2018-06-14 17:11:56 -07002860tcp_input_set_error_next (tcp_main_t * tm, u16 * next, u32 * error, u8 is_ip4)
2861{
Florin Corasb5e55a22019-01-10 12:42:47 -08002862 if (*error == TCP_ERROR_FILTERED || *error == TCP_ERROR_WRONG_THREAD)
Florin Coras0c8a3bc2018-06-14 17:11:56 -07002863 {
2864 *next = TCP_INPUT_NEXT_DROP;
2865 }
2866 else if ((is_ip4 && tm->punt_unknown4) || (!is_ip4 && tm->punt_unknown6))
2867 {
2868 *next = TCP_INPUT_NEXT_PUNT;
2869 *error = TCP_ERROR_PUNT;
2870 }
2871 else
2872 {
2873 *next = TCP_INPUT_NEXT_RESET;
2874 *error = TCP_ERROR_NO_LISTENER;
2875 }
2876}
Dave Barach68b0fb02017-02-28 15:15:56 -05002877
Florin Coras0c8a3bc2018-06-14 17:11:56 -07002878static inline void
Florin Coras045a6ae2023-02-28 12:43:39 -08002879tcp_input_dispatch_buffer (tcp_main_t *tm, tcp_connection_t *tc,
2880 vlib_buffer_t *b, u16 *next, u16 *err_counters)
Florin Coras0c8a3bc2018-06-14 17:11:56 -07002881{
2882 tcp_header_t *tcp;
Florin Corasdeb6f782020-02-11 02:42:36 +00002883 u32 error;
Florin Coras0c8a3bc2018-06-14 17:11:56 -07002884 u8 flags;
2885
2886 tcp = tcp_buffer_hdr (b);
2887 flags = tcp->flags & filter_flags;
2888 *next = tm->dispatch_table[tc->state][flags].next;
Florin Corasdeb6f782020-02-11 02:42:36 +00002889 error = tm->dispatch_table[tc->state][flags].error;
Florin Corasedfe0ee2019-07-29 18:13:25 -07002890 tc->segs_in += 1;
Florin Coras0c8a3bc2018-06-14 17:11:56 -07002891
Michal Kalderon3effadc2021-08-08 04:30:39 -07002892 /* Track connection state when packet was received. It is required
2893 * for @ref tcp46_listen_inline to detect whether we reached
2894 * the node as a result of a SYN packet received while in time-wait
2895 * state. In this case the connection_index in vnet buffer will point
2896 * to the existing tcp connection and not the listener
2897 */
Florin Corasee1cb462020-12-29 09:42:58 -08002898 vnet_buffer (b)->tcp.flags = tc->state;
2899
Florin Corasdeb6f782020-02-11 02:42:36 +00002900 if (PREDICT_FALSE (error != TCP_ERROR_NONE))
Florin Coras0c8a3bc2018-06-14 17:11:56 -07002901 {
Florin Coras045a6ae2023-02-28 12:43:39 -08002902 tcp_inc_err_counter (err_counters, error, 1);
Florin Corasdeb6f782020-02-11 02:42:36 +00002903 if (error == TCP_ERROR_DISPATCH)
Florin Corasa9d5bea2018-12-17 08:24:19 -08002904 clib_warning ("tcp conn %u disp error state %U flags %U",
Florin Coras360336f2020-02-13 18:46:18 +00002905 tc->c_c_index, format_tcp_state, tc->state,
Florin Corasa9d5bea2018-12-17 08:24:19 -08002906 format_tcp_flags, (int) flags);
Florin Coras0c8a3bc2018-06-14 17:11:56 -07002907 }
2908}
2909
2910always_inline uword
2911tcp46_input_inline (vlib_main_t * vm, vlib_node_runtime_t * node,
Vladimir Kropylev456d2f92019-07-16 21:22:29 +03002912 vlib_frame_t * frame, int is_ip4, u8 is_nolookup)
Florin Coras0c8a3bc2018-06-14 17:11:56 -07002913{
2914 u32 n_left_from, *from, thread_index = vm->thread_index;
2915 tcp_main_t *tm = vnet_get_tcp_main ();
2916 vlib_buffer_t *bufs[VLIB_FRAME_SIZE], **b;
2917 u16 nexts[VLIB_FRAME_SIZE], *next;
Filip Tehlar5035bf02023-02-20 13:46:32 +01002918 u16 err_counters[TCP_N_ERROR] = { 0 };
Florin Coras0c8a3bc2018-06-14 17:11:56 -07002919
Florin Coras8f10b902021-04-02 18:32:00 -07002920 tcp_update_time_now (tcp_get_worker (thread_index));
Florin Coras0c8a3bc2018-06-14 17:11:56 -07002921
2922 from = vlib_frame_vector_args (frame);
2923 n_left_from = frame->n_vectors;
2924 vlib_get_buffers (vm, from, bufs, n_left_from);
2925
2926 b = bufs;
2927 next = nexts;
2928
2929 while (n_left_from >= 4)
2930 {
2931 u32 error0 = TCP_ERROR_NO_LISTENER, error1 = TCP_ERROR_NO_LISTENER;
2932 tcp_connection_t *tc0, *tc1;
2933
2934 {
2935 vlib_prefetch_buffer_header (b[2], STORE);
2936 CLIB_PREFETCH (b[2]->data, 2 * CLIB_CACHE_LINE_BYTES, LOAD);
2937
2938 vlib_prefetch_buffer_header (b[3], STORE);
2939 CLIB_PREFETCH (b[3]->data, 2 * CLIB_CACHE_LINE_BYTES, LOAD);
2940 }
2941
2942 next[0] = next[1] = TCP_INPUT_NEXT_DROP;
2943
Vladimir Kropylev456d2f92019-07-16 21:22:29 +03002944 tc0 = tcp_input_lookup_buffer (b[0], thread_index, &error0, is_ip4,
2945 is_nolookup);
2946 tc1 = tcp_input_lookup_buffer (b[1], thread_index, &error1, is_ip4,
2947 is_nolookup);
Florin Coras0c8a3bc2018-06-14 17:11:56 -07002948
2949 if (PREDICT_TRUE (!tc0 + !tc1 == 0))
2950 {
Srikanth Akulacf4c2102019-11-06 18:53:13 -08002951 ASSERT (tcp_lookup_is_valid (tc0, b[0], tcp_buffer_hdr (b[0])));
2952 ASSERT (tcp_lookup_is_valid (tc1, b[1], tcp_buffer_hdr (b[1])));
Florin Coras0c8a3bc2018-06-14 17:11:56 -07002953
2954 vnet_buffer (b[0])->tcp.connection_index = tc0->c_c_index;
2955 vnet_buffer (b[1])->tcp.connection_index = tc1->c_c_index;
2956
Florin Coras045a6ae2023-02-28 12:43:39 -08002957 tcp_input_dispatch_buffer (tm, tc0, b[0], &next[0], err_counters);
2958 tcp_input_dispatch_buffer (tm, tc1, b[1], &next[1], err_counters);
Florin Coras0c8a3bc2018-06-14 17:11:56 -07002959 }
2960 else
2961 {
2962 if (PREDICT_TRUE (tc0 != 0))
2963 {
Srikanth Akulacf4c2102019-11-06 18:53:13 -08002964 ASSERT (tcp_lookup_is_valid (tc0, b[0], tcp_buffer_hdr (b[0])));
Florin Coras0c8a3bc2018-06-14 17:11:56 -07002965 vnet_buffer (b[0])->tcp.connection_index = tc0->c_c_index;
Florin Coras045a6ae2023-02-28 12:43:39 -08002966 tcp_input_dispatch_buffer (tm, tc0, b[0], &next[0],
2967 err_counters);
Florin Coras0c8a3bc2018-06-14 17:11:56 -07002968 }
2969 else
Florin Corasdeb6f782020-02-11 02:42:36 +00002970 {
2971 tcp_input_set_error_next (tm, &next[0], &error0, is_ip4);
Filip Tehlar5035bf02023-02-20 13:46:32 +01002972 tcp_inc_err_counter (err_counters, error0, 1);
Florin Corasdeb6f782020-02-11 02:42:36 +00002973 }
Florin Coras0c8a3bc2018-06-14 17:11:56 -07002974
2975 if (PREDICT_TRUE (tc1 != 0))
2976 {
Srikanth Akulacf4c2102019-11-06 18:53:13 -08002977 ASSERT (tcp_lookup_is_valid (tc1, b[1], tcp_buffer_hdr (b[1])));
Florin Coras0c8a3bc2018-06-14 17:11:56 -07002978 vnet_buffer (b[1])->tcp.connection_index = tc1->c_c_index;
Florin Coras045a6ae2023-02-28 12:43:39 -08002979 tcp_input_dispatch_buffer (tm, tc1, b[1], &next[1],
2980 err_counters);
Florin Coras0c8a3bc2018-06-14 17:11:56 -07002981 }
2982 else
Florin Corasdeb6f782020-02-11 02:42:36 +00002983 {
2984 tcp_input_set_error_next (tm, &next[1], &error1, is_ip4);
Filip Tehlar5035bf02023-02-20 13:46:32 +01002985 tcp_inc_err_counter (err_counters, error1, 1);
Florin Corasdeb6f782020-02-11 02:42:36 +00002986 }
Florin Coras0c8a3bc2018-06-14 17:11:56 -07002987 }
2988
2989 b += 2;
2990 next += 2;
2991 n_left_from -= 2;
2992 }
2993 while (n_left_from > 0)
2994 {
2995 tcp_connection_t *tc0;
2996 u32 error0 = TCP_ERROR_NO_LISTENER;
2997
2998 if (n_left_from > 1)
2999 {
3000 vlib_prefetch_buffer_header (b[1], STORE);
3001 CLIB_PREFETCH (b[1]->data, 2 * CLIB_CACHE_LINE_BYTES, LOAD);
3002 }
3003
3004 next[0] = TCP_INPUT_NEXT_DROP;
Vladimir Kropylev456d2f92019-07-16 21:22:29 +03003005 tc0 = tcp_input_lookup_buffer (b[0], thread_index, &error0, is_ip4,
3006 is_nolookup);
Florin Coras0c8a3bc2018-06-14 17:11:56 -07003007 if (PREDICT_TRUE (tc0 != 0))
3008 {
Srikanth Akulacf4c2102019-11-06 18:53:13 -08003009 ASSERT (tcp_lookup_is_valid (tc0, b[0], tcp_buffer_hdr (b[0])));
Florin Coras0c8a3bc2018-06-14 17:11:56 -07003010 vnet_buffer (b[0])->tcp.connection_index = tc0->c_c_index;
Florin Coras045a6ae2023-02-28 12:43:39 -08003011 tcp_input_dispatch_buffer (tm, tc0, b[0], &next[0], err_counters);
Florin Coras0c8a3bc2018-06-14 17:11:56 -07003012 }
3013 else
Florin Corasdeb6f782020-02-11 02:42:36 +00003014 {
3015 tcp_input_set_error_next (tm, &next[0], &error0, is_ip4);
Filip Tehlar5035bf02023-02-20 13:46:32 +01003016 tcp_inc_err_counter (err_counters, error0, 1);
Florin Corasdeb6f782020-02-11 02:42:36 +00003017 }
Florin Coras0c8a3bc2018-06-14 17:11:56 -07003018
3019 b += 1;
3020 next += 1;
3021 n_left_from -= 1;
3022 }
3023
3024 if (PREDICT_FALSE (node->flags & VLIB_NODE_FLAG_TRACE))
Filip Tehlarbc4dc162023-04-17 12:22:12 +02003025 tcp_input_trace_frame (vm, node, bufs, nexts, frame->n_vectors, is_ip4);
Florin Coras0c8a3bc2018-06-14 17:11:56 -07003026
Filip Tehlar5035bf02023-02-20 13:46:32 +01003027 tcp_store_err_counters (input, err_counters);
Florin Coras0c8a3bc2018-06-14 17:11:56 -07003028 vlib_buffer_enqueue_to_next (vm, node, from, nexts, frame->n_vectors);
3029 return frame->n_vectors;
Dave Barach68b0fb02017-02-28 15:15:56 -05003030}
3031
Vladimir Kropylev456d2f92019-07-16 21:22:29 +03003032VLIB_NODE_FN (tcp4_input_nolookup_node) (vlib_main_t * vm,
3033 vlib_node_runtime_t * node,
3034 vlib_frame_t * from_frame)
3035{
3036 return tcp46_input_inline (vm, node, from_frame, 1 /* is_ip4 */ ,
3037 1 /* is_nolookup */ );
3038}
3039
3040VLIB_NODE_FN (tcp6_input_nolookup_node) (vlib_main_t * vm,
3041 vlib_node_runtime_t * node,
3042 vlib_frame_t * from_frame)
3043{
3044 return tcp46_input_inline (vm, node, from_frame, 0 /* is_ip4 */ ,
3045 1 /* is_nolookup */ );
3046}
3047
3048/* *INDENT-OFF* */
3049VLIB_REGISTER_NODE (tcp4_input_nolookup_node) =
3050{
3051 .name = "tcp4-input-nolookup",
3052 /* Takes a vector of packets. */
3053 .vector_size = sizeof (u32),
3054 .n_errors = TCP_N_ERROR,
Filip Tehlar0c4931c2021-10-06 09:47:41 +00003055 .error_counters = tcp_input_error_counters,
Vladimir Kropylev456d2f92019-07-16 21:22:29 +03003056 .n_next_nodes = TCP_INPUT_N_NEXT,
3057 .next_nodes =
3058 {
3059#define _(s,n) [TCP_INPUT_NEXT_##s] = n,
3060 foreach_tcp4_input_next
3061#undef _
3062 },
3063 .format_buffer = format_tcp_header,
3064 .format_trace = format_tcp_rx_trace,
3065};
3066/* *INDENT-ON* */
3067
3068/* *INDENT-OFF* */
3069VLIB_REGISTER_NODE (tcp6_input_nolookup_node) =
3070{
3071 .name = "tcp6-input-nolookup",
3072 /* Takes a vector of packets. */
3073 .vector_size = sizeof (u32),
3074 .n_errors = TCP_N_ERROR,
Filip Tehlar0c4931c2021-10-06 09:47:41 +00003075 .error_counters = tcp_input_error_counters,
Vladimir Kropylev456d2f92019-07-16 21:22:29 +03003076 .n_next_nodes = TCP_INPUT_N_NEXT,
3077 .next_nodes =
3078 {
3079#define _(s,n) [TCP_INPUT_NEXT_##s] = n,
3080 foreach_tcp6_input_next
3081#undef _
3082 },
3083 .format_buffer = format_tcp_header,
3084 .format_trace = format_tcp_rx_trace,
3085};
3086/* *INDENT-ON* */
3087
Filip Tehlare275bed2019-03-06 00:06:56 -08003088VLIB_NODE_FN (tcp4_input_node) (vlib_main_t * vm, vlib_node_runtime_t * node,
3089 vlib_frame_t * from_frame)
Dave Barach68b0fb02017-02-28 15:15:56 -05003090{
Vladimir Kropylev456d2f92019-07-16 21:22:29 +03003091 return tcp46_input_inline (vm, node, from_frame, 1 /* is_ip4 */ ,
3092 0 /* is_nolookup */ );
Dave Barach68b0fb02017-02-28 15:15:56 -05003093}
3094
Filip Tehlare275bed2019-03-06 00:06:56 -08003095VLIB_NODE_FN (tcp6_input_node) (vlib_main_t * vm, vlib_node_runtime_t * node,
3096 vlib_frame_t * from_frame)
Dave Barach68b0fb02017-02-28 15:15:56 -05003097{
Vladimir Kropylev456d2f92019-07-16 21:22:29 +03003098 return tcp46_input_inline (vm, node, from_frame, 0 /* is_ip4 */ ,
3099 0 /* is_nolookup */ );
Dave Barach68b0fb02017-02-28 15:15:56 -05003100}
3101
3102/* *INDENT-OFF* */
3103VLIB_REGISTER_NODE (tcp4_input_node) =
3104{
Dave Barach68b0fb02017-02-28 15:15:56 -05003105 .name = "tcp4-input",
3106 /* Takes a vector of packets. */
3107 .vector_size = sizeof (u32),
3108 .n_errors = TCP_N_ERROR,
Filip Tehlar0c4931c2021-10-06 09:47:41 +00003109 .error_counters = tcp_input_error_counters,
Dave Barach68b0fb02017-02-28 15:15:56 -05003110 .n_next_nodes = TCP_INPUT_N_NEXT,
3111 .next_nodes =
3112 {
3113#define _(s,n) [TCP_INPUT_NEXT_##s] = n,
3114 foreach_tcp4_input_next
3115#undef _
3116 },
3117 .format_buffer = format_tcp_header,
3118 .format_trace = format_tcp_rx_trace,
3119};
3120/* *INDENT-ON* */
3121
Dave Barach68b0fb02017-02-28 15:15:56 -05003122/* *INDENT-OFF* */
3123VLIB_REGISTER_NODE (tcp6_input_node) =
3124{
Dave Barach68b0fb02017-02-28 15:15:56 -05003125 .name = "tcp6-input",
3126 /* Takes a vector of packets. */
3127 .vector_size = sizeof (u32),
3128 .n_errors = TCP_N_ERROR,
Filip Tehlar0c4931c2021-10-06 09:47:41 +00003129 .error_counters = tcp_input_error_counters,
Dave Barach68b0fb02017-02-28 15:15:56 -05003130 .n_next_nodes = TCP_INPUT_N_NEXT,
3131 .next_nodes =
3132 {
3133#define _(s,n) [TCP_INPUT_NEXT_##s] = n,
3134 foreach_tcp6_input_next
3135#undef _
3136 },
3137 .format_buffer = format_tcp_header,
3138 .format_trace = format_tcp_rx_trace,
3139};
3140/* *INDENT-ON* */
3141
Filip Tehlare275bed2019-03-06 00:06:56 -08003142#ifndef CLIB_MARCH_VARIANT
Florin Coras04ae8272021-04-12 19:55:37 -07003143void
3144tcp_check_gso (tcp_connection_t *tc)
3145{
3146 tcp_check_tx_offload (tc, tc->c_is_ip4);
3147}
3148
Dave Barach68b0fb02017-02-28 15:15:56 -05003149static void
3150tcp_dispatch_table_init (tcp_main_t * tm)
3151{
3152 int i, j;
3153 for (i = 0; i < ARRAY_LEN (tm->dispatch_table); i++)
3154 for (j = 0; j < ARRAY_LEN (tm->dispatch_table[i]); j++)
3155 {
3156 tm->dispatch_table[i][j].next = TCP_INPUT_NEXT_DROP;
3157 tm->dispatch_table[i][j].error = TCP_ERROR_DISPATCH;
3158 }
3159
3160#define _(t,f,n,e) \
3161do { \
3162 tm->dispatch_table[TCP_STATE_##t][f].next = (n); \
3163 tm->dispatch_table[TCP_STATE_##t][f].error = (e); \
3164} while (0)
3165
Florin Coras678a6572018-12-17 21:31:25 -08003166 /* RFC 793: In LISTEN if RST drop and if ACK return RST */
Florin Coras5c0f1662018-12-19 01:38:57 -08003167 _(LISTEN, 0, TCP_INPUT_NEXT_DROP, TCP_ERROR_SEGMENT_INVALID);
Florin Coras678a6572018-12-17 21:31:25 -08003168 _(LISTEN, TCP_FLAG_ACK, TCP_INPUT_NEXT_RESET, TCP_ERROR_ACK_INVALID);
3169 _(LISTEN, TCP_FLAG_RST, TCP_INPUT_NEXT_DROP, TCP_ERROR_INVALID_CONNECTION);
Dave Barach68b0fb02017-02-28 15:15:56 -05003170 _(LISTEN, TCP_FLAG_SYN, TCP_INPUT_NEXT_LISTEN, TCP_ERROR_NONE);
Florin Coras678a6572018-12-17 21:31:25 -08003171 _(LISTEN, TCP_FLAG_SYN | TCP_FLAG_ACK, TCP_INPUT_NEXT_RESET,
3172 TCP_ERROR_ACK_INVALID);
3173 _(LISTEN, TCP_FLAG_SYN | TCP_FLAG_RST, TCP_INPUT_NEXT_DROP,
3174 TCP_ERROR_SEGMENT_INVALID);
3175 _(LISTEN, TCP_FLAG_SYN | TCP_FLAG_RST | TCP_FLAG_ACK, TCP_INPUT_NEXT_DROP,
3176 TCP_ERROR_SEGMENT_INVALID);
3177 _(LISTEN, TCP_FLAG_RST | TCP_FLAG_ACK, TCP_INPUT_NEXT_DROP,
3178 TCP_ERROR_INVALID_CONNECTION);
3179 _(LISTEN, TCP_FLAG_FIN, TCP_INPUT_NEXT_RESET, TCP_ERROR_SEGMENT_INVALID);
Dave Barach2c25a622017-06-26 11:35:07 -04003180 _(LISTEN, TCP_FLAG_FIN | TCP_FLAG_ACK, TCP_INPUT_NEXT_RESET,
Florin Coras678a6572018-12-17 21:31:25 -08003181 TCP_ERROR_SEGMENT_INVALID);
3182 _(LISTEN, TCP_FLAG_FIN | TCP_FLAG_RST, TCP_INPUT_NEXT_DROP,
3183 TCP_ERROR_SEGMENT_INVALID);
3184 _(LISTEN, TCP_FLAG_FIN | TCP_FLAG_RST | TCP_FLAG_ACK, TCP_INPUT_NEXT_DROP,
Florin Corasdeb6f782020-02-11 02:42:36 +00003185 TCP_ERROR_SEGMENT_INVALID);
Florin Coras678a6572018-12-17 21:31:25 -08003186 _(LISTEN, TCP_FLAG_FIN | TCP_FLAG_SYN, TCP_INPUT_NEXT_DROP,
3187 TCP_ERROR_SEGMENT_INVALID);
3188 _(LISTEN, TCP_FLAG_FIN | TCP_FLAG_SYN | TCP_FLAG_ACK, TCP_INPUT_NEXT_DROP,
3189 TCP_ERROR_SEGMENT_INVALID);
3190 _(LISTEN, TCP_FLAG_FIN | TCP_FLAG_SYN | TCP_FLAG_RST, TCP_INPUT_NEXT_DROP,
3191 TCP_ERROR_SEGMENT_INVALID);
3192 _(LISTEN, TCP_FLAG_FIN | TCP_FLAG_SYN | TCP_FLAG_RST | TCP_FLAG_ACK,
3193 TCP_INPUT_NEXT_DROP, TCP_ERROR_SEGMENT_INVALID);
Dave Barach68b0fb02017-02-28 15:15:56 -05003194 /* ACK for for a SYN-ACK -> tcp-rcv-process. */
3195 _(SYN_RCVD, TCP_FLAG_ACK, TCP_INPUT_NEXT_RCV_PROCESS, TCP_ERROR_NONE);
Florin Corasc28764f2017-04-26 00:08:42 -07003196 _(SYN_RCVD, TCP_FLAG_RST, TCP_INPUT_NEXT_RCV_PROCESS, TCP_ERROR_NONE);
Florin Corase8460c72018-09-24 14:40:40 -07003197 _(SYN_RCVD, TCP_FLAG_RST | TCP_FLAG_ACK, TCP_INPUT_NEXT_RCV_PROCESS,
3198 TCP_ERROR_NONE);
Dave Barach2c25a622017-06-26 11:35:07 -04003199 _(SYN_RCVD, TCP_FLAG_SYN, TCP_INPUT_NEXT_RCV_PROCESS, TCP_ERROR_NONE);
Florin Coras678a6572018-12-17 21:31:25 -08003200 _(SYN_RCVD, TCP_FLAG_SYN | TCP_FLAG_ACK, TCP_INPUT_NEXT_RCV_PROCESS,
3201 TCP_ERROR_NONE);
3202 _(SYN_RCVD, TCP_FLAG_SYN | TCP_FLAG_RST, TCP_INPUT_NEXT_RCV_PROCESS,
3203 TCP_ERROR_NONE);
3204 _(SYN_RCVD, TCP_FLAG_SYN | TCP_FLAG_RST | TCP_FLAG_ACK,
3205 TCP_INPUT_NEXT_RCV_PROCESS, TCP_ERROR_NONE);
3206 _(SYN_RCVD, TCP_FLAG_FIN, TCP_INPUT_NEXT_RCV_PROCESS, TCP_ERROR_NONE);
Florin Corasc01c4452018-09-20 18:36:54 -07003207 _(SYN_RCVD, TCP_FLAG_FIN | TCP_FLAG_ACK, TCP_INPUT_NEXT_RCV_PROCESS,
3208 TCP_ERROR_NONE);
Florin Coras678a6572018-12-17 21:31:25 -08003209 _(SYN_RCVD, TCP_FLAG_FIN | TCP_FLAG_RST, TCP_INPUT_NEXT_RCV_PROCESS,
3210 TCP_ERROR_NONE);
3211 _(SYN_RCVD, TCP_FLAG_FIN | TCP_FLAG_RST | TCP_FLAG_ACK,
3212 TCP_INPUT_NEXT_RCV_PROCESS, TCP_ERROR_NONE);
3213 _(SYN_RCVD, TCP_FLAG_FIN | TCP_FLAG_SYN, TCP_INPUT_NEXT_RCV_PROCESS,
3214 TCP_ERROR_NONE);
3215 _(SYN_RCVD, TCP_FLAG_FIN | TCP_FLAG_SYN | TCP_FLAG_RST,
3216 TCP_INPUT_NEXT_RCV_PROCESS, TCP_ERROR_NONE);
3217 _(SYN_RCVD, TCP_FLAG_FIN | TCP_FLAG_SYN | TCP_FLAG_ACK,
3218 TCP_INPUT_NEXT_RCV_PROCESS, TCP_ERROR_NONE);
3219 _(SYN_RCVD, TCP_FLAG_FIN | TCP_FLAG_SYN | TCP_FLAG_RST | TCP_FLAG_ACK,
3220 TCP_INPUT_NEXT_RCV_PROCESS, TCP_ERROR_NONE);
3221 _(SYN_RCVD, 0, TCP_INPUT_NEXT_DROP, TCP_ERROR_SEGMENT_INVALID);
Dave Barach68b0fb02017-02-28 15:15:56 -05003222 /* SYN-ACK for a SYN */
3223 _(SYN_SENT, TCP_FLAG_SYN | TCP_FLAG_ACK, TCP_INPUT_NEXT_SYN_SENT,
3224 TCP_ERROR_NONE);
3225 _(SYN_SENT, TCP_FLAG_ACK, TCP_INPUT_NEXT_SYN_SENT, TCP_ERROR_NONE);
3226 _(SYN_SENT, TCP_FLAG_RST, TCP_INPUT_NEXT_SYN_SENT, TCP_ERROR_NONE);
3227 _(SYN_SENT, TCP_FLAG_RST | TCP_FLAG_ACK, TCP_INPUT_NEXT_SYN_SENT,
3228 TCP_ERROR_NONE);
Florin Coras222e1f412019-02-16 20:47:32 -08003229 _(SYN_SENT, TCP_FLAG_FIN, TCP_INPUT_NEXT_SYN_SENT, TCP_ERROR_NONE);
3230 _(SYN_SENT, TCP_FLAG_FIN | TCP_FLAG_ACK, TCP_INPUT_NEXT_SYN_SENT,
3231 TCP_ERROR_NONE);
Dave Barach68b0fb02017-02-28 15:15:56 -05003232 /* ACK for for established connection -> tcp-established. */
3233 _(ESTABLISHED, TCP_FLAG_ACK, TCP_INPUT_NEXT_ESTABLISHED, TCP_ERROR_NONE);
3234 /* FIN for for established connection -> tcp-established. */
3235 _(ESTABLISHED, TCP_FLAG_FIN, TCP_INPUT_NEXT_ESTABLISHED, TCP_ERROR_NONE);
3236 _(ESTABLISHED, TCP_FLAG_FIN | TCP_FLAG_ACK, TCP_INPUT_NEXT_ESTABLISHED,
3237 TCP_ERROR_NONE);
Florin Coras678a6572018-12-17 21:31:25 -08003238 _(ESTABLISHED, TCP_FLAG_FIN | TCP_FLAG_RST, TCP_INPUT_NEXT_ESTABLISHED,
3239 TCP_ERROR_NONE);
3240 _(ESTABLISHED, TCP_FLAG_FIN | TCP_FLAG_RST | TCP_FLAG_ACK,
3241 TCP_INPUT_NEXT_ESTABLISHED, TCP_ERROR_NONE);
3242 _(ESTABLISHED, TCP_FLAG_FIN | TCP_FLAG_SYN, TCP_INPUT_NEXT_ESTABLISHED,
3243 TCP_ERROR_NONE);
3244 _(ESTABLISHED, TCP_FLAG_FIN | TCP_FLAG_SYN | TCP_FLAG_ACK,
3245 TCP_INPUT_NEXT_ESTABLISHED, TCP_ERROR_NONE);
3246 _(ESTABLISHED, TCP_FLAG_FIN | TCP_FLAG_SYN | TCP_FLAG_RST,
3247 TCP_INPUT_NEXT_ESTABLISHED, TCP_ERROR_NONE);
3248 _(ESTABLISHED, TCP_FLAG_FIN | TCP_FLAG_SYN | TCP_FLAG_RST | TCP_FLAG_ACK,
3249 TCP_INPUT_NEXT_ESTABLISHED, TCP_ERROR_NONE);
Florin Corasd79b41e2017-03-04 05:37:52 -08003250 _(ESTABLISHED, TCP_FLAG_RST, TCP_INPUT_NEXT_ESTABLISHED, TCP_ERROR_NONE);
Florin Corasdc629cd2017-05-09 00:52:37 -07003251 _(ESTABLISHED, TCP_FLAG_RST | TCP_FLAG_ACK, TCP_INPUT_NEXT_ESTABLISHED,
3252 TCP_ERROR_NONE);
Dave Barach2c25a622017-06-26 11:35:07 -04003253 _(ESTABLISHED, TCP_FLAG_SYN, TCP_INPUT_NEXT_ESTABLISHED, TCP_ERROR_NONE);
Florin Coras3eb50622017-07-13 01:24:57 -04003254 _(ESTABLISHED, TCP_FLAG_SYN | TCP_FLAG_ACK, TCP_INPUT_NEXT_ESTABLISHED,
3255 TCP_ERROR_NONE);
Florin Coras678a6572018-12-17 21:31:25 -08003256 _(ESTABLISHED, TCP_FLAG_SYN | TCP_FLAG_RST, TCP_INPUT_NEXT_ESTABLISHED,
3257 TCP_ERROR_NONE);
3258 _(ESTABLISHED, TCP_FLAG_SYN | TCP_FLAG_RST | TCP_FLAG_ACK,
3259 TCP_INPUT_NEXT_ESTABLISHED, TCP_ERROR_NONE);
3260 _(ESTABLISHED, 0, TCP_INPUT_NEXT_DROP, TCP_ERROR_SEGMENT_INVALID);
Dave Barach68b0fb02017-02-28 15:15:56 -05003261 /* ACK or FIN-ACK to our FIN */
3262 _(FIN_WAIT_1, TCP_FLAG_ACK, TCP_INPUT_NEXT_RCV_PROCESS, TCP_ERROR_NONE);
3263 _(FIN_WAIT_1, TCP_FLAG_ACK | TCP_FLAG_FIN, TCP_INPUT_NEXT_RCV_PROCESS,
3264 TCP_ERROR_NONE);
3265 /* FIN in reply to our FIN from the other side */
Florin Coras76bc1302018-12-23 23:36:36 -08003266 _(FIN_WAIT_1, 0, TCP_INPUT_NEXT_DROP, TCP_ERROR_SEGMENT_INVALID);
Dave Barach68b0fb02017-02-28 15:15:56 -05003267 _(FIN_WAIT_1, TCP_FLAG_FIN, TCP_INPUT_NEXT_RCV_PROCESS, TCP_ERROR_NONE);
Florin Coras76bc1302018-12-23 23:36:36 -08003268 _(FIN_WAIT_1, TCP_FLAG_FIN | TCP_FLAG_SYN, TCP_INPUT_NEXT_RCV_PROCESS,
3269 TCP_ERROR_NONE);
3270 _(FIN_WAIT_1, TCP_FLAG_FIN | TCP_FLAG_SYN | TCP_FLAG_ACK,
3271 TCP_INPUT_NEXT_RCV_PROCESS, TCP_ERROR_NONE);
3272 _(FIN_WAIT_1, TCP_FLAG_FIN | TCP_FLAG_SYN | TCP_FLAG_RST,
3273 TCP_INPUT_NEXT_RCV_PROCESS, TCP_ERROR_NONE);
3274 _(FIN_WAIT_1, TCP_FLAG_FIN | TCP_FLAG_SYN | TCP_FLAG_RST | TCP_FLAG_ACK,
3275 TCP_INPUT_NEXT_RCV_PROCESS, TCP_ERROR_NONE);
3276 _(FIN_WAIT_1, TCP_FLAG_FIN | TCP_FLAG_RST, TCP_INPUT_NEXT_RCV_PROCESS,
3277 TCP_ERROR_NONE);
3278 _(FIN_WAIT_1, TCP_FLAG_FIN | TCP_FLAG_RST | TCP_FLAG_ACK,
3279 TCP_INPUT_NEXT_RCV_PROCESS, TCP_ERROR_NONE);
Florin Coras5c0f1662018-12-19 01:38:57 -08003280 _(FIN_WAIT_1, TCP_FLAG_SYN, TCP_INPUT_NEXT_RCV_PROCESS, TCP_ERROR_NONE);
Florin Coras76bc1302018-12-23 23:36:36 -08003281 _(FIN_WAIT_1, TCP_FLAG_SYN | TCP_FLAG_RST, TCP_INPUT_NEXT_RCV_PROCESS,
3282 TCP_ERROR_NONE);
3283 _(FIN_WAIT_1, TCP_FLAG_SYN | TCP_FLAG_ACK, TCP_INPUT_NEXT_RCV_PROCESS,
3284 TCP_ERROR_NONE);
3285 _(FIN_WAIT_1, TCP_FLAG_SYN | TCP_FLAG_RST | TCP_FLAG_ACK,
3286 TCP_INPUT_NEXT_RCV_PROCESS, TCP_ERROR_NONE);
Dave Barach2c25a622017-06-26 11:35:07 -04003287 _(FIN_WAIT_1, TCP_FLAG_RST, TCP_INPUT_NEXT_RCV_PROCESS, TCP_ERROR_NONE);
Florin Corase96bf632018-12-18 22:44:27 -08003288 _(FIN_WAIT_1, TCP_FLAG_RST | TCP_FLAG_ACK, TCP_INPUT_NEXT_RCV_PROCESS,
3289 TCP_ERROR_NONE);
Florin Corasb56fcf12019-01-02 10:10:08 -08003290 _(CLOSING, 0, TCP_INPUT_NEXT_DROP, TCP_ERROR_SEGMENT_INVALID);
Florin Coras56318932018-05-23 20:44:12 -07003291 _(CLOSING, TCP_FLAG_ACK, TCP_INPUT_NEXT_RCV_PROCESS, TCP_ERROR_NONE);
Florin Coras54ddf432018-12-21 13:54:09 -08003292 _(CLOSING, TCP_FLAG_SYN, TCP_INPUT_NEXT_RCV_PROCESS, TCP_ERROR_NONE);
Florin Corasb56fcf12019-01-02 10:10:08 -08003293 _(CLOSING, TCP_FLAG_SYN | TCP_FLAG_RST, TCP_INPUT_NEXT_RCV_PROCESS,
3294 TCP_ERROR_NONE);
3295 _(CLOSING, TCP_FLAG_SYN | TCP_FLAG_ACK, TCP_INPUT_NEXT_RCV_PROCESS,
3296 TCP_ERROR_NONE);
3297 _(CLOSING, TCP_FLAG_SYN | TCP_FLAG_RST | TCP_FLAG_ACK,
3298 TCP_INPUT_NEXT_RCV_PROCESS, TCP_ERROR_NONE);
Florin Coras54ddf432018-12-21 13:54:09 -08003299 _(CLOSING, TCP_FLAG_RST, TCP_INPUT_NEXT_RCV_PROCESS, TCP_ERROR_NONE);
3300 _(CLOSING, TCP_FLAG_RST | TCP_FLAG_ACK, TCP_INPUT_NEXT_RCV_PROCESS,
3301 TCP_ERROR_NONE);
Florin Corasb56fcf12019-01-02 10:10:08 -08003302 _(CLOSING, TCP_FLAG_FIN, TCP_INPUT_NEXT_RCV_PROCESS, TCP_ERROR_NONE);
Florin Coras3c514d52018-12-22 11:39:33 -08003303 _(CLOSING, TCP_FLAG_FIN | TCP_FLAG_ACK, TCP_INPUT_NEXT_RCV_PROCESS,
3304 TCP_ERROR_NONE);
Florin Corasb56fcf12019-01-02 10:10:08 -08003305 _(CLOSING, TCP_FLAG_FIN | TCP_FLAG_RST, TCP_INPUT_NEXT_RCV_PROCESS,
3306 TCP_ERROR_NONE);
3307 _(CLOSING, TCP_FLAG_FIN | TCP_FLAG_RST | TCP_FLAG_ACK,
3308 TCP_INPUT_NEXT_RCV_PROCESS, TCP_ERROR_NONE);
3309 _(CLOSING, TCP_FLAG_FIN | TCP_FLAG_SYN, TCP_INPUT_NEXT_RCV_PROCESS,
3310 TCP_ERROR_NONE);
3311 _(CLOSING, TCP_FLAG_FIN | TCP_FLAG_SYN | TCP_FLAG_ACK,
3312 TCP_INPUT_NEXT_RCV_PROCESS, TCP_ERROR_NONE);
Florin Coras79c04d62019-08-11 19:49:05 -07003313 _(CLOSING, TCP_FLAG_FIN | TCP_FLAG_SYN | TCP_FLAG_RST,
3314 TCP_INPUT_NEXT_RCV_PROCESS, TCP_ERROR_NONE);
Florin Corasb56fcf12019-01-02 10:10:08 -08003315 _(CLOSING, TCP_FLAG_FIN | TCP_FLAG_SYN | TCP_FLAG_RST | TCP_FLAG_ACK,
3316 TCP_INPUT_NEXT_RCV_PROCESS, TCP_ERROR_NONE);
Dave Barach68b0fb02017-02-28 15:15:56 -05003317 /* FIN confirming that the peer (app) has closed */
3318 _(FIN_WAIT_2, TCP_FLAG_FIN, TCP_INPUT_NEXT_RCV_PROCESS, TCP_ERROR_NONE);
Florin Corasf6d68ed2017-05-07 19:12:02 -07003319 _(FIN_WAIT_2, TCP_FLAG_ACK, TCP_INPUT_NEXT_RCV_PROCESS, TCP_ERROR_NONE);
Dave Barach68b0fb02017-02-28 15:15:56 -05003320 _(FIN_WAIT_2, TCP_FLAG_FIN | TCP_FLAG_ACK, TCP_INPUT_NEXT_RCV_PROCESS,
3321 TCP_ERROR_NONE);
Florin Coras678a6572018-12-17 21:31:25 -08003322 _(FIN_WAIT_2, TCP_FLAG_RST, TCP_INPUT_NEXT_RCV_PROCESS, TCP_ERROR_NONE);
3323 _(FIN_WAIT_2, TCP_FLAG_RST | TCP_FLAG_ACK, TCP_INPUT_NEXT_RCV_PROCESS,
3324 TCP_ERROR_NONE);
Florin Corasf9512bf2020-07-29 10:13:07 -07003325 _(FIN_WAIT_2, TCP_FLAG_SYN, TCP_INPUT_NEXT_RCV_PROCESS, TCP_ERROR_NONE);
Florin Corasf03a59a2017-06-09 21:07:32 -07003326 _(CLOSE_WAIT, TCP_FLAG_ACK, TCP_INPUT_NEXT_RCV_PROCESS, TCP_ERROR_NONE);
3327 _(CLOSE_WAIT, TCP_FLAG_FIN | TCP_FLAG_ACK, TCP_INPUT_NEXT_RCV_PROCESS,
3328 TCP_ERROR_NONE);
Florin Coras678a6572018-12-17 21:31:25 -08003329 _(CLOSE_WAIT, TCP_FLAG_RST, TCP_INPUT_NEXT_RCV_PROCESS, TCP_ERROR_NONE);
3330 _(CLOSE_WAIT, TCP_FLAG_RST | TCP_FLAG_ACK, TCP_INPUT_NEXT_RCV_PROCESS,
3331 TCP_ERROR_NONE);
Florin Corasd75213f2020-07-28 10:57:09 -07003332 _(CLOSE_WAIT, TCP_FLAG_SYN, TCP_INPUT_NEXT_RCV_PROCESS, TCP_ERROR_NONE);
Florin Corasd12ff502018-12-25 10:55:01 -08003333 _(LAST_ACK, 0, TCP_INPUT_NEXT_DROP, TCP_ERROR_SEGMENT_INVALID);
Dave Barach68b0fb02017-02-28 15:15:56 -05003334 _(LAST_ACK, TCP_FLAG_ACK, TCP_INPUT_NEXT_RCV_PROCESS, TCP_ERROR_NONE);
Florin Coras93992a92017-05-24 18:03:56 -07003335 _(LAST_ACK, TCP_FLAG_FIN, TCP_INPUT_NEXT_RCV_PROCESS, TCP_ERROR_NONE);
3336 _(LAST_ACK, TCP_FLAG_FIN | TCP_FLAG_ACK, TCP_INPUT_NEXT_RCV_PROCESS,
3337 TCP_ERROR_NONE);
Florin Corasd12ff502018-12-25 10:55:01 -08003338 _(LAST_ACK, TCP_FLAG_FIN | TCP_FLAG_SYN, TCP_INPUT_NEXT_RCV_PROCESS,
3339 TCP_ERROR_NONE);
3340 _(LAST_ACK, TCP_FLAG_FIN | TCP_FLAG_SYN | TCP_FLAG_ACK,
3341 TCP_INPUT_NEXT_RCV_PROCESS, TCP_ERROR_NONE);
3342 _(LAST_ACK, TCP_FLAG_FIN | TCP_FLAG_RST, TCP_INPUT_NEXT_RCV_PROCESS,
3343 TCP_ERROR_NONE);
3344 _(LAST_ACK, TCP_FLAG_FIN | TCP_FLAG_RST | TCP_FLAG_ACK,
3345 TCP_INPUT_NEXT_RCV_PROCESS, TCP_ERROR_NONE);
3346 _(LAST_ACK, TCP_FLAG_FIN | TCP_FLAG_SYN | TCP_FLAG_RST,
3347 TCP_INPUT_NEXT_RCV_PROCESS, TCP_ERROR_NONE);
3348 _(LAST_ACK, TCP_FLAG_FIN | TCP_FLAG_SYN | TCP_FLAG_RST | TCP_FLAG_ACK,
3349 TCP_INPUT_NEXT_RCV_PROCESS, TCP_ERROR_NONE);
Florin Corasdb84e572017-05-09 18:54:52 -07003350 _(LAST_ACK, TCP_FLAG_RST, TCP_INPUT_NEXT_RCV_PROCESS, TCP_ERROR_NONE);
Florin Coras678a6572018-12-17 21:31:25 -08003351 _(LAST_ACK, TCP_FLAG_RST | TCP_FLAG_ACK, TCP_INPUT_NEXT_RCV_PROCESS,
3352 TCP_ERROR_NONE);
Florin Corasc01d5782018-10-17 14:53:11 -07003353 _(LAST_ACK, TCP_FLAG_SYN, TCP_INPUT_NEXT_RCV_PROCESS, TCP_ERROR_NONE);
Florin Corasd12ff502018-12-25 10:55:01 -08003354 _(LAST_ACK, TCP_FLAG_SYN | TCP_FLAG_ACK, TCP_INPUT_NEXT_RCV_PROCESS,
3355 TCP_ERROR_NONE);
3356 _(LAST_ACK, TCP_FLAG_SYN | TCP_FLAG_RST, TCP_INPUT_NEXT_RCV_PROCESS,
3357 TCP_ERROR_NONE);
3358 _(LAST_ACK, TCP_FLAG_SYN | TCP_FLAG_RST | TCP_FLAG_ACK,
3359 TCP_INPUT_NEXT_RCV_PROCESS, TCP_ERROR_NONE);
Yu Pingb092b772019-12-27 04:04:33 +08003360 _(TIME_WAIT, TCP_FLAG_SYN, TCP_INPUT_NEXT_LISTEN, TCP_ERROR_NONE);
Florin Coras93992a92017-05-24 18:03:56 -07003361 _(TIME_WAIT, TCP_FLAG_FIN, TCP_INPUT_NEXT_RCV_PROCESS, TCP_ERROR_NONE);
3362 _(TIME_WAIT, TCP_FLAG_FIN | TCP_FLAG_ACK, TCP_INPUT_NEXT_RCV_PROCESS,
3363 TCP_ERROR_NONE);
Florin Coras3eb50622017-07-13 01:24:57 -04003364 _(TIME_WAIT, TCP_FLAG_RST, TCP_INPUT_NEXT_RCV_PROCESS, TCP_ERROR_NONE);
Florin Coras678a6572018-12-17 21:31:25 -08003365 _(TIME_WAIT, TCP_FLAG_RST | TCP_FLAG_ACK, TCP_INPUT_NEXT_RCV_PROCESS,
3366 TCP_ERROR_NONE);
Florin Coras50958952017-08-29 14:50:13 -07003367 _(TIME_WAIT, TCP_FLAG_ACK, TCP_INPUT_NEXT_RCV_PROCESS, TCP_ERROR_NONE);
Florin Coras678a6572018-12-17 21:31:25 -08003368 /* RFC793 CLOSED: An incoming segment containing a RST is discarded. An
3369 * incoming segment not containing a RST causes a RST to be sent in
3370 * response.*/
Florin Corasdc629cd2017-05-09 00:52:37 -07003371 _(CLOSED, TCP_FLAG_RST, TCP_INPUT_NEXT_DROP, TCP_ERROR_CONNECTION_CLOSED);
Florin Coras678a6572018-12-17 21:31:25 -08003372 _(CLOSED, TCP_FLAG_RST | TCP_FLAG_ACK, TCP_INPUT_NEXT_DROP,
Dave Barach2c25a622017-06-26 11:35:07 -04003373 TCP_ERROR_CONNECTION_CLOSED);
Florin Corasdeb6f782020-02-11 02:42:36 +00003374 _(CLOSED, TCP_FLAG_ACK, TCP_INPUT_NEXT_RESET, TCP_ERROR_CONNECTION_CLOSED);
Florin Coras3ffc77d2023-02-16 18:59:38 -08003375 _ (CLOSED, TCP_FLAG_SYN, TCP_INPUT_NEXT_LISTEN, TCP_ERROR_NONE);
Florin Coras678a6572018-12-17 21:31:25 -08003376 _(CLOSED, TCP_FLAG_FIN | TCP_FLAG_ACK, TCP_INPUT_NEXT_RESET,
Florin Corasdeb6f782020-02-11 02:42:36 +00003377 TCP_ERROR_CONNECTION_CLOSED);
Dave Barach68b0fb02017-02-28 15:15:56 -05003378#undef _
3379}
3380
Florin Coras0dbd5172018-06-25 16:19:34 -07003381static clib_error_t *
Dave Barach68b0fb02017-02-28 15:15:56 -05003382tcp_input_init (vlib_main_t * vm)
3383{
3384 clib_error_t *error = 0;
3385 tcp_main_t *tm = vnet_get_tcp_main ();
3386
3387 if ((error = vlib_call_init_function (vm, tcp_init)))
3388 return error;
3389
3390 /* Initialize dispatch table. */
3391 tcp_dispatch_table_init (tm);
3392
3393 return error;
3394}
3395
3396VLIB_INIT_FUNCTION (tcp_input_init);
3397
Filip Tehlare275bed2019-03-06 00:06:56 -08003398#endif /* CLIB_MARCH_VARIANT */
3399
Dave Barach68b0fb02017-02-28 15:15:56 -05003400/*
3401 * fd.io coding-style-patch-verification: ON
3402 *
3403 * Local Variables:
3404 * eval: (c-set-style "gnu")
3405 * End:
3406 */