bgpd: encap: add encap SAFI (RFC5512)

Adds RFC5512 and Encapsulation Attribute.

Signed-off-by: Lou Berger <lberger@labn.net>
diff --git a/bgpd/Makefile.am b/bgpd/Makefile.am
index 82c69ea..7da4dd8 100644
--- a/bgpd/Makefile.am
+++ b/bgpd/Makefile.am
@@ -16,7 +16,7 @@
 	bgp_packet.c bgp_network.c bgp_filter.c bgp_regex.c bgp_clist.c \
 	bgp_dump.c bgp_snmp.c bgp_ecommunity.c bgp_mplsvpn.c bgp_nexthop.c \
 	bgp_damp.c bgp_table.c bgp_advertise.c bgp_vty.c bgp_mpath.c \
-	bgp_encap_tlv.c
+	bgp_encap.c bgp_encap_tlv.c
 
 noinst_HEADERS = \
 	bgp_aspath.h bgp_attr.h bgp_community.h bgp_debug.h bgp_fsm.h \
@@ -24,7 +24,7 @@
 	bgpd.h bgp_filter.h bgp_clist.h bgp_dump.h bgp_zebra.h \
 	bgp_ecommunity.h bgp_mplsvpn.h bgp_nexthop.h bgp_damp.h bgp_table.h \
 	bgp_advertise.h bgp_snmp.h bgp_vty.h bgp_mpath.h \
-	bgp_encap_tlv.h bgp_encap_types.h
+	bgp_encap.h bgp_encap_tlv.h bgp_encap_types.h
 
 bgpd_SOURCES = bgp_main.c
 bgpd_LDADD = libbgp.a ../lib/libzebra.la @LIBCAP@ @LIBM@
diff --git a/bgpd/bgp_attr.c b/bgpd/bgp_attr.c
index b019347..5dc118b 100644
--- a/bgpd/bgp_attr.c
+++ b/bgpd/bgp_attr.c
@@ -1891,9 +1891,11 @@
     uint16_t	sublength;
     struct bgp_attr_encap_subtlv *tlv;
 
-    subtype = stream_getw (BGP_INPUT (peer));
-    sublength = stream_getw (BGP_INPUT (peer));
-    length -= 4;
+    if (BGP_ATTR_ENCAP == type) {
+        subtype   = stream_getc (BGP_INPUT (peer));
+        sublength = stream_getc (BGP_INPUT (peer));
+        length   -= 2;
+    }
 
     if (sublength > length) {
       zlog (peer->log, LOG_ERR,
@@ -2423,6 +2425,10 @@
 	  stream_putl (s, 0);
 	  stream_put (s, &attr->extra->mp_nexthop_global_in, 4);
 	  break;
+	case SAFI_ENCAP:
+	  stream_putc (s, 4);
+	  stream_put (s, &attr->extra->mp_nexthop_global_in, 4);
+	  break;
 	case SAFI_UNICAST:      /* invalid for IPv4 */
 	default:
 	  break;
@@ -2465,6 +2471,11 @@
           }
         }
 	break;
+	case SAFI_ENCAP:
+          assert (attr->extra);
+          stream_putc (s, 16);
+	  stream_put (s, &attr->extra->mp_nexthop_global, 16);
+	  break;
       default:
 	break;
       }
@@ -2517,6 +2528,7 @@
     uint8_t		attrtype)
 {
     unsigned int			attrlenfield = 0;
+    unsigned int			attrhdrlen   = 0;
     struct bgp_attr_encap_subtlv	*subtlvs;
     struct bgp_attr_encap_subtlv	*st;
     const char				*attrname;
@@ -2536,6 +2548,7 @@
 	     * V = concatenated subtlvs.
 	     */
 	    attrlenfield = 2 + 2;	/* T + L */
+            attrhdrlen   = 1 + 1;	/* subTLV T + L */
 	    break;
 
 	default:
@@ -2543,15 +2556,15 @@
     }
 
 
+    /* if no tlvs, don't make attr */
+    if (subtlvs == NULL)
+	return;
+
     /* compute attr length */
     for (st = subtlvs; st; st = st->next) {
-	attrlenfield += (4 + st->length);
+	attrlenfield += (attrhdrlen + st->length);
     }
 
-    /* if no tlvs, don't make attr */
-    if (!attrlenfield)
-	return;
-
     if (attrlenfield > 0xffff) {
 	zlog (peer->log, LOG_ERR,
 	    "%s attribute is too long (length=%d), can't send it",
@@ -2581,8 +2594,10 @@
 
     /* write each sub-tlv */
     for (st = subtlvs; st; st = st->next) {
-	stream_putw (s, st->type);
-	stream_putw (s, st->length);
+        if (attrtype == BGP_ATTR_ENCAP) {
+            stream_putc (s, st->type);
+            stream_putc (s, st->length);
+        }
 	stream_put (s, st->value, st->length);
     }
 }
@@ -2944,7 +2959,14 @@
       stream_putl (s, attr->extra->aggregator_as);
       stream_put_ipv4 (s, attr->extra->aggregator_addr.s_addr);
     }
-  
+
+  if ((afi == AFI_IP || afi == AFI_IP6) &&
+      (safi == SAFI_ENCAP || safi == SAFI_MPLS_VPN))
+    {
+	/* Tunnel Encap attribute */
+	bgp_packet_mpattr_tea(bgp, peer, s, attr, BGP_ATTR_ENCAP);
+    }
+
   /* Unknown transit attribute. */
   if (attr->extra && attr->extra->transit)
     stream_put (s, attr->extra->transit->val, attr->extra->transit->length);
diff --git a/bgpd/bgp_encap.c b/bgpd/bgp_encap.c
new file mode 100644
index 0000000..f936508
--- /dev/null
+++ b/bgpd/bgp_encap.c
@@ -0,0 +1,992 @@
+
+/*
+ * This file created by LabN Consulting, L.L.C.
+ *
+ *
+ * This file is based on bgp_mplsvpn.c which is Copyright (C) 2000
+ * Kunihiro Ishiguro <kunihiro@zebra.org>
+ *
+ */
+
+/* 
+
+This file is part of GNU Zebra.
+
+GNU Zebra is free software; you can redistribute it and/or modify it
+under the terms of the GNU General Public License as published by the
+Free Software Foundation; either version 2, or (at your option) any
+later version.
+
+GNU Zebra is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Zebra; see the file COPYING.  If not, write to the Free
+Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
+02111-1307, USA.  */
+
+#include <zebra.h>
+
+#include "command.h"
+#include "prefix.h"
+#include "log.h"
+#include "memory.h"
+#include "stream.h"
+#include "filter.h"
+
+#include "bgpd/bgpd.h"
+#include "bgpd/bgp_table.h"
+#include "bgpd/bgp_route.h"
+#include "bgpd/bgp_attr.h"
+#include "bgpd/bgp_ecommunity.h"
+#include "bgpd/bgp_mplsvpn.h"
+#include "bgpd/bgp_vty.h"
+#include "bgpd/bgp_encap.h"
+
+static u_int16_t
+decode_rd_type (u_char *pnt)
+{
+  u_int16_t v;
+  
+  v = ((u_int16_t) *pnt++ << 8);
+  v |= (u_int16_t) *pnt;
+  return v;
+}
+
+
+static void
+decode_rd_as (u_char *pnt, struct rd_as *rd_as)
+{
+  rd_as->as  = (u_int16_t) *pnt++ << 8;
+  rd_as->as |= (u_int16_t) *pnt++;
+  
+  rd_as->val  = ((u_int32_t) *pnt++) << 24;
+  rd_as->val |= ((u_int32_t) *pnt++) << 16;
+  rd_as->val |= ((u_int32_t) *pnt++) << 8;
+  rd_as->val |= (u_int32_t) *pnt;
+}
+
+static void
+decode_rd_as4 (u_char *pnt, struct rd_as *rd_as)
+{
+  rd_as->as  = (u_int32_t) *pnt++ << 24;
+  rd_as->as |= (u_int32_t) *pnt++ << 16;
+  rd_as->as |= (u_int32_t) *pnt++ << 8;
+  rd_as->as |= (u_int32_t) *pnt++;
+  
+  rd_as->val  = ((u_int32_t) *pnt++ << 8);
+  rd_as->val |= (u_int32_t) *pnt;
+}
+
+static void
+decode_rd_ip (u_char *pnt, struct rd_ip *rd_ip)
+{
+  memcpy (&rd_ip->ip, pnt, 4);
+  pnt += 4;
+  
+  rd_ip->val = ((u_int16_t) *pnt++ << 8);
+  rd_ip->val |= (u_int16_t) *pnt;
+}
+
+static void
+ecom2prd(struct ecommunity *ecom, struct prefix_rd *prd)
+{
+    int	i;
+
+    memset(prd, 0, sizeof(struct prefix_rd));
+    prd->family = AF_UNSPEC;
+    prd->prefixlen = 64;
+
+    if (!ecom)
+	return;
+
+    for (i = 0; i < (ecom->size * ECOMMUNITY_SIZE); i += ECOMMUNITY_SIZE) {
+
+	uint8_t *ep;
+
+	ep = ecom->val + i;
+
+	switch (ep[0]) {
+	    default:
+		continue;
+
+	    case 0x80:
+	    case 0x81:
+	    case 0x82:
+		if (ep[1] == 0x0) {
+		    prd->val[1] = ep[0] & 0x03;
+		    memcpy(prd->val + 2, ep + 2, 6);
+		    return;
+		}
+	}
+    }
+}
+
+int
+bgp_nlri_parse_encap(
+    afi_t		afi,
+    struct peer		*peer,
+    struct attr		*attr, 		/* Need even for withdraw */
+    struct bgp_nlri	*packet,
+    int			withdraw)	/* 0=update, !0 = withdraw */
+{
+  u_char *pnt;
+  u_char *lim;
+  struct prefix p;
+  int psize = 0;
+  int prefixlen;
+  struct rd_as rd_as;
+  struct rd_ip rd_ip;
+  struct prefix_rd prd;
+  struct ecommunity *pEcom = NULL;
+  u_int16_t rdtype = 0xffff;
+  char buf[BUFSIZ];
+
+  /* Check peer status. */
+  if (peer->status != Established)
+    return 0;
+  
+  /* Make prefix_rd */
+  if (attr && attr->extra && attr->extra->ecommunity)
+      pEcom = attr->extra->ecommunity;
+
+  ecom2prd(pEcom, &prd);
+  memset(&rd_as, 0, sizeof(rd_as));
+  memset(&rd_ip, 0, sizeof(rd_ip));
+
+  if (pEcom) {
+
+      rdtype = (prd.val[0] << 8) | prd.val[1];
+
+      /* Decode RD value. */
+      if (rdtype == RD_TYPE_AS)
+	decode_rd_as (prd.val + 2, &rd_as);
+      else if (rdtype == RD_TYPE_IP)
+	decode_rd_ip (prd.val + 2, &rd_ip);
+      else if (rdtype == RD_TYPE_AS4)
+	decode_rd_as4 (prd.val + 2, &rd_as);
+      else
+	{
+	  zlog_err ("Invalid RD type %d", rdtype);
+	}
+
+  }
+
+  /*
+   * NB: this code was based on the MPLS VPN code, which supported RDs.
+   * For the moment we are retaining the underlying RIB structure that
+   * keeps a per-RD radix tree, but since the RDs are not carried over
+   * the wire, we set the RD internally to 0.
+   */
+  prd.family = AF_UNSPEC;
+  prd.prefixlen = 64;
+  memset(prd.val, 0, sizeof(prd.val));
+
+  pnt = packet->nlri;
+  lim = pnt + packet->length;
+
+  for (; pnt < lim; pnt += psize)
+    {
+      /* Clear prefix structure. */
+      memset (&p, 0, sizeof (struct prefix));
+
+      /* Fetch prefix length. */
+      prefixlen = *pnt++;
+      p.family = afi2family(afi);
+      if (p.family == 0) {
+	/* bad afi, shouldn't happen */
+	zlog_warn("%s: bad afi %d, dropping incoming route", __func__, afi);
+	continue;
+      }
+      psize = PSIZE (prefixlen);
+
+      p.prefixlen = prefixlen;
+      memcpy (&p.u.prefix, pnt, psize);
+
+      if (pnt + psize > lim)
+	return -1;
+
+
+      if (rdtype == RD_TYPE_AS)
+	zlog_info ("rd-as %u:%u prefix %s/%d", rd_as.as, rd_as.val,
+		   inet_ntop (p.family, &p.u.prefix, buf, BUFSIZ),
+		   p.prefixlen);
+      else if (rdtype == RD_TYPE_IP)
+	zlog_info ("rd-ip %s:%u prefix %s/%d", inet_ntoa (rd_ip.ip),
+		   rd_ip.val,
+		   inet_ntop (p.family, &p.u.prefix, buf, BUFSIZ),
+		   p.prefixlen);
+      else if (rdtype == RD_TYPE_AS4)
+	zlog_info ("rd-as4 %u:%u prefix %s/%d", rd_as.as, rd_as.val,
+		   inet_ntop (p.family, &p.u.prefix, buf, BUFSIZ),
+		   p.prefixlen);
+      else
+	zlog_info ("rd unknown, default to 0:0 prefix %s/%d",
+	    inet_ntop (p.family, &p.u.prefix, buf, BUFSIZ),
+	    p.prefixlen);
+
+      if (!withdraw) {
+	bgp_update (peer, &p, attr, afi, SAFI_ENCAP,
+		    ZEBRA_ROUTE_BGP, BGP_ROUTE_NORMAL, &prd, NULL, 0);
+      } else {
+	bgp_withdraw (peer, &p, attr, afi, SAFI_ENCAP,
+		      ZEBRA_ROUTE_BGP, BGP_ROUTE_NORMAL, &prd, NULL);
+      }
+    }
+
+  /* Packet length consistency check. */
+  if (pnt != lim)
+    return -1;
+
+  return 0;
+}
+
+
+/* TBD: these routes should probably all be host routes */
+
+/* For testing purpose, static route of ENCAP. */
+DEFUN (encap_network,
+       encap_network_cmd,
+       "network A.B.C.D/M rd ASN:nn_or_IP-address:nn tag WORD",
+       "Specify a network to announce via BGP\n"
+       "IP prefix <network>/<length>, e.g., 35.0.0.0/8\n"
+       "Specify Route Distinguisher\n"
+       "ENCAP Route Distinguisher\n"
+       "BGP tag\n"
+       "tag value\n")
+{
+  return bgp_static_set_safi (SAFI_ENCAP, vty, argv[0], argv[1], argv[2], NULL);
+}
+
+/* For testing purpose, static route of ENCAP. */
+DEFUN (no_encap_network,
+       no_encap_network_cmd,
+       "no network A.B.C.D/M rd ASN:nn_or_IP-address:nn tag WORD",
+       NO_STR
+       "Specify a network to announce via BGP\n"
+       "IP prefix <network>/<length>, e.g., 35.0.0.0/8\n"
+       "Specify Route Distinguisher\n"
+       "ENCAP Route Distinguisher\n"
+       "BGP tag\n"
+       "tag value\n")
+{
+  return bgp_static_unset_safi (SAFI_ENCAP, vty, argv[0], argv[1], argv[2]);
+}
+
+static int
+show_adj_route_encap (struct vty *vty, struct peer *peer, struct prefix_rd *prd)
+{
+  struct bgp *bgp;
+  struct bgp_table *table;
+  struct bgp_node *rn;
+  struct bgp_node *rm;
+  struct attr *attr;
+  int rd_header;
+  int header = 1;
+  char v4_header[] = "   Network          Next Hop            Metric LocPrf Weight Path%s";
+
+  bgp = bgp_get_default ();
+  if (bgp == NULL)
+    {
+      vty_out (vty, "No BGP process is configured%s", VTY_NEWLINE);
+      return CMD_WARNING;
+    }
+
+  for (rn = bgp_table_top (bgp->rib[AFI_IP][SAFI_ENCAP]); rn;
+       rn = bgp_route_next (rn))
+    {
+      if (prd && memcmp (rn->p.u.val, prd->val, 8) != 0)
+        continue;
+
+      if ((table = rn->info) != NULL)
+        {
+          rd_header = 1;
+
+          for (rm = bgp_table_top (table); rm; rm = bgp_route_next (rm))
+            if ((attr = rm->info) != NULL)
+              {
+                if (header)
+                  {
+                    vty_out (vty, "BGP table version is 0, local router ID is %s%s",
+                             inet_ntoa (bgp->router_id), VTY_NEWLINE);
+                    vty_out (vty, "Status codes: s suppressed, d damped, h history, * valid, > best, i - internal%s",
+                             VTY_NEWLINE);
+                    vty_out (vty, "Origin codes: i - IGP, e - EGP, ? - incomplete%s%s",
+                             VTY_NEWLINE, VTY_NEWLINE);
+                    vty_out (vty, v4_header, VTY_NEWLINE);
+                    header = 0;
+                  }
+
+                if (rd_header)
+                  {
+                    u_int16_t type;
+                    struct rd_as rd_as;
+                    struct rd_ip rd_ip;
+                    u_char *pnt;
+
+                    pnt = rn->p.u.val;
+
+                    vty_out (vty, "Route Distinguisher: ");
+
+                    /* Decode RD type. */
+                    type = decode_rd_type (pnt);
+
+		    switch (type) {
+
+		    case RD_TYPE_AS:
+                      decode_rd_as (pnt + 2, &rd_as);
+                      vty_out (vty, "%u:%d", rd_as.as, rd_as.val);
+		      break;
+
+		    case RD_TYPE_IP:
+                      decode_rd_ip (pnt + 2, &rd_ip);
+                      vty_out (vty, "%s:%d", inet_ntoa (rd_ip.ip), rd_ip.val);
+		      break;
+
+		    default:
+                      vty_out (vty, "unknown RD type");
+		    }
+
+
+                    vty_out (vty, "%s", VTY_NEWLINE);
+                    rd_header = 0;
+                  }
+                route_vty_out_tmp (vty, &rm->p, attr, SAFI_ENCAP);
+              }
+        }
+    }
+  return CMD_SUCCESS;
+}
+
+enum bgp_show_type
+{
+  bgp_show_type_normal,
+  bgp_show_type_regexp,
+  bgp_show_type_prefix_list,
+  bgp_show_type_filter_list,
+  bgp_show_type_neighbor,
+  bgp_show_type_cidr_only,
+  bgp_show_type_prefix_longer,
+  bgp_show_type_community_all,
+  bgp_show_type_community,
+  bgp_show_type_community_exact,
+  bgp_show_type_community_list,
+  bgp_show_type_community_list_exact
+};
+
+static int
+bgp_show_encap (
+    struct vty *vty,
+    afi_t afi,
+    struct prefix_rd *prd,
+    enum bgp_show_type type,
+    void *output_arg,
+    int tags)
+{
+  struct bgp *bgp;
+  struct bgp_table *table;
+  struct bgp_node *rn;
+  struct bgp_node *rm;
+  struct bgp_info *ri;
+  int rd_header;
+  int header = 1;
+  char v4_header[] = "   Network          Next Hop            Metric LocPrf Weight Path%s";
+  char v4_header_tag[] = "   Network          Next Hop      In tag/Out tag%s";
+
+  unsigned long output_count = 0;
+  unsigned long total_count  = 0;
+
+  bgp = bgp_get_default ();
+  if (bgp == NULL)
+    {
+      vty_out (vty, "No BGP process is configured%s", VTY_NEWLINE);
+      return CMD_WARNING;
+    }
+
+  if ((afi != AFI_IP) && (afi != AFI_IP6)) {
+      vty_out (vty, "Afi %d not supported%s", afi, VTY_NEWLINE);
+      return CMD_WARNING;
+  }
+  
+  for (rn = bgp_table_top (bgp->rib[afi][SAFI_ENCAP]); rn; rn = bgp_route_next (rn))
+    {
+      if (prd && memcmp (rn->p.u.val, prd->val, 8) != 0)
+	continue;
+
+      if ((table = rn->info) != NULL)
+	{
+	  rd_header = 1;
+
+	  for (rm = bgp_table_top (table); rm; rm = bgp_route_next (rm))
+	    for (ri = rm->info; ri; ri = ri->next)
+	      {
+                total_count++;
+		if (type == bgp_show_type_neighbor)
+		  {
+		    union sockunion *su = output_arg;
+
+		    if (ri->peer->su_remote == NULL || ! sockunion_same(ri->peer->su_remote, su))
+		      continue;
+		  }
+		if (header)
+		  {
+		    if (tags)
+		      vty_out (vty, v4_header_tag, VTY_NEWLINE);
+		    else
+		      {
+			vty_out (vty, "BGP table version is 0, local router ID is %s%s",
+				 inet_ntoa (bgp->router_id), VTY_NEWLINE);
+			vty_out (vty, "Status codes: s suppressed, d damped, h history, * valid, > best, i - internal%s",
+				 VTY_NEWLINE);
+			vty_out (vty, "Origin codes: i - IGP, e - EGP, ? - incomplete%s%s",
+				 VTY_NEWLINE, VTY_NEWLINE);
+			vty_out (vty, v4_header, VTY_NEWLINE);
+		      }
+		    header = 0;
+		  }
+
+		if (rd_header)
+		  {
+		    u_int16_t type;
+		    struct rd_as rd_as;
+		    struct rd_ip rd_ip;
+		    u_char *pnt;
+
+		    pnt = rn->p.u.val;
+
+		    /* Decode RD type. */
+		    type = decode_rd_type (pnt);
+
+		    vty_out (vty, "Route Distinguisher: ");
+
+		    switch (type) {
+
+		    case RD_TYPE_AS:
+		      decode_rd_as (pnt + 2, &rd_as);
+		      vty_out (vty, "%u:%d", rd_as.as, rd_as.val);
+		      break;
+
+		    case RD_TYPE_IP:
+		      decode_rd_ip (pnt + 2, &rd_ip);
+		      vty_out (vty, "%s:%d", inet_ntoa (rd_ip.ip), rd_ip.val);
+		      break;
+
+		    default:
+		      vty_out (vty, "Unknown RD type");
+		      break;
+		    }
+
+		    vty_out (vty, "%s", VTY_NEWLINE);		  
+		    rd_header = 0;
+		  }
+	        if (tags)
+		  route_vty_out_tag (vty, &rm->p, ri, 0, SAFI_ENCAP);
+	        else
+		  route_vty_out (vty, &rm->p, ri, 0, SAFI_ENCAP);
+                output_count++;
+	      }
+        }
+    }
+
+  if (output_count == 0)
+    {
+        vty_out (vty, "No prefixes displayed, %ld exist%s", total_count, VTY_NEWLINE);
+    }
+  else
+    vty_out (vty, "%sDisplayed %ld out of %ld total prefixes%s",
+	     VTY_NEWLINE, output_count, total_count, VTY_NEWLINE);
+
+  return CMD_SUCCESS;
+}
+
+DEFUN (show_bgp_ipv4_encap,
+       show_bgp_ipv4_encap_cmd,
+       "show bgp ipv4 encap",
+       SHOW_STR
+       BGP_STR
+       "Address Family\n"
+       "Display ENCAP NLRI specific information\n")
+{
+  return bgp_show_encap (vty, AFI_IP, NULL, bgp_show_type_normal, NULL, 0);
+}
+#ifdef HAVE_IPV6
+DEFUN (show_bgp_ipv6_encap,
+       show_bgp_ipv6_encap_cmd,
+       "show bgp ipv6 encap",
+       SHOW_STR
+       BGP_STR
+       "Address Family\n"
+       "Display ENCAP NLRI specific information\n")
+{
+  return bgp_show_encap (vty, AFI_IP6, NULL, bgp_show_type_normal, NULL, 0);
+}
+#endif
+
+DEFUN (show_bgp_ipv4_encap_rd,
+       show_bgp_ipv4_encap_rd_cmd,
+       "show bgp ipv4 encap rd ASN:nn_or_IP-address:nn",
+       SHOW_STR
+       BGP_STR
+       "Address Family\n"
+       "Display ENCAP NLRI specific information\n"
+       "Display information for a route distinguisher\n"
+       "ENCAP Route Distinguisher\n")
+{
+  int ret;
+  struct prefix_rd prd;
+
+  ret = str2prefix_rd (argv[0], &prd);
+  if (! ret)
+    {
+      vty_out (vty, "%% Malformed Route Distinguisher%s", VTY_NEWLINE);
+      return CMD_WARNING;
+    }
+  return bgp_show_encap (vty, AFI_IP, &prd, bgp_show_type_normal, NULL, 0);
+}
+#ifdef HAVE_IPV6
+DEFUN (show_bgp_ipv6_encap_rd,
+       show_bgp_ipv6_encap_rd_cmd,
+       "show bgp ipv6 encap rd ASN:nn_or_IP-address:nn",
+       SHOW_STR
+       BGP_STR
+       "Address Family\n"
+       "Display ENCAP NLRI specific information\n"
+       "Display information for a route distinguisher\n"
+       "ENCAP Route Distinguisher\n"
+       "Display BGP tags for prefixes\n")
+{
+  int ret;
+  struct prefix_rd prd;
+
+  ret = str2prefix_rd (argv[0], &prd);
+  if (! ret)
+    {
+      vty_out (vty, "%% Malformed Route Distinguisher%s", VTY_NEWLINE);
+      return CMD_WARNING;
+    }
+  return bgp_show_encap (vty, AFI_IP6, &prd, bgp_show_type_normal, NULL, 0);
+}
+#endif
+
+DEFUN (show_bgp_ipv4_encap_tags,
+       show_bgp_ipv4_encap_tags_cmd,
+       "show bgp ipv4 encap tags",
+       SHOW_STR
+       BGP_STR
+       "Address Family\n"
+       "Display ENCAP NLRI specific information\n"
+       "Display BGP tags for prefixes\n")
+{
+  return bgp_show_encap (vty, AFI_IP, NULL, bgp_show_type_normal, NULL,  1);
+}
+#ifdef HAVE_IPV6
+DEFUN (show_bgp_ipv6_encap_tags,
+       show_bgp_ipv6_encap_tags_cmd,
+       "show bgp ipv6 encap tags",
+       SHOW_STR
+       BGP_STR
+       "Address Family\n"
+       "Display ENCAP NLRI specific information\n"
+       "Display BGP tags for prefixes\n")
+{
+  return bgp_show_encap (vty, AFI_IP6, NULL, bgp_show_type_normal, NULL,  1);
+}
+#endif
+
+DEFUN (show_bgp_ipv4_encap_rd_tags,
+       show_bgp_ipv4_encap_rd_tags_cmd,
+       "show bgp ipv4 encap rd ASN:nn_or_IP-address:nn tags",
+       SHOW_STR
+       BGP_STR
+       "Address Family\n"
+       "Display ENCAP NLRI specific information\n"
+       "Display information for a route distinguisher\n"
+       "ENCAP Route Distinguisher\n"
+       "Display BGP tags for prefixes\n")
+{
+  int ret;
+  struct prefix_rd prd;
+
+  ret = str2prefix_rd (argv[0], &prd);
+  if (! ret)
+    {
+      vty_out (vty, "%% Malformed Route Distinguisher%s", VTY_NEWLINE);
+      return CMD_WARNING;
+    }
+  return bgp_show_encap (vty, AFI_IP, &prd, bgp_show_type_normal, NULL, 1);
+}
+#ifdef HAVE_IPV6
+DEFUN (show_bgp_ipv6_encap_rd_tags,
+       show_bgp_ipv6_encap_rd_tags_cmd,
+       "show bgp ipv6 encap rd ASN:nn_or_IP-address:nn tags",
+       SHOW_STR
+       BGP_STR
+       "Address Family\n"
+       "Display ENCAP NLRI specific information\n"
+       "Display information for a route distinguisher\n"
+       "ENCAP Route Distinguisher\n"
+       "Display BGP tags for prefixes\n")
+{
+  int ret;
+  struct prefix_rd prd;
+
+  ret = str2prefix_rd (argv[0], &prd);
+  if (! ret)
+    {
+      vty_out (vty, "%% Malformed Route Distinguisher%s", VTY_NEWLINE);
+      return CMD_WARNING;
+    }
+  return bgp_show_encap (vty, AFI_IP6, &prd, bgp_show_type_normal, NULL, 1);
+}
+#endif
+
+DEFUN (show_bgp_ipv4_encap_neighbor_routes,
+       show_bgp_ipv4_encap_neighbor_routes_cmd,
+       "show bgp ipv4 encap neighbors A.B.C.D routes",
+       SHOW_STR
+       BGP_STR
+       "Address Family\n"
+       "Display ENCAP NLRI specific information\n"
+       "Detailed information on TCP and BGP neighbor connections\n"
+       "Neighbor to display information about\n"
+       "Display routes learned from neighbor\n")
+{
+  union sockunion *su;
+  struct peer *peer;
+  
+  su = sockunion_str2su (argv[0]);
+  if (su == NULL)
+    {
+      vty_out (vty, "Malformed address: %s%s", argv[0], VTY_NEWLINE);
+               return CMD_WARNING;
+    }
+
+  peer = peer_lookup (NULL, su);
+  if (! peer || ! peer->afc[AFI_IP][SAFI_ENCAP])
+    {
+      vty_out (vty, "%% No such neighbor or address family%s", VTY_NEWLINE);
+      return CMD_WARNING;
+    }
+
+  return bgp_show_encap (vty, AFI_IP, NULL, bgp_show_type_neighbor, su, 0);
+}
+#ifdef HAVE_IPV6
+DEFUN (show_bgp_ipv6_encap_neighbor_routes,
+       show_bgp_ipv6_encap_neighbor_routes_cmd,
+       "show bgp ipv6 encap neighbors A.B.C.D routes",
+       SHOW_STR
+       BGP_STR
+       "Address Family\n"
+       "Display ENCAP NLRI specific information\n"
+       "Detailed information on TCP and BGP neighbor connections\n"
+       "Neighbor to display information about\n"
+       "Display routes learned from neighbor\n")
+{
+  union sockunion *su;
+  struct peer *peer;
+  
+  su = sockunion_str2su (argv[0]);
+  if (su == NULL)
+    {
+      vty_out (vty, "Malformed address: %s%s", argv[0], VTY_NEWLINE);
+               return CMD_WARNING;
+    }
+
+  peer = peer_lookup (NULL, su);
+  if (! peer || ! peer->afc[AFI_IP6][SAFI_ENCAP])
+    {
+      vty_out (vty, "%% No such neighbor or address family%s", VTY_NEWLINE);
+      return CMD_WARNING;
+    }
+
+  return bgp_show_encap (vty, AFI_IP6, NULL, bgp_show_type_neighbor, su, 0);
+}
+#endif
+
+DEFUN (show_bgp_ipv4_encap_rd_neighbor_routes,
+       show_bgp_ipv4_encap_rd_neighbor_routes_cmd,
+       "show bgp ipv4 encap rd ASN:nn_or_IP-address:nn neighbors (A.B.C.D|X:X::X:X) routes",
+       SHOW_STR
+       BGP_STR
+       "Address Family\n"
+       "Display ENCAP NLRI specific information\n"
+       "Display information for a route distinguisher\n"
+       "ENCAP Route Distinguisher\n"
+       "Detailed information on TCP and BGP neighbor connections\n"
+       "Neighbor to display information about\n"
+       "Neighbor to display information about\n"
+       "Display routes learned from neighbor\n")
+{
+  int ret;
+  union sockunion *su;
+  struct peer *peer;
+  struct prefix_rd prd;
+
+  ret = str2prefix_rd (argv[0], &prd);
+  if (! ret)
+    {
+      vty_out (vty, "%% Malformed Route Distinguisher%s", VTY_NEWLINE);
+      return CMD_WARNING;
+    }
+
+  su = sockunion_str2su (argv[1]);
+  if (su == NULL)
+    {
+      vty_out (vty, "Malformed address: %s%s", argv[1], VTY_NEWLINE);
+               return CMD_WARNING;
+    }
+
+  peer = peer_lookup (NULL, su);
+  if (! peer || ! peer->afc[AFI_IP][SAFI_ENCAP])
+    {
+      vty_out (vty, "%% No such neighbor or address family%s", VTY_NEWLINE);
+      return CMD_WARNING;
+    }
+
+  return bgp_show_encap (vty, AFI_IP, &prd, bgp_show_type_neighbor, su, 0);
+}
+#ifdef HAVE_IPV6
+DEFUN (show_bgp_ipv6_encap_rd_neighbor_routes,
+       show_bgp_ipv6_encap_rd_neighbor_routes_cmd,
+       "show bgp ipv6 encap rd ASN:nn_or_IP-address:nn neighbors (A.B.C.D|X:X::X:X) routes",
+       SHOW_STR
+       BGP_STR
+       "Address Family\n"
+       "Display ENCAP NLRI specific information\n"
+       "Display information for a route distinguisher\n"
+       "ENCAP Route Distinguisher\n"
+       "Detailed information on TCP and BGP neighbor connections\n"
+       "Neighbor to display information about\n"
+       "Neighbor to display information about\n"
+       "Display routes learned from neighbor\n")
+{
+  int ret;
+  union sockunion *su;
+  struct peer *peer;
+  struct prefix_rd prd;
+
+  ret = str2prefix_rd (argv[0], &prd);
+  if (! ret)
+    {
+      vty_out (vty, "%% Malformed Route Distinguisher%s", VTY_NEWLINE);
+      return CMD_WARNING;
+    }
+
+  su = sockunion_str2su (argv[1]);
+  if (su == NULL)
+    {
+      vty_out (vty, "Malformed address: %s%s", argv[1], VTY_NEWLINE);
+               return CMD_WARNING;
+    }
+
+  peer = peer_lookup (NULL, su);
+  if (! peer || ! peer->afc[AFI_IP6][SAFI_ENCAP])
+    {
+      vty_out (vty, "%% No such neighbor or address family%s", VTY_NEWLINE);
+      return CMD_WARNING;
+    }
+
+  return bgp_show_encap (vty, AFI_IP6, &prd, bgp_show_type_neighbor, su, 0);
+}
+#endif
+
+DEFUN (show_bgp_ipv4_encap_neighbor_advertised_routes,
+       show_bgp_ipv4_encap_neighbor_advertised_routes_cmd,
+       "show bgp ipv4 encap neighbors A.B.C.D advertised-routes",
+       SHOW_STR
+       BGP_STR
+       "Address Family\n"
+       "Display ENCAP NLRI specific information\n"
+       "Detailed information on TCP and BGP neighbor connections\n"
+       "Neighbor to display information about\n"
+       "Display the routes advertised to a BGP neighbor\n")
+{
+  int ret;
+  struct peer *peer;
+  union sockunion su;
+
+  ret = str2sockunion (argv[0], &su);
+  if (ret < 0)
+    {
+      vty_out (vty, "%% Malformed address: %s%s", argv[0], VTY_NEWLINE);
+      return CMD_WARNING;
+    }
+  peer = peer_lookup (NULL, &su);
+  if (! peer || ! peer->afc[AFI_IP][SAFI_ENCAP])
+    {
+      vty_out (vty, "%% No such neighbor or address family%s", VTY_NEWLINE);
+      return CMD_WARNING;
+    }
+
+  return show_adj_route_encap (vty, peer, NULL);
+}
+#ifdef HAVE_IPV6
+DEFUN (show_bgp_ipv6_encap_neighbor_advertised_routes,
+       show_bgp_ipv6_encap_neighbor_advertised_routes_cmd,
+       "show bgp ipv6 encap neighbors A.B.C.D advertised-routes",
+       SHOW_STR
+       BGP_STR
+       "Address Family\n"
+       "Display ENCAP NLRI specific information\n"
+       "Detailed information on TCP and BGP neighbor connections\n"
+       "Neighbor to display information about\n"
+       "Display the routes advertised to a BGP neighbor\n")
+{
+  int ret;
+  struct peer *peer;
+  union sockunion su;
+
+  ret = str2sockunion (argv[0], &su);
+  if (ret < 0)
+    {
+      vty_out (vty, "%% Malformed address: %s%s", argv[0], VTY_NEWLINE);
+      return CMD_WARNING;
+    }
+  peer = peer_lookup (NULL, &su);
+  if (! peer || ! peer->afc[AFI_IP6][SAFI_ENCAP])
+    {
+      vty_out (vty, "%% No such neighbor or address family%s", VTY_NEWLINE);
+      return CMD_WARNING;
+    }
+
+  return show_adj_route_encap (vty, peer, NULL);
+}
+#endif
+
+DEFUN (show_bgp_ipv4_encap_rd_neighbor_advertised_routes,
+       show_bgp_ipv4_encap_rd_neighbor_advertised_routes_cmd,
+       "show bgp ipv4 encap rd ASN:nn_or_IP-address:nn neighbors (A.B.C.D|X:X::X:X) advertised-routes",
+       SHOW_STR
+       BGP_STR
+       "Address Family\n"
+       "Display ENCAP NLRI specific information\n"
+       "Display information for a route distinguisher\n"
+       "ENCAP Route Distinguisher\n"
+       "Detailed information on TCP and BGP neighbor connections\n"
+       "Neighbor to display information about\n"
+       "Neighbor to display information about\n"
+       "Display the routes advertised to a BGP neighbor\n")
+{
+  int ret;
+  struct peer *peer;
+  struct prefix_rd prd;
+  union sockunion su;
+
+  ret = str2sockunion (argv[1], &su);
+  if (ret < 0)
+    {
+      vty_out (vty, "%% Malformed address: %s%s", argv[1], VTY_NEWLINE);
+      return CMD_WARNING;
+    }
+  peer = peer_lookup (NULL, &su);
+  if (! peer || ! peer->afc[AFI_IP][SAFI_ENCAP])
+    {
+      vty_out (vty, "%% No such neighbor or address family%s", VTY_NEWLINE);
+      return CMD_WARNING;
+    }
+
+  ret = str2prefix_rd (argv[0], &prd);
+  if (! ret)
+    {
+      vty_out (vty, "%% Malformed Route Distinguisher%s", VTY_NEWLINE);
+      return CMD_WARNING;
+    }
+
+  return show_adj_route_encap (vty, peer, &prd);
+}
+#ifdef HAVE_IPV6
+DEFUN (show_bgp_ipv6_encap_rd_neighbor_advertised_routes,
+       show_bgp_ipv6_encap_rd_neighbor_advertised_routes_cmd,
+       "show bgp ipv6 encap rd ASN:nn_or_IP-address:nn neighbors (A.B.C.D|X:X::X:X) advertised-routes",
+       SHOW_STR
+       BGP_STR
+       "Address Family\n"
+       "Display ENCAP NLRI specific information\n"
+       "Display information for a route distinguisher\n"
+       "ENCAP Route Distinguisher\n"
+       "Detailed information on TCP and BGP neighbor connections\n"
+       "Neighbor to display information about\n"
+       "Neighbor to display information about\n"
+       "Display the routes advertised to a BGP neighbor\n")
+{
+  int ret;
+  struct peer *peer;
+  struct prefix_rd prd;
+  union sockunion su;
+
+  ret = str2sockunion (argv[1], &su);
+  if (ret < 0)
+    {
+      vty_out (vty, "%% Malformed address: %s%s", argv[1], VTY_NEWLINE);
+      return CMD_WARNING;
+    }
+  peer = peer_lookup (NULL, &su);
+  if (! peer || ! peer->afc[AFI_IP6][SAFI_ENCAP])
+    {
+      vty_out (vty, "%% No such neighbor or address family%s", VTY_NEWLINE);
+      return CMD_WARNING;
+    }
+
+  ret = str2prefix_rd (argv[0], &prd);
+  if (! ret)
+    {
+      vty_out (vty, "%% Malformed Route Distinguisher%s", VTY_NEWLINE);
+      return CMD_WARNING;
+    }
+
+  return show_adj_route_encap (vty, peer, &prd);
+}
+#endif
+
+void
+bgp_encap_init (void)
+{
+  //install_element (BGP_ENCAP_NODE, &encap_network_cmd);
+  //install_element (BGP_ENCAP_NODE, &no_encap_network_cmd);
+
+
+  install_element (VIEW_NODE, &show_bgp_ipv4_encap_cmd);
+  install_element (VIEW_NODE, &show_bgp_ipv4_encap_rd_cmd);
+  install_element (VIEW_NODE, &show_bgp_ipv4_encap_tags_cmd);
+  install_element (VIEW_NODE, &show_bgp_ipv4_encap_rd_tags_cmd);
+  install_element (VIEW_NODE, &show_bgp_ipv4_encap_neighbor_routes_cmd);
+  install_element (VIEW_NODE, &show_bgp_ipv4_encap_rd_neighbor_routes_cmd);
+  install_element (VIEW_NODE, &show_bgp_ipv4_encap_neighbor_advertised_routes_cmd);
+  install_element (VIEW_NODE, &show_bgp_ipv4_encap_rd_neighbor_advertised_routes_cmd);
+
+#ifdef HAVE_IPV6
+  install_element (VIEW_NODE, &show_bgp_ipv6_encap_cmd);
+  install_element (VIEW_NODE, &show_bgp_ipv6_encap_rd_cmd);
+  install_element (VIEW_NODE, &show_bgp_ipv6_encap_tags_cmd);
+  install_element (VIEW_NODE, &show_bgp_ipv6_encap_rd_tags_cmd);
+  install_element (VIEW_NODE, &show_bgp_ipv6_encap_neighbor_routes_cmd);
+  install_element (VIEW_NODE, &show_bgp_ipv6_encap_rd_neighbor_routes_cmd);
+  install_element (VIEW_NODE, &show_bgp_ipv6_encap_neighbor_advertised_routes_cmd);
+  install_element (VIEW_NODE, &show_bgp_ipv6_encap_rd_neighbor_advertised_routes_cmd);
+#endif
+
+
+  install_element (ENABLE_NODE, &show_bgp_ipv4_encap_cmd);
+  install_element (ENABLE_NODE, &show_bgp_ipv4_encap_rd_cmd);
+  install_element (ENABLE_NODE, &show_bgp_ipv4_encap_tags_cmd);
+  install_element (ENABLE_NODE, &show_bgp_ipv4_encap_rd_tags_cmd);
+  install_element (ENABLE_NODE, &show_bgp_ipv4_encap_neighbor_routes_cmd);
+  install_element (ENABLE_NODE, &show_bgp_ipv4_encap_rd_neighbor_routes_cmd);
+  install_element (ENABLE_NODE, &show_bgp_ipv4_encap_neighbor_advertised_routes_cmd);
+  install_element (ENABLE_NODE, &show_bgp_ipv4_encap_rd_neighbor_advertised_routes_cmd);
+
+#ifdef HAVE_IPV6
+  install_element (ENABLE_NODE, &show_bgp_ipv6_encap_cmd);
+  install_element (ENABLE_NODE, &show_bgp_ipv6_encap_rd_cmd);
+  install_element (ENABLE_NODE, &show_bgp_ipv6_encap_tags_cmd);
+  install_element (ENABLE_NODE, &show_bgp_ipv6_encap_rd_tags_cmd);
+  install_element (ENABLE_NODE, &show_bgp_ipv6_encap_neighbor_routes_cmd);
+  install_element (ENABLE_NODE, &show_bgp_ipv6_encap_rd_neighbor_routes_cmd);
+  install_element (ENABLE_NODE, &show_bgp_ipv6_encap_neighbor_advertised_routes_cmd);
+  install_element (ENABLE_NODE, &show_bgp_ipv6_encap_rd_neighbor_advertised_routes_cmd);
+#endif
+
+
+}
diff --git a/bgpd/bgp_encap.h b/bgpd/bgp_encap.h
new file mode 100644
index 0000000..6f43b7b
--- /dev/null
+++ b/bgpd/bgp_encap.h
@@ -0,0 +1,34 @@
+/* 
+ *
+ * Copyright 2009-2015, LabN Consulting, L.L.C.
+ *
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License
+ * as published by the Free Software Foundation; either version 2
+ * of the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
+ *
+ */
+
+#ifndef _QUAGGA_BGP_ENCAP_H
+#define _QUAGGA_BGP_ENCAP_H
+
+extern void bgp_encap_init (void);
+extern int bgp_nlri_parse_encap (
+    afi_t,
+    struct peer *,
+    struct attr *,
+    struct bgp_nlri *,
+    int withdraw);
+
+#include "bgp_encap_types.h"
+#endif /* _QUAGGA_BGP_ENCAP_H */
diff --git a/bgpd/bgp_encap_tlv.c b/bgpd/bgp_encap_tlv.c
index 058b41e..347b4b3 100644
--- a/bgpd/bgp_encap_tlv.c
+++ b/bgpd/bgp_encap_tlv.c
@@ -196,6 +196,34 @@
     return new;
 }
 
+/* draft-rosen-idr-tunnel-encaps 2.1 */
+static struct bgp_attr_encap_subtlv *
+subtlv_encode_remote_endpoint(
+    struct bgp_tea_subtlv_remote_endpoint	*st)
+{
+    struct bgp_attr_encap_subtlv	*new;
+    uint8_t				*p;
+    
+    int	total = (st->family==AF_INET?8:20);
+
+    assert(total <= 0xff);
+
+    new = XCALLOC(MTYPE_ENCAP_TLV, sizeof(struct bgp_attr_encap_subtlv) - 1 + total);
+    assert(new);
+    new->type = BGP_ENCAP_SUBTLV_TYPE_REMOTE_ENDPOINT;
+    new->length = total;
+    p = new->value;
+    if (st->family == AF_INET) {
+        memcpy (p, &(st->ip_address.v4.s_addr), 4);
+        p+=4;
+    } else {
+        assert (st->family == AF_INET6);
+        memcpy (p, &(st->ip_address.v6.s6_addr), 16);
+        p+=16;
+    }
+    memcpy (p, &(st->as4), 4);
+    return new;
+}
 
 /***********************************************************************
  *		TUNNEL TYPE-SPECIFIC TLV ENCODE
@@ -235,6 +263,7 @@
     ENC_SUBTLV(BGP_TEA_SUBTLV_ENCAP, subtlv_encode_encap_l2tpv3_over_ip, st_encap);
     ENC_SUBTLV(BGP_TEA_SUBTLV_PROTO_TYPE, subtlv_encode_proto_type, st_proto);
     ENC_SUBTLV(BGP_TEA_SUBTLV_COLOR, subtlv_encode_color, st_color);
+    ENC_SUBTLV(BGP_TEA_SUBTLV_REMOTE_ENDPOINT, subtlv_encode_remote_endpoint, st_endpoint);
 }
 
 void
@@ -253,6 +282,7 @@
     ENC_SUBTLV(BGP_TEA_SUBTLV_ENCAP, subtlv_encode_encap_gre, st_encap);
     ENC_SUBTLV(BGP_TEA_SUBTLV_PROTO_TYPE, subtlv_encode_proto_type, st_proto);
     ENC_SUBTLV(BGP_TEA_SUBTLV_COLOR, subtlv_encode_color, st_color);
+    ENC_SUBTLV(BGP_TEA_SUBTLV_REMOTE_ENDPOINT, subtlv_encode_remote_endpoint, st_endpoint);
 }
 
 void
@@ -270,6 +300,7 @@
 
     ENC_SUBTLV(BGP_TEA_SUBTLV_PROTO_TYPE, subtlv_encode_proto_type, st_proto);
     ENC_SUBTLV(BGP_TEA_SUBTLV_COLOR, subtlv_encode_color, st_color);
+    ENC_SUBTLV(BGP_TEA_SUBTLV_REMOTE_ENDPOINT, subtlv_encode_remote_endpoint, st_endpoint);
 }
 
 void
@@ -542,6 +573,36 @@
     return 0;
 }
 
+/* draft-rosen-idr-tunnel-encaps 2.1 */
+static int
+subtlv_decode_remote_endpoint(
+    struct bgp_attr_encap_subtlv 	  *subtlv,
+    struct bgp_tea_subtlv_remote_endpoint *st)
+{
+   int i;
+   if (subtlv->length != 8 && subtlv->length != 20 ) {
+	zlog_debug("%s, subtlv length %d does not equal 8 or 20",
+	    __func__, subtlv->length);
+	return -1;
+    }
+    if (subtlv->length == 8) {
+        st->family = AF_INET;
+        st->ip_address.v4.s_addr = ((subtlv->value[0] << 24) |
+                                    (subtlv->value[1] << 16) |
+                                    (subtlv->value[2] << 8)  |
+                                    subtlv->value[3]);
+    } else {
+        st->family = AF_INET6;
+        memcpy (&(st->ip_address.v6.s6_addr), subtlv->value, 16);
+    }
+    i = subtlv->length - 4;
+    st->as4 = ((subtlv->value[i] << 24) |
+               (subtlv->value[i+1] << 16) |
+               (subtlv->value[i+2] << 8)  |
+               subtlv->value[i+3]);
+    return 0;
+}
+
 /***********************************************************************
  *		TUNNEL TYPE-SPECIFIC TLV DECODE
  ***********************************************************************/
@@ -558,17 +619,22 @@
 	switch (st->type) {
 	    case BGP_ENCAP_SUBTLV_TYPE_ENCAPSULATION:
 		rc |= subtlv_decode_encap_l2tpv3_over_ip(st, &bet->st_encap);
-		bet->valid_subtlvs |= BGP_TEA_SUBTLV_ENCAP;
+		SET_SUBTLV_FLAG(bet, BGP_TEA_SUBTLV_ENCAP);
 		break;
 
 	    case BGP_ENCAP_SUBTLV_TYPE_PROTO_TYPE:
 		rc |= subtlv_decode_proto_type(st, &bet->st_proto);
-		bet->valid_subtlvs |= BGP_TEA_SUBTLV_PROTO_TYPE;
+		SET_SUBTLV_FLAG(bet, BGP_TEA_SUBTLV_PROTO_TYPE);
 		break;
 
 	    case BGP_ENCAP_SUBTLV_TYPE_COLOR:
 		rc |= subtlv_decode_color(st, &bet->st_color);
-		bet->valid_subtlvs |= BGP_TEA_SUBTLV_COLOR;
+		SET_SUBTLV_FLAG(bet, BGP_TEA_SUBTLV_COLOR);
+		break;
+
+	    case BGP_ENCAP_SUBTLV_TYPE_REMOTE_ENDPOINT:
+		rc |= subtlv_decode_remote_endpoint(st, &bet->st_endpoint);
+		SET_SUBTLV_FLAG(bet, BGP_TEA_SUBTLV_REMOTE_ENDPOINT);
 		break;
 
 	    default:
@@ -592,17 +658,22 @@
 	switch (st->type) {
 	    case BGP_ENCAP_SUBTLV_TYPE_ENCAPSULATION:
 		rc |= subtlv_decode_encap_gre(st, &bet->st_encap);
-		bet->valid_subtlvs |= BGP_TEA_SUBTLV_ENCAP;
+		SET_SUBTLV_FLAG(bet, BGP_TEA_SUBTLV_ENCAP);
 		break;
 
 	    case BGP_ENCAP_SUBTLV_TYPE_PROTO_TYPE:
 		rc |= subtlv_decode_proto_type(st, &bet->st_proto);
-		bet->valid_subtlvs |= BGP_TEA_SUBTLV_PROTO_TYPE;
+		SET_SUBTLV_FLAG(bet, BGP_TEA_SUBTLV_PROTO_TYPE);
 		break;
 
 	    case BGP_ENCAP_SUBTLV_TYPE_COLOR:
 		rc |= subtlv_decode_color(st, &bet->st_color);
-		bet->valid_subtlvs |= BGP_TEA_SUBTLV_COLOR;
+		SET_SUBTLV_FLAG(bet, BGP_TEA_SUBTLV_COLOR);
+		break;
+
+	    case BGP_ENCAP_SUBTLV_TYPE_REMOTE_ENDPOINT:
+		rc |= subtlv_decode_remote_endpoint(st, &bet->st_endpoint);
+		SET_SUBTLV_FLAG(bet, BGP_TEA_SUBTLV_REMOTE_ENDPOINT);
 		break;
 
 	    default:
@@ -626,12 +697,17 @@
 	switch (st->type) {
 	    case BGP_ENCAP_SUBTLV_TYPE_PROTO_TYPE:
 		rc |= subtlv_decode_proto_type(st, &bet->st_proto);
-		bet->valid_subtlvs |= BGP_TEA_SUBTLV_PROTO_TYPE;
+		SET_SUBTLV_FLAG(bet, BGP_TEA_SUBTLV_PROTO_TYPE);
 		break;
 
 	    case BGP_ENCAP_SUBTLV_TYPE_COLOR:
 		rc |= subtlv_decode_color(st, &bet->st_color);
-		bet->valid_subtlvs |= BGP_TEA_SUBTLV_COLOR;
+		SET_SUBTLV_FLAG(bet, BGP_TEA_SUBTLV_COLOR);
+		break;
+
+	    case BGP_ENCAP_SUBTLV_TYPE_REMOTE_ENDPOINT:
+		rc |= subtlv_decode_remote_endpoint(st, &bet->st_endpoint);
+		SET_SUBTLV_FLAG(bet, BGP_TEA_SUBTLV_REMOTE_ENDPOINT);
 		break;
 
 	    default:
@@ -653,6 +729,12 @@
 
     for (st = stlv; st; st = st->next) {
 	switch (st->type) {
+
+	    case BGP_ENCAP_SUBTLV_TYPE_REMOTE_ENDPOINT:
+		rc |= subtlv_decode_remote_endpoint(st, &bet->st_endpoint);
+		SET_SUBTLV_FLAG(bet, BGP_TEA_SUBTLV_REMOTE_ENDPOINT);
+		break;
+
 	    default:
 		zlog_debug("%s: unexpected subtlv type %d", __func__, st->type);
 		rc |= -1;
@@ -674,7 +756,12 @@
 	switch (st->type) {
 	    case BGP_ENCAP_SUBTLV_TYPE_IPSEC_TA:
 		rc |= subtlv_decode_ipsec_ta(st, &bet->st_ipsec_ta);
-		bet->valid_subtlvs |= BGP_TEA_SUBTLV_IPSEC_TA;
+		SET_SUBTLV_FLAG(bet, BGP_TEA_SUBTLV_IPSEC_TA);
+		break;
+
+	    case BGP_ENCAP_SUBTLV_TYPE_REMOTE_ENDPOINT:
+		rc |= subtlv_decode_remote_endpoint(st, &bet->st_endpoint);
+		SET_SUBTLV_FLAG(bet, BGP_TEA_SUBTLV_REMOTE_ENDPOINT);
 		break;
 
 	    default:
@@ -698,7 +785,12 @@
 	switch (st->type) {
 	    case BGP_ENCAP_SUBTLV_TYPE_IPSEC_TA:
 		rc |= subtlv_decode_ipsec_ta(st, &bet->st_ipsec_ta);
-		bet->valid_subtlvs |= BGP_TEA_SUBTLV_IPSEC_TA;
+		SET_SUBTLV_FLAG(bet, BGP_TEA_SUBTLV_IPSEC_TA);
+		break;
+
+	    case BGP_ENCAP_SUBTLV_TYPE_REMOTE_ENDPOINT:
+		rc |= subtlv_decode_remote_endpoint(st, &bet->st_endpoint);
+		SET_SUBTLV_FLAG(bet, BGP_TEA_SUBTLV_REMOTE_ENDPOINT);
 		break;
 
 	    default:
@@ -722,7 +814,12 @@
 	switch (st->type) {
 	    case BGP_ENCAP_SUBTLV_TYPE_IPSEC_TA:
 		rc |= subtlv_decode_ipsec_ta(st, &bet->st_ipsec_ta);
-		bet->valid_subtlvs |= BGP_TEA_SUBTLV_IPSEC_TA;
+		SET_SUBTLV_FLAG(bet, BGP_TEA_SUBTLV_IPSEC_TA);
+		break;
+
+	    case BGP_ENCAP_SUBTLV_TYPE_REMOTE_ENDPOINT:
+		rc |= subtlv_decode_remote_endpoint(st, &bet->st_endpoint);
+		SET_SUBTLV_FLAG(bet, BGP_TEA_SUBTLV_REMOTE_ENDPOINT);
 		break;
 
 	    default:
@@ -744,6 +841,12 @@
 
     for (st = stlv; st; st = st->next) {
 	switch (st->type) {
+
+	    case BGP_ENCAP_SUBTLV_TYPE_REMOTE_ENDPOINT:
+		rc |= subtlv_decode_remote_endpoint(st, &bet->st_endpoint);
+		SET_SUBTLV_FLAG(bet, BGP_TEA_SUBTLV_REMOTE_ENDPOINT);
+		break;
+
 	    default:
 		zlog_debug("%s: unexpected subtlv type %d", __func__, st->type);
 		rc |= -1;
@@ -763,6 +866,12 @@
 
     for (st = stlv; st; st = st->next) {
 	switch (st->type) {
+
+	    case BGP_ENCAP_SUBTLV_TYPE_REMOTE_ENDPOINT:
+		rc |= subtlv_decode_remote_endpoint(st, &bet->st_endpoint);
+		SET_SUBTLV_FLAG(bet, BGP_TEA_SUBTLV_REMOTE_ENDPOINT);
+		break;
+
 	    default:
 		zlog_debug("%s: unexpected subtlv type %d", __func__, st->type);
 		rc |= -1;
@@ -782,6 +891,12 @@
 
     for (st = stlv; st; st = st->next) {
 	switch (st->type) {
+
+	    case BGP_ENCAP_SUBTLV_TYPE_REMOTE_ENDPOINT:
+		rc |= subtlv_decode_remote_endpoint(st, &bet->st_endpoint);
+		SET_SUBTLV_FLAG(bet, BGP_TEA_SUBTLV_REMOTE_ENDPOINT);
+		break;
+
 	    default:
 		zlog_debug("%s: unexpected subtlv type %d", __func__, st->type);
 		rc |= -1;
@@ -801,6 +916,12 @@
 
     for (st = stlv; st; st = st->next) {
 	switch (st->type) {
+
+	    case BGP_ENCAP_SUBTLV_TYPE_REMOTE_ENDPOINT:
+		rc |= subtlv_decode_remote_endpoint(st, &bet->st_endpoint);
+		SET_SUBTLV_FLAG(bet, BGP_TEA_SUBTLV_REMOTE_ENDPOINT);
+		break;
+
 	    default:
 		zlog_debug("%s: unexpected subtlv type %d", __func__, st->type);
 		rc |= -1;
@@ -820,6 +941,12 @@
 
     for (st = stlv; st; st = st->next) {
 	switch (st->type) {
+
+	    case BGP_ENCAP_SUBTLV_TYPE_REMOTE_ENDPOINT:
+		rc |= subtlv_decode_remote_endpoint(st, &bet->st_endpoint);
+		SET_SUBTLV_FLAG(bet, BGP_TEA_SUBTLV_REMOTE_ENDPOINT);
+		break;
+
 	    default:
 		zlog_debug("%s: unexpected subtlv type %d", __func__, st->type);
 		rc |= -1;
@@ -839,6 +966,12 @@
 
     for (st = stlv; st; st = st->next) {
 	switch (st->type) {
+
+	    case BGP_ENCAP_SUBTLV_TYPE_REMOTE_ENDPOINT:
+		rc |= subtlv_decode_remote_endpoint(st, &bet->st_endpoint);
+		SET_SUBTLV_FLAG(bet, BGP_TEA_SUBTLV_REMOTE_ENDPOINT);
+		break;
+
 	    default:
 		zlog_debug("%s: unexpected subtlv type %d", __func__, st->type);
 		rc |= -1;
@@ -860,7 +993,12 @@
 	switch (st->type) {
 	    case BGP_ENCAP_SUBTLV_TYPE_ENCAPSULATION:
 		rc |= subtlv_decode_encap_pbb(st, &bet->st_encap);
-		bet->valid_subtlvs |= BGP_TEA_SUBTLV_ENCAP;
+		SET_SUBTLV_FLAG(bet, BGP_TEA_SUBTLV_ENCAP);
+		break;
+
+	    case BGP_ENCAP_SUBTLV_TYPE_REMOTE_ENDPOINT:
+		rc |= subtlv_decode_remote_endpoint(st, &bet->st_endpoint);
+		SET_SUBTLV_FLAG(bet, BGP_TEA_SUBTLV_REMOTE_ENDPOINT);
 		break;
 
 	    default:
diff --git a/bgpd/bgp_encap_types.h b/bgpd/bgp_encap_types.h
index c81b729..603ff9d 100644
--- a/bgpd/bgp_encap_types.h
+++ b/bgpd/bgp_encap_types.h
@@ -44,6 +44,7 @@
     BGP_ENCAP_SUBTLV_TYPE_PROTO_TYPE=2,
     BGP_ENCAP_SUBTLV_TYPE_IPSEC_TA=3,
     BGP_ENCAP_SUBTLV_TYPE_COLOR=4,
+    BGP_ENCAP_SUBTLV_TYPE_REMOTE_ENDPOINT=6 /* speculative, IANA assignment TBD */
 } bgp_encap_subtlv_types;
 
 /*
@@ -75,6 +76,16 @@
     uint32_t	color;
 };
 
+/* per draft-rosen-idr-tunnel-encaps */
+struct bgp_tea_subtlv_remote_endpoint {
+    u_char family;               /* IPv4 or IPv6 */
+    union {
+        struct in_addr  v4;
+        struct in6_addr v6;
+    } ip_address;
+    as_t	        as4;     /* always 4 bytes */
+};
+
 /*
  * This is the length of the value part of the ipsec tunnel authenticator
  * subtlv. Currently we only support the length for authenticator type 1.
@@ -95,12 +106,18 @@
 #define BGP_TEA_SUBTLV_PROTO_TYPE	0x00000002
 #define BGP_TEA_SUBTLV_COLOR		0x00000004
 #define BGP_TEA_SUBTLV_IPSEC_TA		0x00000008
+#define BGP_TEA_SUBTLV_REMOTE_ENDPOINT	0x00000010
 
+#define CHECK_SUBTLV_FLAG(ptr, flag)  CHECK_FLAG((ptr)->valid_subtlvs, (flag))
+#define SET_SUBTLV_FLAG(ptr, flag)      SET_FLAG((ptr)->valid_subtlvs, (flag))
+#define UNSET_SUBTLV_FLAG(ptr, flag)  UNSET_FLAG((ptr)->valid_subtlvs, (flag))
 
 /*
  * Tunnel Type-specific APIs
  */
 struct bgp_encap_type_reserved {
+    uint32_t					valid_subtlvs;
+    struct bgp_tea_subtlv_remote_endpoint       st_endpoint;    /* optional */
 };
 
 struct bgp_encap_type_l2tpv3_over_ip {
@@ -108,6 +125,7 @@
     struct bgp_tea_subtlv_encap_l2tpv3_over_ip	st_encap;
     struct bgp_tea_subtlv_proto_type		st_proto;	/* optional */
     struct bgp_tea_subtlv_color			st_color;	/* optional */
+    struct bgp_tea_subtlv_remote_endpoint       st_endpoint;    /* optional */
 };
 
 struct bgp_encap_type_gre {
@@ -115,59 +133,79 @@
     struct bgp_tea_subtlv_encap_gre_key		st_encap;	/* optional */
     struct bgp_tea_subtlv_proto_type		st_proto;	/* optional */
     struct bgp_tea_subtlv_color			st_color;	/* optional */
+    struct bgp_tea_subtlv_remote_endpoint       st_endpoint;    /* optional */
 };
 
 struct bgp_encap_type_ip_in_ip {
     uint32_t					valid_subtlvs;
     struct bgp_tea_subtlv_proto_type		st_proto;	/* optional */
     struct bgp_tea_subtlv_color			st_color;	/* optional */
+    struct bgp_tea_subtlv_remote_endpoint       st_endpoint;    /* optional */
 };
 
 struct bgp_encap_type_transmit_tunnel_endpoint {
+    uint32_t					valid_subtlvs;
+    struct bgp_tea_subtlv_remote_endpoint       st_endpoint;    /* optional */
     /* No subtlvs defined in spec? */
 };
 
 struct bgp_encap_type_ipsec_in_tunnel_mode {
     uint32_t					valid_subtlvs;
     struct bgp_tea_subtlv_ipsec_ta		st_ipsec_ta;	/* optional */
+    struct bgp_tea_subtlv_remote_endpoint       st_endpoint;    /* optional */
 };
 
 struct bgp_encap_type_ip_in_ip_tunnel_with_ipsec_transport_mode {
     uint32_t					valid_subtlvs;
     struct bgp_tea_subtlv_ipsec_ta		st_ipsec_ta;	/* optional */
+    struct bgp_tea_subtlv_remote_endpoint       st_endpoint;    /* optional */
 };
 
 struct bgp_encap_type_mpls_in_ip_tunnel_with_ipsec_transport_mode {
     uint32_t					valid_subtlvs;
     struct bgp_tea_subtlv_ipsec_ta		st_ipsec_ta;	/* optional */
+    struct bgp_tea_subtlv_remote_endpoint       st_endpoint;    /* optional */
 };
 
 struct bgp_encap_type_vxlan {
+    uint32_t					valid_subtlvs;
+    struct bgp_tea_subtlv_remote_endpoint       st_endpoint;    /* optional */
     /* No subtlvs defined in spec? */
 };
 
 struct bgp_encap_type_nvgre {
+    uint32_t					valid_subtlvs;
+    struct bgp_tea_subtlv_remote_endpoint       st_endpoint;    /* optional */
     /* No subtlvs defined in spec? */
 };
 
 struct bgp_encap_type_mpls {
+    uint32_t					valid_subtlvs;
+    struct bgp_tea_subtlv_remote_endpoint       st_endpoint;    /* optional */
     /* No subtlvs defined in spec? */
 };
 
 struct bgp_encap_type_mpls_in_gre {
+    uint32_t					valid_subtlvs;
+    struct bgp_tea_subtlv_remote_endpoint       st_endpoint;    /* optional */
     /* No subtlvs defined in spec? */
 };
 
 struct bgp_encap_type_vxlan_gpe {
+    uint32_t					valid_subtlvs;
+    struct bgp_tea_subtlv_remote_endpoint       st_endpoint;    /* optional */
     /* No subtlvs defined in spec? */
 };
 
 struct bgp_encap_type_mpls_in_udp {
+    uint32_t					valid_subtlvs;
+    struct bgp_tea_subtlv_remote_endpoint       st_endpoint;    /* optional */
     /* No subtlvs defined in spec? */
 };
 
 struct bgp_encap_type_pbb {
     uint32_t					valid_subtlvs;
+    struct bgp_tea_subtlv_remote_endpoint       st_endpoint;    /* optional */
     struct bgp_tea_subtlv_encap_pbb		st_encap;
 };
 
diff --git a/bgpd/bgp_open.c b/bgpd/bgp_open.c
index 302e4ce..1c2ebd6 100644
--- a/bgpd/bgp_open.c
+++ b/bgpd/bgp_open.c
@@ -97,6 +97,9 @@
 	    case SAFI_MPLS_LABELED_VPN:
 	      vty_out (vty, "SAFI MPLS-labeled VPN");
 	      break;
+	    case SAFI_ENCAP:
+	      vty_out (vty, "SAFI ENCAP");
+	      break;
 	    default:
 	      vty_out (vty, "SAFI Unknown %d ", mpc.safi);
 	      break;
@@ -137,6 +140,7 @@
 	case SAFI_UNICAST:
 	case SAFI_MULTICAST:
 	case SAFI_MPLS_VPN:
+	case SAFI_ENCAP:
 	  return 1;
 	}
       break;
@@ -841,9 +845,11 @@
       if (! peer->afc_nego[AFI_IP][SAFI_UNICAST] 
 	  && ! peer->afc_nego[AFI_IP][SAFI_MULTICAST]
 	  && ! peer->afc_nego[AFI_IP][SAFI_MPLS_VPN]
+	  && ! peer->afc_nego[AFI_IP][SAFI_ENCAP]
 	  && ! peer->afc_nego[AFI_IP6][SAFI_UNICAST]
 	  && ! peer->afc_nego[AFI_IP6][SAFI_MULTICAST]
-	  && ! peer->afc_nego[AFI_IP6][SAFI_MPLS_VPN])
+	  && ! peer->afc_nego[AFI_IP6][SAFI_MPLS_VPN]
+	  && ! peer->afc_nego[AFI_IP6][SAFI_ENCAP])
 	{
 	  plog_err (peer->log, "%s [Error] Configured AFI/SAFIs do not "
 		    "overlap with received MP capabilities",
@@ -988,6 +994,18 @@
       stream_putc (s, 0);
       stream_putc (s, SAFI_MPLS_LABELED_VPN);
     }
+  /* ENCAP */
+  if (peer->afc[AFI_IP][SAFI_ENCAP])
+    {
+      peer->afc_adv[AFI_IP][SAFI_ENCAP] = 1;
+      stream_putc (s, BGP_OPEN_OPT_CAP);
+      stream_putc (s, CAPABILITY_CODE_MP_LEN + 2);
+      stream_putc (s, CAPABILITY_CODE_MP);
+      stream_putc (s, CAPABILITY_CODE_MP_LEN);
+      stream_putw (s, AFI_IP);
+      stream_putc (s, 0);
+      stream_putc (s, SAFI_ENCAP);
+    }
 #ifdef HAVE_IPV6
   /* IPv6 unicast. */
   if (peer->afc[AFI_IP6][SAFI_UNICAST])
@@ -1025,6 +1043,18 @@
       stream_putc (s, 0);
       stream_putc (s, SAFI_MPLS_LABELED_VPN);
     }
+  /* IPv6 ENCAP. */
+  if (peer->afc[AFI_IP6][SAFI_ENCAP])
+    {
+      peer->afc_adv[AFI_IP6][SAFI_ENCAP] = 1;
+      stream_putc (s, BGP_OPEN_OPT_CAP);
+      stream_putc (s, CAPABILITY_CODE_MP_LEN + 2);
+      stream_putc (s, CAPABILITY_CODE_MP);
+      stream_putc (s, CAPABILITY_CODE_MP_LEN);
+      stream_putw (s, AFI_IP6);
+      stream_putc (s, 0);
+      stream_putc (s, SAFI_ENCAP);
+    }
 #endif /* HAVE_IPV6 */
 
   /* Route refresh. */
diff --git a/bgpd/bgp_packet.c b/bgpd/bgp_packet.c
index 841eaab..1d18779 100644
--- a/bgpd/bgp_packet.c
+++ b/bgpd/bgp_packet.c
@@ -47,6 +47,7 @@
 #include "bgpd/bgp_ecommunity.h"
 #include "bgpd/bgp_network.h"
 #include "bgpd/bgp_mplsvpn.h"
+#include "bgpd/bgp_encap.h"
 #include "bgpd/bgp_advertise.h"
 #include "bgpd/bgp_vty.h"
 
@@ -212,7 +213,9 @@
 	  /* 5: Encode all the attributes, except MP_REACH_NLRI attr. */
 	  total_attr_len = bgp_packet_attribute (NULL, peer, s,
 	                                         adv->baa->attr,
-	                                         &rn->p, afi, safi,
+                                                 ((afi == AFI_IP && safi == SAFI_UNICAST) ?
+                                                  &rn->p : NULL),
+                                                 afi, safi,
 	                                         from, prd, tag);
 	}
 
@@ -1939,6 +1942,54 @@
 		  peer->host);
 	}
     }
+  if (peer->afc[AFI_IP][SAFI_ENCAP])
+    {
+      if (mp_update.length
+	  && mp_update.afi == AFI_IP
+	  && mp_update.safi == SAFI_ENCAP)
+	bgp_nlri_parse_encap (mp_update.afi, peer, &attr, &mp_update, 0);
+
+      if (mp_withdraw.length
+	  && mp_withdraw.afi == AFI_IP
+	  && mp_withdraw.safi == SAFI_ENCAP)
+	bgp_nlri_parse_encap (mp_withdraw.afi, peer, &attr, &mp_withdraw, 1);
+
+      if (! withdraw_len
+	  && mp_withdraw.afi == AFI_IP
+	  && mp_withdraw.safi == SAFI_MPLS_LABELED_VPN
+	  && mp_withdraw.length == 0)
+	{
+	  /* End-of-RIB received */
+
+	  if (BGP_DEBUG (update, UPDATE_IN))
+	    zlog (peer->log, LOG_DEBUG, "rcvd End-of-RIB for Encap Unicast from %s",
+		  peer->host);
+	}
+    }
+  if (peer->afc[AFI_IP6][SAFI_ENCAP])
+    {
+      if (mp_update.length 
+	  && mp_update.afi == AFI_IP6 
+	  && mp_update.safi == SAFI_ENCAP)
+	bgp_nlri_parse_encap (mp_update.afi, peer, &attr, &mp_update, 0);
+
+      if (mp_withdraw.length 
+	  && mp_withdraw.afi == AFI_IP6
+	  && mp_withdraw.safi == SAFI_ENCAP)
+	bgp_nlri_parse_encap (mp_withdraw.afi, peer, &attr, &mp_withdraw, 1);
+
+      if (! withdraw_len
+	  && mp_withdraw.afi == AFI_IP6
+	  && mp_withdraw.safi == SAFI_MPLS_LABELED_VPN
+	  && mp_withdraw.length == 0)
+	{
+	  /* End-of-RIB received */
+
+	  if (BGP_DEBUG (update, UPDATE_IN))
+	    zlog (peer->log, LOG_DEBUG, "rcvd End-of-RIB for Encap Unicast from %s",
+		  peer->host);
+	}
+    }
 
   /* Everything is done.  We unintern temporary structures which
      interned in bgp_attr_parse(). */
diff --git a/bgpd/bgp_route.c b/bgpd/bgp_route.c
index b024d80..a780e9d 100644
--- a/bgpd/bgp_route.c
+++ b/bgpd/bgp_route.c
@@ -71,7 +71,7 @@
   if (!table)
     return NULL;
   
-  if (safi == SAFI_MPLS_VPN)
+  if ((safi == SAFI_MPLS_VPN) || (safi == SAFI_ENCAP))
     {
       prn = bgp_node_get (table, (struct prefix *) prd);
 
@@ -84,7 +84,7 @@
 
   rn = bgp_node_get (table, p);
 
-  if (safi == SAFI_MPLS_VPN)
+  if ((safi == SAFI_MPLS_VPN) || (safi == SAFI_ENCAP))
     rn->prn = prn;
 
   return rn;
@@ -980,13 +980,24 @@
 	attr->flag &= ~(ATTR_FLAG_BIT (BGP_ATTR_MULTI_EXIT_DISC));
     }
 
+
+#define NEXTHOP_IS_V4 (\
+    (safi != SAFI_ENCAP && p->family == AF_INET) || \
+    (safi == SAFI_ENCAP && attr->extra->mp_nexthop_len == 4))
+
+#ifdef HAVE_IPV6
+#define NEXTHOP_IS_V6 (\
+    (safi != SAFI_ENCAP && p->family == AF_INET6) || \
+    (safi == SAFI_ENCAP && attr->extra->mp_nexthop_len == 16))
+#endif
+
   /* next-hop-set */
   if (transparent
       || (reflect && ! CHECK_FLAG (peer->af_flags[afi][safi], PEER_FLAG_NEXTHOP_SELF_ALL))
       || (CHECK_FLAG (peer->af_flags[afi][safi], PEER_FLAG_NEXTHOP_UNCHANGED)
-	  && ((p->family == AF_INET && attr->nexthop.s_addr)
+	  && ((NEXTHOP_IS_V4 && attr->nexthop.s_addr)
 #ifdef HAVE_IPV6
-	      || (p->family == AF_INET6 && 
+	      || (NEXTHOP_IS_V6 &&
                   ! IN6_IS_ADDR_UNSPECIFIED(&attr->extra->mp_nexthop_global))
 #endif /* HAVE_IPV6 */
 	      )))
@@ -994,18 +1005,18 @@
       /* NEXT-HOP Unchanged. */
     }
   else if (CHECK_FLAG (peer->af_flags[afi][safi], PEER_FLAG_NEXTHOP_SELF)
-	   || (p->family == AF_INET && attr->nexthop.s_addr == 0)
+	   || (NEXTHOP_IS_V4 && attr->nexthop.s_addr == 0)
 #ifdef HAVE_IPV6
-	   || (p->family == AF_INET6 && 
+	   || (NEXTHOP_IS_V6 &&
                IN6_IS_ADDR_UNSPECIFIED(&attr->extra->mp_nexthop_global))
 #endif /* HAVE_IPV6 */
 	   || (peer->sort == BGP_PEER_EBGP
 	       && bgp_multiaccess_check_v4 (attr->nexthop, peer->host) == 0))
     {
       /* Set IPv4 nexthop. */
-      if (p->family == AF_INET)
+      if (NEXTHOP_IS_V4)
 	{
-	  if (safi == SAFI_MPLS_VPN)
+	  if ((safi == SAFI_MPLS_VPN) || (safi == SAFI_ENCAP))
 	    memcpy (&attr->extra->mp_nexthop_global_in, &peer->nexthop.v4,
 	            IPV4_MAX_BYTELEN);
 	  else
@@ -1013,7 +1024,7 @@
 	}
 #ifdef HAVE_IPV6
       /* Set IPv6 nexthop. */
-      if (p->family == AF_INET6)
+      if (NEXTHOP_IS_V6)
 	{
 	  /* IPv6 global nexthop must be included. */
 	  memcpy (&attr->extra->mp_nexthop_global, &peer->nexthop.v6_global, 
@@ -1024,7 +1035,7 @@
     }
 
 #ifdef HAVE_IPV6
-  if (p->family == AF_INET6)
+  if (p->family == AF_INET6 && safi != SAFI_ENCAP)
     {
       /* Left nexthop_local unchanged if so configured. */ 
       if ( CHECK_FLAG (peer->af_flags[afi][safi], 
@@ -1214,7 +1225,7 @@
     /* Set IPv4 nexthop. */
     if (p->family == AF_INET)
       {
-        if (safi == SAFI_MPLS_VPN)
+        if ((safi == SAFI_MPLS_VPN) || (safi == SAFI_ENCAP))
           memcpy (&attr->extra->mp_nexthop_global_in, &rsclient->nexthop.v4,
                   IPV4_MAX_BYTELEN);
         else
@@ -2657,10 +2668,12 @@
   struct attr attr;
   struct attr_extra extra;
 
+  memset(&extra, 0, sizeof(extra));
+
   if (! table)
     table = (rsclient) ? peer->rib[afi][safi] : peer->bgp->rib[afi][safi];
 
-  if (safi != SAFI_MPLS_VPN
+  if ((safi != SAFI_MPLS_VPN) && (safi != SAFI_ENCAP)
       && CHECK_FLAG (peer->af_flags[afi][safi], PEER_FLAG_DEFAULT_ORIGINATE))
     bgp_default_originate (peer, afi, safi, 0);
 
@@ -2678,6 +2691,8 @@
 	  else
 	    bgp_adj_out_unset (rn, peer, &rn->p, afi, safi);
 	}
+
+  bgp_attr_flush_encap(&attr);
 }
 
 void
@@ -2696,7 +2711,7 @@
   if (CHECK_FLAG (peer->af_sflags[afi][safi], PEER_STATUS_ORF_WAIT_REFRESH))
     return;
 
-  if (safi != SAFI_MPLS_VPN)
+  if ((safi != SAFI_MPLS_VPN) && (safi != SAFI_ENCAP))
     bgp_announce_table (peer, afi, safi, NULL, 0);
   else
     for (rn = bgp_table_top (peer->bgp->rib[afi][safi]); rn;
@@ -2746,7 +2761,7 @@
   struct bgp_table *table;
   struct bgp_node *rn;
   
-  if (safi != SAFI_MPLS_VPN)
+  if ((safi != SAFI_MPLS_VPN) && (safi != SAFI_ENCAP))
     bgp_soft_reconfig_table_rsclient (rsclient, afi, safi, NULL, NULL);
 
   else
@@ -2805,7 +2820,7 @@
   if (peer->status != Established)
     return;
 
-  if (safi != SAFI_MPLS_VPN)
+  if ((safi != SAFI_MPLS_VPN) && (safi != SAFI_ENCAP))
     bgp_soft_reconfig_table (peer, afi, safi, NULL, NULL);
   else
     for (rn = bgp_table_top (peer->bgp->rib[afi][safi]); rn;
@@ -3022,7 +3037,7 @@
   switch (purpose)
     {
     case BGP_CLEAR_ROUTE_NORMAL:
-      if (safi != SAFI_MPLS_VPN)
+      if ((safi != SAFI_MPLS_VPN) && (safi != SAFI_ENCAP))
         bgp_clear_route_table (peer, afi, safi, NULL, NULL, purpose);
       else
         for (rn = bgp_table_top (peer->bgp->rib[afi][safi]); rn;
@@ -3197,13 +3212,27 @@
 	   */
 	  for (rn = bgp_table_top(bgp->rib[afi][SAFI_MPLS_VPN]); rn;
                rn = bgp_route_next (rn))
-	    if (rn->info)
+	    {
+	      if (rn->info)
                 {
-                   bgp_cleanup_table((struct bgp_table *)(rn->info), SAFI_MPLS_VPN);
-                   bgp_table_finish ((struct bgp_table **)&(rn->info));
-		   rn->info = NULL;
-		   bgp_unlock_node(rn);
+		  bgp_cleanup_table((struct bgp_table *)(rn->info), SAFI_MPLS_VPN);
+		  bgp_table_finish ((struct bgp_table **)&(rn->info));
+		  rn->info = NULL;
+		  bgp_unlock_node(rn);
                 }
+	    }
+
+	  for (rn = bgp_table_top(bgp->rib[afi][SAFI_ENCAP]); rn;
+               rn = bgp_route_next (rn))
+	    {
+	      if (rn->info)
+		{
+		  bgp_cleanup_table((struct bgp_table *)(rn->info), SAFI_ENCAP);
+		  bgp_table_finish ((struct bgp_table **)&(rn->info));
+		  rn->info = NULL;
+		  bgp_unlock_node(rn);
+		}
+	    }
 	}
     }
 }
@@ -3335,11 +3364,22 @@
 
   while (pnt < end)
     {
+      int	badlength;
       prefixlen = *pnt++;
       
       /* Prefix length check. */
-      if ((afi == AFI_IP && prefixlen > 32)
-	  || (afi == AFI_IP6 && prefixlen > 128))
+      badlength = 0;
+      if (safi == SAFI_ENCAP) {
+	if (prefixlen > 128)
+	  badlength = 1;
+      } else {
+        if ((afi == AFI_IP && prefixlen > 32) ||
+	    (afi == AFI_IP6 && prefixlen > 128)) {
+
+	    badlength = 1;
+	}
+      }
+      if (badlength)
 	{
 	  plog_err (peer->log, 
 		    "%s [Error] Update packet error (wrong prefix length %d)",
@@ -4082,7 +4122,7 @@
       for (rn = bgp_table_top (bgp->route[afi][safi]); rn; rn = bgp_route_next (rn))
 	if (rn->info != NULL)
 	  {      
-	    if (safi == SAFI_MPLS_VPN)
+	    if ((safi == SAFI_MPLS_VPN) || (safi == SAFI_ENCAP))
 	      {
 		table = rn->info;
 
@@ -4938,7 +4978,7 @@
   struct bgp_table *table;
 
   /* MPLS-VPN aggregation is not yet supported. */
-  if (safi == SAFI_MPLS_VPN)
+  if ((safi == SAFI_MPLS_VPN) || (safi == SAFI_ENCAP))
     return;
 
   table = bgp->aggregate[afi][safi];
@@ -4975,7 +5015,7 @@
   struct bgp_table *table;
 
   /* MPLS-VPN aggregation is not yet supported. */
-  if (safi == SAFI_MPLS_VPN)
+  if ((safi == SAFI_MPLS_VPN) || (safi == SAFI_ENCAP))
     return;
 
   table = bgp->aggregate[afi][safi];
@@ -5918,30 +5958,71 @@
   attr = binfo->attr;
   if (attr) 
     {
-      if (p->family == AF_INET)
-	{
-	  if (safi == SAFI_MPLS_VPN)
-	    vty_out (vty, "%-16s",
-                     inet_ntoa (attr->extra->mp_nexthop_global_in));
-	  else
-	    vty_out (vty, "%-16s", inet_ntoa (attr->nexthop));
-	}
-#ifdef HAVE_IPV6      
-      else if (p->family == AF_INET6)
-	{
-	  int len;
-	  char buf[BUFSIZ];
 
-	  len = vty_out (vty, "%s", 
-			 inet_ntop (AF_INET6, &attr->extra->mp_nexthop_global,
-			 buf, BUFSIZ));
-	  len = 16 - len;
-	  if (len < 1)
-	    vty_out (vty, "%s%*s", VTY_NEWLINE, 36, " ");
-	  else
-	    vty_out (vty, "%*s", len, " ");
+      /*
+       * NEXTHOP start
+       */
+
+      /*
+       * For ENCAP routes, nexthop address family is not
+       * neccessarily the same as the prefix address family.
+       * Both SAFI_MPLS_VPN and SAFI_ENCAP use the MP nexthop field
+       */
+      if ((safi == SAFI_ENCAP) || (safi == SAFI_MPLS_VPN)) {
+	if (attr->extra) {
+	    char	buf[BUFSIZ];
+	    int		af = NEXTHOP_FAMILY(attr->extra->mp_nexthop_len);
+
+	    switch (af) {
+		case AF_INET:
+		    vty_out (vty, "%s", inet_ntop(af,
+			&attr->extra->mp_nexthop_global_in, buf, BUFSIZ));
+		    break;
+#if HAVE_IPV6
+		case AF_INET6:
+		    vty_out (vty, "%s", inet_ntop(af,
+			&attr->extra->mp_nexthop_global, buf, BUFSIZ));
+		    break;
+#endif
+
+		default:
+		    vty_out(vty, "?");
+	    }
+	} else {
+	    vty_out(vty, "?");
 	}
+      } else {
+
+	  if (p->family == AF_INET)
+	    {
+		vty_out (vty, "%-16s", inet_ntoa (attr->nexthop));
+	    }
+#ifdef HAVE_IPV6      
+	  else if (p->family == AF_INET6)
+	    {
+	      int len;
+	      char buf[BUFSIZ];
+
+	      len = vty_out (vty, "%s",
+			     inet_ntop (AF_INET6, &attr->extra->mp_nexthop_global,
+			     buf, BUFSIZ));
+	      len = 16 - len;
+	      if (len < 1)
+		vty_out (vty, "%s%*s", VTY_NEWLINE, 36, " ");
+	      else
+		vty_out (vty, "%*s", len, " ");
+	    }
 #endif /* HAVE_IPV6 */
+         else
+	   {
+	     vty_out(vty, "?");
+	   }
+      }
+
+      /*
+       * NEXTHOP end
+       */
+
 
       if (attr->flag & ATTR_FLAG_BIT (BGP_ATTR_MULTI_EXIT_DISC))
 	vty_out (vty, "%10u", attr->med);
@@ -5983,7 +6064,7 @@
     {
       if (p->family == AF_INET)
 	{
-	  if (safi == SAFI_MPLS_VPN)
+	  if ((safi == SAFI_MPLS_VPN) || (safi == SAFI_ENCAP))
 	    vty_out (vty, "%-16s",
                      inet_ntoa (attr->extra->mp_nexthop_global_in));
 	  else
@@ -6056,7 +6137,7 @@
     {
       if (p->family == AF_INET)
 	{
-	  if (safi == SAFI_MPLS_VPN)
+	  if ((safi == SAFI_MPLS_VPN) || (safi == SAFI_ENCAP))
 	    vty_out (vty, "%-16s",
                      inet_ntoa (attr->extra->mp_nexthop_global_in));
 	  else
@@ -6239,7 +6320,7 @@
       /* Line2 display Next-hop, Neighbor, Router-id */
       if (p->family == AF_INET)
 	{
-	  vty_out (vty, "    %s", safi == SAFI_MPLS_VPN ?
+	  vty_out (vty, "    %s", ((safi == SAFI_MPLS_VPN) || (safi == SAFI_ENCAP)) ?
 		   inet_ntoa (attr->extra->mp_nexthop_global_in) :
 		   inet_ntoa (attr->nexthop));
 	}
@@ -6675,12 +6756,12 @@
   int no_advertise = 0;
   int local_as = 0;
   int first = 0;
+  int printrd = ((safi == SAFI_MPLS_VPN) || (safi == SAFI_ENCAP));
 
   p = &rn->p;
   vty_out (vty, "BGP routing table entry for %s%s%s/%d%s",
-	   (safi == SAFI_MPLS_VPN ?
-	   prefix_rd2str (prd, buf1, RD_ADDRSTRLEN) : ""),
-	   safi == SAFI_MPLS_VPN ? ":" : "",
+	   (printrd ?  prefix_rd2str (prd, buf1, RD_ADDRSTRLEN) : ""),
+	   printrd ?  ":" : "",
 	   inet_ntop (p->family, &p->u.prefix, buf2, INET6_ADDRSTRLEN),
 	   p->prefixlen, VTY_NEWLINE);
 
@@ -6766,7 +6847,7 @@
 
   match.family = afi2family (afi);
 
-  if (safi == SAFI_MPLS_VPN)
+  if ((safi == SAFI_MPLS_VPN) || (safi == SAFI_ENCAP))
     {
       for (rn = bgp_table_top (rib); rn; rn = bgp_route_next (rn))
         {
@@ -6790,12 +6871,12 @@
                       if (header)
                         {
                           route_vty_out_detail_header (vty, bgp, rm, (struct prefix_rd *)&rn->p,
-                                                       AFI_IP, SAFI_MPLS_VPN);
+                                                       AFI_IP, safi);
 
                           header = 0;
                         }
                       display++;
-                      route_vty_out_detail (vty, bgp, &rm->p, ri, AFI_IP, SAFI_MPLS_VPN);
+                      route_vty_out_detail (vty, bgp, &rm->p, ri, AFI_IP, safi);
                     }
 
                   bgp_unlock_node (rm);
@@ -9835,22 +9916,28 @@
                    afi_str, VTY_NEWLINE);
           return CMD_WARNING;
         }
-      if (strncmp (safi_str, "m", 1) == 0)
-        safi = SAFI_MULTICAST;
-      else if (strncmp (safi_str, "u", 1) == 0)
-        safi = SAFI_UNICAST;
-      else if (strncmp (safi_str, "vpnv4", 5) == 0 || strncmp (safi_str, "vpnv6", 5) == 0)
-        safi = SAFI_MPLS_LABELED_VPN;
-      else
-        {
-          vty_out (vty, "%% Invalid subsequent address family %s%s",
+      switch (safi_str[0]) {
+	case 'm':
+	    safi = SAFI_MULTICAST;
+	    break;
+	case 'u':
+	    safi = SAFI_UNICAST;
+	    break;
+	case 'v':
+	    safi =  SAFI_MPLS_LABELED_VPN;
+	    break;
+	case 'e':
+	    safi = SAFI_ENCAP;
+	    break;
+	default:
+	    vty_out (vty, "%% Invalid subsequent address family %s%s",
                    safi_str, VTY_NEWLINE);
-          return CMD_WARNING;
-        }
+            return CMD_WARNING;
+      }
     }
   else
     {
-      vty_out (vty, "%% Invalid address family %s%s",
+      vty_out (vty, "%% Invalid address family \"%s\"%s",
                afi_str, VTY_NEWLINE);
       return CMD_WARNING;
     }
@@ -9860,30 +9947,23 @@
 
 DEFUN (show_bgp_statistics,
        show_bgp_statistics_cmd,
-       "show bgp (ipv4|ipv6) (unicast|multicast) statistics",
+       "show bgp (ipv4|ipv6) (encap|multicast|unicast|vpn) statistics",
        SHOW_STR
        BGP_STR
        "Address family\n"
        "Address family\n"
        "Address Family modifier\n"
        "Address Family modifier\n"
+       "Address Family modifier\n"
+       "Address Family modifier\n"
        "BGP RIB advertisement statistics\n")
 {
   return bgp_table_stats_vty (vty, NULL, argv[0], argv[1]);
 }
 
-ALIAS (show_bgp_statistics,
-       show_bgp_statistics_vpnv4_cmd,
-       "show bgp (ipv4) (vpnv4) statistics",
-       SHOW_STR
-       BGP_STR
-       "Address family\n"
-       "Address Family modifier\n"
-       "BGP RIB advertisement statistics\n")
-
 DEFUN (show_bgp_statistics_view,
        show_bgp_statistics_view_cmd,
-       "show bgp view WORD (ipv4|ipv6) (unicast|multicast) statistics",
+       "show bgp view WORD (ipv4|ipv6) (encap|multicast|unicast|vpn) statistics",
        SHOW_STR
        BGP_STR
        "BGP view\n"
@@ -9891,20 +9971,24 @@
        "Address family\n"
        "Address Family modifier\n"
        "Address Family modifier\n"
+       "Address Family modifier\n"
+       "Address Family modifier\n"
        "BGP RIB advertisement statistics\n")
 {
   return bgp_table_stats_vty (vty, NULL, argv[0], argv[1]);
 }
 
+#if 0 /* added as options to above command */
 ALIAS (show_bgp_statistics_view,
-       show_bgp_statistics_view_vpnv4_cmd,
-       "show bgp view WORD (ipv4) (vpnv4) statistics",
+       show_bgp_statistics_view_encap_cmd,
+       "show bgp view WORD (ipv4) (encap) statistics",
        SHOW_STR
        BGP_STR
        "BGP view\n"
        "Address family\n"
        "Address Family modifier\n"
        "BGP RIB advertisement statistics\n")
+#endif
 
 enum bgp_pcounts
 {
@@ -12408,9 +12492,9 @@
 
   match.family = afi2family (afi);
 
-  if (safi == SAFI_MPLS_VPN)
+  if ((safi == SAFI_MPLS_VPN) || (safi == SAFI_ENCAP))
     {
-      for (rn = bgp_table_top (bgp->rib[AFI_IP][SAFI_MPLS_VPN]); rn; rn = bgp_route_next (rn))
+      for (rn = bgp_table_top (bgp->rib[AFI_IP][safi]); rn; rn = bgp_route_next (rn))
         {
           if (prd && memcmp (rn->p.u.val, prd->val, 8) != 0)
             continue;
@@ -12527,6 +12611,7 @@
 			       SAFI_UNICAST, NULL, 0);
 }
 
+/* also used for encap safi */
 static int
 bgp_config_write_network_vpnv4 (struct vty *vty, struct bgp *bgp,
 				afi_t afi, safi_t safi, int *write)
@@ -12578,7 +12663,7 @@
   struct bgp_aggregate *bgp_aggregate;
   char buf[SU_ADDRSTRLEN];
   
-  if (afi == AFI_IP && safi == SAFI_MPLS_VPN)
+  if (afi == AFI_IP && ((safi == SAFI_MPLS_VPN) || (safi == SAFI_ENCAP)))
     return bgp_config_write_network_vpnv4 (vty, bgp, afi, safi, write);
 
   /* Network configuration. */
@@ -13302,9 +13387,9 @@
   
   /* Statistics */
   install_element (ENABLE_NODE, &show_bgp_statistics_cmd);
-  install_element (ENABLE_NODE, &show_bgp_statistics_vpnv4_cmd);
+  //install_element (ENABLE_NODE, &show_bgp_statistics_vpnv4_cmd);
   install_element (ENABLE_NODE, &show_bgp_statistics_view_cmd);
-  install_element (ENABLE_NODE, &show_bgp_statistics_view_vpnv4_cmd);
+  //install_element (ENABLE_NODE, &show_bgp_statistics_view_vpnv4_cmd);
   
   /* old command */
   install_element (VIEW_NODE, &show_ipv6_bgp_cmd);
diff --git a/bgpd/bgp_vty.c b/bgpd/bgp_vty.c
index 041a6a1..e0f1c2b 100644
--- a/bgpd/bgp_vty.c
+++ b/bgpd/bgp_vty.c
@@ -4802,6 +4802,33 @@
        "Address Family Modifier\n"
        "Soft reconfig outbound update\n")
 
+DEFUN (clear_ip_bgp_all_encap_soft_out,
+       clear_ip_bgp_all_encap_soft_out_cmd,
+       "clear ip bgp * encap unicast soft out",
+       CLEAR_STR
+       IP_STR
+       BGP_STR
+       "Clear all peers\n"
+       "Address family\n"
+       "Address Family Modifier\n"
+       "Soft reconfig\n"
+       "Soft reconfig outbound update\n")
+{
+  return bgp_clear_vty (vty, NULL, AFI_IP, SAFI_ENCAP, clear_all,
+			BGP_CLEAR_SOFT_OUT, NULL);
+}
+
+ALIAS (clear_ip_bgp_all_encap_soft_out,
+       clear_ip_bgp_all_encap_out_cmd,
+       "clear ip bgp * encap unicast out",
+       CLEAR_STR
+       IP_STR
+       BGP_STR
+       "Clear all peers\n"
+       "Address family\n"
+       "Address Family Modifier\n"
+       "Soft reconfig outbound update\n")
+
 DEFUN (clear_bgp_all_soft_out,
        clear_bgp_all_soft_out_cmd,
        "clear bgp * soft out",
@@ -4940,6 +4967,33 @@
        "Address Family Modifier\n"
        "Soft reconfig outbound update\n")
 
+DEFUN (clear_ip_bgp_peer_encap_soft_out,
+       clear_ip_bgp_peer_encap_soft_out_cmd,
+       "clear ip bgp A.B.C.D encap unicast soft out",
+       CLEAR_STR
+       IP_STR
+       BGP_STR
+       "BGP neighbor address to clear\n"
+       "Address family\n"
+       "Address Family Modifier\n"
+       "Soft reconfig\n"
+       "Soft reconfig outbound update\n")
+{
+  return bgp_clear_vty (vty, NULL, AFI_IP, SAFI_ENCAP, clear_peer,
+			BGP_CLEAR_SOFT_OUT, argv[0]);
+}
+
+ALIAS (clear_ip_bgp_peer_encap_soft_out,
+       clear_ip_bgp_peer_encap_out_cmd,
+       "clear ip bgp A.B.C.D encap unicast out",
+       CLEAR_STR
+       IP_STR
+       BGP_STR
+       "BGP neighbor address to clear\n"
+       "Address family\n"
+       "Address Family Modifier\n"
+       "Soft reconfig outbound update\n")
+
 DEFUN (clear_bgp_peer_soft_out,
        clear_bgp_peer_soft_out_cmd,
        "clear bgp (A.B.C.D|X:X::X:X) soft out",
@@ -5267,6 +5321,33 @@
        "Address Family modifier\n"
        "Soft reconfig outbound update\n")
 
+DEFUN (clear_ip_bgp_as_encap_soft_out,
+       clear_ip_bgp_as_encap_soft_out_cmd,
+       "clear ip bgp " CMD_AS_RANGE " encap unicast soft out",
+       CLEAR_STR
+       IP_STR
+       BGP_STR
+       "Clear peers with the AS number\n"
+       "Address family\n"
+       "Address Family modifier\n"
+       "Soft reconfig\n"
+       "Soft reconfig outbound update\n")
+{
+  return bgp_clear_vty (vty, NULL, AFI_IP, SAFI_ENCAP, clear_as,
+			BGP_CLEAR_SOFT_OUT, argv[0]);
+}
+
+ALIAS (clear_ip_bgp_as_encap_soft_out,
+       clear_ip_bgp_as_encap_out_cmd,
+       "clear ip bgp " CMD_AS_RANGE " encap unicast out",
+       CLEAR_STR
+       IP_STR
+       BGP_STR
+       "Clear peers with the AS number\n"
+       "Address family\n"
+       "Address Family modifier\n"
+       "Soft reconfig outbound update\n")
+
 DEFUN (clear_bgp_as_soft_out,
        clear_bgp_as_soft_out_cmd,
        "clear bgp " CMD_AS_RANGE " soft out",
@@ -5503,6 +5584,33 @@
        "Address Family Modifier\n"
        "Soft reconfig inbound update\n")
 
+DEFUN (clear_ip_bgp_all_encap_soft_in,
+       clear_ip_bgp_all_encap_soft_in_cmd,
+       "clear ip bgp * encap unicast soft in",
+       CLEAR_STR
+       IP_STR
+       BGP_STR
+       "Clear all peers\n"
+       "Address family\n"
+       "Address Family Modifier\n"
+       "Soft reconfig\n"
+       "Soft reconfig inbound update\n")
+{
+  return bgp_clear_vty (vty, NULL, AFI_IP, SAFI_ENCAP, clear_all,
+			BGP_CLEAR_SOFT_IN, NULL);
+}
+
+ALIAS (clear_ip_bgp_all_encap_soft_in,
+       clear_ip_bgp_all_encap_in_cmd,
+       "clear ip bgp * encap unicast in",
+       CLEAR_STR
+       IP_STR
+       BGP_STR
+       "Clear all peers\n"
+       "Address family\n"
+       "Address Family Modifier\n"
+       "Soft reconfig inbound update\n")
+
 DEFUN (clear_bgp_all_soft_in,
        clear_bgp_all_soft_in_cmd,
        "clear bgp * soft in",
@@ -5699,6 +5807,33 @@
        "Address Family Modifier\n"
        "Soft reconfig inbound update\n")
 
+DEFUN (clear_ip_bgp_peer_encap_soft_in,
+       clear_ip_bgp_peer_encap_soft_in_cmd,
+       "clear ip bgp A.B.C.D encap unicast soft in",
+       CLEAR_STR
+       IP_STR
+       BGP_STR
+       "BGP neighbor address to clear\n"
+       "Address family\n"
+       "Address Family Modifier\n"
+       "Soft reconfig\n"
+       "Soft reconfig inbound update\n")
+{
+  return bgp_clear_vty (vty, NULL, AFI_IP, SAFI_ENCAP, clear_peer,
+			BGP_CLEAR_SOFT_IN, argv[0]);
+}
+
+ALIAS (clear_ip_bgp_peer_encap_soft_in,
+       clear_ip_bgp_peer_encap_in_cmd,
+       "clear ip bgp A.B.C.D encap unicast in",
+       CLEAR_STR
+       IP_STR
+       BGP_STR
+       "BGP neighbor address to clear\n"
+       "Address family\n"
+       "Address Family Modifier\n"
+       "Soft reconfig inbound update\n")
+
 DEFUN (clear_bgp_peer_soft_in,
        clear_bgp_peer_soft_in_cmd,
        "clear bgp (A.B.C.D|X:X::X:X) soft in",
@@ -6206,6 +6341,33 @@
        "Address Family modifier\n"
        "Soft reconfig inbound update\n")
 
+DEFUN (clear_ip_bgp_as_encap_soft_in,
+       clear_ip_bgp_as_encap_soft_in_cmd,
+       "clear ip bgp " CMD_AS_RANGE " encap unicast soft in",
+       CLEAR_STR
+       IP_STR
+       BGP_STR
+       "Clear peers with the AS number\n"
+       "Address family\n"
+       "Address Family modifier\n"
+       "Soft reconfig\n"
+       "Soft reconfig inbound update\n")
+{
+  return bgp_clear_vty (vty, NULL, AFI_IP, SAFI_ENCAP, clear_as,
+			BGP_CLEAR_SOFT_IN, argv[0]);
+}
+
+ALIAS (clear_ip_bgp_as_encap_soft_in,
+       clear_ip_bgp_as_encap_in_cmd,
+       "clear ip bgp " CMD_AS_RANGE " encap unicast in",
+       CLEAR_STR
+       IP_STR
+       BGP_STR
+       "Clear peers with the AS number\n"
+       "Address family\n"
+       "Address Family modifier\n"
+       "Soft reconfig inbound update\n")
+
 DEFUN (clear_bgp_as_soft_in,
        clear_bgp_as_soft_in_cmd,
        "clear bgp " CMD_AS_RANGE " soft in",
@@ -6356,6 +6518,21 @@
 			BGP_CLEAR_SOFT_BOTH, argv[0]);
 }
 
+DEFUN (clear_ip_bgp_all_encap_soft,
+       clear_ip_bgp_all_encap_soft_cmd,
+       "clear ip bgp * encap unicast soft",
+       CLEAR_STR
+       IP_STR
+       BGP_STR
+       "Clear all peers\n"
+       "Address family\n"
+       "Address Family Modifier\n"
+       "Soft reconfig\n")
+{
+  return bgp_clear_vty (vty, NULL, AFI_IP, SAFI_ENCAP, clear_all,
+			BGP_CLEAR_SOFT_BOTH, argv[0]);
+}
+
 DEFUN (clear_bgp_all_soft,
        clear_bgp_all_soft_cmd,
        "clear bgp * soft",
@@ -6439,6 +6616,21 @@
 			BGP_CLEAR_SOFT_BOTH, argv[0]);
 }
 
+DEFUN (clear_ip_bgp_peer_encap_soft,
+       clear_ip_bgp_peer_encap_soft_cmd,
+       "clear ip bgp A.B.C.D encap unicast soft",
+       CLEAR_STR
+       IP_STR
+       BGP_STR
+       "BGP neighbor address to clear\n"
+       "Address family\n"
+       "Address Family Modifier\n"
+       "Soft reconfig\n")
+{
+  return bgp_clear_vty (vty, NULL, AFI_IP, SAFI_ENCAP, clear_peer,
+			BGP_CLEAR_SOFT_BOTH, argv[0]);
+}
+
 DEFUN (clear_bgp_peer_soft,
        clear_bgp_peer_soft_cmd,
        "clear bgp (A.B.C.D|X:X::X:X) soft",
@@ -6622,6 +6814,21 @@
 			BGP_CLEAR_SOFT_BOTH, argv[0]);
 }
 
+DEFUN (clear_ip_bgp_as_encap_soft,
+       clear_ip_bgp_as_encap_soft_cmd,
+       "clear ip bgp " CMD_AS_RANGE " encap unicast soft",
+       CLEAR_STR
+       IP_STR
+       BGP_STR
+       "Clear peers with the AS number\n"
+       "Address family\n"
+       "Address Family Modifier\n"
+       "Soft reconfig\n")
+{
+  return bgp_clear_vty (vty, NULL, AFI_IP, SAFI_ENCAP, clear_as,
+			BGP_CLEAR_SOFT_BOTH, argv[0]);
+}
+
 DEFUN (clear_bgp_as_soft,
        clear_bgp_as_soft_cmd,
        "clear bgp " CMD_AS_RANGE " soft",
@@ -10029,6 +10236,12 @@
   install_element (ENABLE_NODE, &clear_ip_bgp_peer_vpnv4_in_cmd);
   install_element (ENABLE_NODE, &clear_ip_bgp_as_vpnv4_soft_in_cmd);
   install_element (ENABLE_NODE, &clear_ip_bgp_as_vpnv4_in_cmd);
+  install_element (ENABLE_NODE, &clear_ip_bgp_all_encap_soft_in_cmd);
+  install_element (ENABLE_NODE, &clear_ip_bgp_all_encap_in_cmd);
+  install_element (ENABLE_NODE, &clear_ip_bgp_peer_encap_soft_in_cmd);
+  install_element (ENABLE_NODE, &clear_ip_bgp_peer_encap_in_cmd);
+  install_element (ENABLE_NODE, &clear_ip_bgp_as_encap_soft_in_cmd);
+  install_element (ENABLE_NODE, &clear_ip_bgp_as_encap_in_cmd);
 #ifdef HAVE_IPV6
   install_element (ENABLE_NODE, &clear_bgp_all_soft_in_cmd);
   install_element (ENABLE_NODE, &clear_bgp_instance_all_soft_in_cmd);
@@ -10092,6 +10305,12 @@
   install_element (ENABLE_NODE, &clear_ip_bgp_peer_vpnv4_out_cmd);
   install_element (ENABLE_NODE, &clear_ip_bgp_as_vpnv4_soft_out_cmd);
   install_element (ENABLE_NODE, &clear_ip_bgp_as_vpnv4_out_cmd);
+  install_element (ENABLE_NODE, &clear_ip_bgp_all_encap_soft_out_cmd);
+  install_element (ENABLE_NODE, &clear_ip_bgp_all_encap_out_cmd);
+  install_element (ENABLE_NODE, &clear_ip_bgp_peer_encap_soft_out_cmd);
+  install_element (ENABLE_NODE, &clear_ip_bgp_peer_encap_out_cmd);
+  install_element (ENABLE_NODE, &clear_ip_bgp_as_encap_soft_out_cmd);
+  install_element (ENABLE_NODE, &clear_ip_bgp_as_encap_out_cmd);
 #ifdef HAVE_IPV6
   install_element (ENABLE_NODE, &clear_bgp_all_soft_out_cmd);
   install_element (ENABLE_NODE, &clear_bgp_instance_all_soft_out_cmd);
@@ -10132,6 +10351,9 @@
   install_element (ENABLE_NODE, &clear_ip_bgp_all_vpnv4_soft_cmd);
   install_element (ENABLE_NODE, &clear_ip_bgp_peer_vpnv4_soft_cmd);
   install_element (ENABLE_NODE, &clear_ip_bgp_as_vpnv4_soft_cmd);
+  install_element (ENABLE_NODE, &clear_ip_bgp_all_encap_soft_cmd);
+  install_element (ENABLE_NODE, &clear_ip_bgp_peer_encap_soft_cmd);
+  install_element (ENABLE_NODE, &clear_ip_bgp_as_encap_soft_cmd);
 #ifdef HAVE_IPV6
   install_element (ENABLE_NODE, &clear_bgp_all_soft_cmd);
   install_element (ENABLE_NODE, &clear_bgp_instance_all_soft_cmd);
diff --git a/bgpd/bgpd.c b/bgpd/bgpd.c
index 636f569..e70d337 100644
--- a/bgpd/bgpd.c
+++ b/bgpd/bgpd.c
@@ -36,6 +36,7 @@
 #include "plist.h"
 #include "linklist.h"
 #include "workqueue.h"
+#include "table.h"
 
 #include "bgpd/bgpd.h"
 #include "bgpd/bgp_table.h"
@@ -55,6 +56,7 @@
 #include "bgpd/bgp_nexthop.h"
 #include "bgpd/bgp_damp.h"
 #include "bgpd/bgp_mplsvpn.h"
+#include "bgpd/bgp_encap.h"
 #include "bgpd/bgp_advertise.h"
 #include "bgpd/bgp_network.h"
 #include "bgpd/bgp_vty.h"
@@ -974,12 +976,16 @@
 		  PEER_FLAG_REFLECTOR_CLIENT);
       UNSET_FLAG (peer->af_flags[AFI_IP][SAFI_MPLS_VPN],
 		  PEER_FLAG_REFLECTOR_CLIENT);
+      UNSET_FLAG (peer->af_flags[AFI_IP][SAFI_ENCAP],
+		  PEER_FLAG_REFLECTOR_CLIENT);
       UNSET_FLAG (peer->af_flags[AFI_IP6][SAFI_UNICAST],
 		  PEER_FLAG_REFLECTOR_CLIENT);
       UNSET_FLAG (peer->af_flags[AFI_IP6][SAFI_MULTICAST],
 		  PEER_FLAG_REFLECTOR_CLIENT);
       UNSET_FLAG (peer->af_flags[AFI_IP6][SAFI_MPLS_VPN],
 		  PEER_FLAG_REFLECTOR_CLIENT);
+      UNSET_FLAG (peer->af_flags[AFI_IP6][SAFI_ENCAP],
+		  PEER_FLAG_REFLECTOR_CLIENT);
     }
 
   /* local-as reset */
@@ -1398,9 +1404,11 @@
   if (peer->af_group[AFI_IP][SAFI_UNICAST]
       || peer->af_group[AFI_IP][SAFI_MULTICAST]
       || peer->af_group[AFI_IP][SAFI_MPLS_VPN]
+      || peer->af_group[AFI_IP][SAFI_ENCAP]
       || peer->af_group[AFI_IP6][SAFI_UNICAST]
       || peer->af_group[AFI_IP6][SAFI_MULTICAST]
-      || peer->af_group[AFI_IP6][SAFI_MPLS_VPN])
+      || peer->af_group[AFI_IP6][SAFI_MPLS_VPN]
+      || peer->af_group[AFI_IP6][SAFI_ENCAP])
     return 1;
   return 0;
 }
@@ -2356,9 +2364,11 @@
   if (peer->afc[AFI_IP][SAFI_UNICAST]
       || peer->afc[AFI_IP][SAFI_MULTICAST]
       || peer->afc[AFI_IP][SAFI_MPLS_VPN]
+      || peer->afc[AFI_IP][SAFI_ENCAP]
       || peer->afc[AFI_IP6][SAFI_UNICAST]
       || peer->afc[AFI_IP6][SAFI_MULTICAST]
-      || peer->afc[AFI_IP6][SAFI_MPLS_VPN])
+      || peer->afc[AFI_IP6][SAFI_MPLS_VPN]
+      || peer->afc[AFI_IP6][SAFI_ENCAP])
     return 1;
   return 0;
 }
@@ -2370,9 +2380,11 @@
   if (peer->afc_nego[AFI_IP][SAFI_UNICAST]
       || peer->afc_nego[AFI_IP][SAFI_MULTICAST]
       || peer->afc_nego[AFI_IP][SAFI_MPLS_VPN]
+      || peer->afc_nego[AFI_IP][SAFI_ENCAP]
       || peer->afc_nego[AFI_IP6][SAFI_UNICAST]
       || peer->afc_nego[AFI_IP6][SAFI_MULTICAST]
-      || peer->afc_nego[AFI_IP6][SAFI_MPLS_VPN])
+      || peer->afc_nego[AFI_IP6][SAFI_MPLS_VPN]
+      || peer->afc_nego[AFI_IP6][SAFI_ENCAP])
     return 1;
   return 0;
 }
@@ -5594,6 +5606,7 @@
   bgp_address_init ();
   bgp_scan_init ();
   bgp_mplsvpn_init ();
+  bgp_encap_init ();
 
   /* Access list initialize. */
   access_list_init ();