ipsec: Reference count the SAs
[vpp.git] / src / vnet / ipsec / ipsec_if_in.c
index 9ba2c10..4e93725 100644 (file)
 #include <vnet/ipsec/ipsec.h>
 #include <vnet/ipsec/esp.h>
 #include <vnet/ipsec/ipsec_io.h>
+#include <vnet/ipsec/ipsec_punt.h>
 
 /* Statistics (not really errors) */
 #define foreach_ipsec_if_input_error                             \
 _(RX, "good packets received")                                   \
 _(DISABLED, "ipsec packets received on disabled interface")       \
-_(NO_TUNNEL, "no matching tunnel")
+_(NO_TUNNEL, "no matching tunnel")                                \
+_(SPI_0, "SPI 0")
 
 static char *ipsec_if_input_error_strings[] = {
 #define _(sym,string) string,
@@ -57,10 +59,44 @@ format_ipsec_if_input_trace (u8 * s, va_list * args)
   CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
   ipsec_if_input_trace_t *t = va_arg (*args, ipsec_if_input_trace_t *);
 
-  s = format (s, "IPSec: spi %u seq %u", t->spi, t->seq);
+  s = format (s, "IPSec: spi %u (0x%08x) seq %u", t->spi, t->spi, t->seq);
   return s;
 }
 
+always_inline u16
+ipsec_ip4_if_no_tunnel (vlib_node_runtime_t * node,
+                       vlib_buffer_t * b,
+                       const esp_header_t * esp,
+                       const ip4_header_t * ip4, u16 offset)
+{
+  if (PREDICT_FALSE (0 == esp->spi))
+    {
+      b->error = node->errors[IPSEC_IF_INPUT_ERROR_SPI_0];
+      b->punt_reason =
+       ipsec_punt_reason[(ip4->protocol == IP_PROTOCOL_UDP ?
+                          IPSEC_PUNT_IP4_SPI_UDP_0 :
+                          IPSEC_PUNT_IP4_NO_SUCH_TUNNEL)];
+    }
+  else
+    {
+      b->error = node->errors[IPSEC_IF_INPUT_ERROR_NO_TUNNEL];
+      b->punt_reason = ipsec_punt_reason[IPSEC_PUNT_IP4_NO_SUCH_TUNNEL];
+    }
+  vlib_buffer_advance (b, -offset);
+  return IPSEC_INPUT_NEXT_PUNT;
+}
+
+always_inline u16
+ipsec_ip6_if_no_tunnel (vlib_node_runtime_t * node,
+                       vlib_buffer_t * b,
+                       const esp_header_t * esp, u16 offset)
+{
+  b->error = node->errors[IPSEC_IF_INPUT_ERROR_NO_TUNNEL];
+  b->punt_reason = ipsec_punt_reason[IPSEC_PUNT_IP6_NO_SUCH_TUNNEL];
+
+  vlib_buffer_advance (b, -offset);
+  return (IPSEC_INPUT_NEXT_PUNT);
+}
 
 always_inline uword
 ipsec_if_input_inline (vlib_main_t * vm, vlib_node_runtime_t * node,
@@ -197,9 +233,9 @@ ipsec_if_input_inline (vlib_main_t * vm, vlib_node_runtime_t * node,
                }
              else
                {
-                 b[0]->error = node->errors[IPSEC_IF_INPUT_ERROR_NO_TUNNEL];
+                 next[0] =
+                   ipsec_ip6_if_no_tunnel (node, b[0], esp0, buf_adv0);
                  n_no_tunnel++;
-                 next[0] = IPSEC_INPUT_NEXT_DROP;
                  goto pkt1;
                }
            }
@@ -225,9 +261,9 @@ ipsec_if_input_inline (vlib_main_t * vm, vlib_node_runtime_t * node,
                }
              else
                {
-                 b[0]->error = node->errors[IPSEC_IF_INPUT_ERROR_NO_TUNNEL];
+                 next[0] =
+                   ipsec_ip4_if_no_tunnel (node, b[0], esp0, ip40, buf_adv0);
                  n_no_tunnel++;
-                 next[0] = IPSEC_INPUT_NEXT_DROP;
                  goto pkt1;
                }
            }
@@ -293,9 +329,9 @@ ipsec_if_input_inline (vlib_main_t * vm, vlib_node_runtime_t * node,
                }
              else
                {
-                 b[1]->error = node->errors[IPSEC_IF_INPUT_ERROR_NO_TUNNEL];
+                 next[1] =
+                   ipsec_ip6_if_no_tunnel (node, b[1], esp1, buf_adv1);
                  n_no_tunnel++;
-                 next[1] = IPSEC_INPUT_NEXT_DROP;
                  goto trace1;
                }
            }
@@ -321,9 +357,9 @@ ipsec_if_input_inline (vlib_main_t * vm, vlib_node_runtime_t * node,
                }
              else
                {
-                 b[1]->error = node->errors[IPSEC_IF_INPUT_ERROR_NO_TUNNEL];
+                 next[1] =
+                   ipsec_ip4_if_no_tunnel (node, b[1], esp1, ip41, buf_adv1);
                  n_no_tunnel++;
-                 next[1] = IPSEC_INPUT_NEXT_DROP;
                  goto trace1;
                }
            }
@@ -398,7 +434,7 @@ ipsec_if_input_inline (vlib_main_t * vm, vlib_node_runtime_t * node,
       ip6_header_t *ip60;
       esp_header_t *esp0;
       u32 len0;
-      u16 buf_adv0;
+      u16 buf_adv0, buf_rewind0;
       u32 tid0;
       ipsec_tunnel_if_t *t0;
       ipsec4_tunnel_key_t key40;
@@ -422,11 +458,12 @@ ipsec_if_input_inline (vlib_main_t * vm, vlib_node_runtime_t * node,
                (esp_header_t *) ((u8 *) ip40 + ip4_header_bytes (ip40) +
                                  sizeof (udp_header_t));
              buf_adv0 = 0;
+             buf_rewind0 = ip4_header_bytes (ip40) + sizeof (udp_header_t);
            }
          else
            {
              esp0 = (esp_header_t *) ((u8 *) ip40 + ip4_header_bytes (ip40));
-             buf_adv0 = ip4_header_bytes (ip40);
+             buf_rewind0 = buf_adv0 = ip4_header_bytes (ip40);
            }
        }
 
@@ -456,9 +493,9 @@ ipsec_if_input_inline (vlib_main_t * vm, vlib_node_runtime_t * node,
                }
              else
                {
-                 b[0]->error = node->errors[IPSEC_IF_INPUT_ERROR_NO_TUNNEL];
+                 next[0] =
+                   ipsec_ip6_if_no_tunnel (node, b[0], esp0, buf_adv0);
                  n_no_tunnel++;
-                 next[0] = IPSEC_INPUT_NEXT_DROP;
                  goto trace00;
                }
            }
@@ -484,9 +521,10 @@ ipsec_if_input_inline (vlib_main_t * vm, vlib_node_runtime_t * node,
                }
              else
                {
-                 b[0]->error = node->errors[IPSEC_IF_INPUT_ERROR_NO_TUNNEL];
+                 next[0] =
+                   ipsec_ip4_if_no_tunnel (node, b[0], esp0, ip40,
+                                           buf_rewind0);
                  n_no_tunnel++;
-                 next[0] = IPSEC_INPUT_NEXT_DROP;
                  goto trace00;
                }
            }