Add support for MAP-T CE (VPP-1058)

Change-Id: Ib99058323c50f20fcdb44185aee027c5c6ef8c91
Signed-off-by: Juraj Sloboda <jsloboda@cisco.com>
diff --git a/src/vnet/map/ip4_map_t.c b/src/vnet/map/ip4_map_t.c
index b89840c..0a1dc8f 100644
--- a/src/vnet/map/ip4_map_t.c
+++ b/src/vnet/map/ip4_map_t.c
@@ -102,19 +102,33 @@
 typedef struct
 {
   map_domain_t *d;
-  u16 recv_port;
+  u16 id;
 } icmp_to_icmp6_ctx_t;
 
 static int
 ip4_to_ip6_set_icmp_cb (ip4_header_t * ip4, ip6_header_t * ip6, void *arg)
 {
   icmp_to_icmp6_ctx_t *ctx = arg;
+  map_main_t *mm = &map_main;
 
-  ip4_map_t_embedded_address (ctx->d, &ip6->src_address, &ip4->src_address);
-  ip6->dst_address.as_u64[0] =
-    map_get_pfx_net (ctx->d, ip4->dst_address.as_u32, ctx->recv_port);
-  ip6->dst_address.as_u64[1] =
-    map_get_sfx_net (ctx->d, ip4->dst_address.as_u32, ctx->recv_port);
+  if (mm->is_ce)
+    {
+      ip6->src_address.as_u64[0] =
+	map_get_pfx_net (ctx->d, ip4->src_address.as_u32, ctx->id);
+      ip6->src_address.as_u64[1] =
+	map_get_sfx_net (ctx->d, ip4->src_address.as_u32, ctx->id);
+      ip4_map_t_embedded_address (ctx->d, &ip6->dst_address,
+				  &ip4->dst_address);
+    }
+  else
+    {
+      ip4_map_t_embedded_address (ctx->d, &ip6->src_address,
+				  &ip4->src_address);
+      ip6->dst_address.as_u64[0] =
+	map_get_pfx_net (ctx->d, ip4->dst_address.as_u32, ctx->id);
+      ip6->dst_address.as_u64[1] =
+	map_get_sfx_net (ctx->d, ip4->dst_address.as_u32, ctx->id);
+    }
 
   return 0;
 }
@@ -124,14 +138,30 @@
 			      void *arg)
 {
   icmp_to_icmp6_ctx_t *ctx = arg;
+  map_main_t *mm = &map_main;
 
-  //Note that the source address is within the domain
-  //while the destination address is the one outside the domain
-  ip4_map_t_embedded_address (ctx->d, &ip6->dst_address, &ip4->dst_address);
-  ip6->src_address.as_u64[0] =
-    map_get_pfx_net (ctx->d, ip4->src_address.as_u32, ctx->recv_port);
-  ip6->src_address.as_u64[1] =
-    map_get_sfx_net (ctx->d, ip4->src_address.as_u32, ctx->recv_port);
+  if (mm->is_ce)
+    {
+      //Note that the destination address is within the domain
+      //while the source address is the one outside the domain
+      ip4_map_t_embedded_address (ctx->d, &ip6->src_address,
+				  &ip4->src_address);
+      ip6->dst_address.as_u64[0] =
+	map_get_pfx_net (ctx->d, ip4->dst_address.as_u32, ctx->id);
+      ip6->dst_address.as_u64[1] =
+	map_get_sfx_net (ctx->d, ip4->dst_address.as_u32, ctx->id);
+    }
+  else
+    {
+      //Note that the source address is within the domain
+      //while the destination address is the one outside the domain
+      ip4_map_t_embedded_address (ctx->d, &ip6->dst_address,
+				  &ip4->dst_address);
+      ip6->src_address.as_u64[0] =
+	map_get_pfx_net (ctx->d, ip4->src_address.as_u32, ctx->id);
+      ip6->src_address.as_u64[1] =
+	map_get_sfx_net (ctx->d, ip4->src_address.as_u32, ctx->id);
+    }
 
   return 0;
 }
@@ -163,6 +193,7 @@
 	  u16 len0;
 	  icmp_to_icmp6_ctx_t ctx0;
 	  ip4_header_t *ip40;
+	  icmp46_header_t *icmp0;
 
 	  next0 = IP4_MAPT_ICMP_NEXT_IP6_LOOKUP;
 	  pi0 = to_next[0] = from[0];
@@ -182,9 +213,11 @@
 			       vnet_buffer (p0)->map_t.map_domain_index);
 
 	  ip40 = vlib_buffer_get_current (p0);
-	  ctx0.recv_port = ip4_get_port (ip40, 1);
+	  icmp0 = (icmp46_header_t *) (ip40 + 1);
+
+	  ctx0.id = ip4_get_port (ip40, icmp0->type == ICMP6_echo_request);
 	  ctx0.d = d0;
-	  if (ctx0.recv_port == 0)
+	  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))
@@ -442,6 +475,14 @@
 		    ip4_header_t * ip40, u16 ip4_len0, i32 * dst_port0,
 		    u8 * error0, ip4_mapt_next_t * next0)
 {
+  map_main_t *mm = &map_main;
+  u32 port_offset;
+
+  if (mm->is_ce)
+    port_offset = 0;
+  else
+    port_offset = 2;
+
   if (PREDICT_FALSE (ip4_get_fragment_offset (ip40)))
     {
       *next0 = IP4_MAPT_NEXT_MAPT_FRAGMENTED;
@@ -460,14 +501,16 @@
       vnet_buffer (p0)->map_t.checksum_offset = 36;
       *next0 = IP4_MAPT_NEXT_MAPT_TCP_UDP;
       *error0 = ip4_len0 < 40 ? MAP_ERROR_MALFORMED : *error0;
-      *dst_port0 = (i32) * ((u16 *) u8_ptr_add (ip40, sizeof (*ip40) + 2));
+      *dst_port0 =
+	(i32) * ((u16 *) u8_ptr_add (ip40, sizeof (*ip40) + port_offset));
     }
   else if (PREDICT_TRUE (ip40->protocol == IP_PROTOCOL_UDP))
     {
       vnet_buffer (p0)->map_t.checksum_offset = 26;
       *next0 = IP4_MAPT_NEXT_MAPT_TCP_UDP;
       *error0 = ip4_len0 < 28 ? MAP_ERROR_MALFORMED : *error0;
-      *dst_port0 = (i32) * ((u16 *) u8_ptr_add (ip40, sizeof (*ip40) + 2));
+      *dst_port0 =
+	(i32) * ((u16 *) u8_ptr_add (ip40, sizeof (*ip40) + port_offset));
     }
   else if (ip40->protocol == IP_PROTOCOL_ICMP)
     {
@@ -496,6 +539,7 @@
   from = vlib_frame_vector_args (frame);
   n_left_from = frame->n_vectors;
   next_index = node->cached_next_index;
+  map_main_t *mm = &map_main;
   vlib_combined_counter_main_t *cm = map_main.domain_counters;
   u32 thread_index = vlib_get_thread_index ();
 
@@ -513,7 +557,7 @@
 	  ip4_mapt_next_t next0 = 0, next1 = 0;
 	  u16 ip4_len0, ip4_len1;
 	  u8 error0, error1;
-	  i32 dst_port0, dst_port1;
+	  i32 map_port0, map_port1;
 	  ip4_mapt_pseudo_header_t *pheader0, *pheader1;
 
 	  pi0 = to_next[0] = from[0];
@@ -556,12 +600,12 @@
 	  vnet_buffer (p0)->map_t.mtu = d0->mtu ? d0->mtu : ~0;
 	  vnet_buffer (p1)->map_t.mtu = d1->mtu ? d1->mtu : ~0;
 
-	  dst_port0 = -1;
-	  dst_port1 = -1;
+	  map_port0 = -1;
+	  map_port1 = -1;
 
-	  ip4_map_t_classify (p0, d0, ip40, ip4_len0, &dst_port0, &error0,
+	  ip4_map_t_classify (p0, d0, ip40, ip4_len0, &map_port0, &error0,
 			      &next0);
-	  ip4_map_t_classify (p1, d1, ip41, ip4_len1, &dst_port1, &error1,
+	  ip4_map_t_classify (p1, d1, ip41, ip4_len1, &map_port1, &error1,
 			      &next1);
 
 	  //Add MAP-T pseudo header in front of the packet
@@ -571,31 +615,57 @@
 	  pheader1 = vlib_buffer_get_current (p1);
 
 	  //Save addresses within the packet
-	  ip4_map_t_embedded_address (d0, &pheader0->saddr,
-				      &ip40->src_address);
-	  ip4_map_t_embedded_address (d1, &pheader1->saddr,
-				      &ip41->src_address);
-	  pheader0->daddr.as_u64[0] =
-	    map_get_pfx_net (d0, ip40->dst_address.as_u32, (u16) dst_port0);
-	  pheader0->daddr.as_u64[1] =
-	    map_get_sfx_net (d0, ip40->dst_address.as_u32, (u16) dst_port0);
-	  pheader1->daddr.as_u64[0] =
-	    map_get_pfx_net (d1, ip41->dst_address.as_u32, (u16) dst_port1);
-	  pheader1->daddr.as_u64[1] =
-	    map_get_sfx_net (d1, ip41->dst_address.as_u32, (u16) dst_port1);
+	  if (mm->is_ce)
+	    {
+	      ip4_map_t_embedded_address (d0, &pheader0->daddr,
+					  &ip40->dst_address);
+	      ip4_map_t_embedded_address (d1, &pheader1->daddr,
+					  &ip41->dst_address);
+	      pheader0->saddr.as_u64[0] =
+		map_get_pfx_net (d0, ip40->src_address.as_u32,
+				 (u16) map_port0);
+	      pheader0->saddr.as_u64[1] =
+		map_get_sfx_net (d0, ip40->src_address.as_u32,
+				 (u16) map_port0);
+	      pheader1->saddr.as_u64[0] =
+		map_get_pfx_net (d1, ip41->src_address.as_u32,
+				 (u16) map_port1);
+	      pheader1->saddr.as_u64[1] =
+		map_get_sfx_net (d1, ip41->src_address.as_u32,
+				 (u16) map_port1);
+	    }
+	  else
+	    {
+	      ip4_map_t_embedded_address (d0, &pheader0->saddr,
+					  &ip40->src_address);
+	      ip4_map_t_embedded_address (d1, &pheader1->saddr,
+					  &ip41->src_address);
+	      pheader0->daddr.as_u64[0] =
+		map_get_pfx_net (d0, ip40->dst_address.as_u32,
+				 (u16) map_port0);
+	      pheader0->daddr.as_u64[1] =
+		map_get_sfx_net (d0, ip40->dst_address.as_u32,
+				 (u16) map_port0);
+	      pheader1->daddr.as_u64[0] =
+		map_get_pfx_net (d1, ip41->dst_address.as_u32,
+				 (u16) map_port1);
+	      pheader1->daddr.as_u64[1] =
+		map_get_sfx_net (d1, ip41->dst_address.as_u32,
+				 (u16) map_port1);
+	    }
 
 	  if (PREDICT_FALSE
-	      (ip4_is_first_fragment (ip40) && (dst_port0 != -1)
+	      (ip4_is_first_fragment (ip40) && (map_port0 != -1)
 	       && (d0->ea_bits_len != 0 || !d0->rules)
-	       && ip4_map_fragment_cache (ip40, dst_port0)))
+	       && ip4_map_fragment_cache (ip40, map_port0)))
 	    {
 	      error0 = MAP_ERROR_FRAGMENT_MEMORY;
 	    }
 
 	  if (PREDICT_FALSE
-	      (ip4_is_first_fragment (ip41) && (dst_port1 != -1)
+	      (ip4_is_first_fragment (ip41) && (map_port1 != -1)
 	       && (d1->ea_bits_len != 0 || !d1->rules)
-	       && ip4_map_fragment_cache (ip41, dst_port1)))
+	       && ip4_map_fragment_cache (ip41, map_port1)))
 	    {
 	      error1 = MAP_ERROR_FRAGMENT_MEMORY;
 	    }
@@ -641,7 +711,7 @@
 	  ip4_mapt_next_t next0;
 	  u16 ip4_len0;
 	  u8 error0;
-	  i32 dst_port0;
+	  i32 map_port0;
 	  ip4_mapt_pseudo_header_t *pheader0;
 
 	  pi0 = to_next[0] = from[0];
@@ -667,8 +737,8 @@
 
 	  vnet_buffer (p0)->map_t.mtu = d0->mtu ? d0->mtu : ~0;
 
-	  dst_port0 = -1;
-	  ip4_map_t_classify (p0, d0, ip40, ip4_len0, &dst_port0, &error0,
+	  map_port0 = -1;
+	  ip4_map_t_classify (p0, d0, ip40, ip4_len0, &map_port0, &error0,
 			      &next0);
 
 	  //Add MAP-T pseudo header in front of the packet
@@ -676,20 +746,36 @@
 	  pheader0 = vlib_buffer_get_current (p0);
 
 	  //Save addresses within the packet
-	  ip4_map_t_embedded_address (d0, &pheader0->saddr,
-				      &ip40->src_address);
-	  pheader0->daddr.as_u64[0] =
-	    map_get_pfx_net (d0, ip40->dst_address.as_u32, (u16) dst_port0);
-	  pheader0->daddr.as_u64[1] =
-	    map_get_sfx_net (d0, ip40->dst_address.as_u32, (u16) dst_port0);
+	  if (mm->is_ce)
+	    {
+	      ip4_map_t_embedded_address (d0, &pheader0->daddr,
+					  &ip40->dst_address);
+	      pheader0->saddr.as_u64[0] =
+		map_get_pfx_net (d0, ip40->src_address.as_u32,
+				 (u16) map_port0);
+	      pheader0->saddr.as_u64[1] =
+		map_get_sfx_net (d0, ip40->src_address.as_u32,
+				 (u16) map_port0);
+	    }
+	  else
+	    {
+	      ip4_map_t_embedded_address (d0, &pheader0->saddr,
+					  &ip40->src_address);
+	      pheader0->daddr.as_u64[0] =
+		map_get_pfx_net (d0, ip40->dst_address.as_u32,
+				 (u16) map_port0);
+	      pheader0->daddr.as_u64[1] =
+		map_get_sfx_net (d0, ip40->dst_address.as_u32,
+				 (u16) map_port0);
+	    }
 
 	  //It is important to cache at this stage because the result might be necessary
 	  //for packets within the same vector.
 	  //Actually, this approach even provides some limited out-of-order fragments support
 	  if (PREDICT_FALSE
-	      (ip4_is_first_fragment (ip40) && (dst_port0 != -1)
+	      (ip4_is_first_fragment (ip40) && (map_port0 != -1)
 	       && (d0->ea_bits_len != 0 || !d0->rules)
-	       && ip4_map_fragment_cache (ip40, dst_port0)))
+	       && ip4_map_fragment_cache (ip40, map_port0)))
 	    {
 	      error0 = MAP_ERROR_UNKNOWN;
 	    }
diff --git a/src/vnet/map/ip6_map_t.c b/src/vnet/map/ip6_map_t.c
index a6314c6..05bac1d 100644
--- a/src/vnet/map/ip6_map_t.c
+++ b/src/vnet/map/ip6_map_t.c
@@ -101,27 +101,49 @@
 typedef struct
 {
   map_domain_t *d;
-  u16 sender_port;
+  u16 id;
 } icmp6_to_icmp_ctx_t;
 
 static int
 ip6_to_ip4_set_icmp_cb (ip6_header_t * ip6, ip4_header_t * ip4, void *arg)
 {
   icmp6_to_icmp_ctx_t *ctx = arg;
-  u32 ip4_sadr;
+  map_main_t *mm = &map_main;
 
-  //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->sender_port)
-      || ip6->src_address.as_u64[1] != map_get_sfx_net (ctx->d, ip4_sadr,
-							ctx->sender_port))
-    return -1;
+  if (mm->is_ce)
+    {
+      u32 ip4_dadr;
 
-  ip4->dst_address.as_u32 =
-    ip6_map_t_embedded_address (ctx->d, &ip6->dst_address);
-  ip4->src_address.as_u32 = ip4_sadr;
+      //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;
+    }
+  else
+    {
+      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;
+    }
 
   return 0;
 }
@@ -131,20 +153,42 @@
 			      void *arg)
 {
   icmp6_to_icmp_ctx_t *ctx = arg;
-  u32 inner_ip4_dadr;
+  map_main_t *mm = &map_main;
 
-  //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->sender_port)
-      || ip6->dst_address.as_u64[1] != map_get_sfx_net (ctx->d,
-							inner_ip4_dadr,
-							ctx->sender_port))
-    return -1;
+  if (mm->is_ce)
+    {
+      u32 inner_ip4_sadr;
 
-  ip4->dst_address.as_u32 = inner_ip4_dadr;
-  ip4->src_address.as_u32 =
-    ip6_map_t_embedded_address (ctx->d, &ip6->src_address);
+      //Security check of inner packet
+      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
+    {
+      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);
+    }
 
   return 0;
 }
@@ -176,6 +220,7 @@
 	  u16 len0;
 	  icmp6_to_icmp_ctx_t ctx0;
 	  ip6_header_t *ip60;
+	  icmp46_header_t *icmp0;
 
 	  pi0 = to_next[0] = from[0];
 	  from += 1;
@@ -188,12 +233,16 @@
 	  p0 = vlib_get_buffer (vm, pi0);
 	  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);
-	  ctx0.sender_port = ip6_get_port (ip60, 0, p0->current_length);
+
+	  ctx0.id =
+	    ip6_get_port (ip60, icmp0->type == ICMP6_echo_request,
+			  p0->current_length);
 	  ctx0.d = d0;
-	  if (ctx0.sender_port == 0)
+	  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))
@@ -492,22 +541,30 @@
 
 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
@@ -519,9 +576,11 @@
       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
@@ -532,9 +591,11 @@
       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)
     {
@@ -543,7 +604,7 @@
       *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 *)
@@ -555,7 +616,7 @@
 			    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));
@@ -574,6 +635,7 @@
   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 ();
 
@@ -593,10 +655,9 @@
 	  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];
@@ -614,29 +675,67 @@
 	  ip60 = vlib_buffer_get_current (p0);
 	  ip61 = vlib_buffer_get_current (p1);
 
-	  saddr0 = 0;		/* TODO */
-	  saddr1 = 0;		/* TODO */
-	  /* NOTE: ip6_map_get_domain currently doesn't utilize second argument */
+	  if (mm->is_ce)
+	    {
+	      u32 daddr0, daddr1;
+	      daddr0 = 0;	/* TODO */
+	      daddr1 = 0;	/* TODO */
+	      /* NOTE: ip6_map_get_domain currently doesn't utilize second argument */
 
-	  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);
+	      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);
 
-	  saddr0 = map_get_ip4 (&ip60->src_address, d0->flags);
-	  saddr1 = map_get_ip4 (&ip61->src_address, d1->flags);
+	      daddr0 = map_get_ip4 (&ip60->dst_address, d0->flags);
+	      daddr1 = map_get_ip4 (&ip61->dst_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.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;
 
@@ -664,7 +763,7 @@
 	      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) +
@@ -678,33 +777,33 @@
 					   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;
 	    }
@@ -715,7 +814,7 @@
 							       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,
@@ -723,7 +822,7 @@
 								     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 &&
@@ -732,7 +831,7 @@
 							       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,
@@ -740,7 +839,7 @@
 								     vnet_buffer
 								     (p1)->map_t.
 								     v6.frag_offset),
-				      d1, src_port1);
+				      d1, map_port1);
 	    }
 
 	  if (PREDICT_TRUE
@@ -782,11 +881,11 @@
 	  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;
@@ -798,21 +897,53 @@
 	  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 = 0;		/* TODO */
-	  /* NOTE: ip6_map_get_domain currently doesn't utilize second argument */
+	  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 */
 
-	  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);
+	      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);
 
-	  saddr = map_get_ip4 (&ip60->src_address, d0->flags);
+	      daddr = map_get_ip4 (&ip60->dst_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);
+	      //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,
@@ -827,7 +958,7 @@
 	      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 =
@@ -839,8 +970,8 @@
 	  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
@@ -853,10 +984,12 @@
 	      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
@@ -868,10 +1001,12 @@
 	      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)
@@ -888,7 +1023,7 @@
 		      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,
@@ -902,14 +1037,14 @@
 
 	  //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;
@@ -922,7 +1057,7 @@
 							       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,
@@ -930,7 +1065,7 @@
 								     vnet_buffer
 								     (p0)->map_t.
 								     v6.frag_offset),
-				      d0, src_port0);
+				      d0, map_port0);
 	    }
 
 	  if (PREDICT_TRUE
diff --git a/src/vnet/map/map.c b/src/vnet/map/map.c
index b2eefee..702b5f8 100644
--- a/src/vnet/map/map.c
+++ b/src/vnet/map/map.c
@@ -106,7 +106,8 @@
   /* How many, and which bits to grab from the IPv4 DA */
   if (ip4_prefix_len + ea_bits_len < 32)
     {
-      flags |= MAP_DOMAIN_PREFIX;
+      if (!(flags & MAP_DOMAIN_TRANSLATION))
+	flags |= MAP_DOMAIN_PREFIX;
       suffix_shift = 32 - ip4_prefix_len - ea_bits_len;
       suffix_len = ea_bits_len;
     }
@@ -126,6 +127,12 @@
       return -1;
     }
 
+  if (mm->is_ce && !(flags & MAP_DOMAIN_TRANSLATION))
+    {
+      clib_warning ("MAP-E CE is not supported yet");
+      return -1;
+    }
+
   /* Get domain index */
   pool_get_aligned (mm->domains, d, CLIB_CACHE_LINE_BYTES);
   memset (d, 0, sizeof (*d));
@@ -157,11 +164,23 @@
     map_dpo_create (DPO_PROTO_IP4, *map_domain_index, &dpo_v4);
 
   /* Create ip4 route */
+  u8 ip4_pfx_len;
+  ip4_address_t ip4_pfx;
+  if (mm->is_ce)
+    {
+      ip4_pfx_len = 0;
+      ip4_pfx.as_u32 = 0;
+    }
+  else
+    {
+      ip4_pfx_len = d->ip4_prefix_len;
+      ip4_pfx = d->ip4_prefix;
+    }
   fib_prefix_t pfx = {
     .fp_proto = FIB_PROTOCOL_IP4,
-    .fp_len = d->ip4_prefix_len,
+    .fp_len = ip4_pfx_len,
     .fp_addr = {
-		.ip4 = d->ip4_prefix,
+		.ip4 = ip4_pfx,
 		}
     ,
   };
@@ -187,10 +206,22 @@
    * already exists and is MAP sourced, it is now MAP source n+1 times
    * and will need to be removed n+1 times.
    */
+  u8 ip6_pfx_len;
+  ip6_address_t ip6_pfx;
+  if (mm->is_ce)
+    {
+      ip6_pfx_len = d->ip6_prefix_len;
+      ip6_pfx = d->ip6_prefix;
+    }
+  else
+    {
+      ip6_pfx_len = d->ip6_src_len;
+      ip6_pfx = d->ip6_src;
+    }
   fib_prefix_t pfx6 = {
     .fp_proto = FIB_PROTOCOL_IP6,
-    .fp_len = d->ip6_src_len,
-    .fp_addr.ip6 = d->ip6_src,
+    .fp_len = ip6_pfx_len,
+    .fp_addr.ip6 = ip6_pfx,
   };
 
   fib_table_entry_special_dpo_add (0, &pfx6,
@@ -2146,7 +2177,7 @@
   .path = "map add domain",
   .short_help = "map add domain ip4-pfx <ip4-pfx> ip6-pfx <ip6-pfx> "
       "ip6-src <ip6-pfx> ea-bits-len <n> psid-offset <n> psid-len <n> "
-      "[map-t] [mtu <mtu>]",
+      "[map-t] [map-ce] [mtu <mtu>]",
   .function = map_add_domain_command_fn,
 };
 
@@ -2216,6 +2247,28 @@
 };
 /* *INDENT-ON* */
 
+static clib_error_t *
+map_config (vlib_main_t * vm, unformat_input_t * input)
+{
+  map_main_t *mm = &map_main;
+  u8 is_ce = false;
+
+  while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
+    {
+      if (unformat (input, "customer edge"))
+	is_ce = true;
+      else
+	return clib_error_return (0, "unknown input '%U'",
+				  format_unformat_error, input);
+    }
+
+  mm->is_ce = is_ce;
+
+  return 0;
+}
+
+VLIB_CONFIG_FUNCTION (map_config, "map");
+
 /*
  * map_init
  */
@@ -2246,6 +2299,8 @@
   /* ICMP6 Type 1, Code 5 for security check failure */
   mm->icmp6_enabled = false;
 
+  mm->is_ce = false;
+
   /* Inner or outer fragmentation */
   mm->frag_inner = false;
   mm->frag_ignore_df = false;
diff --git a/src/vnet/map/map.h b/src/vnet/map/map.h
index 8bae222..7243e81 100644
--- a/src/vnet/map/map.h
+++ b/src/vnet/map/map.h
@@ -242,6 +242,8 @@
   bool sec_check_frag;		/* Inbound security check for (subsequent) fragments */
   bool icmp6_enabled;		/* Send destination unreachable for security check failure */
 
+  bool is_ce;                   /* If this MAP node is a Customer Edge router*/
+
   /* ICMPv6 -> ICMPv4 relay parameters */
   ip4_address_t icmp4_src_address;
   vlib_simple_counter_main_t icmp_relayed;
diff --git a/src/vnet/map/test.c b/src/vnet/map/test.c
index f3c893a..ff10fc9 100644
--- a/src/vnet/map/test.c
+++ b/src/vnet/map/test.c
@@ -48,7 +48,8 @@
   /* How many, and which bits to grab from the IPv4 DA */
   if (ip4_prefix_len + ea_bits_len < 32)
     {
-      d->flags |= MAP_DOMAIN_PREFIX;
+      if (!(flags & MAP_DOMAIN_TRANSLATION))
+        d->flags |= MAP_DOMAIN_PREFIX;
       d->suffix_shift = 32 - ip4_prefix_len - ea_bits_len;
       suffix_len = ea_bits_len;
     }