+static void
+tcp_input_trace_frame (vlib_main_t * vm, vlib_node_runtime_t * node,
+ vlib_buffer_t ** bs, u32 n_bufs, u8 is_ip4)
+{
+ tcp_connection_t *tc;
+ tcp_header_t *tcp;
+ tcp_rx_trace_t *t;
+ int i;
+
+ for (i = 0; i < n_bufs; i++)
+ {
+ if (bs[i]->flags & VLIB_BUFFER_IS_TRACED)
+ {
+ t = vlib_add_trace (vm, node, bs[i], sizeof (*t));
+ tc = tcp_connection_get (vnet_buffer (bs[i])->tcp.connection_index,
+ vm->thread_index);
+ tcp = vlib_buffer_get_current (bs[i]);
+ tcp_set_rx_trace_data (t, tc, tcp, bs[i], is_ip4);
+ }
+ }
+}
+
+static void
+tcp_input_set_error_next (tcp_main_t * tm, u16 * next, u32 * error, u8 is_ip4)
+{
+ if (*error == TCP_ERROR_FILTERED)
+ {
+ *next = TCP_INPUT_NEXT_DROP;
+ }
+ else if ((is_ip4 && tm->punt_unknown4) || (!is_ip4 && tm->punt_unknown6))
+ {
+ *next = TCP_INPUT_NEXT_PUNT;
+ *error = TCP_ERROR_PUNT;
+ }
+ else
+ {
+ *next = TCP_INPUT_NEXT_RESET;
+ *error = TCP_ERROR_NO_LISTENER;
+ }
+}
+
+static inline tcp_connection_t *
+tcp_input_lookup_buffer (vlib_buffer_t * b, u8 thread_index, u32 * error,
+ u8 is_ip4)
+{
+ u32 fib_index = vnet_buffer (b)->ip.fib_index;
+ int n_advance_bytes, n_data_bytes;
+ transport_connection_t *tc;
+ tcp_header_t *tcp;
+ u8 is_filtered = 0;
+
+ if (is_ip4)
+ {
+ ip4_header_t *ip4 = vlib_buffer_get_current (b);
+ int ip_hdr_bytes = ip4_header_bytes (ip4);
+ if (PREDICT_FALSE (b->current_length < ip_hdr_bytes + sizeof (*tcp)))
+ {
+ *error = TCP_ERROR_LENGTH;
+ return 0;
+ }
+ tcp = ip4_next_header (ip4);
+ vnet_buffer (b)->tcp.hdr_offset = (u8 *) tcp - (u8 *) ip4;
+ n_advance_bytes = (ip_hdr_bytes + tcp_header_bytes (tcp));
+ n_data_bytes = clib_net_to_host_u16 (ip4->length) - n_advance_bytes;
+
+ /* Length check. Checksum computed by ipx_local no need to compute again */
+ if (PREDICT_FALSE (n_data_bytes < 0))
+ {
+ *error = TCP_ERROR_LENGTH;
+ return 0;
+ }
+
+ tc = session_lookup_connection_wt4 (fib_index, &ip4->dst_address,
+ &ip4->src_address, tcp->dst_port,
+ tcp->src_port, TRANSPORT_PROTO_TCP,
+ thread_index, &is_filtered);
+ }
+ else
+ {
+ ip6_header_t *ip6 = vlib_buffer_get_current (b);
+ if (PREDICT_FALSE (b->current_length < sizeof (*ip6) + sizeof (*tcp)))
+ {
+ *error = TCP_ERROR_LENGTH;
+ return 0;
+ }
+ tcp = ip6_next_header (ip6);
+ vnet_buffer (b)->tcp.hdr_offset = (u8 *) tcp - (u8 *) ip6;
+ n_advance_bytes = tcp_header_bytes (tcp);
+ n_data_bytes = clib_net_to_host_u16 (ip6->payload_length)
+ - n_advance_bytes;
+ n_advance_bytes += sizeof (ip6[0]);
+
+ if (PREDICT_FALSE (n_data_bytes < 0))
+ {
+ *error = TCP_ERROR_LENGTH;
+ return 0;
+ }
+
+ tc = session_lookup_connection_wt6 (fib_index, &ip6->dst_address,
+ &ip6->src_address, tcp->dst_port,
+ tcp->src_port, TRANSPORT_PROTO_TCP,
+ thread_index, &is_filtered);
+ }
+
+ vnet_buffer (b)->tcp.seq_number = clib_net_to_host_u32 (tcp->seq_number);
+ vnet_buffer (b)->tcp.ack_number = clib_net_to_host_u32 (tcp->ack_number);
+ vnet_buffer (b)->tcp.data_offset = n_advance_bytes;
+ vnet_buffer (b)->tcp.data_len = n_data_bytes;
+ vnet_buffer (b)->tcp.flags = 0;
+
+ *error = is_filtered ? TCP_ERROR_FILTERED : *error;
+
+ return tcp_get_connection_from_transport (tc);
+}
+
+static inline void
+tcp_input_dispatch_buffer (tcp_main_t * tm, tcp_connection_t * tc,
+ vlib_buffer_t * b, u16 * next, u32 * error)
+{
+ tcp_header_t *tcp;
+ u8 flags;
+
+ tcp = tcp_buffer_hdr (b);
+ flags = tcp->flags & filter_flags;
+ *next = tm->dispatch_table[tc->state][flags].next;
+ *error = tm->dispatch_table[tc->state][flags].error;
+
+ if (PREDICT_FALSE (*error == TCP_ERROR_DISPATCH
+ || *next == TCP_INPUT_NEXT_RESET))
+ {
+ /* Overload tcp flags to store state */
+ tcp_state_t state = tc->state;
+ vnet_buffer (b)->tcp.flags = tc->state;
+
+ if (*error == TCP_ERROR_DISPATCH)
+ clib_warning ("tcp conn %u disp error state %U flags %U",
+ tc->c_c_index, format_tcp_state, state,
+ format_tcp_flags, (int) flags);
+ }
+}
+