ip: SVR fix race condition
[vpp.git] / src / vnet / ip / reass / ip6_sv_reass.c
index fb435ba..23ae678 100644 (file)
@@ -41,6 +41,7 @@ typedef enum
   IP6_SV_REASS_RC_TOO_MANY_FRAGMENTS,
   IP6_SV_REASS_RC_INTERNAL_ERROR,
   IP6_SV_REASS_RC_UNSUPP_IP_PROTO,
+  IP6_SV_REASS_RC_INVALID_FRAG_LEN,
 } ip6_sv_reass_rc_t;
 
 typedef struct
@@ -215,7 +216,7 @@ format_ip6_sv_reass_trace (u8 * s, va_list * args)
                clib_net_to_host_u16 (t->l4_dst_port));
       break;
     case REASS_PASSTHROUGH:
-      s = format (s, "[not-fragmented]");
+      s = format (s, "[not fragmented or atomic fragment]");
       break;
     }
   return s;
@@ -310,6 +311,8 @@ ip6_sv_reass_find_or_create (vlib_main_t *vm, ip6_sv_reass_main_t *rm,
   ip6_sv_reass_t *reass = NULL;
   f64 now = vlib_time_now (vm);
 
+again:
+
   if (!clib_bihash_search_48_8 (&rm->hash, &kv->kv, &kv->kv))
     {
       if (vm->thread_index != kv->v.thread_index)
@@ -369,10 +372,14 @@ ip6_sv_reass_find_or_create (vlib_main_t *vm, ip6_sv_reass_main_t *rm,
   kv->v.thread_index = vm->thread_index;
   reass->last_heard = now;
 
-  if (clib_bihash_add_del_48_8 (&rm->hash, &kv->kv, 1))
+  int rv = clib_bihash_add_del_48_8 (&rm->hash, &kv->kv, 2);
+  if (rv)
     {
       ip6_sv_reass_free (vm, rm, rt, reass);
       reass = NULL;
+      // if other worker created a context already work with the other copy
+      if (-2 == rv)
+       goto again;
     }
 
   return reass;
@@ -400,6 +407,10 @@ ip6_sv_reass_update (vlib_main_t *vm, vlib_node_runtime_t *node,
   u32 fragment_length =
     vlib_buffer_length_in_chain (vm, fb) -
     (fvnb->ip.reass.ip6_frag_hdr_offset + sizeof (*frag_hdr));
+  if (0 == fragment_length)
+    {
+      return IP6_SV_REASS_RC_INVALID_FRAG_LEN;
+    }
   u32 fragment_last = fvnb->ip.reass.fragment_last =
     fragment_first + fragment_length - 1;
   fvnb->ip.reass.range_first = fragment_first;
@@ -532,13 +543,24 @@ ip6_sv_reassembly_inline (vlib_main_t * vm,
          ip6_header_t *ip0 = vlib_buffer_get_current (b0);
          ip6_frag_hdr_t *frag_hdr;
          ip6_ext_hdr_chain_t hdr_chain;
+         bool is_atomic_fragment = false;
 
          int res = ip6_ext_header_walk (
            b0, ip0, IP_PROTOCOL_IPV6_FRAGMENTATION, &hdr_chain);
+         if (res >= 0 &&
+             hdr_chain.eh[res].protocol == IP_PROTOCOL_IPV6_FRAGMENTATION)
+           {
+             frag_hdr =
+               ip6_ext_next_header_offset (ip0, hdr_chain.eh[res].offset);
+             is_atomic_fragment = (0 == ip6_frag_hdr_offset (frag_hdr) &&
+                                   !ip6_frag_hdr_more (frag_hdr));
+           }
+
          if (res < 0 ||
-             hdr_chain.eh[res].protocol != IP_PROTOCOL_IPV6_FRAGMENTATION)
+             hdr_chain.eh[res].protocol != IP_PROTOCOL_IPV6_FRAGMENTATION ||
+             is_atomic_fragment)
            {
-             // this is a regular packet - no fragmentation
+             // this is a regular unfragmented packet or an atomic fragment
              if (!ip6_get_port
                  (vm, b0, ip0, b0->current_length,
                   &(vnet_buffer (b0)->ip.reass.ip_proto),
@@ -565,10 +587,10 @@ ip6_sv_reassembly_inline (vlib_main_t * vm,
                }
              goto packet_enqueue;
            }
-         frag_hdr =
-           ip6_ext_next_header_offset (ip0, hdr_chain.eh[res].offset);
+
          vnet_buffer (b0)->ip.reass.ip6_frag_hdr_offset =
            hdr_chain.eh[res].offset;
+
          if (0 == ip6_frag_hdr_offset (frag_hdr))
            {
              // first fragment - verify upper-layer is present
@@ -656,6 +678,9 @@ ip6_sv_reassembly_inline (vlib_main_t * vm,
            case IP6_SV_REASS_RC_INTERNAL_ERROR:
              counter = IP6_ERROR_REASS_INTERNAL_ERROR;
              break;
+           case IP6_SV_REASS_RC_INVALID_FRAG_LEN:
+             counter = IP6_ERROR_REASS_INVALID_FRAG_LEN;
+             break;
            }
          if (~0 != counter)
            {