tcp: validate connections in output
[vpp.git] / src / vnet / tcp / tcp_output.c
index 751d8b3..ff281b5 100644 (file)
@@ -75,19 +75,6 @@ tcp_window_compute_scale (u32 window)
   return wnd_scale;
 }
 
-/**
- * Update max segment size we're able to process.
- *
- * The value is constrained by our interface's MTU and IP options. It is
- * also what we advertise to our peer.
- */
-void
-tcp_update_rcv_mss (tcp_connection_t * tc)
-{
-  /* TODO find our iface MTU */
-  tc->mss = tcp_main.default_mtu - sizeof (tcp_header_t);
-}
-
 /**
  * TCP's initial window
  */
@@ -103,7 +90,7 @@ tcp_initial_wnd_unscaled (tcp_connection_t * tc)
      tcp_update_rcv_mss (tc);
      TCP_IW_N_SEGMENTS * tc->mss;
    */
-  return TCP_MIN_RX_FIFO_SIZE;
+  return tcp_cfg.min_rx_fifo;
 }
 
 /**
@@ -113,17 +100,9 @@ tcp_initial_wnd_unscaled (tcp_connection_t * tc)
 u32
 tcp_initial_window_to_advertise (tcp_connection_t * tc)
 {
-  tcp_main_t *tm = &tcp_main;
-  u32 max_fifo;
-
-  /* Initial wnd for SYN. Fifos are not allocated yet.
-   * Use some predefined value. For SYN-ACK we still want the
-   * scale to be computed in the same way */
-  max_fifo = tm->max_rx_fifo ? tm->max_rx_fifo : TCP_MAX_RX_FIFO_SIZE;
-
   /* Compute rcv wscale only if peer advertised support for it */
   if (tc->state != TCP_STATE_SYN_RCVD || tcp_opts_wscale (&tc->rcv_opts))
-    tc->rcv_wscale = tcp_window_compute_scale (max_fifo);
+    tc->rcv_wscale = tcp_window_compute_scale (tcp_cfg.max_rx_fifo);
 
   tc->rcv_wnd = tcp_initial_wnd_unscaled (tc);
 
@@ -143,7 +122,10 @@ tcp_update_rcv_wnd (tcp_connection_t * tc)
    */
   available_space = transport_max_rx_enqueue (&tc->connection);
   if (PREDICT_FALSE (available_space < tc->rcv_opts.mss))
-    available_space = 0;
+    {
+      tc->rcv_wnd = 0;
+      return;
+    }
 
   /*
    * Use the above and what we know about what we've previously advertised
@@ -155,7 +137,7 @@ tcp_update_rcv_wnd (tcp_connection_t * tc)
   if (PREDICT_FALSE ((i32) available_space < observed_wnd))
     {
       wnd = clib_max (observed_wnd, 0);
-      TCP_EVT_DBG (TCP_EVT_RCV_WND_SHRUNK, tc, observed_wnd, available_space);
+      TCP_EVT (TCP_EVT_RCV_WND_SHRUNK, tc, observed_wnd, available_space);
     }
   else
     {
@@ -271,16 +253,16 @@ tcp_options_write (u8 * data, tcp_options_t * opts)
 }
 
 static int
-tcp_make_syn_options (tcp_options_t * opts, u8 wnd_scale)
+tcp_make_syn_options (tcp_connection_t * tc, tcp_options_t * opts)
 {
   u8 len = 0;
 
   opts->flags |= TCP_OPTS_FLAG_MSS;
-  opts->mss = tcp_main.default_mtu;    /*XXX discover that */
+  opts->mss = tc->mss;
   len += TCP_OPTION_LEN_MSS;
 
   opts->flags |= TCP_OPTS_FLAG_WSCALE;
-  opts->wscale = wnd_scale;
+  opts->wscale = tc->rcv_wscale;
   len += TCP_OPTION_LEN_WINDOW_SCALE;
 
   opts->flags |= TCP_OPTS_FLAG_TSTAMP;
@@ -387,7 +369,7 @@ tcp_make_options (tcp_connection_t * tc, tcp_options_t * opts,
     case TCP_STATE_SYN_RCVD:
       return tcp_make_synack_options (tc, opts);
     case TCP_STATE_SYN_SENT:
-      return tcp_make_syn_options (opts, tc->rcv_wscale);
+      return tcp_make_syn_options (tc, opts);
     default:
       clib_warning ("State not handled! %d", state);
       return 0;
@@ -430,29 +412,6 @@ tcp_update_burst_snd_vars (tcp_connection_t * tc)
     tcp_cc_event (tc, TCP_CC_EVT_START_TX);
 }
 
-void
-tcp_init_mss (tcp_connection_t * tc)
-{
-  u16 default_min_mss = 536;
-  tcp_update_rcv_mss (tc);
-
-  /* TODO cache mss and consider PMTU discovery */
-  tc->snd_mss = clib_min (tc->rcv_opts.mss, tc->mss);
-
-  if (tc->snd_mss < 45)
-    {
-      /* Assume that at least the min default mss works */
-      tc->snd_mss = default_min_mss;
-      tc->rcv_opts.mss = default_min_mss;
-    }
-
-  /* We should have enough space for 40 bytes of options */
-  ASSERT (tc->snd_mss > 45);
-
-  /* If we use timestamp option, account for it */
-  if (tcp_opts_tstamp (&tc->rcv_opts))
-    tc->snd_mss -= TCP_OPTION_LEN_TIMESTAMP;
-}
 #endif /* CLIB_MARCH_VARIANT */
 
 static void *
@@ -522,7 +481,7 @@ static inline void
 tcp_make_ack (tcp_connection_t * tc, vlib_buffer_t * b)
 {
   tcp_make_ack_i (tc, b, TCP_STATE_ESTABLISHED, TCP_FLAG_ACK);
-  TCP_EVT_DBG (TCP_EVT_ACK_SENT, tc);
+  TCP_EVT (TCP_EVT_ACK_SENT, tc);
   tc->rcv_las = tc->rcv_nxt;
 }
 
@@ -550,7 +509,7 @@ tcp_make_syn (tcp_connection_t * tc, vlib_buffer_t * b)
 
   /* Make and write options */
   clib_memset (&snd_opts, 0, sizeof (snd_opts));
-  tcp_opts_len = tcp_make_syn_options (&snd_opts, tc->rcv_wscale);
+  tcp_opts_len = tcp_make_syn_options (tc, &snd_opts);
   tcp_hdr_opts_len = tcp_opts_len + sizeof (tcp_header_t);
 
   th = vlib_buffer_push_tcp (b, tc->c_lcl_port, tc->c_rmt_port, tc->iss,
@@ -842,7 +801,7 @@ tcp_send_reset_w_pkt (tcp_connection_t * tc, vlib_buffer_t * pkt,
     }
 
   tcp_enqueue_to_ip_lookup_now (wrk, b, bi, is_ip4, fib_index);
-  TCP_EVT_DBG (TCP_EVT_RST_SENT, tc);
+  TCP_EVT (TCP_EVT_RST_SENT, tc);
   vlib_node_increment_counter (vm, tcp_node_index (output, tc->c_is_ip4),
                               TCP_ERROR_RST_SENT, 1);
 }
@@ -876,24 +835,8 @@ tcp_send_reset (tcp_connection_t * tc)
   opts_write_len = tcp_options_write ((u8 *) (th + 1), &tc->snd_opts);
   ASSERT (opts_write_len == tc->snd_opts_len);
   vnet_buffer (b)->tcp.connection_index = tc->c_c_index;
-  if (tc->c_is_ip4)
-    {
-      ip4_header_t *ih4;
-      ih4 = vlib_buffer_push_ip4 (vm, b, &tc->c_lcl_ip.ip4,
-                                 &tc->c_rmt_ip.ip4, IP_PROTOCOL_TCP, 0);
-      th->checksum = ip4_tcp_udp_compute_checksum (vm, b, ih4);
-    }
-  else
-    {
-      int bogus = ~0;
-      ip6_header_t *ih6;
-      ih6 = vlib_buffer_push_ip6 (vm, b, &tc->c_lcl_ip.ip6,
-                                 &tc->c_rmt_ip.ip6, IP_PROTOCOL_TCP);
-      th->checksum = ip6_tcp_udp_icmp_compute_checksum (vm, b, ih6, &bogus);
-      ASSERT (!bogus);
-    }
-  tcp_enqueue_to_ip_lookup_now (wrk, b, bi, tc->c_is_ip4, tc->c_fib_index);
-  TCP_EVT_DBG (TCP_EVT_RST_SENT, tc);
+  tcp_enqueue_to_output (wrk, b, bi, tc->c_is_ip4);
+  TCP_EVT (TCP_EVT_RST_SENT, tc);
   vlib_node_increment_counter (vm, tcp_node_index (output, tc->c_is_ip4),
                               TCP_ERROR_RST_SENT, 1);
 }
@@ -942,7 +885,6 @@ tcp_send_syn (tcp_connection_t * tc)
    * Setup retransmit and establish timers before requesting buffer
    * such that we can return if we've ran out.
    */
-  tcp_timer_set (tc, TCP_TIMER_ESTABLISH_AO, TCP_ESTABLISH_TIME);
   tcp_timer_update (tc, TCP_TIMER_RETRANSMIT_SYN,
                    tc->rto * TCP_TO_TIMER_TICK);
 
@@ -963,7 +905,7 @@ tcp_send_syn (tcp_connection_t * tc)
 
   tcp_push_ip_hdr (wrk, tc, b);
   tcp_enqueue_to_ip_lookup (wrk, b, bi, tc->c_is_ip4, tc->c_fib_index);
-  TCP_EVT_DBG (TCP_EVT_SYN_SENT, tc);
+  TCP_EVT (TCP_EVT_SYN_SENT, tc);
 }
 
 void
@@ -987,7 +929,7 @@ tcp_send_synack (tcp_connection_t * tc)
   tcp_init_buffer (vm, b);
   tcp_make_synack (tc, b);
   tcp_enqueue_to_output (wrk, b, bi, tc->c_is_ip4);
-  TCP_EVT_DBG (TCP_EVT_SYNACK_SENT, tc);
+  TCP_EVT (TCP_EVT_SYNACK_SENT, tc);
 }
 
 /**
@@ -1066,7 +1008,7 @@ tcp_send_fin (tcp_connection_t * tc)
   tcp_init_buffer (vm, b);
   tcp_make_fin (tc, b);
   tcp_enqueue_to_output_now (wrk, b, bi, tc->c_is_ip4);
-  TCP_EVT_DBG (TCP_EVT_FIN_SENT, tc);
+  TCP_EVT (TCP_EVT_FIN_SENT, tc);
   /* Account for the FIN */
   tc->snd_nxt += 1;
   if (!fin_snt)
@@ -1140,7 +1082,7 @@ tcp_push_hdr_i (tcp_connection_t * tc, vlib_buffer_t * b, u32 snd_nxt,
   tc->bytes_out += data_len;
   tc->data_segs_out += 1;
 
-  TCP_EVT_DBG (TCP_EVT_PKTIZE, tc);
+  TCP_EVT (TCP_EVT_PKTIZE, tc);
 }
 
 u32
@@ -1243,19 +1185,19 @@ tcp_timer_delack_handler (u32 index)
 }
 
 /**
- * Send Window Update ACK,
- * ensuring that it will be sent once, if RWND became non-zero,
- * after zero RWND has been advertised in ACK before
+ * Send window update ack
+ *
+ * Ensures that it will be sent only once, after a zero rwnd has been
+ * advertised in a previous ack, and only if rwnd has grown beyond a
+ * configurable value.
  */
 void
 tcp_send_window_update_ack (tcp_connection_t * tc)
 {
-  u32 win;
-
   if (tcp_zero_rwnd_sent (tc))
     {
-      win = tcp_window_to_advertise (tc, tc->state);
-      if (win > 0)
+      tcp_update_rcv_wnd (tc);
+      if (tc->rcv_wnd >= tcp_cfg.rwnd_min_update_ack * tc->snd_mss)
        {
          tcp_zero_rwnd_sent_off (tc);
          tcp_program_ack (tc);
@@ -1436,7 +1378,7 @@ tcp_prepare_retransmit_segment (tcp_worker_ctx_t * wrk,
 
   tc->bytes_retrans += n_bytes;
   tc->segs_retrans += 1;
-  TCP_EVT_DBG (TCP_EVT_CC_RTX, tc, offset, n_bytes);
+  TCP_EVT (TCP_EVT_CC_RTX, tc, offset, n_bytes);
   return n_bytes;
 }
 
@@ -1446,7 +1388,7 @@ tcp_prepare_retransmit_segment (tcp_worker_ctx_t * wrk,
 static void
 tcp_cc_init_rxt_timeout (tcp_connection_t * tc)
 {
-  TCP_EVT_DBG (TCP_EVT_CC_EVT, tc, 6);
+  TCP_EVT (TCP_EVT_CC_EVT, tc, 6);
   tc->prev_ssthresh = tc->ssthresh;
   tc->prev_cwnd = tc->cwnd;
 
@@ -1467,8 +1409,8 @@ tcp_cc_init_rxt_timeout (tcp_connection_t * tc)
   tcp_recovery_on (tc);
 }
 
-static inline void
-tcp_timer_retransmit_handler_i (u32 index, u8 is_syn)
+void
+tcp_timer_retransmit_handler (u32 tc_index)
 {
   u32 thread_index = vlib_get_thread_index ();
   tcp_worker_ctx_t *wrk = tcp_get_worker (thread_index);
@@ -1477,29 +1419,21 @@ tcp_timer_retransmit_handler_i (u32 index, u8 is_syn)
   vlib_buffer_t *b = 0;
   u32 bi, n_bytes;
 
-  if (is_syn)
-    {
-      tc = tcp_half_open_connection_get (index);
-      /* Note: the connection may have transitioned to ESTABLISHED... */
-      if (PREDICT_FALSE (tc == 0 || tc->state != TCP_STATE_SYN_SENT))
-       return;
-      tc->timers[TCP_TIMER_RETRANSMIT_SYN] = TCP_TIMER_HANDLE_INVALID;
-    }
-  else
-    {
-      tc = tcp_connection_get (index, thread_index);
-      /* Note: the connection may have been closed and pool_put */
-      if (PREDICT_FALSE (tc == 0 || tc->state == TCP_STATE_SYN_SENT))
-       return;
-      tc->timers[TCP_TIMER_RETRANSMIT] = TCP_TIMER_HANDLE_INVALID;
-      /* Wait-close and retransmit could pop at the same time */
-      if (tc->state == TCP_STATE_CLOSED)
-       return;
-    }
+  tc = tcp_connection_get (tc_index, thread_index);
+
+  /* Note: the connection may have been closed and pool_put */
+  if (PREDICT_FALSE (tc == 0 || tc->state == TCP_STATE_SYN_SENT))
+    return;
+
+  tc->timers[TCP_TIMER_RETRANSMIT] = TCP_TIMER_HANDLE_INVALID;
+
+  /* Wait-close and retransmit could pop at the same time */
+  if (tc->state == TCP_STATE_CLOSED)
+    return;
 
   if (tc->state >= TCP_STATE_ESTABLISHED)
     {
-      TCP_EVT_DBG (TCP_EVT_CC_EVT, tc, 2);
+      TCP_EVT (TCP_EVT_CC_EVT, tc, 2);
 
       /* Lost FIN, retransmit and return */
       if (tc->flags & TCP_CONN_FINSNT)
@@ -1538,7 +1472,7 @@ tcp_timer_retransmit_handler_i (u32 index, u8 is_syn)
          tcp_connection_set_state (tc, TCP_STATE_CLOSED);
          session_transport_closing_notify (&tc->connection);
          tcp_connection_timers_reset (tc);
-         tcp_timer_update (tc, TCP_TIMER_WAITCLOSE, TCP_CLOSEWAIT_TIME);
+         tcp_timer_update (tc, TCP_TIMER_WAITCLOSE, tcp_cfg.closewait_time);
          return;
        }
 
@@ -1551,19 +1485,20 @@ tcp_timer_retransmit_handler_i (u32 index, u8 is_syn)
 
       /* First retransmit timeout */
       if (tc->rto_boff == 1)
-       tcp_cc_init_rxt_timeout (tc);
+       {
+         tcp_cc_init_rxt_timeout (tc);
+         /* Record timestamp. Eifel detection algorithm RFC3522 */
+         tc->snd_rxt_ts = tcp_tstamp (tc);
+       }
 
       if (tc->flags & TCP_CONN_RATE_SAMPLE)
        tcp_bt_flush_samples (tc);
 
       /* If we've sent beyond snd_congestion, update it */
       tc->snd_congestion = seq_max (tc->snd_nxt, tc->snd_congestion);
-
       tc->snd_nxt = tc->snd_una;
-      tc->rto = clib_min (tc->rto << 1, TCP_RTO_MAX);
 
-      /* Send one segment. Note that n_bytes may be zero due to buffer
-       * shortfall */
+      /* Send one segment. n_bytes may be zero due to buffer shortfall */
       n_bytes = tcp_prepare_retransmit_segment (wrk, tc, 0, tc->snd_mss, &b);
       if (!n_bytes)
        {
@@ -1572,77 +1507,43 @@ tcp_timer_retransmit_handler_i (u32 index, u8 is_syn)
        }
 
       bi = vlib_get_buffer_index (vm, b);
-
-      /* For first retransmit, record timestamp (Eifel detection RFC3522) */
-      if (tc->rto_boff == 1)
-       tc->snd_rxt_ts = tcp_tstamp (tc);
-
       tcp_enqueue_to_output (wrk, b, bi, tc->c_is_ip4);
+
+      tc->rto = clib_min (tc->rto << 1, TCP_RTO_MAX);
       tcp_retransmit_timer_force_update (tc);
     }
-  /* Retransmit for SYN */
-  else if (tc->state == TCP_STATE_SYN_SENT)
+  /* Retransmit SYN-ACK */
+  else if (tc->state == TCP_STATE_SYN_RCVD)
     {
-      /* Half-open connection actually moved to established but we were
-       * waiting for syn retransmit to pop to call cleanup from the right
-       * thread. */
-      if (tc->flags & TCP_CONN_HALF_OPEN_DONE)
+      TCP_EVT (TCP_EVT_CC_EVT, tc, 2);
+
+      tc->rtt_ts = 0;
+
+      /* Passive open establish timeout */
+      if (tc->rto > TCP_ESTABLISH_TIME >> 1)
        {
-         if (tcp_half_open_connection_cleanup (tc))
-           TCP_DBG ("could not remove half-open connection");
+         tcp_connection_set_state (tc, TCP_STATE_CLOSED);
+         tcp_connection_timers_reset (tc);
+         tcp_timer_update (tc, TCP_TIMER_WAITCLOSE, tcp_cfg.cleanup_time);
          return;
        }
 
-      TCP_EVT_DBG (TCP_EVT_CC_EVT, tc, 2);
-
-      /* Try without increasing RTO a number of times. If this fails,
-       * start growing RTO exponentially */
-      tc->rto_boff += 1;
-      if (tc->rto_boff > TCP_RTO_SYN_RETRIES)
-       tc->rto = clib_min (tc->rto << 1, TCP_RTO_MAX);
-
-      tcp_timer_update (tc, TCP_TIMER_RETRANSMIT_SYN,
-                       tc->rto * TCP_TO_TIMER_TICK);
-
       if (PREDICT_FALSE (!vlib_buffer_alloc (vm, &bi, 1)))
        {
-         tcp_timer_update (tc, TCP_TIMER_RETRANSMIT_SYN, 1);
+         tcp_timer_update (tc, TCP_TIMER_RETRANSMIT, 1);
          return;
        }
 
-      b = vlib_get_buffer (vm, bi);
-      tcp_init_buffer (vm, b);
-      tcp_make_syn (tc, b);
-
-      tc->rtt_ts = 0;
-      TCP_EVT_DBG (TCP_EVT_SYN_RXT, tc, 0);
-
-      /* This goes straight to ipx_lookup. Retransmit timer set already */
-      tcp_push_ip_hdr (wrk, tc, b);
-      tcp_enqueue_to_ip_lookup (wrk, b, bi, tc->c_is_ip4, tc->c_fib_index);
-    }
-  /* Retransmit SYN-ACK */
-  else if (tc->state == TCP_STATE_SYN_RCVD)
-    {
-      TCP_EVT_DBG (TCP_EVT_CC_EVT, tc, 2);
-
       tc->rto_boff += 1;
       if (tc->rto_boff > TCP_RTO_SYN_RETRIES)
        tc->rto = clib_min (tc->rto << 1, TCP_RTO_MAX);
-      tc->rtt_ts = 0;
 
       tcp_retransmit_timer_force_update (tc);
 
-      if (PREDICT_FALSE (!vlib_buffer_alloc (vm, &bi, 1)))
-       {
-         tcp_timer_update (tc, TCP_TIMER_RETRANSMIT, 1);
-         return;
-       }
-
       b = vlib_get_buffer (vm, bi);
       tcp_init_buffer (vm, b);
       tcp_make_synack (tc, b);
-      TCP_EVT_DBG (TCP_EVT_SYN_RXT, tc, 1);
+      TCP_EVT (TCP_EVT_SYN_RXT, tc, 1);
 
       /* Retransmit timer already updated, just enqueue to output */
       tcp_enqueue_to_output (wrk, b, bi, tc->c_is_ip4);
@@ -1654,16 +1555,72 @@ tcp_timer_retransmit_handler_i (u32 index, u8 is_syn)
     }
 }
 
+/**
+ * SYN retransmit timer handler. Active open only.
+ */
 void
-tcp_timer_retransmit_handler (u32 index)
+tcp_timer_retransmit_syn_handler (u32 tc_index)
 {
-  tcp_timer_retransmit_handler_i (index, 0);
-}
+  u32 thread_index = vlib_get_thread_index ();
+  tcp_worker_ctx_t *wrk = tcp_get_worker (thread_index);
+  vlib_main_t *vm = wrk->vm;
+  tcp_connection_t *tc;
+  vlib_buffer_t *b = 0;
+  u32 bi;
 
-void
-tcp_timer_retransmit_syn_handler (u32 index)
-{
-  tcp_timer_retransmit_handler_i (index, 1);
+  tc = tcp_half_open_connection_get (tc_index);
+
+  /* Note: the connection may have transitioned to ESTABLISHED... */
+  if (PREDICT_FALSE (tc == 0 || tc->state != TCP_STATE_SYN_SENT))
+    return;
+
+  tc->timers[TCP_TIMER_RETRANSMIT_SYN] = TCP_TIMER_HANDLE_INVALID;
+
+  /* Half-open connection actually moved to established but we were
+   * waiting for syn retransmit to pop to call cleanup from the right
+   * thread. */
+  if (tc->flags & TCP_CONN_HALF_OPEN_DONE)
+    {
+      if (tcp_half_open_connection_cleanup (tc))
+       TCP_DBG ("could not remove half-open connection");
+      return;
+    }
+
+  TCP_EVT (TCP_EVT_CC_EVT, tc, 2);
+  tc->rtt_ts = 0;
+
+  /* Active open establish timeout */
+  if (tc->rto >= TCP_ESTABLISH_TIME >> 1)
+    {
+      session_stream_connect_notify (&tc->connection, 1 /* fail */ );
+      tcp_connection_cleanup (tc);
+      return;
+    }
+
+  if (PREDICT_FALSE (!vlib_buffer_alloc (vm, &bi, 1)))
+    {
+      tcp_timer_update (tc, TCP_TIMER_RETRANSMIT_SYN, 1);
+      return;
+    }
+
+  /* Try without increasing RTO a number of times. If this fails,
+   * start growing RTO exponentially */
+  tc->rto_boff += 1;
+  if (tc->rto_boff > TCP_RTO_SYN_RETRIES)
+    tc->rto = clib_min (tc->rto << 1, TCP_RTO_MAX);
+
+  b = vlib_get_buffer (vm, bi);
+  tcp_init_buffer (vm, b);
+  tcp_make_syn (tc, b);
+
+  TCP_EVT (TCP_EVT_SYN_RXT, tc, 0);
+
+  /* This goes straight to ipx_lookup */
+  tcp_push_ip_hdr (wrk, tc, b);
+  tcp_enqueue_to_ip_lookup (wrk, b, bi, tc->c_is_ip4, tc->c_fib_index);
+
+  tcp_timer_update (tc, TCP_TIMER_RETRANSMIT_SYN,
+                   tc->rto * TCP_TO_TIMER_TICK);
 }
 
 /**
@@ -1758,7 +1715,7 @@ tcp_retransmit_first_unacked (tcp_worker_ctx_t * wrk, tcp_connection_t * tc)
   vlib_buffer_t *b;
   u32 bi, n_bytes;
 
-  TCP_EVT_DBG (TCP_EVT_CC_EVT, tc, 1);
+  TCP_EVT (TCP_EVT_CC_EVT, tc, 1);
 
   n_bytes = tcp_prepare_retransmit_segment (wrk, tc, 0, tc->snd_mss, &b);
   if (!n_bytes)
@@ -1830,7 +1787,7 @@ tcp_fast_retransmit_sack (tcp_worker_ctx_t * wrk, tcp_connection_t * tc,
       return 0;
     }
 
-  TCP_EVT_DBG (TCP_EVT_CC_EVT, tc, 0);
+  TCP_EVT (TCP_EVT_CC_EVT, tc, 0);
   sb = &tc->sack_sb;
   hole = scoreboard_get_hole (sb, sb->cur_rxt_hole);
 
@@ -1921,7 +1878,7 @@ tcp_fast_retransmit_no_sack (tcp_worker_ctx_t * wrk, tcp_connection_t * tc,
   vlib_buffer_t *b;
 
   ASSERT (tcp_in_fastrecovery (tc));
-  TCP_EVT_DBG (TCP_EVT_CC_EVT, tc, 0);
+  TCP_EVT (TCP_EVT_CC_EVT, tc, 0);
 
   snd_space = tcp_available_cc_snd_space (tc);
 
@@ -2152,7 +2109,7 @@ tcp_output_push_ip (vlib_main_t * vm, vlib_buffer_t * b0,
   tcp_header_t *th0 = 0;
 
   th0 = vlib_buffer_get_current (b0);
-  TCP_EVT_DBG (TCP_EVT_OUTPUT, tc0, th0->flags, b0->current_length);
+  TCP_EVT (TCP_EVT_OUTPUT, tc0, th0->flags, b0->current_length);
   if (is_ip4)
     {
       vlib_buffer_push_ip4 (vm, b0, &tc0->c_lcl_ip4, &tc0->c_rmt_ip4,
@@ -2175,30 +2132,35 @@ tcp_output_push_ip (vlib_main_t * vm, vlib_buffer_t * b0,
 
 always_inline void
 tcp_output_handle_packet (tcp_connection_t * tc0, vlib_buffer_t * b0,
-                         u32 * error0, u16 * next0, u8 is_ip4)
+                         vlib_node_runtime_t * error_node, u16 * next0,
+                         u8 is_ip4)
 {
-
-  if (PREDICT_FALSE (tc0->state == TCP_STATE_CLOSED))
-    {
-      *error0 = TCP_ERROR_INVALID_CONNECTION;
-      *next0 = TCP_OUTPUT_NEXT_DROP;
-      return;
-    }
-
   /* If next_index is not drop use it */
   if (tc0->next_node_index)
     {
       *next0 = tc0->next_node_index;
       vnet_buffer (b0)->tcp.next_node_opaque = tc0->next_node_opaque;
     }
+  else
+    {
+      *next0 = TCP_OUTPUT_NEXT_IP_LOOKUP;
+    }
 
   vnet_buffer (b0)->sw_if_index[VLIB_TX] = tc0->c_fib_index;
   vnet_buffer (b0)->sw_if_index[VLIB_RX] = 0;
 
   if (!is_ip4)
     {
+      u32 error0 = 0;
+
       if (PREDICT_FALSE (ip6_address_is_link_local_unicast (&tc0->c_rmt_ip6)))
-       tcp_output_handle_link_local (tc0, b0, next0, error0);
+       tcp_output_handle_link_local (tc0, b0, next0, &error0);
+
+      if (PREDICT_FALSE (error0))
+       {
+         b0->error = error_node->errors[error0];
+         return;
+       }
     }
 
   if (!TCP_ALWAYS_ACK)
@@ -2214,6 +2176,9 @@ tcp46_output_inline (vlib_main_t * vm, vlib_node_runtime_t * node,
   u32 n_left_from, *from, thread_index = vm->thread_index;
   vlib_buffer_t *bufs[VLIB_FRAME_SIZE], **b;
   u16 nexts[VLIB_FRAME_SIZE], *next;
+  vlib_node_runtime_t *error_node;
+
+  error_node = vlib_node_get_runtime (vm, tcp_node_index (output, is_ip4));
 
   from = vlib_frame_vector_args (frame);
   n_left_from = frame->n_vectors;
@@ -2228,7 +2193,6 @@ tcp46_output_inline (vlib_main_t * vm, vlib_node_runtime_t * node,
 
   while (n_left_from >= 4)
     {
-      u32 error0 = TCP_ERROR_PKTS_SENT, error1 = TCP_ERROR_PKTS_SENT;
       tcp_connection_t *tc0, *tc1;
 
       {
@@ -2239,18 +2203,44 @@ tcp46_output_inline (vlib_main_t * vm, vlib_node_runtime_t * node,
        CLIB_PREFETCH (b[3]->data, 2 * CLIB_CACHE_LINE_BYTES, STORE);
       }
 
-      next[0] = next[1] = TCP_OUTPUT_NEXT_IP_LOOKUP;
-
       tc0 = tcp_connection_get (vnet_buffer (b[0])->tcp.connection_index,
                                thread_index);
       tc1 = tcp_connection_get (vnet_buffer (b[1])->tcp.connection_index,
                                thread_index);
 
-      tcp_output_push_ip (vm, b[0], tc0, is_ip4);
-      tcp_output_push_ip (vm, b[1], tc1, is_ip4);
+      if (PREDICT_TRUE (!tc0 + !tc1 == 0))
+       {
+         tcp_output_push_ip (vm, b[0], tc0, is_ip4);
+         tcp_output_push_ip (vm, b[1], tc1, is_ip4);
 
-      tcp_output_handle_packet (tc0, b[0], &error0, &next[0], is_ip4);
-      tcp_output_handle_packet (tc1, b[1], &error1, &next[1], is_ip4);
+         tcp_output_handle_packet (tc0, b[0], error_node, &next[0], is_ip4);
+         tcp_output_handle_packet (tc1, b[1], error_node, &next[1], is_ip4);
+       }
+      else
+       {
+         if (tc0 != 0)
+           {
+             tcp_output_push_ip (vm, b[0], tc0, is_ip4);
+             tcp_output_handle_packet (tc0, b[0], error_node, &next[0],
+                                       is_ip4);
+           }
+         else
+           {
+             b[0]->error = error_node->errors[TCP_ERROR_INVALID_CONNECTION];
+             next[0] = TCP_OUTPUT_NEXT_DROP;
+           }
+         if (tc1 != 0)
+           {
+             tcp_output_push_ip (vm, b[1], tc1, is_ip4);
+             tcp_output_handle_packet (tc1, b[1], error_node, &next[1],
+                                       is_ip4);
+           }
+         else
+           {
+             b[1]->error = error_node->errors[TCP_ERROR_INVALID_CONNECTION];
+             next[1] = TCP_OUTPUT_NEXT_DROP;
+           }
+       }
 
       b += 2;
       next += 2;
@@ -2258,7 +2248,6 @@ tcp46_output_inline (vlib_main_t * vm, vlib_node_runtime_t * node,
     }
   while (n_left_from > 0)
     {
-      u32 error0 = TCP_ERROR_PKTS_SENT;
       tcp_connection_t *tc0;
 
       if (n_left_from > 1)
@@ -2267,12 +2256,19 @@ tcp46_output_inline (vlib_main_t * vm, vlib_node_runtime_t * node,
          CLIB_PREFETCH (b[1]->data, 2 * CLIB_CACHE_LINE_BYTES, STORE);
        }
 
-      next[0] = TCP_OUTPUT_NEXT_IP_LOOKUP;
       tc0 = tcp_connection_get (vnet_buffer (b[0])->tcp.connection_index,
                                thread_index);
 
-      tcp_output_push_ip (vm, b[0], tc0, is_ip4);
-      tcp_output_handle_packet (tc0, b[0], &error0, &next[0], is_ip4);
+      if (PREDICT_TRUE (tc0 != 0))
+       {
+         tcp_output_push_ip (vm, b[0], tc0, is_ip4);
+         tcp_output_handle_packet (tc0, b[0], error_node, &next[0], is_ip4);
+       }
+      else
+       {
+         b[0]->error = error_node->errors[TCP_ERROR_INVALID_CONNECTION];
+         next[0] = TCP_OUTPUT_NEXT_DROP;
+       }
 
       b += 1;
       next += 1;
@@ -2280,6 +2276,8 @@ tcp46_output_inline (vlib_main_t * vm, vlib_node_runtime_t * node,
     }
 
   vlib_buffer_enqueue_to_next (vm, node, from, nexts, frame->n_vectors);
+  vlib_node_increment_counter (vm, tcp_node_index (output, is_ip4),
+                              TCP_ERROR_PKTS_SENT, frame->n_vectors);
   return frame->n_vectors;
 }