Add support for MAP-T CE (VPP-1058)
[vpp.git] / src / vnet / map / ip6_map_t.c
index 9915167..05bac1d 100644 (file)
@@ -15,6 +15,8 @@
 #include "map.h"
 
 #include "../ip/ip_frag.h"
+#include <vnet/ip/ip6_to_ip4.h>
+#include <vnet/ip/ip4_to_ip6.h>
 
 #define IP6_MAP_T_DUAL_LOOP
 
@@ -57,7 +59,8 @@ ip6_map_fragment_cache (ip6_header_t * ip6, ip6_frag_hdr_t * frag,
 {
   u32 *ignore = NULL;
   map_ip4_reass_lock ();
-  map_ip4_reass_t *r = map_ip4_reass_get (map_get_ip4 (&ip6->src_address),
+  map_ip4_reass_t *r = map_ip4_reass_get (map_get_ip4 (&ip6->src_address,
+                                                      d->flags),
                                          ip6_map_t_embedded_address (d,
                                                                      &ip6->
                                                                      dst_address),
@@ -80,7 +83,8 @@ ip6_map_fragment_get (ip6_header_t * ip6, ip6_frag_hdr_t * frag,
 {
   u32 *ignore = NULL;
   map_ip4_reass_lock ();
-  map_ip4_reass_t *r = map_ip4_reass_get (map_get_ip4 (&ip6->src_address),
+  map_ip4_reass_t *r = map_ip4_reass_get (map_get_ip4 (&ip6->src_address,
+                                                      d->flags),
                                          ip6_map_t_embedded_address (d,
                                                                      &ip6->
                                                                      dst_address),
@@ -94,347 +98,99 @@ ip6_map_fragment_get (ip6_header_t * ip6, ip6_frag_hdr_t * frag,
   return ret;
 }
 
-static_always_inline u8
-ip6_translate_tos (const ip6_header_t * ip6)
+typedef struct
 {
-#ifdef IP6_MAP_T_OVERRIDE_TOS
-  return IP6_MAP_T_OVERRIDE_TOS;
-#else
-  return (clib_net_to_host_u32 (ip6->ip_version_traffic_class_and_flow_label)
-         & 0x0ff00000) >> 20;
-#endif
-}
-
-//TODO: Find right place in memory for that
-/* *INDENT-OFF* */
-static u8 icmp6_to_icmp_updater_pointer_table[] =
-  { 0, 1, ~0, ~0,
-    2, 2, 9, 8,
-    12, 12, 12, 12,
-    12, 12, 12, 12,
-    12, 12, 12, 12,
-    12, 12, 12, 12,
-    24, 24, 24, 24,
-    24, 24, 24, 24,
-    24, 24, 24, 24,
-    24, 24, 24, 24
-  };
-/* *INDENT-ON* */
+  map_domain_t *d;
+  u16 id;
+} icmp6_to_icmp_ctx_t;
 
-static_always_inline int
-ip6_icmp_to_icmp6_in_place (icmp46_header_t * icmp, u32 icmp_len,
-                           i32 * sender_port, ip6_header_t ** inner_ip6)
+static int
+ip6_to_ip4_set_icmp_cb (ip6_header_t * ip6, ip4_header_t * ip4, void *arg)
 {
-  *inner_ip6 = NULL;
-  switch (icmp->type)
-    {
-    case ICMP6_echo_request:
-      *sender_port = ((u16 *) icmp)[2];
-      icmp->type = ICMP4_echo_request;
-      break;
-    case ICMP6_echo_reply:
-      *sender_port = ((u16 *) icmp)[2];
-      icmp->type = ICMP4_echo_reply;
-      break;
-    case ICMP6_destination_unreachable:
-      *inner_ip6 = (ip6_header_t *) u8_ptr_add (icmp, 8);
-      *sender_port = ip6_get_port (*inner_ip6, MAP_RECEIVER, icmp_len);
-
-      switch (icmp->code)
-       {
-       case ICMP6_destination_unreachable_no_route_to_destination:     //0
-       case ICMP6_destination_unreachable_beyond_scope_of_source_address:      //2
-       case ICMP6_destination_unreachable_address_unreachable: //3
-         icmp->type = ICMP4_destination_unreachable;
-         icmp->code =
-           ICMP4_destination_unreachable_destination_unreachable_host;
-         break;
-       case ICMP6_destination_unreachable_destination_administratively_prohibited:     //1
-         icmp->type =
-           ICMP4_destination_unreachable;
-         icmp->code =
-           ICMP4_destination_unreachable_communication_administratively_prohibited;
-         break;
-       case ICMP6_destination_unreachable_port_unreachable:
-         icmp->type = ICMP4_destination_unreachable;
-         icmp->code = ICMP4_destination_unreachable_port_unreachable;
-         break;
-       default:
-         return -1;
-       }
-      break;
-    case ICMP6_packet_too_big:
-      *inner_ip6 = (ip6_header_t *) u8_ptr_add (icmp, 8);
-      *sender_port = ip6_get_port (*inner_ip6, MAP_RECEIVER, icmp_len);
-
-      icmp->type = ICMP4_destination_unreachable;
-      icmp->code = 4;
-      {
-       u32 advertised_mtu = clib_net_to_host_u32 (*((u32 *) (icmp + 1)));
-       advertised_mtu -= 20;
-       //FIXME: = minimum(advertised MTU-20, MTU_of_IPv4_nexthop, (MTU_of_IPv6_nexthop)-20)
-       ((u16 *) (icmp))[3] = clib_host_to_net_u16 (advertised_mtu);
-      }
-      break;
-
-    case ICMP6_time_exceeded:
-      *inner_ip6 = (ip6_header_t *) u8_ptr_add (icmp, 8);
-      *sender_port = ip6_get_port (*inner_ip6, MAP_RECEIVER, icmp_len);
-
-      icmp->type = ICMP4_time_exceeded;
-      break;
-
-    case ICMP6_parameter_problem:
-      *inner_ip6 = (ip6_header_t *) u8_ptr_add (icmp, 8);
-      *sender_port = ip6_get_port (*inner_ip6, MAP_RECEIVER, icmp_len);
-
-      switch (icmp->code)
-       {
-       case ICMP6_parameter_problem_erroneous_header_field:
-         icmp->type = ICMP4_parameter_problem;
-         icmp->code = ICMP4_parameter_problem_pointer_indicates_error;
-         u32 pointer = clib_net_to_host_u32 (*((u32 *) (icmp + 1)));
-         if (pointer >= 40)
-           return -1;
-
-         ((u8 *) (icmp + 1))[0] =
-           icmp6_to_icmp_updater_pointer_table[pointer];
-         break;
-       case ICMP6_parameter_problem_unrecognized_next_header:
-         icmp->type = ICMP4_destination_unreachable;
-         icmp->code = ICMP4_destination_unreachable_port_unreachable;
-         break;
-       case ICMP6_parameter_problem_unrecognized_option:
-       default:
-         return -1;
-       }
-      break;
-    default:
-      return -1;
-      break;
-    }
-  return 0;
-}
+  icmp6_to_icmp_ctx_t *ctx = arg;
+  map_main_t *mm = &map_main;
 
-static_always_inline void
-_ip6_map_t_icmp (map_domain_t * d, vlib_buffer_t * p, u8 * error)
-{
-  ip6_header_t *ip6, *inner_ip6;
-  ip4_header_t *ip4, *inner_ip4;
-  u32 ip6_pay_len;
-  icmp46_header_t *icmp;
-  i32 sender_port;
-  ip_csum_t csum;
-  u32 ip4_sadr, inner_ip4_dadr;
-
-  ip6 = vlib_buffer_get_current (p);
-  ip6_pay_len = clib_net_to_host_u16 (ip6->payload_length);
-  icmp = (icmp46_header_t *) (ip6 + 1);
-  ASSERT (ip6_pay_len + sizeof (*ip6) <= p->current_length);
-
-  if (ip6->protocol != IP_PROTOCOL_ICMP6)
+  if (mm->is_ce)
     {
-      //No extensions headers allowed here
-      //TODO: SR header
-      *error = MAP_ERROR_MALFORMED;
-      return;
+      u32 ip4_dadr;
+
+      //Security check
+      //Note that this prevents an intermediate IPv6 router from answering the request
+      ip4_dadr = map_get_ip4 (&ip6->dst_address, ctx->d->flags);
+      if (ip6->dst_address.as_u64[0] !=
+         map_get_pfx_net (ctx->d, ip4_dadr, ctx->id)
+         || ip6->dst_address.as_u64[1] != map_get_sfx_net (ctx->d, ip4_dadr,
+                                                           ctx->id))
+       return -1;
+
+      ip4->src_address.as_u32 =
+       ip6_map_t_embedded_address (ctx->d, &ip6->src_address);
+      ip4->dst_address.as_u32 = ip4_dadr;
     }
-
-  //There are no fragmented ICMP messages, so no extension header for now
-
-  if (ip6_icmp_to_icmp6_in_place
-      (icmp, ip6_pay_len, &sender_port, &inner_ip6))
+  else
     {
-      //TODO: In case of 1:1 mapping it is not necessary to have the sender port
-      *error = MAP_ERROR_ICMP;
-      return;
+      u32 ip4_sadr;
+
+      //Security check
+      //Note that this prevents an intermediate IPv6 router from answering the request
+      ip4_sadr = map_get_ip4 (&ip6->src_address, ctx->d->flags);
+      if (ip6->src_address.as_u64[0] !=
+         map_get_pfx_net (ctx->d, ip4_sadr, ctx->id)
+         || ip6->src_address.as_u64[1] != map_get_sfx_net (ctx->d, ip4_sadr,
+                                                           ctx->id))
+       return -1;
+
+      ip4->dst_address.as_u32 =
+       ip6_map_t_embedded_address (ctx->d, &ip6->dst_address);
+      ip4->src_address.as_u32 = ip4_sadr;
     }
 
-  if (sender_port < 0)
-    {
-      // In case of 1:1 mapping, we don't care about the port
-      if (d->ea_bits_len == 0 && d->rules)
-       {
-         sender_port = 0;
-       }
-      else
-       {
-         *error = MAP_ERROR_ICMP;
-         return;
-       }
-    }
+  return 0;
+}
 
-  //Security check
-  //Note that this prevents an intermediate IPv6 router from answering the request
-  ip4_sadr = map_get_ip4 (&ip6->src_address);
-  if (ip6->src_address.as_u64[0] != map_get_pfx_net (d, ip4_sadr, sender_port)
-      || ip6->src_address.as_u64[1] != map_get_sfx_net (d, ip4_sadr,
-                                                       sender_port))
-    {
-      *error = MAP_ERROR_SEC_CHECK;
-      return;
-    }
+static int
+ip6_to_ip4_set_inner_icmp_cb (ip6_header_t * ip6, ip4_header_t * ip4,
+                             void *arg)
+{
+  icmp6_to_icmp_ctx_t *ctx = arg;
+  map_main_t *mm = &map_main;
 
-  if (inner_ip6)
+  if (mm->is_ce)
     {
-      u16 *inner_L4_checksum, inner_l4_offset, inner_frag_offset,
-       inner_frag_id;
-      u8 *inner_l4, inner_protocol;
-
-      //We have two headers to translate
-      //   FROM
-      //   [   IPv6   ]<- ext ->[IC][   IPv6   ]<- ext ->[L4 header ...
-      // Handled cases:
-      //                     [   IPv6   ][IC][   IPv6   ][L4 header ...
-      //                 [   IPv6   ][IC][   IPv6   ][Fr][L4 header ...
-      //    TO
-      //                               [ IPv4][IC][ IPv4][L4 header ...
-
-      //TODO: This was already done deep in ip6_icmp_to_icmp6_in_place
-      //We shouldn't have to do it again
-      if (ip6_parse (inner_ip6, ip6_pay_len - 8,
-                    &inner_protocol, &inner_l4_offset, &inner_frag_offset))
-       {
-         *error = MAP_ERROR_MALFORMED;
-         return;
-       }
-
-      inner_l4 = u8_ptr_add (inner_ip6, inner_l4_offset);
-      inner_ip4 =
-       (ip4_header_t *) u8_ptr_add (inner_l4, -sizeof (*inner_ip4));
-      if (inner_frag_offset)
-       {
-         ip6_frag_hdr_t *inner_frag =
-           (ip6_frag_hdr_t *) u8_ptr_add (inner_ip6, inner_frag_offset);
-         inner_frag_id = frag_id_6to4 (inner_frag->identification);
-       }
-      else
-       {
-         inner_frag_id = 0;
-       }
-
-      //Do the translation of the inner packet
-      if (inner_protocol == IP_PROTOCOL_TCP)
-       {
-         inner_L4_checksum = (u16 *) u8_ptr_add (inner_l4, 16);
-       }
-      else if (inner_protocol == IP_PROTOCOL_UDP)
-       {
-         inner_L4_checksum = (u16 *) u8_ptr_add (inner_l4, 6);
-       }
-      else if (inner_protocol == IP_PROTOCOL_ICMP6)
-       {
-         icmp46_header_t *inner_icmp = (icmp46_header_t *) inner_l4;
-         csum = inner_icmp->checksum;
-         csum = ip_csum_sub_even (csum, *((u16 *) inner_icmp));
-         //It cannot be of a different type as ip6_icmp_to_icmp6_in_place succeeded
-         inner_icmp->type = (inner_icmp->type == ICMP6_echo_request) ?
-           ICMP4_echo_request : ICMP4_echo_reply;
-         csum = ip_csum_add_even (csum, *((u16 *) inner_icmp));
-         inner_icmp->checksum = ip_csum_fold (csum);
-         inner_protocol = IP_PROTOCOL_ICMP;    //Will be copied to ip6 later
-         inner_L4_checksum = &inner_icmp->checksum;
-       }
-      else
-       {
-         *error = MAP_ERROR_BAD_PROTOCOL;
-         return;
-       }
-
-      csum = *inner_L4_checksum;
-      csum = ip_csum_sub_even (csum, inner_ip6->src_address.as_u64[0]);
-      csum = ip_csum_sub_even (csum, inner_ip6->src_address.as_u64[1]);
-      csum = ip_csum_sub_even (csum, inner_ip6->dst_address.as_u64[0]);
-      csum = ip_csum_sub_even (csum, inner_ip6->dst_address.as_u64[1]);
-
-      //Sanity check of the outer destination address
-      if (ip6->dst_address.as_u64[0] != inner_ip6->src_address.as_u64[0] &&
-         ip6->dst_address.as_u64[1] != inner_ip6->src_address.as_u64[1])
-       {
-         *error = MAP_ERROR_SEC_CHECK;
-         return;
-       }
+      u32 inner_ip4_sadr;
 
       //Security check of inner packet
-      inner_ip4_dadr = map_get_ip4 (&inner_ip6->dst_address);
-      if (inner_ip6->dst_address.as_u64[0] !=
-         map_get_pfx_net (d, inner_ip4_dadr, sender_port)
-         || inner_ip6->dst_address.as_u64[1] != map_get_sfx_net (d,
-                                                                 inner_ip4_dadr,
-                                                                 sender_port))
-       {
-         *error = MAP_ERROR_SEC_CHECK;
-         return;
-       }
-
-      inner_ip4->dst_address.as_u32 = inner_ip4_dadr;
-      inner_ip4->src_address.as_u32 =
-       ip6_map_t_embedded_address (d, &inner_ip6->src_address);
-      inner_ip4->ip_version_and_header_length =
-       IP4_VERSION_AND_HEADER_LENGTH_NO_OPTIONS;
-      inner_ip4->tos = ip6_translate_tos (inner_ip6);
-      inner_ip4->length =
-       u16_net_add (inner_ip6->payload_length,
-                    sizeof (*ip4) + sizeof (*ip6) - inner_l4_offset);
-      inner_ip4->fragment_id = inner_frag_id;
-      inner_ip4->flags_and_fragment_offset =
-       clib_host_to_net_u16 (IP4_HEADER_FLAG_MORE_FRAGMENTS);
-      inner_ip4->ttl = inner_ip6->hop_limit;
-      inner_ip4->protocol = inner_protocol;
-      inner_ip4->checksum = ip4_header_checksum (inner_ip4);
-
-      if (inner_ip4->protocol == IP_PROTOCOL_ICMP)
-       {
-         //Remove remainings of the pseudo-header in the csum
-         csum =
-           ip_csum_sub_even (csum, clib_host_to_net_u16 (IP_PROTOCOL_ICMP6));
-         csum =
-           ip_csum_sub_even (csum, inner_ip4->length - sizeof (*inner_ip4));
-       }
-      else
-       {
-         //Update to new pseudo-header
-         csum = ip_csum_add_even (csum, inner_ip4->src_address.as_u32);
-         csum = ip_csum_add_even (csum, inner_ip4->dst_address.as_u32);
-       }
-      *inner_L4_checksum = ip_csum_fold (csum);
-
-      //Move up icmp header
-      ip4 = (ip4_header_t *) u8_ptr_add (inner_l4, -2 * sizeof (*ip4) - 8);
-      clib_memcpy (u8_ptr_add (inner_l4, -sizeof (*ip4) - 8), icmp, 8);
-      icmp = (icmp46_header_t *) u8_ptr_add (inner_l4, -sizeof (*ip4) - 8);
+      inner_ip4_sadr = map_get_ip4 (&ip6->src_address, ctx->d->flags);
+      if (ip6->src_address.as_u64[0] !=
+         map_get_pfx_net (ctx->d, inner_ip4_sadr, ctx->id)
+         || ip6->src_address.as_u64[1] != map_get_sfx_net (ctx->d,
+                                                           inner_ip4_sadr,
+                                                           ctx->id))
+       return -1;
+
+      ip4->src_address.as_u32 = inner_ip4_sadr;
+      ip4->dst_address.as_u32 =
+       ip6_map_t_embedded_address (ctx->d, &ip6->dst_address);
     }
   else
     {
-      //Only one header to translate
-      ip4 = (ip4_header_t *) u8_ptr_add (ip6, sizeof (*ip6) - sizeof (*ip4));
+      u32 inner_ip4_dadr;
+
+      //Security check of inner packet
+      inner_ip4_dadr = map_get_ip4 (&ip6->dst_address, ctx->d->flags);
+      if (ip6->dst_address.as_u64[0] !=
+         map_get_pfx_net (ctx->d, inner_ip4_dadr, ctx->id)
+         || ip6->dst_address.as_u64[1] != map_get_sfx_net (ctx->d,
+                                                           inner_ip4_dadr,
+                                                           ctx->id))
+       return -1;
+
+      ip4->dst_address.as_u32 = inner_ip4_dadr;
+      ip4->src_address.as_u32 =
+       ip6_map_t_embedded_address (ctx->d, &ip6->src_address);
     }
-  vlib_buffer_advance (p, (u32) (((u8 *) ip4) - ((u8 *) ip6)));
-
-  ip4->dst_address.as_u32 = ip6_map_t_embedded_address (d, &ip6->dst_address);
-  ip4->src_address.as_u32 = ip4_sadr;
-  ip4->ip_version_and_header_length =
-    IP4_VERSION_AND_HEADER_LENGTH_NO_OPTIONS;
-  ip4->tos = ip6_translate_tos (ip6);
-  ip4->fragment_id = 0;
-  ip4->flags_and_fragment_offset = 0;
-  ip4->ttl = ip6->hop_limit;
-  ip4->protocol = IP_PROTOCOL_ICMP;
-  //TODO fix the length depending on offset length
-  ip4->length = u16_net_add (ip6->payload_length,
-                            (inner_ip6 ==
-                             NULL) ? sizeof (*ip4) : (2 * sizeof (*ip4) -
-                                                      sizeof (*ip6)));
-  ip4->checksum = ip4_header_checksum (ip4);
-
-  //TODO: We could do an easy diff-checksum for echo requests/replies
-  //Recompute ICMP checksum
-  icmp->checksum = 0;
-  csum =
-    ip_incremental_checksum (0, icmp,
-                            clib_net_to_host_u16 (ip4->length) -
-                            sizeof (*ip4));
-  icmp->checksum = ~ip_csum_fold (csum);
+
+  return 0;
 }
 
 static uword
@@ -462,6 +218,9 @@ ip6_map_t_icmp (vlib_main_t * vm,
          ip6_mapt_icmp_next_t next0;
          map_domain_t *d0;
          u16 len0;
+         icmp6_to_icmp_ctx_t ctx0;
+         ip6_header_t *ip60;
+         icmp46_header_t *icmp0;
 
          pi0 = to_next[0] = from[0];
          from += 1;
@@ -472,14 +231,34 @@ ip6_map_t_icmp (vlib_main_t * vm,
          next0 = IP6_MAPT_ICMP_NEXT_IP4_LOOKUP;
 
          p0 = vlib_get_buffer (vm, pi0);
-         len0 =
-           clib_net_to_host_u16 (((ip6_header_t *)
-                                  vlib_buffer_get_current
-                                  (p0))->payload_length);
+         ip60 = vlib_buffer_get_current (p0);
+         len0 = clib_net_to_host_u16 (ip60->payload_length);
+         icmp0 = (icmp46_header_t *) (ip60 + 1);
          d0 =
            pool_elt_at_index (map_main.domains,
                               vnet_buffer (p0)->map_t.map_domain_index);
-         _ip6_map_t_icmp (d0, p0, &error0);
+
+         ctx0.id =
+           ip6_get_port (ip60, icmp0->type == ICMP6_echo_request,
+                         p0->current_length);
+         ctx0.d = d0;
+         if (ctx0.id == 0)
+           {
+             // In case of 1:1 mapping, we don't care about the port
+             if (!(d0->ea_bits_len == 0 && d0->rules))
+               {
+                 error0 = MAP_ERROR_ICMP;
+                 goto err0;
+               }
+           }
+
+         if (icmp6_to_icmp
+             (p0, ip6_to_ip4_set_icmp_cb, &ctx0,
+              ip6_to_ip4_set_inner_icmp_cb, &ctx0))
+           {
+             error0 = MAP_ERROR_ICMP;
+             goto err0;
+           }
 
          if (vnet_buffer (p0)->map_t.mtu < p0->current_length)
            {
@@ -489,7 +268,7 @@ ip6_map_t_icmp (vlib_main_t * vm,
              vnet_buffer (p0)->ip_frag.next_index = IP4_FRAG_NEXT_IP4_LOOKUP;
              next0 = IP6_MAPT_ICMP_NEXT_IP4_FRAG;
            }
-
+       err0:
          if (PREDICT_TRUE (error0 == MAP_ERROR_NONE))
            {
              vlib_increment_combined_counter (cm + MAP_DOMAIN_COUNTER_RX,
@@ -513,6 +292,17 @@ ip6_map_t_icmp (vlib_main_t * vm,
   return frame->n_vectors;
 }
 
+static int
+ip6_to_ip4_set_cb (ip6_header_t * ip6, ip4_header_t * ip4, void *ctx)
+{
+  vlib_buffer_t *p = ctx;
+
+  ip4->dst_address.as_u32 = vnet_buffer (p)->map_t.v6.daddr;
+  ip4->src_address.as_u32 = vnet_buffer (p)->map_t.v6.saddr;
+
+  return 0;
+}
+
 static uword
 ip6_map_t_fragmented (vlib_main_t * vm,
                      vlib_node_runtime_t * node, vlib_frame_t * frame)
@@ -521,6 +311,8 @@ ip6_map_t_fragmented (vlib_main_t * vm,
   from = vlib_frame_vector_args (frame);
   n_left_from = frame->n_vectors;
   next_index = node->cached_next_index;
+  vlib_node_runtime_t *error_node =
+    vlib_node_get_runtime (vm, ip6_map_t_fragmented_node.index);
 
   while (n_left_from > 0)
     {
@@ -531,11 +323,6 @@ ip6_map_t_fragmented (vlib_main_t * vm,
        {
          u32 pi0, pi1;
          vlib_buffer_t *p0, *p1;
-         ip6_header_t *ip60, *ip61;
-         ip6_frag_hdr_t *frag0, *frag1;
-         ip4_header_t *ip40, *ip41;
-         u16 frag_id0, frag_offset0, frag_id1, frag_offset1;
-         u8 frag_more0, frag_more1;
          u32 next0, next1;
 
          pi0 = to_next[0] = from[0];
@@ -549,94 +336,41 @@ ip6_map_t_fragmented (vlib_main_t * vm,
          next1 = IP6_MAPT_TCP_UDP_NEXT_IP4_LOOKUP;
          p0 = vlib_get_buffer (vm, pi0);
          p1 = vlib_get_buffer (vm, pi1);
-         ip60 = vlib_buffer_get_current (p0);
-         ip61 = vlib_buffer_get_current (p1);
-         frag0 =
-           (ip6_frag_hdr_t *) u8_ptr_add (ip60,
-                                          vnet_buffer (p0)->map_t.
-                                          v6.frag_offset);
-         frag1 =
-           (ip6_frag_hdr_t *) u8_ptr_add (ip61,
-                                          vnet_buffer (p1)->map_t.
-                                          v6.frag_offset);
-         ip40 =
-           (ip4_header_t *) u8_ptr_add (ip60,
-                                        vnet_buffer (p0)->map_t.
-                                        v6.l4_offset - sizeof (*ip40));
-         ip41 =
-           (ip4_header_t *) u8_ptr_add (ip61,
-                                        vnet_buffer (p1)->map_t.
-                                        v6.l4_offset - sizeof (*ip40));
-         vlib_buffer_advance (p0,
-                              vnet_buffer (p0)->map_t.v6.l4_offset -
-                              sizeof (*ip40));
-         vlib_buffer_advance (p1,
-                              vnet_buffer (p1)->map_t.v6.l4_offset -
-                              sizeof (*ip40));
-
-         frag_id0 = frag_id_6to4 (frag0->identification);
-         frag_id1 = frag_id_6to4 (frag1->identification);
-         frag_more0 = ip6_frag_hdr_more (frag0);
-         frag_more1 = ip6_frag_hdr_more (frag1);
-         frag_offset0 = ip6_frag_hdr_offset (frag0);
-         frag_offset1 = ip6_frag_hdr_offset (frag1);
-
-         ip40->dst_address.as_u32 = vnet_buffer (p0)->map_t.v6.daddr;
-         ip41->dst_address.as_u32 = vnet_buffer (p1)->map_t.v6.daddr;
-         ip40->src_address.as_u32 = vnet_buffer (p0)->map_t.v6.saddr;
-         ip41->src_address.as_u32 = vnet_buffer (p1)->map_t.v6.saddr;
-         ip40->ip_version_and_header_length =
-           IP4_VERSION_AND_HEADER_LENGTH_NO_OPTIONS;
-         ip41->ip_version_and_header_length =
-           IP4_VERSION_AND_HEADER_LENGTH_NO_OPTIONS;
-         ip40->tos = ip6_translate_tos (ip60);
-         ip41->tos = ip6_translate_tos (ip61);
-         ip40->length = u16_net_add (ip60->payload_length,
-                                     sizeof (*ip40) -
-                                     vnet_buffer (p0)->map_t.v6.l4_offset +
-                                     sizeof (*ip60));
-         ip41->length =
-           u16_net_add (ip61->payload_length,
-                        sizeof (*ip40) -
-                        vnet_buffer (p1)->map_t.v6.l4_offset +
-                        sizeof (*ip60));
-         ip40->fragment_id = frag_id0;
-         ip41->fragment_id = frag_id1;
-         ip40->flags_and_fragment_offset =
-           clib_host_to_net_u16 (frag_offset0 |
-                                 (frag_more0 ? IP4_HEADER_FLAG_MORE_FRAGMENTS
-                                  : 0));
-         ip41->flags_and_fragment_offset =
-           clib_host_to_net_u16 (frag_offset1 |
-                                 (frag_more1 ? IP4_HEADER_FLAG_MORE_FRAGMENTS
-                                  : 0));
-         ip40->ttl = ip60->hop_limit;
-         ip41->ttl = ip61->hop_limit;
-         ip40->protocol =
-           (vnet_buffer (p0)->map_t.v6.l4_protocol ==
-            IP_PROTOCOL_ICMP6) ? IP_PROTOCOL_ICMP : vnet_buffer (p0)->
-           map_t.v6.l4_protocol;
-         ip41->protocol =
-           (vnet_buffer (p1)->map_t.v6.l4_protocol ==
-            IP_PROTOCOL_ICMP6) ? IP_PROTOCOL_ICMP : vnet_buffer (p1)->
-           map_t.v6.l4_protocol;
-         ip40->checksum = ip4_header_checksum (ip40);
-         ip41->checksum = ip4_header_checksum (ip41);
 
-         if (vnet_buffer (p0)->map_t.mtu < p0->current_length)
+         if (ip6_to_ip4_fragmented (p0, ip6_to_ip4_set_cb, p0))
            {
-             vnet_buffer (p0)->ip_frag.mtu = vnet_buffer (p0)->map_t.mtu;
-             vnet_buffer (p0)->ip_frag.header_offset = 0;
-             vnet_buffer (p0)->ip_frag.next_index = IP4_FRAG_NEXT_IP4_LOOKUP;
-             next0 = IP6_MAPT_FRAGMENTED_NEXT_IP4_FRAG;
+             p0->error = error_node->errors[MAP_ERROR_FRAGMENT_DROPPED];
+             next0 = IP6_MAPT_FRAGMENTED_NEXT_DROP;
+           }
+         else
+           {
+             if (vnet_buffer (p0)->map_t.mtu < p0->current_length)
+               {
+                 //Send to fragmentation node if necessary
+                 vnet_buffer (p0)->ip_frag.mtu = vnet_buffer (p0)->map_t.mtu;
+                 vnet_buffer (p0)->ip_frag.header_offset = 0;
+                 vnet_buffer (p0)->ip_frag.next_index =
+                   IP4_FRAG_NEXT_IP4_LOOKUP;
+                 next0 = IP6_MAPT_FRAGMENTED_NEXT_IP4_FRAG;
+               }
            }
 
-         if (vnet_buffer (p1)->map_t.mtu < p1->current_length)
+         if (ip6_to_ip4_fragmented (p1, ip6_to_ip4_set_cb, p1))
            {
-             vnet_buffer (p1)->ip_frag.mtu = vnet_buffer (p1)->map_t.mtu;
-             vnet_buffer (p1)->ip_frag.header_offset = 0;
-             vnet_buffer (p1)->ip_frag.next_index = IP4_FRAG_NEXT_IP4_LOOKUP;
-             next1 = IP6_MAPT_FRAGMENTED_NEXT_IP4_FRAG;
+             p1->error = error_node->errors[MAP_ERROR_FRAGMENT_DROPPED];
+             next1 = IP6_MAPT_FRAGMENTED_NEXT_DROP;
+           }
+         else
+           {
+             if (vnet_buffer (p1)->map_t.mtu < p1->current_length)
+               {
+                 //Send to fragmentation node if necessary
+                 vnet_buffer (p1)->ip_frag.mtu = vnet_buffer (p1)->map_t.mtu;
+                 vnet_buffer (p1)->ip_frag.header_offset = 0;
+                 vnet_buffer (p1)->ip_frag.next_index =
+                   IP4_FRAG_NEXT_IP4_LOOKUP;
+                 next1 = IP6_MAPT_FRAGMENTED_NEXT_IP4_FRAG;
+               }
            }
 
          vlib_validate_buffer_enqueue_x2 (vm, node, next_index,
@@ -649,12 +383,6 @@ ip6_map_t_fragmented (vlib_main_t * vm,
        {
          u32 pi0;
          vlib_buffer_t *p0;
-         ip6_header_t *ip60;
-         ip6_frag_hdr_t *frag0;
-         ip4_header_t *ip40;
-         u16 frag_id0;
-         u8 frag_more0;
-         u16 frag_offset0;
          u32 next0;
 
          pi0 = to_next[0] = from[0];
@@ -665,51 +393,23 @@ ip6_map_t_fragmented (vlib_main_t * vm,
 
          next0 = IP6_MAPT_TCP_UDP_NEXT_IP4_LOOKUP;
          p0 = vlib_get_buffer (vm, pi0);
-         ip60 = vlib_buffer_get_current (p0);
-         frag0 =
-           (ip6_frag_hdr_t *) u8_ptr_add (ip60,
-                                          vnet_buffer (p0)->map_t.
-                                          v6.frag_offset);
-         ip40 =
-           (ip4_header_t *) u8_ptr_add (ip60,
-                                        vnet_buffer (p0)->map_t.
-                                        v6.l4_offset - sizeof (*ip40));
-         vlib_buffer_advance (p0,
-                              vnet_buffer (p0)->map_t.v6.l4_offset -
-                              sizeof (*ip40));
-
-         frag_id0 = frag_id_6to4 (frag0->identification);
-         frag_more0 = ip6_frag_hdr_more (frag0);
-         frag_offset0 = ip6_frag_hdr_offset (frag0);
-
-         ip40->dst_address.as_u32 = vnet_buffer (p0)->map_t.v6.daddr;
-         ip40->src_address.as_u32 = vnet_buffer (p0)->map_t.v6.saddr;
-         ip40->ip_version_and_header_length =
-           IP4_VERSION_AND_HEADER_LENGTH_NO_OPTIONS;
-         ip40->tos = ip6_translate_tos (ip60);
-         ip40->length = u16_net_add (ip60->payload_length,
-                                     sizeof (*ip40) -
-                                     vnet_buffer (p0)->map_t.v6.l4_offset +
-                                     sizeof (*ip60));
-         ip40->fragment_id = frag_id0;
-         ip40->flags_and_fragment_offset =
-           clib_host_to_net_u16 (frag_offset0 |
-                                 (frag_more0 ? IP4_HEADER_FLAG_MORE_FRAGMENTS
-                                  : 0));
-         ip40->ttl = ip60->hop_limit;
-         ip40->protocol =
-           (vnet_buffer (p0)->map_t.v6.l4_protocol ==
-            IP_PROTOCOL_ICMP6) ? IP_PROTOCOL_ICMP : vnet_buffer (p0)->
-           map_t.v6.l4_protocol;
-         ip40->checksum = ip4_header_checksum (ip40);
 
-         if (vnet_buffer (p0)->map_t.mtu < p0->current_length)
+         if (ip6_to_ip4_fragmented (p0, ip6_to_ip4_set_cb, p0))
            {
-             //Send to fragmentation node if necessary
-             vnet_buffer (p0)->ip_frag.mtu = vnet_buffer (p0)->map_t.mtu;
-             vnet_buffer (p0)->ip_frag.header_offset = 0;
-             vnet_buffer (p0)->ip_frag.next_index = IP4_FRAG_NEXT_IP4_LOOKUP;
-             next0 = IP6_MAPT_FRAGMENTED_NEXT_IP4_FRAG;
+             p0->error = error_node->errors[MAP_ERROR_FRAGMENT_DROPPED];
+             next0 = IP6_MAPT_FRAGMENTED_NEXT_DROP;
+           }
+         else
+           {
+             if (vnet_buffer (p0)->map_t.mtu < p0->current_length)
+               {
+                 //Send to fragmentation node if necessary
+                 vnet_buffer (p0)->ip_frag.mtu = vnet_buffer (p0)->map_t.mtu;
+                 vnet_buffer (p0)->ip_frag.header_offset = 0;
+                 vnet_buffer (p0)->ip_frag.next_index =
+                   IP4_FRAG_NEXT_IP4_LOOKUP;
+                 next0 = IP6_MAPT_FRAGMENTED_NEXT_IP4_FRAG;
+               }
            }
 
          vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
@@ -726,6 +426,9 @@ ip6_map_t_tcp_udp (vlib_main_t * vm,
                   vlib_node_runtime_t * node, vlib_frame_t * frame)
 {
   u32 n_left_from, *from, next_index, *to_next, n_left_to_next;
+  vlib_node_runtime_t *error_node =
+    vlib_node_get_runtime (vm, ip6_map_t_tcp_udp_node.index);
+
   from = vlib_frame_vector_args (frame);
   n_left_from = frame->n_vectors;
   next_index = node->cached_next_index;
@@ -738,11 +441,6 @@ ip6_map_t_tcp_udp (vlib_main_t * vm,
        {
          u32 pi0, pi1;
          vlib_buffer_t *p0, *p1;
-         ip6_header_t *ip60, *ip61;
-         ip_csum_t csum0, csum1;
-         ip4_header_t *ip40, *ip41;
-         u16 fragment_id0, flags0, *checksum0,
-           fragment_id1, flags1, *checksum1;
          ip6_mapt_tcp_udp_next_t next0, next1;
 
          pi0 = to_next[0] = from[0];
@@ -756,118 +454,41 @@ ip6_map_t_tcp_udp (vlib_main_t * vm,
 
          p0 = vlib_get_buffer (vm, pi0);
          p1 = vlib_get_buffer (vm, pi1);
-         ip60 = vlib_buffer_get_current (p0);
-         ip61 = vlib_buffer_get_current (p1);
-         ip40 =
-           (ip4_header_t *) u8_ptr_add (ip60,
-                                        vnet_buffer (p0)->map_t.
-                                        v6.l4_offset - sizeof (*ip40));
-         ip41 =
-           (ip4_header_t *) u8_ptr_add (ip61,
-                                        vnet_buffer (p1)->map_t.
-                                        v6.l4_offset - sizeof (*ip40));
-         vlib_buffer_advance (p0,
-                              vnet_buffer (p0)->map_t.v6.l4_offset -
-                              sizeof (*ip40));
-         vlib_buffer_advance (p1,
-                              vnet_buffer (p1)->map_t.v6.l4_offset -
-                              sizeof (*ip40));
-         checksum0 =
-           (u16 *) u8_ptr_add (ip60,
-                               vnet_buffer (p0)->map_t.checksum_offset);
-         checksum1 =
-           (u16 *) u8_ptr_add (ip61,
-                               vnet_buffer (p1)->map_t.checksum_offset);
-
-         csum0 = ip_csum_sub_even (*checksum0, ip60->src_address.as_u64[0]);
-         csum1 = ip_csum_sub_even (*checksum1, ip61->src_address.as_u64[0]);
-         csum0 = ip_csum_sub_even (csum0, ip60->src_address.as_u64[1]);
-         csum1 = ip_csum_sub_even (csum1, ip61->src_address.as_u64[1]);
-         csum0 = ip_csum_sub_even (csum0, ip60->dst_address.as_u64[0]);
-         csum1 = ip_csum_sub_even (csum0, ip61->dst_address.as_u64[0]);
-         csum0 = ip_csum_sub_even (csum0, ip60->dst_address.as_u64[1]);
-         csum1 = ip_csum_sub_even (csum1, ip61->dst_address.as_u64[1]);
-         csum0 = ip_csum_add_even (csum0, vnet_buffer (p0)->map_t.v6.daddr);
-         csum1 = ip_csum_add_even (csum1, vnet_buffer (p1)->map_t.v6.daddr);
-         csum0 = ip_csum_add_even (csum0, vnet_buffer (p0)->map_t.v6.saddr);
-         csum1 = ip_csum_add_even (csum1, vnet_buffer (p1)->map_t.v6.saddr);
-         *checksum0 = ip_csum_fold (csum0);
-         *checksum1 = ip_csum_fold (csum1);
-
-         if (PREDICT_FALSE (vnet_buffer (p0)->map_t.v6.frag_offset))
-           {
-             ip6_frag_hdr_t *hdr = (ip6_frag_hdr_t *) u8_ptr_add (ip60,
-                                                                  vnet_buffer
-                                                                  (p0)->
-                                                                  map_t.
-                                                                  v6.frag_offset);
-             fragment_id0 = frag_id_6to4 (hdr->identification);
-             flags0 = clib_host_to_net_u16 (IP4_HEADER_FLAG_MORE_FRAGMENTS);
-           }
-         else
-           {
-             fragment_id0 = 0;
-             flags0 = 0;
-           }
 
-         if (PREDICT_FALSE (vnet_buffer (p1)->map_t.v6.frag_offset))
+         if (ip6_to_ip4_tcp_udp (p0, ip6_to_ip4_set_cb, p0, 1))
            {
-             ip6_frag_hdr_t *hdr = (ip6_frag_hdr_t *) u8_ptr_add (ip61,
-                                                                  vnet_buffer
-                                                                  (p1)->
-                                                                  map_t.
-                                                                  v6.frag_offset);
-             fragment_id1 = frag_id_6to4 (hdr->identification);
-             flags1 = clib_host_to_net_u16 (IP4_HEADER_FLAG_MORE_FRAGMENTS);
+             p0->error = error_node->errors[MAP_ERROR_UNKNOWN];
+             next0 = IP6_MAPT_TCP_UDP_NEXT_DROP;
            }
          else
            {
-             fragment_id1 = 0;
-             flags1 = 0;
+             if (vnet_buffer (p0)->map_t.mtu < p0->current_length)
+               {
+                 //Send to fragmentation node if necessary
+                 vnet_buffer (p0)->ip_frag.mtu = vnet_buffer (p0)->map_t.mtu;
+                 vnet_buffer (p0)->ip_frag.header_offset = 0;
+                 vnet_buffer (p0)->ip_frag.next_index =
+                   IP4_FRAG_NEXT_IP4_LOOKUP;
+                 next0 = IP6_MAPT_TCP_UDP_NEXT_IP4_FRAG;
+               }
            }
 
-         ip40->dst_address.as_u32 = vnet_buffer (p0)->map_t.v6.daddr;
-         ip41->dst_address.as_u32 = vnet_buffer (p1)->map_t.v6.daddr;
-         ip40->src_address.as_u32 = vnet_buffer (p0)->map_t.v6.saddr;
-         ip41->src_address.as_u32 = vnet_buffer (p1)->map_t.v6.saddr;
-         ip40->ip_version_and_header_length =
-           IP4_VERSION_AND_HEADER_LENGTH_NO_OPTIONS;
-         ip41->ip_version_and_header_length =
-           IP4_VERSION_AND_HEADER_LENGTH_NO_OPTIONS;
-         ip40->tos = ip6_translate_tos (ip60);
-         ip41->tos = ip6_translate_tos (ip61);
-         ip40->length = u16_net_add (ip60->payload_length,
-                                     sizeof (*ip40) + sizeof (*ip60) -
-                                     vnet_buffer (p0)->map_t.v6.l4_offset);
-         ip41->length =
-           u16_net_add (ip61->payload_length,
-                        sizeof (*ip40) + sizeof (*ip60) -
-                        vnet_buffer (p1)->map_t.v6.l4_offset);
-         ip40->fragment_id = fragment_id0;
-         ip41->fragment_id = fragment_id1;
-         ip40->flags_and_fragment_offset = flags0;
-         ip41->flags_and_fragment_offset = flags1;
-         ip40->ttl = ip60->hop_limit;
-         ip41->ttl = ip61->hop_limit;
-         ip40->protocol = vnet_buffer (p0)->map_t.v6.l4_protocol;
-         ip41->protocol = vnet_buffer (p1)->map_t.v6.l4_protocol;
-         ip40->checksum = ip4_header_checksum (ip40);
-         ip41->checksum = ip4_header_checksum (ip41);
-
-         if (vnet_buffer (p0)->map_t.mtu < p0->current_length)
+         if (ip6_to_ip4_tcp_udp (p1, ip6_to_ip4_set_cb, p1, 1))
            {
-             vnet_buffer (p0)->ip_frag.mtu = vnet_buffer (p0)->map_t.mtu;
-             vnet_buffer (p0)->ip_frag.header_offset = 0;
-             vnet_buffer (p0)->ip_frag.next_index = IP4_FRAG_NEXT_IP4_LOOKUP;
-             next0 = IP6_MAPT_TCP_UDP_NEXT_IP4_FRAG;
+             p1->error = error_node->errors[MAP_ERROR_UNKNOWN];
+             next1 = IP6_MAPT_TCP_UDP_NEXT_DROP;
            }
-
-         if (vnet_buffer (p1)->map_t.mtu < p1->current_length)
+         else
            {
-             vnet_buffer (p1)->ip_frag.mtu = vnet_buffer (p1)->map_t.mtu;
-             vnet_buffer (p1)->ip_frag.header_offset = 0;
-             vnet_buffer (p1)->ip_frag.next_index = IP4_FRAG_NEXT_IP4_LOOKUP;
-             next1 = IP6_MAPT_TCP_UDP_NEXT_IP4_FRAG;
+             if (vnet_buffer (p1)->map_t.mtu < p1->current_length)
+               {
+                 //Send to fragmentation node if necessary
+                 vnet_buffer (p1)->ip_frag.mtu = vnet_buffer (p1)->map_t.mtu;
+                 vnet_buffer (p1)->ip_frag.header_offset = 0;
+                 vnet_buffer (p1)->ip_frag.next_index =
+                   IP4_FRAG_NEXT_IP4_LOOKUP;
+                 next1 = IP6_MAPT_TCP_UDP_NEXT_IP4_FRAG;
+               }
            }
 
          vlib_validate_buffer_enqueue_x2 (vm, node, next_index, to_next,
@@ -880,12 +501,6 @@ ip6_map_t_tcp_udp (vlib_main_t * vm,
        {
          u32 pi0;
          vlib_buffer_t *p0;
-         ip6_header_t *ip60;
-         u16 *checksum0;
-         ip_csum_t csum0;
-         ip4_header_t *ip40;
-         u16 fragment_id0;
-         u16 flags0;
          ip6_mapt_tcp_udp_next_t next0;
 
          pi0 = to_next[0] = from[0];
@@ -896,65 +511,23 @@ ip6_map_t_tcp_udp (vlib_main_t * vm,
          next0 = IP6_MAPT_TCP_UDP_NEXT_IP4_LOOKUP;
 
          p0 = vlib_get_buffer (vm, pi0);
-         ip60 = vlib_buffer_get_current (p0);
-         ip40 =
-           (ip4_header_t *) u8_ptr_add (ip60,
-                                        vnet_buffer (p0)->map_t.
-                                        v6.l4_offset - sizeof (*ip40));
-         vlib_buffer_advance (p0,
-                              vnet_buffer (p0)->map_t.v6.l4_offset -
-                              sizeof (*ip40));
-         checksum0 =
-           (u16 *) u8_ptr_add (ip60,
-                               vnet_buffer (p0)->map_t.checksum_offset);
-
-         //TODO: This can probably be optimized
-         csum0 = ip_csum_sub_even (*checksum0, ip60->src_address.as_u64[0]);
-         csum0 = ip_csum_sub_even (csum0, ip60->src_address.as_u64[1]);
-         csum0 = ip_csum_sub_even (csum0, ip60->dst_address.as_u64[0]);
-         csum0 = ip_csum_sub_even (csum0, ip60->dst_address.as_u64[1]);
-         csum0 = ip_csum_add_even (csum0, vnet_buffer (p0)->map_t.v6.daddr);
-         csum0 = ip_csum_add_even (csum0, vnet_buffer (p0)->map_t.v6.saddr);
-         *checksum0 = ip_csum_fold (csum0);
-
-         if (PREDICT_FALSE (vnet_buffer (p0)->map_t.v6.frag_offset))
+
+         if (ip6_to_ip4_tcp_udp (p0, ip6_to_ip4_set_cb, p0, 1))
            {
-             //Only the first fragment
-             ip6_frag_hdr_t *hdr = (ip6_frag_hdr_t *) u8_ptr_add (ip60,
-                                                                  vnet_buffer
-                                                                  (p0)->
-                                                                  map_t.
-                                                                  v6.frag_offset);
-             fragment_id0 = frag_id_6to4 (hdr->identification);
-             flags0 = clib_host_to_net_u16 (IP4_HEADER_FLAG_MORE_FRAGMENTS);
+             p0->error = error_node->errors[MAP_ERROR_UNKNOWN];
+             next0 = IP6_MAPT_TCP_UDP_NEXT_DROP;
            }
          else
            {
-             fragment_id0 = 0;
-             flags0 = 0;
-           }
-
-         ip40->dst_address.as_u32 = vnet_buffer (p0)->map_t.v6.daddr;
-         ip40->src_address.as_u32 = vnet_buffer (p0)->map_t.v6.saddr;
-         ip40->ip_version_and_header_length =
-           IP4_VERSION_AND_HEADER_LENGTH_NO_OPTIONS;
-         ip40->tos = ip6_translate_tos (ip60);
-         ip40->length = u16_net_add (ip60->payload_length,
-                                     sizeof (*ip40) + sizeof (*ip60) -
-                                     vnet_buffer (p0)->map_t.v6.l4_offset);
-         ip40->fragment_id = fragment_id0;
-         ip40->flags_and_fragment_offset = flags0;
-         ip40->ttl = ip60->hop_limit;
-         ip40->protocol = vnet_buffer (p0)->map_t.v6.l4_protocol;
-         ip40->checksum = ip4_header_checksum (ip40);
-
-         if (vnet_buffer (p0)->map_t.mtu < p0->current_length)
-           {
-             //Send to fragmentation node if necessary
-             vnet_buffer (p0)->ip_frag.mtu = vnet_buffer (p0)->map_t.mtu;
-             vnet_buffer (p0)->ip_frag.header_offset = 0;
-             vnet_buffer (p0)->ip_frag.next_index = IP4_FRAG_NEXT_IP4_LOOKUP;
-             next0 = IP6_MAPT_TCP_UDP_NEXT_IP4_FRAG;
+             if (vnet_buffer (p0)->map_t.mtu < p0->current_length)
+               {
+                 //Send to fragmentation node if necessary
+                 vnet_buffer (p0)->ip_frag.mtu = vnet_buffer (p0)->map_t.mtu;
+                 vnet_buffer (p0)->ip_frag.header_offset = 0;
+                 vnet_buffer (p0)->ip_frag.next_index =
+                   IP4_FRAG_NEXT_IP4_LOOKUP;
+                 next0 = IP6_MAPT_TCP_UDP_NEXT_IP4_FRAG;
+               }
            }
 
          vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
@@ -968,22 +541,30 @@ ip6_map_t_tcp_udp (vlib_main_t * vm,
 
 static_always_inline void
 ip6_map_t_classify (vlib_buffer_t * p0, ip6_header_t * ip60,
-                   map_domain_t * d0, i32 * src_port0,
+                   map_domain_t * d0, i32 * map_port0,
                    u8 * error0, ip6_mapt_next_t * next0,
                    u32 l4_len0, ip6_frag_hdr_t * frag0)
 {
+  map_main_t *mm = &map_main;
+  u32 port_offset;
+
+  if (mm->is_ce)
+    port_offset = 2;
+  else
+    port_offset = 0;
+
   if (PREDICT_FALSE (vnet_buffer (p0)->map_t.v6.frag_offset &&
                     ip6_frag_hdr_offset (frag0)))
     {
       *next0 = IP6_MAPT_NEXT_MAPT_FRAGMENTED;
       if (d0->ea_bits_len == 0 && d0->rules)
        {
-         *src_port0 = 0;
+         *map_port0 = 0;
        }
       else
        {
-         *src_port0 = ip6_map_fragment_get (ip60, frag0, d0);
-         *error0 = (*src_port0 != -1) ? *error0 : MAP_ERROR_FRAGMENT_DROPPED;
+         *map_port0 = ip6_map_fragment_get (ip60, frag0, d0);
+         *error0 = (*map_port0 != -1) ? *error0 : MAP_ERROR_FRAGMENT_DROPPED;
        }
     }
   else
@@ -995,9 +576,11 @@ ip6_map_t_classify (vlib_buffer_t * p0, ip6_header_t * ip60,
       vnet_buffer (p0)->map_t.checksum_offset =
        vnet_buffer (p0)->map_t.v6.l4_offset + 16;
       *next0 = IP6_MAPT_NEXT_MAPT_TCP_UDP;
-      *src_port0 =
+      *map_port0 =
        (i32) *
-       ((u16 *) u8_ptr_add (ip60, vnet_buffer (p0)->map_t.v6.l4_offset));
+       ((u16 *)
+        u8_ptr_add (ip60,
+                    vnet_buffer (p0)->map_t.v6.l4_offset + port_offset));
     }
   else
     if (PREDICT_TRUE
@@ -1008,9 +591,11 @@ ip6_map_t_classify (vlib_buffer_t * p0, ip6_header_t * ip60,
       vnet_buffer (p0)->map_t.checksum_offset =
        vnet_buffer (p0)->map_t.v6.l4_offset + 6;
       *next0 = IP6_MAPT_NEXT_MAPT_TCP_UDP;
-      *src_port0 =
+      *map_port0 =
        (i32) *
-       ((u16 *) u8_ptr_add (ip60, vnet_buffer (p0)->map_t.v6.l4_offset));
+       ((u16 *)
+        u8_ptr_add (ip60,
+                    vnet_buffer (p0)->map_t.v6.l4_offset + port_offset));
     }
   else if (vnet_buffer (p0)->map_t.v6.l4_protocol == IP_PROTOCOL_ICMP6)
     {
@@ -1019,7 +604,7 @@ ip6_map_t_classify (vlib_buffer_t * p0, ip6_header_t * ip60,
       *next0 = IP6_MAPT_NEXT_MAPT_ICMP;
       if (d0->ea_bits_len == 0 && d0->rules)
        {
-         *src_port0 = 0;
+         *map_port0 = 0;
        }
       else
        if (((icmp46_header_t *)
@@ -1031,7 +616,7 @@ ip6_map_t_classify (vlib_buffer_t * p0, ip6_header_t * ip60,
                            vnet_buffer (p0)->map_t.v6.l4_offset))->code ==
            ICMP6_echo_request)
        {
-         *src_port0 =
+         *map_port0 =
            (i32) *
            ((u16 *)
             u8_ptr_add (ip60, vnet_buffer (p0)->map_t.v6.l4_offset + 6));
@@ -1050,6 +635,7 @@ ip6_map_t (vlib_main_t * vm, vlib_node_runtime_t * node, vlib_frame_t * frame)
   u32 n_left_from, *from, next_index, *to_next, n_left_to_next;
   vlib_node_runtime_t *error_node =
     vlib_node_get_runtime (vm, ip6_map_t_node.index);
+  map_main_t *mm = &map_main;
   vlib_combined_counter_main_t *cm = map_main.domain_counters;
   u32 thread_index = vlib_get_thread_index ();
 
@@ -1069,10 +655,9 @@ ip6_map_t (vlib_main_t * vm, vlib_node_runtime_t * node, vlib_frame_t * frame)
          u8 error0, error1;
          ip6_mapt_next_t next0, next1;
          u32 l4_len0, l4_len1;
-         i32 src_port0, src_port1;
+         i32 map_port0, map_port1;
          map_domain_t *d0, *d1;
          ip6_frag_hdr_t *frag0, *frag1;
-         u32 saddr0, saddr1;
          next0 = next1 = 0;    //Because compiler whines
 
          pi0 = to_next[0] = from[0];
@@ -1090,24 +675,67 @@ ip6_map_t (vlib_main_t * vm, vlib_node_runtime_t * node, vlib_frame_t * frame)
          ip60 = vlib_buffer_get_current (p0);
          ip61 = vlib_buffer_get_current (p1);
 
-         saddr0 = map_get_ip4 (&ip60->src_address);
-         saddr1 = map_get_ip4 (&ip61->src_address);
-         d0 = ip6_map_get_domain (vnet_buffer (p0)->ip.adj_index[VLIB_TX],
-                                  (ip4_address_t *) & saddr0,
-                                  &vnet_buffer (p0)->map_t.map_domain_index,
-                                  &error0);
-         d1 =
-           ip6_map_get_domain (vnet_buffer (p1)->ip.adj_index[VLIB_TX],
-                               (ip4_address_t *) & saddr1,
-                               &vnet_buffer (p1)->map_t.map_domain_index,
-                               &error1);
-
-         vnet_buffer (p0)->map_t.v6.saddr = saddr0;
-         vnet_buffer (p1)->map_t.v6.saddr = saddr1;
-         vnet_buffer (p0)->map_t.v6.daddr =
-           ip6_map_t_embedded_address (d0, &ip60->dst_address);
-         vnet_buffer (p1)->map_t.v6.daddr =
-           ip6_map_t_embedded_address (d1, &ip61->dst_address);
+         if (mm->is_ce)
+           {
+             u32 daddr0, daddr1;
+             daddr0 = 0;       /* TODO */
+             daddr1 = 0;       /* TODO */
+             /* NOTE: ip6_map_get_domain currently doesn't utilize second argument */
+
+             daddr0 = map_get_ip4 (&ip60->dst_address, 0 /*TODO*/);
+             daddr1 = map_get_ip4 (&ip61->dst_address, 0 /*TODO*/);
+             d0 =
+               ip6_map_get_domain (vnet_buffer (p0)->ip.adj_index[VLIB_TX],
+                                   (ip4_address_t *) & daddr0,
+                                   &vnet_buffer (p0)->map_t.map_domain_index,
+                                   &error0);
+             d1 =
+               ip6_map_get_domain (vnet_buffer (p1)->ip.adj_index[VLIB_TX],
+                                   (ip4_address_t *) & daddr1,
+                                   &vnet_buffer (p1)->map_t.map_domain_index,
+                                   &error1);
+
+             daddr0 = map_get_ip4 (&ip60->dst_address, d0->flags);
+             daddr1 = map_get_ip4 (&ip61->dst_address, d1->flags);
+
+             vnet_buffer (p0)->map_t.v6.daddr = daddr0;
+             vnet_buffer (p1)->map_t.v6.daddr = daddr1;
+             vnet_buffer (p0)->map_t.v6.saddr =
+               ip6_map_t_embedded_address (d0, &ip60->src_address);
+             vnet_buffer (p1)->map_t.v6.saddr =
+               ip6_map_t_embedded_address (d1, &ip61->src_address);
+           }
+         else
+           {
+             u32 saddr0, saddr1;
+             saddr0 = 0;       /* TODO */
+             saddr1 = 0;       /* TODO */
+             /* NOTE: ip6_map_get_domain currently doesn't utilize second argument */
+
+             saddr0 = map_get_ip4 (&ip60->src_address, 0 /*TODO*/);
+             saddr1 = map_get_ip4 (&ip61->src_address, 0 /*TODO*/);
+             d0 =
+               ip6_map_get_domain (vnet_buffer (p0)->ip.adj_index[VLIB_TX],
+                                   (ip4_address_t *) & saddr0,
+                                   &vnet_buffer (p0)->map_t.map_domain_index,
+                                   &error0);
+             d1 =
+               ip6_map_get_domain (vnet_buffer (p1)->ip.adj_index[VLIB_TX],
+                                   (ip4_address_t *) & saddr1,
+                                   &vnet_buffer (p1)->map_t.map_domain_index,
+                                   &error1);
+
+             saddr0 = map_get_ip4 (&ip60->src_address, d0->flags);
+             saddr1 = map_get_ip4 (&ip61->src_address, d1->flags);
+
+             vnet_buffer (p0)->map_t.v6.saddr = saddr0;
+             vnet_buffer (p1)->map_t.v6.saddr = saddr1;
+             vnet_buffer (p0)->map_t.v6.daddr =
+               ip6_map_t_embedded_address (d0, &ip60->dst_address);
+             vnet_buffer (p1)->map_t.v6.daddr =
+               ip6_map_t_embedded_address (d1, &ip61->dst_address);
+           }
+
          vnet_buffer (p0)->map_t.mtu = d0->mtu ? d0->mtu : ~0;
          vnet_buffer (p1)->map_t.mtu = d1->mtu ? d1->mtu : ~0;
 
@@ -1135,7 +763,7 @@ ip6_map_t (vlib_main_t * vm, vlib_node_runtime_t * node, vlib_frame_t * frame)
              next1 = IP6_MAPT_NEXT_DROP;
            }
 
-         src_port0 = src_port1 = -1;
+         map_port0 = map_port1 = -1;
          l4_len0 = (u32) clib_net_to_host_u16 (ip60->payload_length) +
            sizeof (*ip60) - vnet_buffer (p0)->map_t.v6.l4_offset;
          l4_len1 = (u32) clib_net_to_host_u16 (ip61->payload_length) +
@@ -1149,33 +777,33 @@ ip6_map_t (vlib_main_t * vm, vlib_node_runtime_t * node, vlib_frame_t * frame)
                                           vnet_buffer (p1)->map_t.
                                           v6.frag_offset);
 
-         ip6_map_t_classify (p0, ip60, d0, &src_port0, &error0, &next0,
+         ip6_map_t_classify (p0, ip60, d0, &map_port0, &error0, &next0,
                              l4_len0, frag0);
-         ip6_map_t_classify (p1, ip61, d1, &src_port1, &error1, &next1,
+         ip6_map_t_classify (p1, ip61, d1, &map_port1, &error1, &next1,
                              l4_len1, frag1);
 
          if (PREDICT_FALSE
-             ((src_port0 != -1)
+             ((map_port0 != -1)
               && (ip60->src_address.as_u64[0] !=
                   map_get_pfx_net (d0, vnet_buffer (p0)->map_t.v6.saddr,
-                                   src_port0)
+                                   map_port0)
                   || ip60->src_address.as_u64[1] != map_get_sfx_net (d0,
                                                                      vnet_buffer
                                                                      (p0)->map_t.v6.saddr,
-                                                                     src_port0))))
+                                                                     map_port0))))
            {
              error0 = MAP_ERROR_SEC_CHECK;
            }
 
          if (PREDICT_FALSE
-             ((src_port1 != -1)
+             ((map_port1 != -1)
               && (ip61->src_address.as_u64[0] !=
                   map_get_pfx_net (d1, vnet_buffer (p1)->map_t.v6.saddr,
-                                   src_port1)
+                                   map_port1)
                   || ip61->src_address.as_u64[1] != map_get_sfx_net (d1,
                                                                      vnet_buffer
                                                                      (p1)->map_t.v6.saddr,
-                                                                     src_port1))))
+                                                                     map_port1))))
            {
              error1 = MAP_ERROR_SEC_CHECK;
            }
@@ -1186,7 +814,7 @@ ip6_map_t (vlib_main_t * vm, vlib_node_runtime_t * node, vlib_frame_t * frame)
                                                               vnet_buffer
                                                               (p0)->map_t.
                                                               v6.frag_offset)))
-             && (src_port0 != -1) && (d0->ea_bits_len != 0 || !d0->rules)
+             && (map_port0 != -1) && (d0->ea_bits_len != 0 || !d0->rules)
              && (error0 == MAP_ERROR_NONE))
            {
              ip6_map_fragment_cache (ip60,
@@ -1194,7 +822,7 @@ ip6_map_t (vlib_main_t * vm, vlib_node_runtime_t * node, vlib_frame_t * frame)
                                                                     vnet_buffer
                                                                     (p0)->map_t.
                                                                     v6.frag_offset),
-                                     d0, src_port0);
+                                     d0, map_port0);
            }
 
          if (PREDICT_FALSE (vnet_buffer (p1)->map_t.v6.frag_offset &&
@@ -1203,7 +831,7 @@ ip6_map_t (vlib_main_t * vm, vlib_node_runtime_t * node, vlib_frame_t * frame)
                                                               vnet_buffer
                                                               (p1)->map_t.
                                                               v6.frag_offset)))
-             && (src_port1 != -1) && (d1->ea_bits_len != 0 || !d1->rules)
+             && (map_port1 != -1) && (d1->ea_bits_len != 0 || !d1->rules)
              && (error1 == MAP_ERROR_NONE))
            {
              ip6_map_fragment_cache (ip61,
@@ -1211,7 +839,7 @@ ip6_map_t (vlib_main_t * vm, vlib_node_runtime_t * node, vlib_frame_t * frame)
                                                                     vnet_buffer
                                                                     (p1)->map_t.
                                                                     v6.frag_offset),
-                                     d1, src_port1);
+                                     d1, map_port1);
            }
 
          if (PREDICT_TRUE
@@ -1253,11 +881,11 @@ ip6_map_t (vlib_main_t * vm, vlib_node_runtime_t * node, vlib_frame_t * frame)
          ip6_header_t *ip60;
          u8 error0;
          u32 l4_len0;
-         i32 src_port0;
+         i32 map_port0;
          map_domain_t *d0;
          ip6_frag_hdr_t *frag0;
+         u32 port_offset;
          ip6_mapt_next_t next0 = 0;
-         u32 saddr;
 
          pi0 = to_next[0] = from[0];
          from += 1;
@@ -1268,17 +896,54 @@ ip6_map_t (vlib_main_t * vm, vlib_node_runtime_t * node, vlib_frame_t * frame)
 
          p0 = vlib_get_buffer (vm, pi0);
          ip60 = vlib_buffer_get_current (p0);
-         //Save saddr in a different variable to not overwrite ip.adj_index
-         saddr = map_get_ip4 (&ip60->src_address);
-         d0 = ip6_map_get_domain (vnet_buffer (p0)->ip.adj_index[VLIB_TX],
-                                  (ip4_address_t *) & saddr,
-                                  &vnet_buffer (p0)->map_t.map_domain_index,
-                                  &error0);
-
-         //FIXME: What if d0 is null
-         vnet_buffer (p0)->map_t.v6.saddr = saddr;
-         vnet_buffer (p0)->map_t.v6.daddr =
-           ip6_map_t_embedded_address (d0, &ip60->dst_address);
+
+         if (mm->is_ce)
+           {
+             u32 daddr;
+             //Save daddr in a different variable to not overwrite ip.adj_index
+             daddr = 0;        /* TODO */
+             /* NOTE: ip6_map_get_domain currently doesn't utilize second argument */
+
+             daddr = map_get_ip4 (&ip60->dst_address, 0 /*TODO*/);
+             d0 =
+               ip6_map_get_domain (vnet_buffer (p0)->ip.adj_index[VLIB_TX],
+                                   (ip4_address_t *) & daddr,
+                                   &vnet_buffer (p0)->map_t.map_domain_index,
+                                   &error0);
+
+             daddr = map_get_ip4 (&ip60->dst_address, d0->flags);
+
+             //FIXME: What if d0 is null
+             vnet_buffer (p0)->map_t.v6.daddr = daddr;
+             vnet_buffer (p0)->map_t.v6.saddr =
+               ip6_map_t_embedded_address (d0, &ip60->src_address);
+
+             port_offset = 2;
+           }
+         else
+           {
+             u32 saddr;
+             //Save saddr in a different variable to not overwrite ip.adj_index
+             saddr = 0;        /* TODO */
+             /* NOTE: ip6_map_get_domain currently doesn't utilize second argument */
+
+             saddr = map_get_ip4 (&ip60->src_address, 0 /*TODO*/);
+             d0 =
+               ip6_map_get_domain (vnet_buffer (p0)->ip.adj_index[VLIB_TX],
+                                   (ip4_address_t *) & saddr,
+                                   &vnet_buffer (p0)->map_t.map_domain_index,
+                                   &error0);
+
+             saddr = map_get_ip4 (&ip60->src_address, d0->flags);
+
+             //FIXME: What if d0 is null
+             vnet_buffer (p0)->map_t.v6.saddr = saddr;
+             vnet_buffer (p0)->map_t.v6.daddr =
+               ip6_map_t_embedded_address (d0, &ip60->dst_address);
+
+             port_offset = 0;
+           }
+
          vnet_buffer (p0)->map_t.mtu = d0->mtu ? d0->mtu : ~0;
 
          if (PREDICT_FALSE (ip6_parse (ip60, p0->current_length,
@@ -1293,7 +958,7 @@ ip6_map_t (vlib_main_t * vm, vlib_node_runtime_t * node, vlib_frame_t * frame)
              next0 = IP6_MAPT_NEXT_DROP;
            }
 
-         src_port0 = -1;
+         map_port0 = -1;
          l4_len0 = (u32) clib_net_to_host_u16 (ip60->payload_length) +
            sizeof (*ip60) - vnet_buffer (p0)->map_t.v6.l4_offset;
          frag0 =
@@ -1305,8 +970,8 @@ ip6_map_t (vlib_main_t * vm, vlib_node_runtime_t * node, vlib_frame_t * frame)
          if (PREDICT_FALSE (vnet_buffer (p0)->map_t.v6.frag_offset &&
                             ip6_frag_hdr_offset (frag0)))
            {
-             src_port0 = ip6_map_fragment_get (ip60, frag0, d0);
-             error0 = (src_port0 != -1) ? error0 : MAP_ERROR_FRAGMENT_MEMORY;
+             map_port0 = ip6_map_fragment_get (ip60, frag0, d0);
+             error0 = (map_port0 != -1) ? error0 : MAP_ERROR_FRAGMENT_MEMORY;
              next0 = IP6_MAPT_NEXT_MAPT_FRAGMENTED;
            }
          else
@@ -1319,10 +984,12 @@ ip6_map_t (vlib_main_t * vm, vlib_node_runtime_t * node, vlib_frame_t * frame)
              vnet_buffer (p0)->map_t.checksum_offset =
                vnet_buffer (p0)->map_t.v6.l4_offset + 16;
              next0 = IP6_MAPT_NEXT_MAPT_TCP_UDP;
-             src_port0 =
+             map_port0 =
                (i32) *
                ((u16 *)
-                u8_ptr_add (ip60, vnet_buffer (p0)->map_t.v6.l4_offset));
+                u8_ptr_add (ip60,
+                            vnet_buffer (p0)->map_t.v6.l4_offset +
+                            port_offset));
            }
          else
            if (PREDICT_TRUE
@@ -1334,10 +1001,12 @@ ip6_map_t (vlib_main_t * vm, vlib_node_runtime_t * node, vlib_frame_t * frame)
              vnet_buffer (p0)->map_t.checksum_offset =
                vnet_buffer (p0)->map_t.v6.l4_offset + 6;
              next0 = IP6_MAPT_NEXT_MAPT_TCP_UDP;
-             src_port0 =
+             map_port0 =
                (i32) *
                ((u16 *)
-                u8_ptr_add (ip60, vnet_buffer (p0)->map_t.v6.l4_offset));
+                u8_ptr_add (ip60,
+                            vnet_buffer (p0)->map_t.v6.l4_offset +
+                            port_offset));
            }
          else if (vnet_buffer (p0)->map_t.v6.l4_protocol ==
                   IP_PROTOCOL_ICMP6)
@@ -1354,7 +1023,7 @@ ip6_map_t (vlib_main_t * vm, vlib_node_runtime_t * node, vlib_frame_t * frame)
                      u8_ptr_add (ip60,
                                  vnet_buffer (p0)->map_t.v6.
                                  l4_offset))->code == ICMP6_echo_request)
-               src_port0 =
+               map_port0 =
                  (i32) *
                  ((u16 *)
                   u8_ptr_add (ip60,
@@ -1368,14 +1037,14 @@ ip6_map_t (vlib_main_t * vm, vlib_node_runtime_t * node, vlib_frame_t * frame)
 
          //Security check
          if (PREDICT_FALSE
-             ((src_port0 != -1)
+             ((!mm->is_ce) && (map_port0 != -1)
               && (ip60->src_address.as_u64[0] !=
                   map_get_pfx_net (d0, vnet_buffer (p0)->map_t.v6.saddr,
-                                   src_port0)
+                                   map_port0)
                   || ip60->src_address.as_u64[1] != map_get_sfx_net (d0,
                                                                      vnet_buffer
                                                                      (p0)->map_t.v6.saddr,
-                                                                     src_port0))))
+                                                                     map_port0))))
            {
              //Security check when src_port0 is not zero (non-first fragment, UDP or TCP)
              error0 = MAP_ERROR_SEC_CHECK;
@@ -1388,7 +1057,7 @@ ip6_map_t (vlib_main_t * vm, vlib_node_runtime_t * node, vlib_frame_t * frame)
                                                               vnet_buffer
                                                               (p0)->map_t.
                                                               v6.frag_offset)))
-             && (src_port0 != -1) && (d0->ea_bits_len != 0 || !d0->rules)
+             && (map_port0 != -1) && (d0->ea_bits_len != 0 || !d0->rules)
              && (error0 == MAP_ERROR_NONE))
            {
              ip6_map_fragment_cache (ip60,
@@ -1396,7 +1065,7 @@ ip6_map_t (vlib_main_t * vm, vlib_node_runtime_t * node, vlib_frame_t * frame)
                                                                     vnet_buffer
                                                                     (p0)->map_t.
                                                                     v6.frag_offset),
-                                     d0, src_port0);
+                                     d0, map_port0);
            }
 
          if (PREDICT_TRUE