[quagga-dev 4946] merging AS4 support

Paul Jakma paul at clubi.ie
Mon Jul 2 14:26:00 BST 2007


Hi Juergen,

It'd be nice to start merging in your AS4 work to Quagga. I was sort 
of hoping this'd have been done by now, in particular that someone'd 
have done up a patch without the problematic asdot stuff..

Attached is a WIP diff of what I've got so far. I've:

- removed all the asdot stuff, obviously..
- redone the AS_PATH/NEW_AS_PATH reconciliation stuff,
   aspath_reconcile_as4
   - there are some basic tests added to aspath_test.c for htis
- fixed the bug you noted about AS_PATH length potentially being
   wrong. aspath_put now returns the length, and that gets written to
   the attribute header's length field.
- shifted the use32bit down as far as possible, so it's a detail the
   ASSEGMENT_LEN, etc.. macros take care of.
- I've made the AS_PATH attribute /always/ use extended-lengths, even
   for lengths < 255, it simplifies the code.
- Shuffled some code around (either for aesthetic reasons, or to
   simplify code)
- I've redone the ECOMMUNITY stuff, to remove the + syntax. I've not
   written tests for it yet though, so it'll probably blow up...
- I've not looked at the MRT stuff yet, it's not included in the
   patch.

Review/comment would be greatly appreciated (still WIP though!).

regards,
-- 
Paul Jakma	paul at clubi.ie	paul at jakma.org	Key ID: 64A2FF6A
Fortune:
"We came.  We saw.  We kicked its ass."
-- Bill Murray, _Ghostbusters_
-------------- next part --------------
diff --git a/bgpd/ChangeLog b/bgpd/ChangeLog
index b2191d4..8553e12 100644
--- a/bgpd/ChangeLog
+++ b/bgpd/ChangeLog
@@ -90,8 +90,19 @@
 	  address family header.
 	  (bgp_config_write) write out ipv6 multicast AF config.
 
-2007-02-22 Paul Jakma <paul.jakma at sun.com>
+2007-03-08 Juergen Kammer <j.kammer at eurodata.de>
 
+	* bgp_attr.c: some more code cleanup, rename new_aggregator to
+	  as4_aggregator, and new_aspath to as4_aspath
+	* bgp_attr.h: some more code cleanup, rename new_aggregator to
+	  as4_aggregator, and new_aspath to as4_aspath
+	* bgp_aspath.c: some more code cleanup, rename new_aggregator to
+	  as4_aggregator, and new_aspath to as4_aspath
+
+2007-03-07 Juergen Kammer <j.kammer at eurodata.de>
+
+	* these changes are from Paul, as of 2007-02-22 Paul Jakma
+	  <paul.jakma at sun.com> (rebase patch on cvs 20070307)
 	* bgp_fsm.c: (bgp_fsm_change_status) Handle state change into
 	  clearing or greater here. Simpler.
 	  (bgp_event) Clearing state change work moved to previous
@@ -107,6 +118,78 @@
 	  that exist and how they need to be dealt with.
 	  (bgp_clear_route) Update comment.
 
+2007-03-06 Juergen Kammer <j.kammer at eurodata.de>
+
+	* bgp_attr.c: import new mrt patch from Erik
+	* bgp_attr.h: import new mrt patch from Erik
+	* bgp_dump.c: import new mrt patch from Erik
+	* bgp_dump.h: import new mrt patch from Erik
+	* bgpd.h: import new mrt patch from Erik
+
+2007-03-05 Juergen Kammer <j.kammer at eurodata.de>
+
+	* bgp_ecommunity.c: Introduce a new syntax, if as4 ecommunity
+	  is to be used, there must be a '+' in front of the
+	  as number.  Thus the as number itself may be in any
+	  format (besides asip, but that is a joke anyway).
+
+2007-02-12 Juergen Kammer <j.kammer at eurodata.de>
+
+	* bgp_attr.c: Change all asn32 to AS4, change NEW_* to AS4_*
+	* bgp_aspath.c: Change all asn32 to AS4, change NEW_* to AS4_*
+	* bgp_vty.c: Change all asn32 to AS4, change NEW_* to AS4_*
+	* bgp_aspath.h: Change all asn32 to AS4, change NEW_* to AS4_*
+	* bgp_ecommunity.c: Change all asn32 to AS4, change NEW_* to AS4_*
+	* bgp_ecommunity.h: Change all asn32 to AS4, change NEW_* to AS4_*
+	* bgp_mplsvpn.c: Add missing include
+	* bgp_open.c: Change all asn32 to AS4, change NEW_* to AS4_*, add
+	  missing include
+	* bgp_open.h: Change all asn32 to AS4, change NEW_* to AS4_*
+	* bgp_packet.c: Change all asn32 to AS4, change NEW_* to AS4_*
+	* bgpd.c: Change all asn32 to AS4, change NEW_* to AS4_*
+	* bgp_debug.c: Change all asn32 to AS4, change NEW_* to AS4_*
+	* bgpd.h: Change all asn32 to AS4, change NEW_* to AS4_*
+	* bgp_debug.h: Change all asn32 to AS4, change NEW_* to AS4_*
+	* bgp_dump.c: Change all asn32 to AS4, change NEW_* to AS4_*
+
+2007-01-17 Juergen Kammer <j.kammer at eurodata.de>
+
+	* bgp_ecommunity.c: string representation of asn32 must be asdot+ if
+	  we use this systax.
+	* bgp_vty.c: ip extcommunity-list <1-99>|standard has no "ASN" at
+	  the place of the AS number in the CLI prompt.
+	* bgpd.c, bgp_open.c: fixed one speling mistake each in comments
+
+2007-01-15 Juergen Kammer <j.kammer at eurodata.de>
+
+	* Several changes to bgp_dump.[ch] to support asn32 compatible mrt dumps
+	  Thanks to Erik Romijn (RIPE NCC) for his help and testing efforts.
+
+2006-12-08 Juergen Kammer <j.kammer at eurodata.de>
+
+	* Many changes to support 4 byte asnumbers as defined in
+	  draft-ietf-idr-as4bytes-12.txt and to support as4octet
+	  extended communities as defined in
+	  draft-rekhter-as4octet-ext-community-01.txt
+	* bgp_aspath.c: Support asn32
+	* bgp_aspath.h: Support asn32
+	* bgp_attr.c: Support asn32
+	* bgp_attr.h: Support asn32
+	* bgp_debug.c: Support asn32, add 'debug asn32' and
+	  'debug asn32 segment'
+	* bgp_debug.h: Support asn32
+	* bgp_ecommunity.c: Support asn32, support ENCODE_AS4BYTE in extended
+	  communities
+	* bgp_mplsvpn.c: Support asn32
+	* bgp_open.c: Support asn32
+	* bgp_open.h: Support asn32
+	* bgp_packet.c: Support asn32
+	* bgp_route.c: Support asn32
+	* bgp_routemap.c: Support asn32
+	* bgp_vty.c: Support asn32
+	* bgpd.c: Support asn32
+	* bgpd.h: Support asn32
+
 2006-12-12 Andrew J. Schorr <ajschorr at alumni.princeton.edu>
 	
 	* bgp_nexthop.c: (bgp_connected_add, bgp_connected_delete)
diff --git a/bgpd/bgp_aspath.c b/bgpd/bgp_aspath.c
index b328e38..31fc543 100644
--- a/bgpd/bgp_aspath.c
+++ b/bgpd/bgp_aspath.c
@@ -28,15 +28,20 @@ Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
 #include "str.h"
 #include "log.h"
 #include "stream.h"
+#include "jhash.h"
 
 #include "bgpd/bgpd.h"
 #include "bgpd/bgp_aspath.h"
+#include "bgpd/bgp_debug.h"
+#include "bgpd/bgp_attr.h"
 
 /* Attr. Flags and Attr. Type Code. */
 #define AS_HEADER_SIZE        2	 
 
-/* Two octet is used for AS value. */
+/* Now FOUR octets are used for AS value. */
 #define AS_VALUE_SIZE         sizeof (as_t)
+/* This is the old one */
+#define AS16_VALUE_SIZE	      sizeof (as16_t)
 
 /* Maximum protocol segment length value */
 #define AS_SEGMENT_MAX		255
@@ -46,16 +51,20 @@ Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
  * sizes and lengths.  At present (200508) they sort of match, however
  * the ONLY functions which should now about the on-wire syntax are
  * aspath_put, assegment_put and assegment_parse.
+ *
+ * aspath_put returns bytes written, the only definitive record of
+ * size of wire-format attribute..
  */
 
 /* Calculated size in bytes of ASN segment data to hold N ASN's */
-#define ASSEGMENT_DATA_SIZE(N) ((N) * AS_VALUE_SIZE)
+#define ASSEGMENT_DATA_SIZE(N,S) \
+	((N) * ( (S) ? AS_VALUE_SIZE : AS16_VALUE_SIZE) )
 
 /* Calculated size of segment struct to hold N ASN's */
-#define ASSEGMENT_SIZE(N)  (AS_HEADER_SIZE + ASSEGMENT_DATA_SIZE (N))
+#define ASSEGMENT_SIZE(N,S)  (AS_HEADER_SIZE + ASSEGMENT_DATA_SIZE (N,S))
 
 /* AS segment octet length. */
-#define ASSEGMENT_LEN(X) ASSEGMENT_SIZE((X)->length)
+#define ASSEGMENT_LEN(X,S) ASSEGMENT_SIZE((X)->length,S)
 
 /* AS_SEQUENCE segments can be packed together */
 /* Can the types of X and Y be considered for packing? */
@@ -85,7 +94,7 @@ static struct stream *snmp_stream;
 static inline as_t *
 assegment_data_new (int num)
 {
-  return (XCALLOC (MTYPE_AS_SEG_DATA, ASSEGMENT_DATA_SIZE (num)));
+  return (XCALLOC (MTYPE_AS_SEG_DATA, ASSEGMENT_DATA_SIZE (num, 1)));
 }
 
 static inline void
@@ -150,7 +159,7 @@ assegment_dup (struct assegment *seg)
   struct assegment *new;
   
   new = assegment_new (seg->type, seg->length);
-  memcpy (new->as, seg->as, ASSEGMENT_DATA_SIZE (new->length) );
+  memcpy (new->as, seg->as, ASSEGMENT_DATA_SIZE (new->length, 1) );
     
   return new;
 }
@@ -197,7 +206,7 @@ assegment_prepend_asns (struct assegment *seg, as_t asnum, int num)
       for (i = 0; i < num; i++)
         newas[i] = asnum;
       
-      memcpy (newas + num, seg->as, ASSEGMENT_DATA_SIZE (seg->length));
+      memcpy (newas + num, seg->as, ASSEGMENT_DATA_SIZE (seg->length, 1));
       XFREE (MTYPE_AS_SEG_DATA, seg->as);
       seg->as = newas; 
       seg->length += num;
@@ -215,12 +224,12 @@ assegment_append_asns (struct assegment *seg, as_t *asnos, int num)
   as_t *newas;
   
   newas = XREALLOC (MTYPE_AS_SEG_DATA, seg->as,
-		      ASSEGMENT_DATA_SIZE (seg->length + num));
+		      ASSEGMENT_DATA_SIZE (seg->length + num, 1));
 
   if (newas)
     {
       seg->as = newas;
-      memcpy (seg->as + seg->length, asnos, ASSEGMENT_DATA_SIZE(num));
+      memcpy (seg->as + seg->length, asnos, ASSEGMENT_DATA_SIZE(num, 1));
       seg->length += num;
       return seg;
     }
@@ -420,6 +429,12 @@ aspath_count_hops (struct aspath *aspath)
   return count;
 }
 
+/* Estimate size aspath /might/ take if encoded into an
+ * ASPATH attribute.
+ *
+ * This is a quick estimate, not definitive! aspath_put()
+ * may return a different number!!
+ */
 unsigned int
 aspath_size (struct aspath *aspath)
 {
@@ -428,7 +443,7 @@ aspath_size (struct aspath *aspath)
   
   while (seg)
     {
-      size += ASSEGMENT_SIZE(seg->length);
+      size += ASSEGMENT_SIZE(seg->length, 1);
       seg = seg->next;
     }
   return size;
@@ -454,6 +469,40 @@ aspath_highest (struct aspath *aspath)
   return highest;
 }
 
+/* Return number of 32bit as numbers in in path */
+unsigned int
+aspath_count_num32as (struct aspath *aspath)
+{
+  struct assegment *seg = aspath->segments;
+  unsigned int i, num;
+  
+  num=0;
+  while (seg)
+    {
+      for (i = 0; i < seg->length; i++)
+        if (seg->as[i] > BGP_AS_MAX)
+	  num++;
+      seg = seg->next;
+    }
+  return num;
+}
+
+/* Return number of as numbers in in path */
+unsigned int
+aspath_count_numas (struct aspath *aspath)
+{
+  struct assegment *seg = aspath->segments;
+  unsigned int num;
+  
+  num=0;
+  while (seg)
+    {
+      num += seg->length;
+      seg = seg->next;
+    }
+  return num;
+}
+
 /* Convert aspath structure to string expression. */
 static char *
 aspath_make_str_count (struct aspath *as)
@@ -478,6 +527,9 @@ aspath_make_str_count (struct aspath *as)
    * 2 chars for segment delimiters, and the final '\0'.
    * Hopefully this is large enough to avoid hitting the realloc
    * code below for most common sequences.
+   *
+   * With 32bit ASNs, this range will increase, but only worth changing
+   * once there are significant numbers of ASN >= 100000
    */
 #define ASN_STR_LEN (5 + 1)
   str_size = MAX (assegment_count_asns (seg, 0) * ASN_STR_LEN + 2 + 1,
@@ -510,6 +562,9 @@ aspath_make_str_count (struct aspath *as)
        * have been wrong.  need 5 chars for ASN, a seperator each and
        * potentially two segment delimiters, plus a space between each
        * segment and trailing zero.
+       *
+       * This may need to revised if/when significant numbers of
+       * ASNs >= 100000 are assigned and in-use on the internet...
        */
 #define SEGMENT_STR_LEN(X) (((X)->length * ASN_STR_LEN) + 2 + 1 + 1)
       if ( (len + SEGMENT_STR_LEN(seg)) > str_size)
@@ -528,7 +583,7 @@ aspath_make_str_count (struct aspath *as)
       /* write out the ASNs, with their seperators, bar the last one*/
       for (i = 0; i < seg->length; i++)
         {
-          len += snprintf (str_buf + len, str_size - len, "%u", seg->as[i]);
+          len += snprintf( str_buf + len, str_size - len, "%u", seg->as[i]);
           
           if (i < (seg->length - 1))
             len += snprintf (str_buf + len, str_size - len, "%c", seperator);
@@ -605,7 +660,7 @@ aspath_hash_alloc (void *arg)
 {
   struct aspath *aspath;
 
-  /* New aspath strucutre is needed. */
+  /* New aspath structure is needed. */
   aspath = aspath_dup (arg);
   
   /* Malformed AS path value. */
@@ -620,7 +675,7 @@ aspath_hash_alloc (void *arg)
 
 /* parse as-segment byte stream in struct assegment */
 static struct assegment *
-assegments_parse (struct stream *s, size_t length)
+assegments_parse (struct stream *s, size_t length, int use32bit)
 {
   struct assegment_header segh;
   struct assegment *seg, *prev = NULL, *head = NULL;
@@ -630,23 +685,33 @@ assegments_parse (struct stream *s, size_t length)
   if (length == 0)
     return NULL;
   
+  if (BGP_DEBUG (as4, AS4_SEGMENT))
+    zlog_debug ("[AS4SEG] Parse aspath segment: got total byte length %lu",
+		(unsigned long) length);
   /* basic checks */
   if ( (STREAM_READABLE(s) < length)
       || (STREAM_READABLE(s) < AS_HEADER_SIZE) 
-      || (length % AS_VALUE_SIZE))
+      || (length % AS16_VALUE_SIZE ))
     return NULL;
   
   while ( (STREAM_READABLE(s) > AS_HEADER_SIZE)
          && (bytes < length))
     {
       int i;
+      int seg_size;
       
       /* softly softly, get the header first on its own */
       segh.type = stream_getc (s);
       segh.length = stream_getc (s);
       
+      seg_size = ASSEGMENT_SIZE(segh.length, use32bit);
+
+      if (BGP_DEBUG (as4, AS4_SEGMENT))
+	zlog_debug ("[AS4SEG] Parse aspath segment: got type %d, length %d",
+                    segh.type, segh.length);
+      
       /* check it.. */
-      if ( ((bytes + ASSEGMENT_SIZE(segh.length)) > length)
+      if ( ((bytes + seg_size) > length)
           /* 1771bis 4.3b: seg length contains one or more */
           || (segh.length == 0) 
           /* Paranoia in case someone changes type of segment length */
@@ -666,9 +731,12 @@ assegments_parse (struct stream *s, size_t length)
         head = prev = seg;
       
       for (i = 0; i < segh.length; i++)
-        seg->as[i] = stream_getw (s);
+	seg->as[i] = (use32bit) ? stream_getl (s) : stream_getw (s);
+
+      bytes += seg_size;
       
-      bytes += ASSEGMENT_SIZE(segh.length);
+      if (BGP_DEBUG (as4, AS4_SEGMENT))
+	zlog_debug ("[AS4SEG] Parse aspath segment: Bytes now: %lu", bytes);
       
       prev = seg;
     }
@@ -680,16 +748,22 @@ assegments_parse (struct stream *s, size_t length)
    is length of byte stream.  If there is same AS path in the the AS
    path hash then return it else make new AS path structure. */
 struct aspath *
-aspath_parse (struct stream *s, size_t length)
+aspath_parse (struct stream *s, size_t length, int use32bit)
 {
   struct aspath as;
   struct aspath *find;
 
   /* If length is odd it's malformed AS path. */
-  if (length % AS_VALUE_SIZE)
+  /* Nit-picking: if (use32bit == 0) it is malformed if odd,
+   * otherwise its malformed when length is larger than 2 and (length-2) 
+   * is not dividable by 4.
+   * But... this time we're lazy
+   */
+  if (length % AS16_VALUE_SIZE )
     return NULL;
 
-  as.segments = assegments_parse (s, length);
+  memset (&as, 0, sizeof (struct aspath));
+  as.segments = assegments_parse (s, length, use32bit);
   
   /* If already same aspath exist then return it. */
   find = hash_get (ashash, &as, aspath_hash_alloc);
@@ -707,13 +781,21 @@ aspath_parse (struct stream *s, size_t length)
 }
 
 static inline void
-assegment_data_put (struct stream *s, as_t *as, int num)
+assegment_data_put (struct stream *s, as_t *as, int num, int use32bit)
 {
   int i;
   assert (num <= AS_SEGMENT_MAX);
   
   for (i = 0; i < num; i++)
-    stream_putw (s, as[i]);
+    if ( use32bit )
+      stream_putl (s, as[i]);
+    else
+      {
+        if ( as[i] <= BGP_AS_MAX )
+	  stream_putw(s, as[i]);
+	else
+	  stream_putw(s, BGP_AS_TRANS);
+      }
 }
 
 static inline size_t
@@ -728,38 +810,51 @@ assegment_header_put (struct stream *s, u_char type, int length)
 }
 
 /* write aspath data to stream */
-void
-aspath_put (struct stream *s, struct aspath *as)
+size_t
+aspath_put (struct stream *s, struct aspath *as, int use32bit )
 {
   struct assegment *seg = as->segments;
+  size_t bytes = 0;
   
   if (!seg || seg->length == 0)
-    return;
+    return 0;
   
   if (seg)
     {
-      while (seg && (ASSEGMENT_LEN (seg) <= STREAM_WRITEABLE(s)))
+      /*
+       * Hey, what do we do when we have > STREAM_WRITABLE(s) here?
+       * At the moment, we would write out a partial aspath, and our peer
+       * will complain and drop the session :-/
+       *
+       * The general assumption here is that many things tested will
+       * never happen.  And, in real live, up to now, they have not.
+       */
+      while (seg && (ASSEGMENT_LEN(seg, use32bit) <= STREAM_WRITEABLE(s)))
         {
+	  struct assegment *next = seg->next;
           int written = 0;
+          int asns_packed = 0;
           size_t lenp;
           
           /* Overlength segments have to be split up */
           while ( (seg->length - written) > AS_SEGMENT_MAX)
             {
               assegment_header_put (s, seg->type, AS_SEGMENT_MAX);
-              assegment_data_put (s, seg->as, AS_SEGMENT_MAX);
+              assegment_data_put (s, seg->as, AS_SEGMENT_MAX, use32bit);
               written += AS_SEGMENT_MAX;
+              bytes += ASSEGMENT_SIZE (written, use32bit);
             }
           
           /* write the final segment, probably is also the first */
           lenp = assegment_header_put (s, seg->type, seg->length - written);
-          assegment_data_put (s, (seg->as + written), seg->length - written);
+          assegment_data_put (s, (seg->as + written), seg->length - written, 
+                              use32bit);
           
           /* Sequence-type segments can be 'packed' together
            * Case of a segment which was overlength and split up
            * will be missed here, but that doesn't matter.
            */
-          if (seg->next && ASSEGMENTS_PACKABLE (seg, seg->next))
+          while (next && ASSEGMENTS_PACKABLE (seg, next))
             {
               /* NB: We should never normally get here given we
                * normalise aspath data when parse them. However, better
@@ -771,17 +866,21 @@ aspath_put (struct stream *s, struct aspath *as)
                */
               
               /* Next segment's data can fit in this one */
-              assegment_data_put (s, seg->next->as, seg->next->length);
+              assegment_data_put (s, next->as, next->length, use32bit);
               
               /* update the length of the segment header */
-	      stream_putc_at (s, lenp, 
-	                      seg->length - written + seg->next->length);
-	      seg = seg->next->next; /* skip to past next */
+	      stream_putc_at (s, lenp, seg->length - written + next->length);
+              asns_packed += next->length;
+               
+	      next = next->next;
 	    }
-          else
-            seg = seg->next;
+          
+          bytes += ASSEGMENT_SIZE (seg->length - written + asns_packed, 
+				   use32bit);
+          seg = next;
         }
     }
+  return bytes;
 }
 
 /* This is for SNMP BGP4PATHATTRASPATHSEGMENT
@@ -803,7 +902,7 @@ aspath_snmp_pathseg (struct aspath *as, size_t *varlen)
       *varlen = 0;
       return NULL;
     }
-  aspath_put (snmp_stream, as);
+  aspath_put (snmp_stream, as, 0); /* use 16 bit for now here */
   
   *varlen = stream_get_endp (snmp_stream);
   return stream_pnt(snmp_stream);
@@ -861,8 +960,9 @@ aspath_aggregate (struct aspath *as1, struct aspath *as2)
   int from;
   struct assegment *seg1 = as1->segments;
   struct assegment *seg2 = as2->segments;
-  struct aspath *aspath;
+  struct aspath *aspath = NULL;
   struct assegment *asset;
+  struct assegment *prevseg = NULL;
 
   match = 0;
   minlen = 0;
@@ -871,7 +971,7 @@ aspath_aggregate (struct aspath *as1, struct aspath *as2)
 
   /* First of all check common leading sequence. */
   while (seg1 && seg2)
-    {
+    {      
       /* Check segment type. */
       if (seg1->type != seg2->type)
 	break;
@@ -885,11 +985,19 @@ aspath_aggregate (struct aspath *as1, struct aspath *as2)
 
       if (match)
 	{
+	  struct assegment *seg = assegment_new (seg1->type, 0);
+	  
+	  seg = assegment_append_asns (seg, seg1->as, match);
+
 	  if (! aspath)
-	    aspath = aspath_new ();
-	  aspath->segments = assegment_new (seg1->type, 0);
-	  aspath->segments = assegment_append_asns (aspath->segments, 
-	                                            seg1->as, match);
+	    {
+	      aspath = aspath_new ();
+	      aspath->segments = seg;
+	     }
+	  else
+	    prevseg->next = seg;
+	  
+	  prevseg = seg;
 	}
 
       if (match != minlen || match != seg1->length 
@@ -1174,6 +1282,116 @@ aspath_cmp_left (struct aspath *aspath1, struct aspath *aspath2)
   return 0;
 }
 
+/* Truncate an aspath after a number of hops, and put the hops remaining
+ * at the front of another aspath.  Needed for AS4 compat.
+ *
+ * Returned aspath is a /new/ aspath, which should either by free'd or
+ * interned by the caller, as desired.
+ */
+struct aspath *
+aspath_reconcile_as4 ( struct aspath *aspath, struct aspath *as4path)
+{
+  struct assegment *seg, *newseg, *prevseg = NULL;
+  struct aspath *newpath = NULL;
+  int hops, cpasns = 0;
+  
+  if (!aspath)
+    return NULL;
+  
+  seg = aspath->segments;
+  
+  /* CONFEDs should get reconciled too.. */
+  hops = (aspath_count_hops (aspath) + aspath_count_confeds (aspath))
+         - aspath_count_hops (as4path);
+  
+  printf ("%s: total hops: %d\n", __func__, hops);
+  
+  if (hops < 0)
+    {
+      if (BGP_DEBUG (as4, AS4))
+        zlog_debug ("[AS4] Fewer hops in AS_PATH than NEW_AS_PATH");
+      return aspath_dup (aspath);
+    }
+  
+  if (!hops)
+   return aspath_dup (as4path);
+  
+  if ( BGP_DEBUG(as4, AS4))
+    zlog_debug("[AS4] got AS_PATH %s and AS4_PATH %s synthesizing now",
+               aspath->str, as4path->str);
+
+  while (seg && hops > 0)
+    {
+      switch (seg->type)
+        {
+          case AS_SET:
+          case AS_CONFED_SET:
+            hops--;
+            cpasns = seg->length;
+            /*printf ("Set: hops %d, cpasns: %d, seg->type %d, seg->as[usehops-1]: %x\n",
+	             hops, cpasns, seg->type, seg->as[cpasns-1]);*/
+            break;
+          case AS_CONFED_SEQUENCE:
+	    /* Should never split a confed-sequence, if hop-count
+	     * suggests we must then something's gone wrong somewhere.
+	     *
+	     * Most important goal is to preserve AS_PATHs prime function
+	     * as loop-detector, so we fudge the numbers so that the entire
+	     * confed-sequence is merged in.
+	     */
+	    if (hops < seg->length)
+	      {
+	      	printf ("confseq up.. hops %d, seg->length: %d\n",
+	      	        hops, seg->length);
+	        if (BGP_DEBUG (as4, AS4))
+	          zlog_debug ("[AS4] AS4PATHmangle: AS_CONFED_SEQUENCE falls"
+	                      " across 2/4 ASN boundary somewhere, broken..");
+	        hops = seg->length;
+	      }
+	  case AS_SEQUENCE:
+	    cpasns = MIN(seg->length, hops);
+	    hops -= seg->length;
+	}
+      
+      assert (cpasns <= seg->length);
+      
+      newseg = assegment_new (seg->type, 0);
+      newseg = assegment_append_asns (newseg, seg->as, cpasns);
+    /*printf ("hops %d, cpasns: %d, seg->type %d, seg->as[cpasns-1]: %d\n",
+	     hops, cpasns, seg->type, seg->as[cpasns-1]);
+      printf ("newseg: type %d, len: %d, as[len-1] %d\n",
+              newseg->type, newseg->length, newseg->as[newseg->length-1]);
+      */
+      if (!newpath)
+        {
+          newpath = aspath_new ();
+          newpath->segments = newseg;
+        }
+      else
+        prevseg->next = newseg;
+
+      prevseg = newseg;
+      seg = seg->next;
+    }
+    
+  /* We may be able to join some segments here, and we must
+   * do this because... we want normalised aspaths in out hash
+   * and we do not want to stumble in aspath_put.
+   */
+  
+  newpath = aspath_merge (newpath, aspath_dup(as4path));
+  newpath->segments = assegment_normalise (newpath->segments);
+  aspath_str_update (newpath);
+  
+  //newpath = aspath_intern (newpath);
+
+  if ( BGP_DEBUG(as4, AS4))
+    zlog_debug ("[AS4] result of synthesizing is %s",
+                newpath->str);
+  
+  return newpath;
+}
+
 /* Compare leftmost AS value for MED check.  If as1's leftmost AS and
    as2's leftmost AS is same return 1. (confederation as-path
    only).  */
@@ -1229,6 +1447,59 @@ aspath_delete_confed_seq (struct aspath *aspath)
   return aspath;
 }
 
+/* get rid of all AS_CONFED_SEQUENCE and AS_CONFED_SET path segments
+ * in an aspath
+ */
+void
+aspath_cleanoutall_asconfeds (struct aspath **aspath)
+{
+  struct assegment *seg, **ptto;
+  struct aspath *cleanedout;
+  int didsomething = 0;
+
+  if (!(*aspath && (*aspath)->segments))
+    return;
+
+  cleanedout = aspath_dup( *aspath );
+  ptto = &cleanedout->segments;
+  seg = *ptto;
+
+  while (seg)
+    {
+      if (seg->type == AS_CONFED_SEQUENCE || seg->type == AS_CONFED_SET)
+	{
+	  /* ignore this path segment */
+	  struct assegment *ignorethis;
+	  ignorethis = seg;
+
+	  seg = ignorethis->next;
+	  *ptto = seg;
+
+          assegment_free (ignorethis);
+	  didsomething = 1;
+	}
+      else
+	{
+	  ptto = &seg->next;
+          seg = *ptto;
+	}
+    }
+  if ( didsomething )
+    {
+      /* update necessary things */
+      /* sigh, have to normalise - otherwise we can not advertise
+       * the result due to aspath_put merging of segments
+       */
+      cleanedout->segments = assegment_normalise(cleanedout->segments);
+      aspath_str_update (cleanedout);
+      *aspath = cleanedout;
+    }
+  else
+    {
+      aspath_free( cleanedout );
+    }
+}
+
 /* Add new AS number to the leftmost part of the aspath as
    AS_CONFED_SEQUENCE.  */
 struct aspath*
@@ -1273,7 +1544,7 @@ aspath_segment_add (struct aspath *as, int type)
 struct aspath *
 aspath_empty (void)
 {
-  return aspath_parse (NULL, 0);
+  return aspath_parse (NULL, 0, 1); /* 32Bit ;-) */
 }
 
 struct aspath *
@@ -1316,7 +1587,7 @@ enum as_token
 
 /* Return next token and point for string parse. */
 static const char *
-aspath_gettoken (const char *buf, enum as_token *token, u_short *asno)
+aspath_gettoken (const char *buf, enum as_token *token, u_long *asno)
 {
   const char *p = buf;
 
@@ -1360,12 +1631,13 @@ aspath_gettoken (const char *buf, enum as_token *token, u_short *asno)
   if (isdigit ((int) *p)) 
     {
       u_short asval;
-
+      
       *token = as_token_asval;
       asval = (*p - '0');
       p++;
+      
       while (isdigit ((int) *p)) 
-	{
+        {
 	  asval *= 10;
 	  asval += (*p - '0');
 	  p++;
@@ -1384,7 +1656,7 @@ aspath_str2aspath (const char *str)
 {
   enum as_token token = as_token_unknown;
   u_short as_type;
-  u_short asno = 0;
+  u_long asno = 0;
   struct aspath *aspath;
   int needtype;
 
@@ -1451,24 +1723,11 @@ aspath_key_make (void *p)
 {
   struct aspath * aspath = (struct aspath *) p;
   unsigned int key = 0;
-  unsigned int i;
-  struct assegment *seg = aspath->segments;
-  struct assegment *prev = NULL;
 
-  while (seg)
-    {
-      /* segment types should be part of the hash
-       * otherwise seq(1) and set(1) will hash to same value
-       */
-      if (!(prev && seg->type == AS_SEQUENCE && seg->type == prev->type))
-        key += seg->type;
-      
-      for (i = 0; i < seg->length; i++)
-	key += seg->as[i];
-      
-      prev = seg;
-      seg = seg->next;
-    }
+  if (!aspath->str)
+    aspath_str_update (aspath);
+  
+  key = jhash (aspath->str, strlen(aspath->str), 2334325);
 
   return key;
 }
diff --git a/bgpd/bgp_aspath.h b/bgpd/bgp_aspath.h
index efec24c..8ca5ca7 100644
--- a/bgpd/bgp_aspath.h
+++ b/bgpd/bgp_aspath.h
@@ -31,7 +31,11 @@ Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
 #define BGP_PRIVATE_AS_MIN       64512U
 #define BGP_PRIVATE_AS_MAX       65535U
 
-#define BGP_AS_MAX		 65535U
+/* we leave BGP_AS_MAX as the 16bit AS MAX number.  */
+#define BGP_AS_MAX		     65535U
+#define BGP_AS4_MAX		4294967295U
+/* Transition 16Bit AS as defined by IANA */
+#define BGP_AS_TRANS		 23456U
 
 /* AS_PATH segment data in abstracted form, no limit is placed on length */
 struct assegment
@@ -61,7 +65,7 @@ struct aspath
 /* Prototypes. */
 extern void aspath_init (void);
 extern void aspath_finish (void);
-extern struct aspath *aspath_parse (struct stream *, size_t);
+extern struct aspath *aspath_parse (struct stream *, size_t, int);
 extern struct aspath *aspath_dup (struct aspath *);
 extern struct aspath *aspath_aggregate (struct aspath *, struct aspath *);
 extern struct aspath *aspath_prepend (struct aspath *, struct aspath *);
@@ -88,7 +92,12 @@ extern unsigned int aspath_count_hops (struct aspath *);
 extern unsigned int aspath_count_confeds (struct aspath *);
 extern unsigned int aspath_size (struct aspath *);
 extern as_t aspath_highest (struct aspath *);
-extern void aspath_put (struct stream *, struct aspath *);
+extern size_t aspath_put (struct stream *, struct aspath *, int);
+
+extern struct aspath *aspath_reconcile_as4 (struct aspath *, struct aspath *);
+extern unsigned int aspath_count_num32as (struct aspath *);
+extern unsigned int aspath_count_numas (struct aspath *);
+extern void aspath_cleanoutall_asconfeds (struct aspath **);
 
 /* For SNMP BGP4PATHATTRASPATHSEGMENT, might be useful for debug */
 extern u_char *aspath_snmp_pathseg (struct aspath *, size_t *);
diff --git a/bgpd/bgp_attr.c b/bgpd/bgp_attr.c
index 07c9413..1c03b21 100644
--- a/bgpd/bgp_attr.c
+++ b/bgpd/bgp_attr.c
@@ -56,6 +56,8 @@ static struct message attr_str [] =
   { BGP_ATTR_RCID_PATH,        "RCID_PATH" },
   { BGP_ATTR_MP_REACH_NLRI,    "MP_REACH_NLRI" },
   { BGP_ATTR_MP_UNREACH_NLRI,  "MP_UNREACH_NLRI" },
+  { BGP_ATTR_AS4_PATH,         "AS4_PATH" }, 
+  { BGP_ATTR_AS4_AGGREGATOR,   "AS4_AGGREGATOR" }, 
   { 0, NULL }
 };
 int attr_str_max = sizeof(attr_str)/sizeof(attr_str[0]);
@@ -749,8 +751,6 @@ static int
 bgp_attr_aspath (struct peer *peer, bgp_size_t length, 
 		 struct attr *attr, u_char flag, u_char *startp)
 {
-  struct bgp *bgp;
-  struct aspath *aspath;
   bgp_size_t total;
 
   total = length + (CHECK_FLAG (flag, BGP_ATTR_FLAG_EXTLEN) ? 4 : 3);
@@ -768,8 +768,14 @@ bgp_attr_aspath (struct peer *peer, bgp_size_t length,
       return -1;
     }
 
+  /*
+   * peer with AS4 => will get 4Byte ASnums
+   * otherwise, will get 16 Bit
+   */
+  attr->aspath = aspath_parse (peer->ibuf, length, 
+                               CHECK_FLAG (peer->cap, PEER_CAP_AS4_RCV));
+
   /* In case of IBGP, length will be zero. */
-  attr->aspath = aspath_parse (peer->ibuf, length);
   if (! attr->aspath)
     {
       zlog (peer->log, LOG_ERR, "Malformed AS path length is %d", length);
@@ -779,6 +785,28 @@ bgp_attr_aspath (struct peer *peer, bgp_size_t length,
       return -1;
     }
 
+  /* Forward pointer. */
+/*  stream_forward_getp (peer->ibuf, length);*/
+
+  /* Set aspath attribute flag. */
+  attr->flag |= ATTR_FLAG_BIT (BGP_ATTR_AS_PATH);
+
+  return 0;
+}
+
+static int bgp_attr_aspath_check( struct peer *peer, 
+		struct attr *attr)
+{
+  /* These checks were part of bgp_attr_aspath, but with
+   * as4 we should to check aspath things when
+   * aspath synthesizing with as4_path has already taken place.
+   * Otherwise we check ASPATH and use the synthesized thing, and that is
+   * not right.
+   * So do the checks later, i.e. here
+   */
+  struct bgp *bgp = peer->bgp;
+  struct aspath *aspath;
+
   bgp = peer->bgp;
     
   /* First AS check for EBGP. */
@@ -806,11 +834,20 @@ bgp_attr_aspath (struct peer *peer, bgp_size_t length,
       attr->aspath = aspath_intern (aspath);
     }
 
-  /* Forward pointer. */
-/*  stream_forward_getp (peer->ibuf, length);*/
+  return 0;
+
+}
+
+/* Parse AS4 path information.  This function is another wrapper of
+   aspath_parse. */
+static int
+bgp_attr_as4_path (struct peer *peer, bgp_size_t length, 
+		 struct attr *attr, struct aspath **as4_path)
+{
+  *as4_path = aspath_parse (peer->ibuf, length, 1);
 
   /* Set aspath attribute flag. */
-  attr->flag |= ATTR_FLAG_BIT (BGP_ATTR_AS_PATH);
+  attr->flag |= ATTR_FLAG_BIT (BGP_ATTR_AS4_PATH);
 
   return 0;
 }
@@ -936,18 +973,27 @@ static int
 bgp_attr_aggregator (struct peer *peer, bgp_size_t length,
 		     struct attr *attr, u_char flag)
 {
+  int wantedlen = 6;
   struct attr_extra *attre = bgp_attr_extra_get (attr);
   
-  if (length != 6)
+  /* peer with AS4 will send 4 Byte AS, peer without will send 2 Byte */
+  if ( CHECK_FLAG (peer->cap, PEER_CAP_AS4_RCV ) )
+    wantedlen = 8;
+  
+  if (length != wantedlen)
     {
-      zlog (peer->log, LOG_ERR, "Aggregator length is not 6 [%d]", length);
+      zlog (peer->log, LOG_ERR, "Aggregator length is not %d [%d]", wantedlen, length);
 
       bgp_notify_send (peer,
 		       BGP_NOTIFY_UPDATE_ERR,
 		       BGP_NOTIFY_UPDATE_ATTR_LENG_ERR);
       return -1;
     }
-  attre->aggregator_as = stream_getw (peer->ibuf);
+  
+  if ( CHECK_FLAG (peer->cap, PEER_CAP_AS4_RCV ) )
+    attre->aggregator_as = stream_getl (peer->ibuf);
+  else
+    attre->aggregator_as = stream_getw (peer->ibuf);
   attre->aggregator_addr.s_addr = stream_get_ipv4 (peer->ibuf);
 
   /* Set atomic aggregate flag. */
@@ -956,6 +1002,153 @@ bgp_attr_aggregator (struct peer *peer, bgp_size_t length,
   return 0;
 }
 
+/* New Aggregator attribute */
+static int
+bgp_attr_as4_aggregator (struct peer *peer, bgp_size_t length,
+		     struct attr *attr, as_t *as4_aggregator_as,
+		     struct in_addr *as4_aggregator_addr)
+{
+  if (length != 8)
+    {
+      zlog (peer->log, LOG_ERR, "New Aggregator length is not 8 [%d]", length);
+
+      bgp_notify_send (peer,
+		       BGP_NOTIFY_UPDATE_ERR,
+		       BGP_NOTIFY_UPDATE_ATTR_LENG_ERR);
+      return -1;
+    }
+  *as4_aggregator_as = stream_getl (peer->ibuf);
+  as4_aggregator_addr->s_addr = stream_get_ipv4 (peer->ibuf);
+
+  attr->flag |= ATTR_FLAG_BIT (BGP_ATTR_AS4_AGGREGATOR);
+
+  return 0;
+}
+
+/* Munge Aggregator and New-Aggregator, AS_PATH and NEW_AS_PATH.
+ */
+static int
+bgp_attr_munge_as4_attrs (struct peer *peer, struct attr *attr,
+                          struct aspath *as4_path, as_t as4_aggregator,
+                          struct in_addr *as4_aggregator_addr,
+                          struct aspath **newpath)
+{
+  int ignore_as4_path = 0;
+  struct attr_extra *attre = attr->extra;
+    
+  if ( CHECK_FLAG (peer->cap, PEER_CAP_AS4_RCV) )
+    {
+      /* peer can do AS4, so we ignore AS4_PATH and AS4_AGGREGATOR
+       * if given.
+       * It is worth a warning though, because the peer really
+       * should not send them
+       */
+      if ( BGP_DEBUG(as4, AS4))
+        zlog_debug ("[AS4] %s BGP AS4 capable peer,"
+                    " %s AS4_PATH, %s AS4_AGGREGATOR",
+                    peer->host,
+                    (attr->flag & (ATTR_FLAG_BIT(BGP_ATTR_AS4_PATH)))
+                      ? "sent" : "didn't send",
+                    (attr->flag & (ATTR_FLAG_BIT(BGP_ATTR_AS_PATH)))
+                      ? "sent" : "didn't send");
+      return 0;
+    }
+  
+  if (attr->flag & ( ATTR_FLAG_BIT( BGP_ATTR_AS4_PATH))
+      && !(attr->flag & ( ATTR_FLAG_BIT( BGP_ATTR_AS_PATH))))
+    {
+      /* Hu? This is not supposed to happen at all!
+       * got as4_path and no aspath,
+       *   This should already
+       *   have been handled by 'well known attributes missing'
+       *   But... yeah, paranoia
+       * Take this as a "malformed attribute"
+       */
+      zlog (peer->log, LOG_ERR, 
+            "%s BGP not AS4 capable peer sent AS4_PATH but"
+            " no AS_PATH, cant do anything here", peer->host);
+      bgp_notify_send (peer, 
+                       BGP_NOTIFY_UPDATE_ERR, 
+                       BGP_NOTIFY_UPDATE_MAL_ATTR);
+      return -1;
+    }
+
+  /* We have a asn16 peer.  First, look for AS4_AGGREGATOR
+   * because that may override AS4_PATH
+   */
+  if (attr->flag & (ATTR_FLAG_BIT (BGP_ATTR_AS4_AGGREGATOR) ) )
+    {
+      assert (attre);
+      
+      if ( attr->flag & (ATTR_FLAG_BIT (BGP_ATTR_AGGREGATOR) ) )
+        {
+          /* received both.
+           * if the as_number in aggregator is not AS_TRANS,
+           *  then AS4_AGGREGATOR and AS4_PATH shall be ignored
+           *        and the Aggregator shall be taken as 
+           *        info on the aggregating node, and the AS_PATH
+           *        shall be taken as the AS_PATH
+           *  otherwise
+           *        the Aggregator shall be ignored and the
+           *        AS4_AGGREGATOR shall be taken as the
+           *        Aggregating node and the AS_PATH is to be
+           *        constructed "as in all other cases"
+           */
+          if ( attre->aggregator_as != BGP_AS_TRANS )
+            {
+              /* ignore */
+              if ( BGP_DEBUG(as4, AS4))
+                zlog_debug ("[AS4] %s BGP not AS4 capable peer" 
+                            " send AGGREGATOR != AS_TRANS and"
+                            " AS4_AGGREGATOR, so ignore"
+                            " AS4_AGGREGATOR and AS4_PATH", peer->host);
+              ignore_as4_path = 1;
+            }
+          else
+            {
+              /* "New_aggregator shall be taken as aggregator" */
+              attre->aggregator_as = as4_aggregator;
+              attre->aggregator_addr.s_addr = as4_aggregator_addr->s_addr;
+            }
+        }
+      else
+        {
+          /* We received a AS4_AGGREGATOR but no AGGREGATOR.
+           * That is bogus - but reading the conditions
+           * we have to handle AS4_AGGREGATOR as if it were
+           * AGGREGATOR in that case
+           */
+          if ( BGP_DEBUG(as4, AS4))
+            zlog_debug ("[AS4] %s BGP not AS4 capable peer send"
+                        " AS4_AGGREGATOR but no AGGREGATOR, will take"
+                        " it as if AGGREGATOR with AS_TRANS had been there", peer->host);
+          attre->aggregator_as = as4_aggregator;
+          /* sweep it under the carpet and simulate a "good" AGGREGATOR */
+          attr->flag |= (ATTR_FLAG_BIT (BGP_ATTR_AGGREGATOR));
+        }
+    }
+
+  if ( !ignore_as4_path && (attr->flag & ( ATTR_FLAG_BIT( BGP_ATTR_AS4_PATH))) )
+    {
+
+      /* We have AS4_PATH AND AS_PATH
+       * Calculate the number of as numbers in both attributes!
+       *  What is meant is the number of "hops"!
+       *   if numas_in_aspath < numas_in_as4_path =>
+       *     ignore AS4_PATH, take AS_PATH.
+       *   else
+       *     take as many as_numbers and path segments from AS_PATH
+       *     and prepend them to AS4_PATH so that
+       *       num_new = numas_in_aspath
+       *      the resulting thing is our AS_PATH
+       *  An AS_SET or AS_CONFED_SET is one hop
+       *  every other thing has as many hops as asnums
+       */
+       *newpath = aspath_reconcile_as4 (attr->aspath, as4_path);
+    }
+  return 0;
+}
+
 /* Community attribute. */
 static int
 bgp_attr_community (struct peer *peer, bgp_size_t length, 
@@ -1275,6 +1468,12 @@ bgp_attr_parse (struct peer *peer, struct attr *attr, bgp_size_t size,
   u_char *startp, *endp;
   u_char *attr_endp;
   u_char seen[BGP_ATTR_BITMAP_SIZE];
+  /* we need the as4_path only until we have synthesized the as_path with it */
+  /* same goes for as4_aggregator */
+  struct aspath *as4_path = NULL;
+  as_t as4_aggregator = 0;
+  struct aspath *newpath;
+  struct in_addr as4_aggregator_addr = { 0 };
 
   /* Initialize bitmap. */
   memset (seen, 0, BGP_ATTR_BITMAP_SIZE);
@@ -1353,6 +1552,9 @@ bgp_attr_parse (struct peer *peer, struct attr *attr, bgp_size_t size,
 	case BGP_ATTR_AS_PATH:
 	  ret = bgp_attr_aspath (peer, length, attr, flag, startp);
 	  break;
+	case BGP_ATTR_AS4_PATH:
+	  ret = bgp_attr_as4_path (peer, length, attr, &as4_path );
+	  break;
 	case BGP_ATTR_NEXT_HOP:	
 	  ret = bgp_attr_nexthop (peer, length, attr, flag, startp);
 	  break;
@@ -1368,6 +1570,9 @@ bgp_attr_parse (struct peer *peer, struct attr *attr, bgp_size_t size,
 	case BGP_ATTR_AGGREGATOR:
 	  ret = bgp_attr_aggregator (peer, length, attr, flag);
 	  break;
+	case BGP_ATTR_AS4_AGGREGATOR:
+	  ret = bgp_attr_as4_aggregator (peer, length, attr, &as4_aggregator, &as4_aggregator_addr);
+	  break;
 	case BGP_ATTR_COMMUNITIES:
 	  ret = bgp_attr_community (peer, length, attr, flag);
 	  break;
@@ -1429,6 +1634,52 @@ bgp_attr_parse (struct peer *peer, struct attr *attr, bgp_size_t size,
       return -1;
     }
 
+  /* 
+   * At this place we can see whether we got AS4_PATH and/or
+   * AS4_AGGREGATOR from a 16Bit peer and act accordingly.
+   * We can not do this before we've read all attributes because
+   * the as4 handling does not say whether AS4_PATH has to be sent
+   * after AS_PATH or not - and when AS4_AGGREGATOR will be send
+   * in relationship to AGGREGATOR.
+   * So, to be defensive, we are not relying on any order and read
+   * all attributes first, including these 32bit ones, and now,
+   * afterwards, we look what and if something is to be done for as4.
+   */
+  if (bgp_attr_munge_as4_attrs (peer, attr, as4_path,
+                                as4_aggregator, &as4_aggregator_addr,
+                                &newpath))
+    return -1;
+
+  /* At this stage, we have done all fiddling with as4, and the
+   * resulting info is in attr->aggregator resp. attr->aspath
+   * so we can chuck as4_aggregator and as4_path alltogether in
+   * order to save memory
+   */
+  if ( as4_path )
+    {
+      aspath_unintern( as4_path ); /* unintern - it is in the hash */
+      as4_path = NULL;
+      /* The flag that we got this is still there, but that does not
+       * do any trouble
+       */
+    }
+  /*
+   * The "rest" of the code does nothing with as4_aggregator.
+   * there is no memory attached specifically which is not part
+   * of the attr.
+   * so ignoring just means do nothing.
+   */
+  /*
+   * Finally do the checks on the aspath we did not do yet
+   * because we waited for a potentially synthesized aspath.
+   */
+  if ( attr->flag & ( ATTR_FLAG_BIT( BGP_ATTR_AS_PATH)))
+    {
+      ret = bgp_attr_aspath_check( peer, attr );
+      if ( ret < 0 )
+	return ret;
+    }
+
   /* Finally intern unknown attribute. */
   if (attr->extra && attr->extra->transit)
     attr->extra->transit = transit_intern (attr->extra->transit);
@@ -1479,8 +1730,11 @@ bgp_packet_attribute (struct bgp *bgp, struct peer *peer,
 		      struct prefix_rd *prd, u_char *tag)
 {
   size_t cp;
-  unsigned int aspath_data_size;
+  size_t aspath_sizep;
+  size_t aspath_put_size;
   struct aspath *aspath;
+  int send_as4_path = 0;
+  int send_as4_aggregator = 0;
 
   if (! bgp)
     bgp = bgp_get_default ();
@@ -1527,25 +1781,31 @@ bgp_packet_attribute (struct bgp *bgp, struct peer *peer,
   else
     aspath = attr->aspath;
 
-  /* AS path attribute extended length bit check. */
-  aspath_data_size = aspath_size (aspath);
-  if (aspath_data_size > 255)
-    {
-      stream_putc (s, BGP_ATTR_FLAG_TRANS|BGP_ATTR_FLAG_EXTLEN);
-      stream_putc (s, BGP_ATTR_AS_PATH);
-      stream_putw (s, aspath_data_size);
-    }
+  /* If peer is not AS4 capable, then:
+   * - send the created AS_PATH out as AS4_PATH (optional, transitive),
+   *   but ensure that no AS_CONFED_SEQUENCE and AS_CONFED_SET path segment
+   *   types are in it (i.e. exclude them if they are there)
+   *   AND do this only if there is at least one asnum > 65535 in the path!
+   * - send an AS_PATH out, but put 16Bit ASnums in it, not 32bit, and change
+   *   all ASnums > 65535 to BGP_AS_TRANS
+   */
+
+  stream_putc (s, BGP_ATTR_FLAG_TRANS|BGP_ATTR_FLAG_EXTLEN);
+  stream_putc (s, BGP_ATTR_AS_PATH);
+  aspath_sizep = stream_get_getp (s);
+  stream_putw (s, 0);
+  
+  if ( CHECK_FLAG (peer->cap, PEER_CAP_AS4_RCV ) )
+    aspath_put_size = aspath_put (s, aspath, 1);
   else
     {
-      stream_putc (s, BGP_ATTR_FLAG_TRANS);
-      stream_putc (s, BGP_ATTR_AS_PATH);
-      stream_putc (s, aspath_data_size);
+      aspath_put_size = aspath_put (s, aspath, 0);
+      if ( aspath_count_num32as( aspath ) > 0 )
+        send_as4_path = 1; /* we'll do this later, at the correct place */
     }
-  aspath_put (s, aspath);
-
-  if (aspath != attr->aspath)
-    aspath_free (aspath);
-
+  
+  stream_putw_at (s, aspath_sizep, aspath_put_size);
+  
   /* Nexthop attribute. */
   if (attr->flag & ATTR_FLAG_BIT (BGP_ATTR_NEXT_HOP) && afi == AFI_IP)
     {
@@ -1594,10 +1854,36 @@ bgp_packet_attribute (struct bgp *bgp, struct peer *peer,
   if (attr->flag & ATTR_FLAG_BIT (BGP_ATTR_AGGREGATOR))
     {
       assert (attr->extra);
+      
+      /* Common to BGP_ATTR_AGGREGATOR, regardless of ASN size */
       stream_putc (s, BGP_ATTR_FLAG_OPTIONAL|BGP_ATTR_FLAG_TRANS);
       stream_putc (s, BGP_ATTR_AGGREGATOR);
-      stream_putc (s, 6);
-      stream_putw (s, attr->extra->aggregator_as);
+      
+      if ( CHECK_FLAG (peer->cap, PEER_CAP_AS4_RCV ) )
+        {
+          /* AS4 capable peer */
+          stream_putc (s, 8);
+          stream_putl (s, attr->extra->aggregator_as);
+        }
+      else
+        {
+          /* 2-byte AS peer */
+          stream_putc (s, 6);
+          
+          /* Is ASN representable in 2-bytes? Or must AS_TRANS be used? */
+          if ( attr->extra->aggregator_as > 65535 )
+            {
+              stream_putw (s, BGP_AS_TRANS);
+              
+              /* we have to send AS4_AGGREGATOR, too.
+               * we'll do that later in order to send attributes in ascending
+               * order.
+               */
+              send_as4_aggregator = 1;
+            }
+          else
+            stream_putw (s, (u_int16_t) attr->extra->aggregator_as);
+        }
       stream_put_ipv4 (s, attr->extra->aggregator_addr.s_addr);
     }
 
@@ -1825,6 +2111,48 @@ bgp_packet_attribute (struct bgp *bgp, struct peer *peer,
 	}
     }
 
+  if ( send_as4_path )
+    {
+      /* If the peer is NOT As4 capable, AND */
+      /* there are ASnums > 65535 in path  THEN
+       * give out AS4_PATH */
+
+      /* Get rid of all AS_CONFED_SEQUENCE and AS_CONFED_SET
+       * path segments!
+       * Hm, I wonder...  confederation things *should* only be at
+       * the beginning of an aspath, right?  Then we should use
+       * aspath_delete_confed_seq for this, because it is already
+       * there! (JK) 
+       * Folks, talk to me: what is reasonable here!?
+       */
+      if ( aspath_count_confeds(aspath) > 0 )
+        aspath_cleanoutall_asconfeds(&aspath);
+
+      stream_putc (s, BGP_ATTR_FLAG_TRANS|BGP_ATTR_FLAG_OPTIONAL|BGP_ATTR_FLAG_EXTLEN);
+      stream_putc (s, BGP_ATTR_AS4_PATH);
+      aspath_sizep = stream_get_getp (s);
+      stream_putw (s, 0);
+      stream_putw_at (s, aspath_sizep, aspath_put (s, aspath, 1));
+    }
+
+  if (aspath != attr->aspath)
+    aspath_free (aspath);
+
+  if ( send_as4_aggregator ) 
+    {
+      assert (attr->extra);
+      
+      /* send AS4_AGGREGATOR, at this place */
+      /* this section of code moved here in order to ensure the correct
+       * *ascending* order of attributes
+       */
+      stream_putc (s, BGP_ATTR_FLAG_OPTIONAL|BGP_ATTR_FLAG_TRANS);
+      stream_putc (s, BGP_ATTR_AS4_AGGREGATOR);
+      stream_putc (s, 8);
+      stream_putl (s, attr->extra->aggregator_as);
+      stream_put_ipv4 (s, attr->extra->aggregator_addr.s_addr);
+    }
+
   /* Unknown transit attribute. */
   if (attr->extra && attr->extra->transit)
     stream_put (s, attr->extra->transit->val, attr->extra->transit->length);
@@ -1900,7 +2228,7 @@ bgp_dump_routes_attr (struct stream *s, struct attr *attr,
 {
   unsigned long cp;
   unsigned long len;
-  unsigned int aspathlen;
+  size_t aspath_lenp;
   struct aspath *aspath;
 
   /* Remember current pointer. */
@@ -1916,20 +2244,13 @@ bgp_dump_routes_attr (struct stream *s, struct attr *attr,
   stream_putc (s, attr->origin);
 
   aspath = attr->aspath;
-
-  if ( (aspathlen = aspath_size (aspath)) > 255 )
-    {
-      stream_putc (s, BGP_ATTR_FLAG_TRANS|BGP_ATTR_FLAG_EXTLEN);
-      stream_putc (s, BGP_ATTR_AS_PATH);
-      stream_putw (s, aspathlen);
-    }
-  else
-    {
-      stream_putc (s, BGP_ATTR_FLAG_TRANS);
-      stream_putc (s, BGP_ATTR_AS_PATH);
-      stream_putc (s, aspathlen);
-    }
-  aspath_put (s, aspath);
+  
+  stream_putc (s, BGP_ATTR_FLAG_TRANS|BGP_ATTR_FLAG_EXTLEN);
+  stream_putc (s, BGP_ATTR_AS_PATH);
+  aspath_lenp = stream_get_getp (s);
+  stream_putw (s, 0);
+  
+  stream_putw_at (s, aspath_lenp, aspath_put (s, aspath, 1));
 
   /* Nexthop attribute. */
   /* If it's an IPv6 prefix, don't dump the IPv4 nexthop to save space */
@@ -1977,6 +2298,10 @@ bgp_dump_routes_attr (struct stream *s, struct attr *attr,
       assert (attr->extra);
       stream_putc (s, BGP_ATTR_FLAG_OPTIONAL|BGP_ATTR_FLAG_TRANS);
       stream_putc (s, BGP_ATTR_AGGREGATOR);
+      /* XXX: AS4: Not AS4 clean.
+       * AS4 patch just updates this, but it's a defined
+       * external format (MRT)
+       */
       stream_putc (s, 6);
       stream_putw (s, attr->extra->aggregator_as);
       stream_put_ipv4 (s, attr->extra->aggregator_addr.s_addr);
diff --git a/bgpd/bgp_debug.c b/bgpd/bgp_debug.c
index 60284a2..0b4ffa0 100644
--- a/bgpd/bgp_debug.c
+++ b/bgpd/bgp_debug.c
@@ -36,6 +36,7 @@ Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
 #include "bgpd/bgp_debug.h"
 #include "bgpd/bgp_community.h"
 
+unsigned long conf_bgp_debug_as4;
 unsigned long conf_bgp_debug_fsm;
 unsigned long conf_bgp_debug_events;
 unsigned long conf_bgp_debug_packet;
@@ -45,6 +46,7 @@ unsigned long conf_bgp_debug_update;
 unsigned long conf_bgp_debug_normal;
 unsigned long conf_bgp_debug_zebra;
 
+unsigned long term_bgp_debug_as4;
 unsigned long term_bgp_debug_fsm;
 unsigned long term_bgp_debug_events;
 unsigned long term_bgp_debug_packet;
@@ -297,6 +299,138 @@ debug (unsigned int option)
   return bgp_debug_option & option; 
 }
 
+DEFUN (debug_bgp_as4,
+       debug_bgp_as4_cmd,
+       "debug bgp as4",
+       DEBUG_STR
+       BGP_STR
+       "BGP AS4 actions\n")
+{
+  if (vty->node == CONFIG_NODE)
+    DEBUG_ON (as4, AS4);
+  else
+    {
+      TERM_DEBUG_ON (as4, AS4);
+      vty_out (vty, "BGP as4 debugging is on%s", VTY_NEWLINE);
+    }
+  return CMD_SUCCESS;
+}
+
+ALIAS_DEPRECATED (debug_bgp_as4,
+       debug_bgp_asn32_cmd,
+       "debug bgp asn32",
+       DEBUG_STR
+       BGP_STR
+       "BGP AS4 actions (old syntax, deprecated)\n")
+
+DEFUN (no_debug_bgp_as4,
+       no_debug_bgp_as4_cmd,
+       "no debug bgp as4",
+       NO_STR
+       DEBUG_STR
+       BGP_STR
+       "BGP AS4 actions\n")
+{
+  if (vty->node == CONFIG_NODE)
+    DEBUG_OFF (as4, AS4);
+  else
+    {
+      TERM_DEBUG_OFF (as4, AS4);
+      vty_out (vty, "BGP as4 debugging is off%s", VTY_NEWLINE);
+    }
+  return CMD_SUCCESS;
+}
+
+ALIAS_DEPRECATED (no_debug_bgp_as4,
+       no_debug_bgp_asn32_cmd,
+       "no debug bgp asn32",
+       NO_STR
+       DEBUG_STR
+       BGP_STR
+       "BGP AS4 actions (old syntax, deprecated)\n")
+
+ALIAS (no_debug_bgp_as4,
+       undebug_bgp_as4_cmd,
+       "undebug bgp as4",
+       UNDEBUG_STR
+       DEBUG_STR
+       BGP_STR
+       "BGP AS4 actions\n")
+
+ALIAS_DEPRECATED (no_debug_bgp_as4,
+       undebug_bgp_asn32_cmd,
+       "undebug bgp asn32",
+       UNDEBUG_STR
+       DEBUG_STR
+       BGP_STR
+       "BGP AS4 actions (old syntax, deprecated)\n")
+
+DEFUN (debug_bgp_as4_segment,
+       debug_bgp_as4_segment_cmd,
+       "debug bgp as4 segment",
+       DEBUG_STR
+       BGP_STR
+       "BGP AS4 aspath segment handling\n")
+{
+  if (vty->node == CONFIG_NODE)
+    DEBUG_ON (as4, AS4_SEGMENT);
+  else
+    {
+      TERM_DEBUG_ON (as4, AS4_SEGMENT);
+      vty_out (vty, "BGP as4 segment debugging is on%s", VTY_NEWLINE);
+    }
+  return CMD_SUCCESS;
+}
+
+ALIAS_DEPRECATED (debug_bgp_as4_segment,
+       debug_bgp_asn32_segment_cmd,
+       "debug bgp asn32 segment",
+       DEBUG_STR
+       BGP_STR
+       "BGP AS4 aspath segment handling (old syntax, deprecated)\n")
+
+DEFUN (no_debug_bgp_as4_segment,
+       no_debug_bgp_as4_segment_cmd,
+       "no debug bgp as4 segment",
+       NO_STR
+       DEBUG_STR
+       BGP_STR
+       "BGP AS4 aspath segment handling\n")
+{
+  if (vty->node == CONFIG_NODE)
+    DEBUG_OFF (as4, AS4_SEGMENT);
+  else
+    {
+      TERM_DEBUG_OFF (as4, AS4_SEGMENT);
+      vty_out (vty, "BGP as4 segment debugging is off%s", VTY_NEWLINE);
+    }
+  return CMD_SUCCESS;
+}
+
+ALIAS_DEPRECATED (no_debug_bgp_as4_segment,
+       no_debug_bgp_asn32_segment_cmd,
+       "no debug bgp asn32 segment",
+       NO_STR
+       DEBUG_STR
+       BGP_STR
+       "BGP AS4 aspath segment handling (old syntax, deprecated)\n")
+
+ALIAS (no_debug_bgp_as4_segment,
+       undebug_bgp_as4_segment_cmd,
+       "undebug bgp as4 segment",
+       UNDEBUG_STR
+       DEBUG_STR
+       BGP_STR
+       "BGP AS4 aspath segment handling\n")
+
+ALIAS_DEPRECATED (no_debug_bgp_as4_segment,
+       undebug_bgp_asn32_segment_cmd,
+       "undebug bgp asn32 segment",
+       UNDEBUG_STR
+       DEBUG_STR
+       BGP_STR
+       "BGP AS4 aspath segment handling (old syntax, deprecated)\n")
+
 DEFUN (debug_bgp_fsm,
        debug_bgp_fsm_cmd,
        "debug bgp fsm",
@@ -651,6 +785,8 @@ DEFUN (no_debug_bgp_all,
   TERM_DEBUG_OFF (keepalive, KEEPALIVE);
   TERM_DEBUG_OFF (update, UPDATE_IN);
   TERM_DEBUG_OFF (update, UPDATE_OUT);
+  TERM_DEBUG_OFF (as4, AS4);
+  TERM_DEBUG_OFF (as4, AS4_SEGMENT);
   TERM_DEBUG_OFF (fsm, FSM);
   TERM_DEBUG_OFF (filter, FILTER);
   TERM_DEBUG_OFF (zebra, ZEBRA);
@@ -693,6 +829,10 @@ DEFUN (show_debugging_bgp,
     vty_out (vty, "  BGP filter debugging is on%s", VTY_NEWLINE);
   if (BGP_DEBUG (zebra, ZEBRA))
     vty_out (vty, "  BGP zebra debugging is on%s", VTY_NEWLINE);
+  if (BGP_DEBUG (as4, AS4))
+    vty_out (vty, "  BGP as4 debugging is on%s", VTY_NEWLINE);
+  if (BGP_DEBUG (as4, AS4_SEGMENT))
+    vty_out (vty, "  BGP as4 aspath segment debugging is on%s", VTY_NEWLINE);
   vty_out (vty, "%s", VTY_NEWLINE);
   return CMD_SUCCESS;
 }
@@ -708,6 +848,18 @@ bgp_config_write_debug (struct vty *vty)
       write++;
     }
 
+  if (CONF_BGP_DEBUG (as4, AS4))
+    {
+      vty_out (vty, "debug bgp as4%s", VTY_NEWLINE);
+      write++;
+    }
+
+  if (CONF_BGP_DEBUG (as4, AS4_SEGMENT))
+    {
+      vty_out (vty, "debug bgp as4 segment%s", VTY_NEWLINE);
+      write++;
+    }
+
   if (CONF_BGP_DEBUG (events, EVENTS))
     {
       vty_out (vty, "debug bgp events%s", VTY_NEWLINE);
@@ -771,6 +923,16 @@ bgp_debug_init (void)
 
   install_element (ENABLE_NODE, &show_debugging_bgp_cmd);
 
+  install_element (ENABLE_NODE, &debug_bgp_as4_cmd);
+  install_element (CONFIG_NODE, &debug_bgp_as4_cmd);
+  install_element (ENABLE_NODE, &debug_bgp_as4_segment_cmd);
+  install_element (CONFIG_NODE, &debug_bgp_as4_segment_cmd);
+  /* compat for former AS4 patch */
+  install_element (ENABLE_NODE, &debug_bgp_asn32_cmd);
+  install_element (CONFIG_NODE, &debug_bgp_asn32_cmd);
+  install_element (ENABLE_NODE, &debug_bgp_asn32_segment_cmd);
+  install_element (CONFIG_NODE, &debug_bgp_asn32_segment_cmd);
+
   install_element (ENABLE_NODE, &debug_bgp_fsm_cmd);
   install_element (CONFIG_NODE, &debug_bgp_fsm_cmd);
   install_element (ENABLE_NODE, &debug_bgp_events_cmd);
@@ -788,6 +950,20 @@ bgp_debug_init (void)
   install_element (ENABLE_NODE, &debug_bgp_zebra_cmd);
   install_element (CONFIG_NODE, &debug_bgp_zebra_cmd);
 
+  install_element (ENABLE_NODE, &no_debug_bgp_as4_cmd);
+  install_element (ENABLE_NODE, &undebug_bgp_as4_cmd);
+  install_element (CONFIG_NODE, &no_debug_bgp_as4_cmd);
+  install_element (ENABLE_NODE, &no_debug_bgp_as4_segment_cmd);
+  install_element (ENABLE_NODE, &undebug_bgp_as4_segment_cmd);
+  install_element (CONFIG_NODE, &no_debug_bgp_as4_segment_cmd);
+  /* compat for former AS4 patch */
+  install_element (ENABLE_NODE, &no_debug_bgp_asn32_cmd);
+  install_element (ENABLE_NODE, &undebug_bgp_asn32_cmd);
+  install_element (CONFIG_NODE, &no_debug_bgp_asn32_cmd);
+  install_element (ENABLE_NODE, &no_debug_bgp_asn32_segment_cmd);
+  install_element (ENABLE_NODE, &undebug_bgp_asn32_segment_cmd);
+  install_element (CONFIG_NODE, &no_debug_bgp_asn32_segment_cmd);
+
   install_element (ENABLE_NODE, &no_debug_bgp_fsm_cmd);
   install_element (ENABLE_NODE, &undebug_bgp_fsm_cmd);
   install_element (CONFIG_NODE, &no_debug_bgp_fsm_cmd);
diff --git a/bgpd/bgp_debug.h b/bgpd/bgp_debug.h
index eab95d0..1eb306f 100644
--- a/bgpd/bgp_debug.h
+++ b/bgpd/bgp_debug.h
@@ -21,6 +21,8 @@ Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
 #ifndef _QUAGGA_BGP_DEBUG_H
 #define _QUAGGA_BGP_DEBUG_H
 
+#include "bgp_attr.h"
+
 /* sort of packet direction */
 #define DUMP_ON        1
 #define DUMP_SEND      2
@@ -56,6 +58,7 @@ extern void bgp_packet_dump (struct stream *);
 
 extern int debug (unsigned int option);
 
+extern unsigned long conf_bgp_debug_as4;
 extern unsigned long conf_bgp_debug_fsm;
 extern unsigned long conf_bgp_debug_events;
 extern unsigned long conf_bgp_debug_packet;
@@ -65,6 +68,7 @@ extern unsigned long conf_bgp_debug_update;
 extern unsigned long conf_bgp_debug_normal;
 extern unsigned long conf_bgp_debug_zebra;
 
+extern unsigned long term_bgp_debug_as4;
 extern unsigned long term_bgp_debug_fsm;
 extern unsigned long term_bgp_debug_events;
 extern unsigned long term_bgp_debug_packet;
@@ -74,6 +78,9 @@ extern unsigned long term_bgp_debug_update;
 extern unsigned long term_bgp_debug_normal;
 extern unsigned long term_bgp_debug_zebra;
 
+#define BGP_DEBUG_AS4                 0x01
+#define BGP_DEBUG_AS4_SEGMENT         0x02
+
 #define BGP_DEBUG_FSM                 0x01
 #define BGP_DEBUG_EVENTS              0x01
 #define BGP_DEBUG_PACKET              0x01
diff --git a/bgpd/bgp_dump.c b/bgpd/bgp_dump.c
index c350e83..8507951 100644
--- a/bgpd/bgp_dump.c
+++ b/bgpd/bgp_dump.c
@@ -26,6 +26,7 @@ Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
 #include "command.h"
 #include "prefix.h"
 #include "thread.h"
+#include "linklist.h"
 #include "bgpd/bgp_table.h"
 
 #include "bgpd/bgpd.h"
@@ -53,7 +54,8 @@ enum MRT_MSG_TYPES {
    MSG_PROTOCOL_BGP4PLUS,       /* msg is a BGP4+ packet */
    MSG_PROTOCOL_BGP4PLUS_01,    /* msg is a BGP4+ (draft 01) packet */
    MSG_PROTOCOL_OSPF,           /* msg is an OSPF packet */
-   MSG_TABLE_DUMP               /* routing table dump */
+   MSG_TABLE_DUMP,              /* routing table dump */
+   MSG_TABLE_DUMP_V2            /* routing table dump, version 2 */
 };
 
 static int bgp_dump_interval_func (struct thread *);
@@ -190,137 +192,189 @@ bgp_dump_set_size (struct stream *s, int type)
 }
 
 static void
-bgp_dump_routes_entry (struct prefix *p, struct bgp_info *info, int afi,
-		       int type, unsigned int seq)
+bgp_dump_routes_index_table(struct bgp *bgp)
 {
-  struct stream *obuf;
-  struct attr *attr;
   struct peer *peer;
-  int plen;
-  int safi = 0;
+  struct listnode *node;
+  uint16_t peerno = 0;
+  struct stream *obuf;
 
-  /* Make dump stream. */
   obuf = bgp_dump_obuf;
   stream_reset (obuf);
 
-  attr = info->attr;
-  peer = info->peer;
+  /* MRT header */
+  bgp_dump_header (obuf, MSG_TABLE_DUMP_V2, TABLE_DUMP_V2_PEER_INDEX_TABLE);
 
-  /* We support MRT's old format. */
-  if (type == MSG_TABLE_DUMP)
+  /* Collector BGP ID */
+  stream_put_in_addr (obuf, &bgp->router_id);
+
+  /* View name */
+  if(bgp->name)
     {
-      bgp_dump_header (obuf, MSG_TABLE_DUMP, afi);
-      stream_putw (obuf, 0);	/* View # */
-      stream_putw (obuf, seq);	/* Sequence number. */
+      stream_putw (obuf, strlen(bgp->name));
+      stream_put(obuf, bgp->name, strlen(bgp->name));
     }
   else
     {
-      bgp_dump_header (obuf, MSG_PROTOCOL_BGP4MP, BGP4MP_ENTRY);
-      
-      stream_putl (obuf, info->uptime); /* Time Last Change */
-      stream_putw (obuf, afi);	/* Address Family */
-      stream_putc (obuf, safi);	/* SAFI */
+      stream_putw(obuf, 0);
     }
 
-  if (afi == AFI_IP)
-    {
-      if (type == MSG_TABLE_DUMP)
-	{
-	  /* Prefix */
-	  stream_put_in_addr (obuf, &p->u.prefix4);
-	  stream_putc (obuf, p->prefixlen);
-
-	  /* Status */
-	  stream_putc (obuf, 1);
-
-	  /* Originated */
-	  stream_putl (obuf, info->uptime);
-
-	  /* Peer's IP address */
-	  stream_put_in_addr (obuf, &peer->su.sin.sin_addr);
+  /* Peer count */
+  stream_putw (obuf, listcount(bgp->peer));
 
-	  /* Peer's AS number. */
-	  stream_putw (obuf, peer->as);
-
-	  /* Dump attribute. */
-	  bgp_dump_routes_attr (obuf, attr, p);
-	}
-      else
-	{
-	  /* Next-Hop-Len */
-	  stream_putc (obuf, IPV4_MAX_BYTELEN);
-	  stream_put_in_addr (obuf, &attr->nexthop);
-	  stream_putc (obuf, p->prefixlen);
-	  plen = PSIZE (p->prefixlen);
-	  stream_put (obuf, &p->u.prefix4, plen);
-	  bgp_dump_routes_attr (obuf, attr, p);
-	}
-    }
-#ifdef HAVE_IPV6
-  else if (afi == AFI_IP6)
+  /* Walk down all peers */
+  for(ALL_LIST_ELEMENTS_RO (bgp->peer, node, peer))
     {
-      if (type == MSG_TABLE_DUMP)
-	{
-	  /* Prefix */
-	  stream_write (obuf, (u_char *)&p->u.prefix6, IPV6_MAX_BYTELEN);
-	  stream_putc (obuf, p->prefixlen);
 
-	  /* Status */
-	  stream_putc (obuf, 1);
+      /* Peer's type */
+      if (sockunion_family(&peer->su) == AF_INET)
+        {
+          stream_putc (obuf, TABLE_DUMP_V2_PEER_INDEX_TABLE_AS4+TABLE_DUMP_V2_PEER_INDEX_TABLE_IP);
+        }
+#ifdef HAVE_IPV6
+      else if (sockunion_family(&peer->su) == AF_INET6)
+        {
+          stream_putc (obuf, TABLE_DUMP_V2_PEER_INDEX_TABLE_AS4+TABLE_DUMP_V2_PEER_INDEX_TABLE_IP6);
+        }
+#endif /* HAVE_IPV6 */
 
-	  /* Originated */
-	  stream_putl (obuf, info->uptime);
+      /* Peer's BGP ID */
+      stream_put_in_addr (obuf, &peer->remote_id);
 
-	  /* Peer's IP address */
-	  stream_write (obuf, (u_char *)&peer->su.sin6.sin6_addr,
-			IPV6_MAX_BYTELEN);
+      /* Peer's IP address */
+      if (sockunion_family(&peer->su) == AF_INET)
+        {
+          stream_put_in_addr (obuf, &peer->su.sin.sin_addr);
+        }
+#ifdef HAVE_IPV6
+      else if (sockunion_family(&peer->su) == AF_INET6)
+        {
+          stream_write (obuf, (u_char *)&peer->su.sin6.sin6_addr,
+                        IPV6_MAX_BYTELEN);
+        }
+#endif /* HAVE_IPV6 */
 
-	  /* Peer's AS number. */
-	  stream_putw (obuf, peer->as);
+      /* Peer's AS number. */
+      /* Note that, as this is an AS4 compliant quagga, the RIB is always AS4 */
+      stream_putl (obuf, peer->as);
 
-	  /* Dump attribute. */
-	  bgp_dump_routes_attr (obuf, attr, p);
-	}
-      else
-	{
-	  ;
-	}
+      /* Store the peer number for this peer */
+      peer->table_dump_index = peerno;
+      peerno++;
     }
-#endif /* HAVE_IPV6 */
 
-  /* Set length. */
-  bgp_dump_set_size (obuf, type);
+  bgp_dump_set_size(obuf, MSG_TABLE_DUMP_V2);
 
   fwrite (STREAM_DATA (obuf), stream_get_endp (obuf), 1, bgp_dump_routes.fp);
   fflush (bgp_dump_routes.fp);
 }
 
+
 /* Runs under child process. */
-static void
-bgp_dump_routes_func (int afi)
+static unsigned int
+bgp_dump_routes_func (int afi, int first_run, unsigned int seq)
 {
   struct stream *obuf;
-  struct bgp_node *rn;
   struct bgp_info *info;
+  struct bgp_node *rn;
   struct bgp *bgp;
   struct bgp_table *table;
-  unsigned int seq = 0;
-
-  obuf = bgp_dump_obuf;
 
   bgp = bgp_get_default ();
   if (!bgp)
-    return;
+    return seq;
 
   if (bgp_dump_routes.fp == NULL)
-    return;
+    return seq;
+
+  /* Note that bgp_dump_routes_index_table will do ipv4 and ipv6 peers,
+     so this should only be done on the first call to bgp_dump_routes_func.
+     ( this function will be called once for ipv4 and once for ipv6 ) */
+  if(first_run)
+    bgp_dump_routes_index_table(bgp);
+
+  obuf = bgp_dump_obuf;
+  stream_reset(obuf);
 
   /* Walk down each BGP route. */
   table = bgp->rib[afi][SAFI_UNICAST];
 
   for (rn = bgp_table_top (table); rn; rn = bgp_route_next (rn))
-    for (info = rn->info; info; info = info->next)
-      bgp_dump_routes_entry (&rn->p, info, afi, MSG_TABLE_DUMP, seq++);
+    {
+      if(!rn->info)
+        continue;
+
+      stream_reset(obuf);
+
+      /* MRT header */
+      if (afi == AFI_IP)
+        {
+          bgp_dump_header (obuf, MSG_TABLE_DUMP_V2, TABLE_DUMP_V2_RIB_IPV4_UNICAST);
+        }
+#ifdef HAVE_IPV6
+      else if (afi == AFI_IP6)
+        {
+          bgp_dump_header (obuf, MSG_TABLE_DUMP_V2, TABLE_DUMP_V2_RIB_IPV6_UNICAST);
+        }
+#endif /* HAVE_IPV6 */
+
+      /* Sequence number */
+      stream_putl(obuf, seq);
+
+      /* Prefix length */
+      stream_putc (obuf, rn->p.prefixlen);
+
+      /* Prefix */
+      if (afi == AFI_IP)
+        {
+          /* We'll dump only the useful bits (those not 0), but have to align on 8 bits */
+          stream_write(obuf, (u_char *)&rn->p.u.prefix4, (rn->p.prefixlen+7)/8);
+        }
+#ifdef HAVE_IPV6
+      else if (afi == AFI_IP6)
+        {
+          /* We'll dump only the useful bits (those not 0), but have to align on 8 bits */
+          stream_write (obuf, (u_char *)&rn->p.u.prefix6, (rn->p.prefixlen+7)/8);
+        }
+#endif /* HAVE_IPV6 */
+
+      /* Save where we are now, so we can overwride the entry count later */
+      int sizep = stream_get_endp(obuf);
+
+      /* Entry count */
+      uint16_t entry_count = 0;
+
+      /* Entry count, note that this is overwritten later */
+      stream_putw(obuf, 0);
+
+      for (info = rn->info; info; info = info->next)
+        {
+          entry_count++;
+
+          /* Peer index */
+          stream_putw(obuf, info->peer->table_dump_index);
+
+          /* Originated */
+          stream_putl (obuf, info->uptime);
+
+          /* Dump attribute. */
+          /* Skip prefix & AFI/SAFI for MP_NLRI */
+          bgp_dump_routes_attr (obuf, info->attr, &rn->p);
+        }
+
+      /* Overwrite the entry count, now that we know the right number */
+      stream_putw_at (obuf, sizep, entry_count);
+
+      seq++;
+
+      bgp_dump_set_size(obuf, MSG_TABLE_DUMP_V2);
+      fwrite (STREAM_DATA (obuf), stream_get_endp (obuf), 1, bgp_dump_routes.fp);
+
+    }
+
+  fflush (bgp_dump_routes.fp);
+
+  return seq;
 }
 
 static int
@@ -336,9 +390,9 @@ bgp_dump_interval_func (struct thread *t)
       /* In case of bgp_dump_routes, we need special route dump function. */
       if (bgp_dump->type == BGP_DUMP_ROUTES)
 	{
-	  bgp_dump_routes_func (AFI_IP);
+	  unsigned int seq = bgp_dump_routes_func (AFI_IP, 1, 0);
 #ifdef HAVE_IPV6
-	  bgp_dump_routes_func (AFI_IP6);
+	  bgp_dump_routes_func (AFI_IP6, 0, seq);
 #endif /* HAVE_IPV6 */
 	  /* Close the file now. For a RIB dump there's no point in leaving
 	   * it open until the next scheduled dump starts. */
@@ -355,13 +409,21 @@ bgp_dump_interval_func (struct thread *t)
 
 /* Dump common information. */
 static void
-bgp_dump_common (struct stream *obuf, struct peer *peer)
+bgp_dump_common (struct stream *obuf, struct peer *peer, int forceas4)
 {
   char empty[16] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
 
   /* Source AS number and Destination AS number. */
-  stream_putw (obuf, peer->as);
-  stream_putw (obuf, peer->local_as);
+  if (forceas4 || CHECK_FLAG (peer->cap, PEER_CAP_AS4_RCV) )
+    {
+      stream_putl (obuf, peer->as);
+      stream_putl (obuf, peer->local_as);
+    }
+  else
+    {
+      stream_putw (obuf, peer->as);
+      stream_putw (obuf, peer->local_as);
+    }
 
   if (peer->su.sa.sa_family == AF_INET)
     {
@@ -407,8 +469,8 @@ bgp_dump_state (struct peer *peer, int status_old, int status_new)
   obuf = bgp_dump_obuf;
   stream_reset (obuf);
 
-  bgp_dump_header (obuf, MSG_PROTOCOL_BGP4MP, BGP4MP_STATE_CHANGE);
-  bgp_dump_common (obuf, peer);
+  bgp_dump_header (obuf, MSG_PROTOCOL_BGP4MP, BGP4MP_STATE_CHANGE_AS4);
+  bgp_dump_common (obuf, peer, 1);/* force this in as4speak*/
 
   stream_putw (obuf, status_old);
   stream_putw (obuf, status_new);
@@ -436,8 +498,15 @@ bgp_dump_packet_func (struct bgp_dump *bgp_dump, struct peer *peer,
   stream_reset (obuf);
 
   /* Dump header and common part. */
-  bgp_dump_header (obuf, MSG_PROTOCOL_BGP4MP, BGP4MP_MESSAGE);
-  bgp_dump_common (obuf, peer);
+  if (CHECK_FLAG (peer->cap, PEER_CAP_AS4_RCV) )
+    { 
+      bgp_dump_header (obuf, MSG_PROTOCOL_BGP4MP, BGP4MP_MESSAGE_AS4);
+    }
+  else
+    {
+      bgp_dump_header (obuf, MSG_PROTOCOL_BGP4MP, BGP4MP_MESSAGE);
+    }
+  bgp_dump_common (obuf, peer, 0);
 
   /* Packet contents. */
   stream_put (obuf, STREAM_DATA (packet), stream_get_endp (packet));
diff --git a/bgpd/bgp_dump.h b/bgpd/bgp_dump.h
index 36447e9..6bb1197 100644
--- a/bgpd/bgp_dump.h
+++ b/bgpd/bgp_dump.h
@@ -25,14 +25,28 @@ Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
 /* type value */
 #define MSG_PROTOCOL_BGP4MP  16
 /* subtype value */
-#define BGP4MP_STATE_CHANGE   0
-#define BGP4MP_MESSAGE        1
-#define BGP4MP_ENTRY          2
-#define BGP4MP_SNAPSHOT       3
+#define BGP4MP_STATE_CHANGE          0
+#define BGP4MP_MESSAGE               1
+#define BGP4MP_ENTRY                 2
+#define BGP4MP_SNAPSHOT              3
+#define BGP4MP_MESSAGE_AS4           4
+#define BGP4MP_STATE_CHANGE_AS4      5
 
 #define BGP_DUMP_HEADER_SIZE 12
 #define BGP_DUMP_MSG_HEADER  40
 
+#define TABLE_DUMP_V2_PEER_INDEX_TABLE   1
+#define TABLE_DUMP_V2_RIB_IPV4_UNICAST   2
+#define TABLE_DUMP_V2_RIB_IPV4_MULTICAST 3
+#define TABLE_DUMP_V2_RIB_IPV6_UNICAST   4
+#define TABLE_DUMP_V2_RIB_IPV6_MULTICAST 5
+#define TABLE_DUMP_V2_RIB_GENERIC        6
+
+#define TABLE_DUMP_V2_PEER_INDEX_TABLE_IP  0
+#define TABLE_DUMP_V2_PEER_INDEX_TABLE_IP6 1
+#define TABLE_DUMP_V2_PEER_INDEX_TABLE_AS2 0
+#define TABLE_DUMP_V2_PEER_INDEX_TABLE_AS4 2
+
 extern void bgp_dump_init (void);
 extern void bgp_dump_state (struct peer *, int, int);
 extern void bgp_dump_packet (struct peer *, int, struct stream *);
diff --git a/bgpd/bgp_ecommunity.c b/bgpd/bgp_ecommunity.c
index 64f4438..53f4a87 100644
--- a/bgpd/bgp_ecommunity.c
+++ b/bgpd/bgp_ecommunity.c
@@ -27,6 +27,7 @@ Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
 
 #include "bgpd/bgpd.h"
 #include "bgpd/bgp_ecommunity.h"
+#include "bgpd/bgp_aspath.h"
 
 /* Hash of community attribute. */
 struct hash *ecomhash;
@@ -279,11 +280,13 @@ ecommunity_gettoken (const char *str, struct ecommunity_val *eval,
   int dot = 0;
   int digit = 0;
   int separator = 0;
-  u_int32_t val_low = 0;
-  u_int32_t val_high = 0;
   const char *p = str;
+  const char *valptr = NULL;
+  char *endptr;
   struct in_addr ip;
-  char ipstr[INET_ADDRSTRLEN + 1];
+  as_t as = 0;
+  u_int32_t val = 0;
+  char buf[INET_ADDRSTRLEN + 1];
 
   /* Skip white space. */
   while (isspace ((int) *p))
@@ -346,32 +349,50 @@ ecommunity_gettoken (const char *str, struct ecommunity_val *eval,
       goto error;
     }
   
+  /* What a mess, there are several possibilities:
+   *
+   * a) A.B.C.D:MN
+   * b) EF:OPQR
+   * c) GHJK:MN
+   *
+   * A.B.C.D: Four Byte IP
+   * EF:      Two byte ASN
+   * GHJK:    Four-byte ASN
+   * MN:      Two byte value
+   * OPQR:    Four byte value
+   *
+   */
   while (isdigit ((int) *p) || *p == ':' || *p == '.') 
     {
-      if (*p == ':') 
+      if (*p == ':')
 	{
 	  if (separator)
 	    goto error;
 
 	  separator = 1;
 	  digit = 0;
-
+	  
+	  if ((p - str) > INET_ADDRSTRLEN)
+	    goto error;
+          memset (buf, 0, INET_ADDRSTRLEN + 1);
+          memcpy (buf, str, p - str);
+          
 	  if (dot)
 	    {
-	      if ((p - str) > INET_ADDRSTRLEN)
-		goto error;
-
-	      memset (ipstr, 0, INET_ADDRSTRLEN + 1);
-	      memcpy (ipstr, str, p - str);
-
-	      ret = inet_aton (ipstr, &ip);
+	      /* Parsing A.B.C.D in:
+               * A.B.C.D:MN
+               */
+	      ret = inet_aton (buf, &ip);
 	      if (ret == 0)
-		goto error;
+	        goto error;
 	    }
-	  else
-	    val_high = val_low;
-
-	  val_low = 0;
+          else
+            {
+              /* ASN */
+              as = strtoul (buf, &endptr, 10);
+              if (*endptr != '\0' || as == BGP_AS4_MAX)
+                goto error;
+            }
 	}
       else if (*p == '.')
 	{
@@ -384,35 +405,64 @@ ecommunity_gettoken (const char *str, struct ecommunity_val *eval,
       else
 	{
 	  digit = 1;
-	  val_low *= 10;
-	  val_low += (*p - '0');
+	  
+	  /* We're past the IP/ASN part */
+	  if (separator)
+	    valptr = p;
 	}
       p++;
     }
 
   /* Low digit part must be there. */
-  if (! digit || ! separator)
+  if (!digit || !separator || !valptr)
     goto error;
 
+  if ((p - valptr) > INET_ADDRSTRLEN)
+    goto error;
+  memset (buf, 0, INET_ADDRSTRLEN + 1);
+  memcpy (buf, valptr, p - valptr);
+  
+  val = strtoul (buf, &endptr, 10);
+  if (*endptr != '\0' || as == BGP_AS4_MAX)
+    goto error;
+  
   /* Encode result into routing distinguisher.  */
   if (dot)
     {
+      if (val > UINT16_MAX)
+        goto error;
+      
       eval->val[0] = ECOMMUNITY_ENCODE_IP;
       eval->val[1] = 0;
       memcpy (&eval->val[2], &ip, sizeof (struct in_addr));
-      eval->val[6] = (val_low >> 8) & 0xff;
-      eval->val[7] = val_low & 0xff;
+      eval->val[6] = (val >> 8) & 0xff;
+      eval->val[7] = val & 0xff;
+    }
+  else if (as > BGP_AS_MAX)
+    {
+      if (val > UINT16_MAX)
+        goto error;
+      
+      eval->val[0] = ECOMMUNITY_ENCODE_AS4;
+      eval->val[1] = 0;
+      eval->val[2] = (as >>24) & 0xff;
+      eval->val[3] = (as >>16) & 0xff;
+      eval->val[4] = (as >>8) & 0xff;
+      eval->val[5] =  as & 0xff;
+      eval->val[6] = (val >> 8) & 0xff;
+      eval->val[7] = val & 0xff;
     }
   else
     {
       eval->val[0] = ECOMMUNITY_ENCODE_AS;
       eval->val[1] = 0;
-      eval->val[2] = (val_high >>8) & 0xff;
-      eval->val[3] = val_high & 0xff;
-      eval->val[4] = (val_low >>24) & 0xff;
-      eval->val[5] = (val_low >>16) & 0xff;
-      eval->val[6] = (val_low >>8) & 0xff;
-      eval->val[7] = val_low & 0xff;
+      
+      eval->val[2] = (as >>8) & 0xff;
+      eval->val[3] = as & 0xff;
+      eval->val[4] = (val >>24) & 0xff;
+      eval->val[5] = (val >>16) & 0xff;
+      eval->val[6] = (val >>8) & 0xff;
+      eval->val[7] = val & 0xff;
     }
   *token = ecommunity_token_val;
   return p;
@@ -533,7 +583,7 @@ ecommunity_ecom2str (struct ecommunity *ecom, int format)
   u_int8_t *pnt;
   int encode = 0;
   int type = 0;
-#define ECOMMUNITY_STR_DEFAULT_LEN  26
+#define ECOMMUNITY_STR_DEFAULT_LEN  27
   int str_size;
   int str_pnt;
   char *str_buf;
@@ -576,7 +626,8 @@ ecommunity_ecom2str (struct ecommunity *ecom, int format)
 
       /* High-order octet of type. */
       encode = *pnt++;
-      if (encode != ECOMMUNITY_ENCODE_AS && encode != ECOMMUNITY_ENCODE_IP)
+      if (encode != ECOMMUNITY_ENCODE_AS && encode != ECOMMUNITY_ENCODE_IP
+		      && encode != ECOMMUNITY_ENCODE_AS4)
 	{
 	  len = sprintf (str_buf + str_pnt, "?");
 	  str_pnt += len;
@@ -618,6 +669,21 @@ ecommunity_ecom2str (struct ecommunity *ecom, int format)
 	}
 
       /* Put string into buffer.  */
+      if (encode == ECOMMUNITY_ENCODE_AS4)
+	{
+	  eas.as = (*pnt++ << 24);
+	  eas.as |= (*pnt++ << 16);
+	  eas.as |= (*pnt++ << 8);
+	  eas.as |= (*pnt++);
+
+	  eas.val = (*pnt++ << 8);
+	  eas.val |= (*pnt++);
+
+	  len = sprintf( str_buf + str_pnt, "%s%d:%d", prefix,
+                        eas.as, eas.val );
+	  str_pnt += len;
+	  first = 0;
+	}
       if (encode == ECOMMUNITY_ENCODE_AS)
 	{
 	  eas.as = (*pnt++ << 8);
diff --git a/bgpd/bgp_ecommunity.h b/bgpd/bgp_ecommunity.h
index 7b2564a..107c399 100644
--- a/bgpd/bgp_ecommunity.h
+++ b/bgpd/bgp_ecommunity.h
@@ -24,6 +24,7 @@ Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
 /* High-order octet of the Extended Communities type field.  */
 #define ECOMMUNITY_ENCODE_AS                0x00
 #define ECOMMUNITY_ENCODE_IP                0x01
+#define ECOMMUNITY_ENCODE_AS4               0x02
 
 /* Low-order octet of the Extended Communityes type field.  */
 #define ECOMMUNITY_ROUTE_TARGET             0x02
diff --git a/bgpd/bgp_open.c b/bgpd/bgp_open.c
index e44bd2a..5bf5a20 100644
--- a/bgpd/bgp_open.c
+++ b/bgpd/bgp_open.c
@@ -33,6 +33,7 @@ Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
 #include "bgpd/bgp_fsm.h"
 #include "bgpd/bgp_packet.h"
 #include "bgpd/bgp_open.h"
+#include "bgpd/bgp_aspath.h"
 #include "bgpd/bgp_vty.h"
 
 /* BGP-4 Multiprotocol Extentions lead us to the complex world. We can
@@ -306,7 +307,9 @@ bgp_capability_orf (struct peer *peer, struct capability *cap,
   return 0;
 }
 
-/* Parse given capability. */
+/* Parse given capability.
+ * XXX: This is reading into a stream, but not using stream API
+ */
 static int
 bgp_capability_parse (struct peer *peer, u_char *pnt, u_char length,
 		      u_char **error)
@@ -483,6 +486,44 @@ bgp_capability_parse (struct peer *peer, u_char *pnt, u_char length,
 
 	  SET_FLAG (peer->cap, PEER_CAP_DYNAMIC_RCV);
 	}
+      else if (cap.code == CAPABILITY_CODE_AS4)
+	{
+	  /* Check length */
+	  if (cap.length != CAPABILITY_CODE_AS4_LEN)
+	    {
+	      zlog_info ("%s AS4 Capability length error %d",
+			 peer->host, cap.length);
+	      bgp_notify_send (peer, BGP_NOTIFY_CEASE, 0);
+	      return -1;
+	    }
+	  if (BGP_DEBUG (normal, NORMAL))
+	    zlog_debug ("%s OPEN has AS4 capability", peer->host);
+
+	  {
+	     /* 
+	      * We did not make a "stream_getl" but read into
+	      * the struct.
+	      * Do the byte swivveling now
+	      */
+	    u_char *gotthis = (u_char *) &cap.mpc;
+	    peer->as4cap = ((u_char) *gotthis++) << 24;
+	    peer->as4cap |= ((u_char) *gotthis++) << 16;
+	    peer->as4cap |= ((u_char) *gotthis++) << 8;
+	    peer->as4cap |= ((u_char) *gotthis);
+	  }
+
+	  if (BGP_DEBUG (as4, AS4))
+	    zlog_debug ("[AS4] %s about to set cap PEER_CAP_AS4_RCV at"
+	                " address %p, value now %d, as4cap is %d", 
+	                peer->host, &peer->cap, peer->cap, peer->as4cap);
+
+	  SET_FLAG (peer->cap, PEER_CAP_AS4_RCV);
+
+	  if (BGP_DEBUG (as4, AS4))
+	    zlog_debug ("[AS4] %s set cap PEER_CAP_AS4_RCV at address %p,"
+	                " value now %d", 
+	                peer->host, &peer->cap, peer->cap);
+	}
  
       else if (cap.code > 128)
 	{
@@ -525,6 +566,92 @@ strict_capability_same (struct peer *peer)
   return 1;
 }
 
+/* peek into option, set as4 value if it is there */
+void peek_for_as4_capability( struct peer *peer, u_char length )
+{
+  u_char *pnt;
+  u_char *end;
+  u_char opt_type;
+  u_char opt_length;
+  struct capability cap;
+
+  pnt = stream_pnt (peer->ibuf);
+  end = pnt + length;
+
+  if (BGP_DEBUG (as4, AS4))
+    zlog_debug ("[AS4] %s rcv OPEN w/ OPTION parameter len: %u, peeking for as4",
+	       peer->host, length);
+  
+  /* the error cases we DONT handle, we ONLY try to read as4 out of
+   * correctly formatted options
+   */
+  while (pnt < end) 
+    {
+
+      /* Check the length. */
+      if (pnt + 2 > end)
+	return;
+
+      /* Fetch option type and length. */
+      opt_type = *pnt++;
+      opt_length = *pnt++;
+
+      /* Option length check. */
+      if (pnt + opt_length > end)
+	return;
+
+      if ( opt_type == BGP_OPEN_OPT_CAP )
+        {
+	  u_char *mypnt = pnt;
+	  u_char mylength = opt_length;
+	  u_char *myend ;
+
+	  myend = mypnt + mylength;
+	  while (mypnt < myend)
+	    {
+	      afi_t afi;
+	      safi_t safi;
+
+	      /* Fetch structure to the byte stream. */
+	      memcpy (&cap, mypnt, sizeof (struct capability));
+
+	      afi = ntohs(cap.mpc.afi);
+	      safi = cap.mpc.safi;
+
+	      if (mypnt + 2 > myend)
+		return;
+	      if (mypnt + (cap.length + 2) > myend)
+		return;
+
+	      if (cap.code == CAPABILITY_CODE_AS4)
+		{
+		  if (cap.length != CAPABILITY_CODE_AS4_LEN)
+		    return;
+		  if (BGP_DEBUG (as4, AS4))
+		    zlog_debug ("[AS4] %s OPEN peeking found AS4 capability", peer->host);
+
+		  {
+		     /* 
+		      * We did not make a "stream_getl"
+		      * Do the byte swivveling now
+		      */
+		    u_char *gotthis = (u_char *) &cap.mpc;
+		    peer->as4cap = ((u_char) *gotthis++) << 24;
+		    peer->as4cap |= ((u_char) *gotthis++) << 16;
+		    peer->as4cap |= ((u_char) *gotthis++) << 8;
+		    peer->as4cap |= ((u_char) *gotthis);
+
+		  }
+		  SET_FLAG (peer->cap, PEER_CAP_AS4_RCV);
+
+		}
+		mypnt += cap.length + 2;
+	    }
+	}
+        pnt += opt_length;
+    }
+}
+
 /* Parse open option */
 int
 bgp_open_option_parse (struct peer *peer, u_char length, int *capability)
@@ -733,6 +860,7 @@ bgp_open_capability (struct stream *s, struct peer *peer)
   unsigned long cp;
   afi_t afi;
   safi_t safi;
+  as_t local_as;
 
   /* Remember current pointer for Opt Parm Len. */
   cp = stream_get_endp (s);
@@ -819,6 +947,18 @@ bgp_open_capability (struct stream *s, struct peer *peer)
   stream_putc (s, CAPABILITY_CODE_REFRESH);
   stream_putc (s, CAPABILITY_CODE_REFRESH_LEN);
 
+  /* AS4 */
+  SET_FLAG (peer->cap, PEER_CAP_AS4_ADV);
+  stream_putc (s, BGP_OPEN_OPT_CAP);
+  stream_putc (s, CAPABILITY_CODE_AS4_LEN + 2);
+  stream_putc (s, CAPABILITY_CODE_AS4);
+  stream_putc (s, CAPABILITY_CODE_AS4_LEN);
+  if ( peer->change_local_as )
+    local_as = peer->change_local_as;
+  else
+    local_as = peer->local_as;
+  stream_putl (s, local_as );
+
   /* ORF capability. */
   for (afi = AFI_IP ; afi < AFI_MAX ; afi++)
     for (safi = SAFI_UNICAST ; safi < SAFI_MAX ; safi++)
diff --git a/bgpd/bgp_open.h b/bgpd/bgp_open.h
index 7515d3f..d30b676 100644
--- a/bgpd/bgp_open.h
+++ b/bgpd/bgp_open.h
@@ -49,7 +49,7 @@ struct graceful_restart_af
 #define CAPABILITY_CODE_REFRESH         2 /* Route Refresh Capability */
 #define CAPABILITY_CODE_ORF             3 /* Cooperative Route Filtering Capability */
 #define CAPABILITY_CODE_RESTART        64 /* Graceful Restart Capability */
-#define CAPABILITY_CODE_4BYTE_AS       65 /* 4-octet AS number Capability */
+#define CAPABILITY_CODE_AS4            65 /* 4-octet AS number Capability */
 #define CAPABILITY_CODE_DYNAMIC        66 /* Dynamic Capability */
 #define CAPABILITY_CODE_REFRESH_OLD   128 /* Route Refresh Capability(cisco) */
 #define CAPABILITY_CODE_ORF_OLD       130 /* Cooperative Route Filtering Capability(cisco) */
@@ -59,6 +59,7 @@ struct graceful_restart_af
 #define CAPABILITY_CODE_REFRESH_LEN     0
 #define CAPABILITY_CODE_DYNAMIC_LEN     0
 #define CAPABILITY_CODE_RESTART_LEN     2 /* Receiving only case */
+#define CAPABILITY_CODE_AS4_LEN         4
 
 /* Cooperative Route Filtering Capability.  */
 
@@ -82,5 +83,6 @@ struct graceful_restart_af
 extern int bgp_open_option_parse (struct peer *, u_char, int *);
 extern void bgp_open_capability (struct stream *, struct peer *);
 extern void bgp_capability_vty_out (struct vty *, struct peer *);
+extern void peek_for_as4_capability (struct peer *, u_char);
 
 #endif /* _QUAGGA_BGP_OPEN_H */
diff --git a/bgpd/bgp_packet.c b/bgpd/bgp_packet.c
index 2653201..530fc47 100644
--- a/bgpd/bgp_packet.c
+++ b/bgpd/bgp_packet.c
@@ -804,7 +804,15 @@ bgp_open_send (struct peer *peer)
 
   /* Set open packet values. */
   stream_putc (s, BGP_VERSION_4);        /* BGP version */
-  stream_putw (s, local_as);		 /* My Autonomous System*/
+  if ( local_as <= BGP_AS_MAX )
+    /* fits in 16 bit, so send real as number in 16 bit */
+    stream_putw (s, (u_int16_t) local_as);	/* My Autonomous System*/
+  else
+    /* does not fit in 16 bit, so as per */
+    /* draft_ietf_idr_as4bytes-13: */
+    /* AS_TRANS is put in the My Autonomous System field */
+    /* of the open message of a NEW BGP speaker */
+    stream_putw (s, BGP_AS_TRANS);		/* My Autonomous System*/
   stream_putw (s, send_holdtime);     	 /* Hold Time */
   stream_put_in_addr (s, &peer->local_id); /* BGP Identifier */
 
@@ -1184,12 +1192,88 @@ bgp_open_receive (struct peer *peer, bgp_size_t size)
   memcpy (notify_data_remote_id, stream_pnt (peer->ibuf), 4);
   remote_id.s_addr = stream_get_ipv4 (peer->ibuf);
 
+  /* BEGIN to read the capability here, but dont do it yet */
+  capability = 0;
+  optlen = stream_getc (peer->ibuf);
+  if (optlen != 0)
+    {
+      /* We got capabilities.  Now, for simplicity, we always read
+       * them when we were through the basic things, i.e. at the end
+       * of this function.
+       * But we need the as4 capability value *right now* because
+       * if it is there, we have not got the remote_as yet, and without
+       * that we do not know which peer is connecting to us now.
+       *
+       * So, keep everything safe, and peek into the capabilites,
+       * and look just for as4!
+       */ 
+      peek_for_as4_capability( peer, optlen );
+    }
+
   /* Receive OPEN message log  */
   if (BGP_DEBUG (normal, NORMAL))
-    zlog_debug ("%s rcv OPEN, version %d, remote-as %d, holdtime %d, id %s",
-	       peer->host, version, remote_as, holdtime,
-	       inet_ntoa (remote_id));
+    zlog_debug ("%s rcv OPEN, version %d, remote-as (in open) %d,"
+                " holdtime %d, id %s",
+	        peer->host, version, remote_as, holdtime,
+	        inet_ntoa (remote_id));
 	  
+  if ( remote_as == BGP_AS_TRANS )
+    {
+      if ( peer->as4cap > BGP_AS_MAX )
+	{
+	  /* Take the AS4 from the capability.
+	   * We must have received the capability now!
+	   * Otherwise we have a asn16 peer who uses BGP_AS_TRANS,
+	   * which is wrong as per the AS4 draft:
+	   * "An OLD BGP speaker MUST NOT use AS_TRANS as
+	   * its Autonomous System number."
+	   * That also means that a NEW BGP speaker can never simulate
+	   * to be an OLD BGP speaker when it has an as number
+	   * larger than 65535.
+	   *
+	   * So... maybe we should tolerate an AS2 neighbor without
+	   * AS4 capability!?  Controversial, this one.
+	   * IF we make "announce AS4 capability" configurable per peer
+	   * at one time that is per se only possible if we have an as
+	   * number smaller 65536.  Otherwise we can not announce ourself
+	   * in case of simulating being only AS2 capable *to another
+	   * AS4 aware equipment which tests for this*.
+	   * Hm... IF we make announcing configurable, we will also make
+	   * tolerating the AS2 AS_TRANS configurable! (jk)
+	   */
+
+	  if ( !CHECK_FLAG (peer->cap, PEER_CAP_AS4_RCV) )
+	    {
+	      zlog_err ("%s bad OPEN, got AS_TRANS but no AS4 capability"
+	                " address peer->cap is %p, value %d",
+	                peer->host, &peer->cap, peer->cap);
+	      /* which error to notify? well, use something generic,
+	       * our log has the right reason
+	       */
+	      bgp_notify_send (peer, BGP_NOTIFY_OPEN_ERR,
+	                       BGP_NOTIFY_OPEN_BAD_PEER_AS);
+	      return -1;
+	    }
+          remote_as = peer->as4cap;
+	}
+    } else {
+      /* We may have a partner with AS4 who has an asno < BGP_AS_MAX */
+      /* If we have got the capability, peer->as4cap must match remote_as */
+      if ( CHECK_FLAG (peer->cap, PEER_CAP_AS4_RCV) &&
+         peer->as4cap != remote_as )
+        {
+	  /* raise error, log this, close session */
+	  zlog_err ("%s bad OPEN, got AS4 capability, but remote_as %u mismatch with 16bit 'myasn' %u in open",
+	      peer->host, peer->as4cap, remote_as);
+	  bgp_notify_send (peer, BGP_NOTIFY_CEASE,
+			   BGP_NOTIFY_CEASE_CONNECT_REJECT);
+	  /* which error to notify? well, use something generic,
+	   * our log has the right reason
+	   */
+	  return -1;
+	}
+    }
+
   /* Lookup peer from Open packet. */
   if (CHECK_FLAG (peer->sflags, PEER_STATUS_ACCEPT_PEER))
     {
@@ -1364,8 +1448,6 @@ bgp_open_receive (struct peer *peer, bgp_size_t size)
   peer->v_keepalive = peer->v_holdtime / 3;
 
   /* Open option part parse. */
-  capability = 0;
-  optlen = stream_getc (peer->ibuf);
   if (optlen != 0) 
     {
       ret = bgp_open_option_parse (peer, optlen, &capability);
diff --git a/bgpd/bgp_route.c b/bgpd/bgp_route.c
index 1c0e6f1..5a102e9 100644
--- a/bgpd/bgp_route.c
+++ b/bgpd/bgp_route.c
@@ -8873,6 +8873,10 @@ bgp_table_stats (struct vty *vty, struct bgp *bgp, afi_t afi, safi_t safi)
                      (float)ts.counts[BGP_STATS_SPACE] / 
                        (float)(1UL << (ts.counts[BGP_STATS_MAXBITLEN] - 24)));
             break;
+	  case BGP_STATS_ASN_HIGHEST:
+	    vty_out (vty, "%-30s: ", table_stats_strs[i]);
+	    vty_out (vty, "%12d", ts.counts[i]);
+	    break;
           default:
             vty_out (vty, "%-30s: ", table_stats_strs[i]);
             vty_out (vty, "%12llu", ts.counts[i]);
diff --git a/bgpd/bgp_routemap.c b/bgpd/bgp_routemap.c
index 6a44c47..134c006 100644
--- a/bgpd/bgp_routemap.c
+++ b/bgpd/bgp_routemap.c
@@ -3245,7 +3245,7 @@ DEFUN (no_set_atomic_aggregate,
 
 DEFUN (set_aggregator_as,
        set_aggregator_as_cmd,
-       "set aggregator as <1-65535> A.B.C.D",
+       "set aggregator as CMD_AS_RANGE A.B.C.D",
        SET_STR
        "BGP aggregator attribute\n"
        "AS number of aggregator\n"
@@ -3257,7 +3257,7 @@ DEFUN (set_aggregator_as,
   struct in_addr address;
   char *argstr;
 
-  VTY_GET_INTEGER_RANGE ("AS Path", as, argv[0], 1, BGP_AS_MAX);
+  VTY_GET_INTEGER_RANGE ("AS", as, argv[0], 1, BGP_AS4_MAX);
   
   ret = inet_aton (argv[1], &address);
   if (ret == 0)
@@ -3294,7 +3294,7 @@ DEFUN (no_set_aggregator_as,
   if (argv == 0)
     return bgp_route_set_delete (vty, vty->index, "aggregator as", NULL);
   
-  VTY_GET_INTEGER_RANGE ("AS Path", as, argv[0], 1, BGP_AS_MAX);
+  VTY_GET_INTEGER_RANGE ("AS", as, argv[0], 1, BGP_AS4_MAX);
 
   ret = inet_aton (argv[1], &address);
   if (ret == 0)
@@ -3317,7 +3317,7 @@ DEFUN (no_set_aggregator_as,
 
 ALIAS (no_set_aggregator_as,
        no_set_aggregator_as_val_cmd,
-       "no set aggregator as <1-65535> A.B.C.D",
+       "no set aggregator as CMD_AS_RANGE A.B.C.D",
        NO_STR
        SET_STR
        "BGP aggregator attribute\n"
diff --git a/bgpd/bgp_vty.c b/bgpd/bgp_vty.c
index 1e21c74..246e0b0 100644
--- a/bgpd/bgp_vty.c
+++ b/bgpd/bgp_vty.c
@@ -308,7 +308,7 @@ DEFUN_DEPRECATED (neighbor_version,
 /* "router bgp" commands. */
 DEFUN (router_bgp, 
        router_bgp_cmd, 
-       "router bgp <1-65535>",
+       "router bgp CMD_AS_RANGE",
        ROUTER_STR
        BGP_STR
        AS_STR)
@@ -318,7 +318,7 @@ DEFUN (router_bgp,
   struct bgp *bgp;
   const char *name = NULL;
 
-  VTY_GET_INTEGER_RANGE ("AS", as, argv[0], 1, 65535);
+  VTY_GET_INTEGER_RANGE ("AS", as, argv[0], 1, BGP_AS4_MAX);
 
   if (argc == 2)
     name = argv[1];
@@ -348,7 +348,7 @@ DEFUN (router_bgp,
 
 ALIAS (router_bgp,
        router_bgp_view_cmd,
-       "router bgp <1-65535> view WORD",
+       "router bgp CMD_AS_RANGE view WORD",
        ROUTER_STR
        BGP_STR
        AS_STR
@@ -358,7 +358,7 @@ ALIAS (router_bgp,
 /* "no router bgp" commands. */
 DEFUN (no_router_bgp,
        no_router_bgp_cmd,
-       "no router bgp <1-65535>",
+       "no router bgp CMD_AS_RANGE",
        NO_STR
        ROUTER_STR
        BGP_STR
@@ -368,7 +368,7 @@ DEFUN (no_router_bgp,
   struct bgp *bgp;
   const char *name = NULL;
 
-  VTY_GET_INTEGER_RANGE ("AS", as, argv[0], 1, 65535);
+  VTY_GET_INTEGER_RANGE ("AS", as, argv[0], 1, BGP_AS4_MAX);
 
   if (argc == 2)
     name = argv[1];
@@ -388,7 +388,7 @@ DEFUN (no_router_bgp,
 
 ALIAS (no_router_bgp,
        no_router_bgp_view_cmd,
-       "no router bgp <1-65535> view WORD",
+       "no router bgp CMD_AS_RANGE view WORD",
        NO_STR
        ROUTER_STR
        BGP_STR
@@ -539,7 +539,7 @@ ALIAS (no_bgp_cluster_id,
 
 DEFUN (bgp_confederation_identifier,
        bgp_confederation_identifier_cmd,
-       "bgp confederation identifier <1-65535>",
+       "bgp confederation identifier CMD_AS_RANGE",
        "BGP specific commands\n"
        "AS confederation parameters\n"
        "AS number\n"
@@ -550,7 +550,7 @@ DEFUN (bgp_confederation_identifier,
 
   bgp = vty->index;
 
-  VTY_GET_INTEGER ("AS", as, argv[0]);
+  VTY_GET_INTEGER_RANGE ("AS", as, argv[0], 1, BGP_AS4_MAX);
 
   bgp_confederation_id_set (bgp, as);
 
@@ -571,7 +571,7 @@ DEFUN (no_bgp_confederation_identifier,
   bgp = vty->index;
 
   if (argc == 1)
-    VTY_GET_INTEGER ("AS", as, argv[0]);
+    VTY_GET_INTEGER_RANGE ("AS", as, argv[0], 1, BGP_AS4_MAX);
 
   bgp_confederation_id_unset (bgp);
 
@@ -580,7 +580,7 @@ DEFUN (no_bgp_confederation_identifier,
 
 ALIAS (no_bgp_confederation_identifier,
        no_bgp_confederation_identifier_arg_cmd,
-       "no bgp confederation identifier <1-65535>",
+       "no bgp confederation identifier CMD_AS_RANGE",
        NO_STR
        "BGP specific commands\n"
        "AS confederation parameters\n"
@@ -589,7 +589,7 @@ ALIAS (no_bgp_confederation_identifier,
 
 DEFUN (bgp_confederation_peers,
        bgp_confederation_peers_cmd,
-       "bgp confederation peers .<1-65535>",
+       "bgp confederation peers .CMD_AS_RANGE",
        "BGP specific commands\n"
        "AS confederation parameters\n"
        "Peer ASs in BGP confederation\n"
@@ -603,7 +603,7 @@ DEFUN (bgp_confederation_peers,
 
   for (i = 0; i < argc; i++)
     {
-      VTY_GET_INTEGER_RANGE ("AS", as, argv[i], 1, 65535);
+      VTY_GET_INTEGER_RANGE ("AS", as, argv[i], 1, BGP_AS4_MAX);
 
       if (bgp->as == as)
 	{
@@ -619,7 +619,7 @@ DEFUN (bgp_confederation_peers,
 
 DEFUN (no_bgp_confederation_peers,
        no_bgp_confederation_peers_cmd,
-       "no bgp confederation peers .<1-65535>",
+       "no bgp confederation peers .CMD_AS_RANGE",
        NO_STR
        "BGP specific commands\n"
        "AS confederation parameters\n"
@@ -634,8 +634,8 @@ DEFUN (no_bgp_confederation_peers,
 
   for (i = 0; i < argc; i++)
     {
-      VTY_GET_INTEGER_RANGE ("AS", as, argv[i], 1, 65535);
-      
+      VTY_GET_INTEGER_RANGE ("AS", as, argv[i], 1, BGP_AS4_MAX);
+
       bgp_confederation_peers_remove (bgp, as);
     }
   return CMD_SUCCESS;
@@ -1249,7 +1249,7 @@ peer_remote_as_vty (struct vty *vty, const char *peer_str,
   bgp = vty->index;
 
   /* Get AS number.  */
-  VTY_GET_INTEGER_RANGE ("AS", as, as_str, 1, 65535);
+  VTY_GET_INTEGER_RANGE ("AS", as, as_str, 1, BGP_AS4_MAX);
 
   /* If peer is peer group, call proper function.  */
   ret = str2sockunion (peer_str, &su);
@@ -1288,7 +1288,7 @@ peer_remote_as_vty (struct vty *vty, const char *peer_str,
 
 DEFUN (neighbor_remote_as,
        neighbor_remote_as_cmd,
-       NEIGHBOR_CMD2 "remote-as <1-65535>",
+       NEIGHBOR_CMD2 "remote-as CMD_AS_RANGE",
        NEIGHBOR_STR
        NEIGHBOR_ADDR_STR2
        "Specify a BGP neighbor\n"
@@ -1352,7 +1352,7 @@ DEFUN (no_neighbor,
 
 ALIAS (no_neighbor,
        no_neighbor_remote_as_cmd,
-       NO_NEIGHBOR_CMD "remote-as <1-65535>",
+       NO_NEIGHBOR_CMD "remote-as CMD_AS_RANGE",
        NO_STR
        NEIGHBOR_STR
        NEIGHBOR_ADDR_STR
@@ -1382,7 +1382,7 @@ DEFUN (no_neighbor_peer_group,
 
 DEFUN (no_neighbor_peer_group_remote_as,
        no_neighbor_peer_group_remote_as_cmd,
-       "no neighbor WORD remote-as <1-65535>",
+       "no neighbor WORD remote-as CMD_AS_RANGE",
        NO_STR
        NEIGHBOR_STR
        "Neighbor tag\n"
@@ -1404,7 +1404,7 @@ DEFUN (no_neighbor_peer_group_remote_as,
 
 DEFUN (neighbor_local_as,
        neighbor_local_as_cmd,
-       NEIGHBOR_CMD2 "local-as <1-65535>",
+       NEIGHBOR_CMD2 "local-as CMD_AS_RANGE",
        NEIGHBOR_STR
        NEIGHBOR_ADDR_STR2
        "Specify a local-as number\n"
@@ -1423,7 +1423,7 @@ DEFUN (neighbor_local_as,
 
 DEFUN (neighbor_local_as_no_prepend,
        neighbor_local_as_no_prepend_cmd,
-       NEIGHBOR_CMD2 "local-as <1-65535> no-prepend",
+       NEIGHBOR_CMD2 "local-as CMD_AS_RANGE no-prepend",
        NEIGHBOR_STR
        NEIGHBOR_ADDR_STR2
        "Specify a local-as number\n"
@@ -1462,7 +1462,7 @@ DEFUN (no_neighbor_local_as,
 
 ALIAS (no_neighbor_local_as,
        no_neighbor_local_as_val_cmd,
-       NO_NEIGHBOR_CMD2 "local-as <1-65535>",
+       NO_NEIGHBOR_CMD2 "local-as CMD_AS_RANGE",
        NO_STR
        NEIGHBOR_STR
        NEIGHBOR_ADDR_STR2
@@ -1471,7 +1471,7 @@ ALIAS (no_neighbor_local_as,
 
 ALIAS (no_neighbor_local_as,
        no_neighbor_local_as_val2_cmd,
-       NO_NEIGHBOR_CMD2 "local-as <1-65535> no-prepend",
+       NO_NEIGHBOR_CMD2 "local-as CMD_AS_RANGE no-prepend",
        NO_STR
        NEIGHBOR_STR
        NEIGHBOR_ADDR_STR2
@@ -4037,7 +4037,7 @@ bgp_clear (struct vty *vty, struct bgp *bgp,  afi_t afi, safi_t safi,
 	  if (ret < 0)
 	    bgp_clear_vty_error (vty, peer, afi, safi, ret);
 	}
-      return 0;
+      return CMD_SUCCESS;
     }
 
   /* Clear specified neighbors. */
@@ -4051,13 +4051,13 @@ bgp_clear (struct vty *vty, struct bgp *bgp,  afi_t afi, safi_t safi,
       if (ret < 0)
 	{
 	  vty_out (vty, "Malformed address: %s%s", arg, VTY_NEWLINE);
-	  return -1;
+	  return CMD_WARNING;
 	}
       peer = peer_lookup (bgp, &su);
       if (! peer)
 	{
 	  vty_out (vty, "%%BGP: Unknown neighbor - \"%s\"%s", arg, VTY_NEWLINE);
-	  return -1;
+	  return CMD_WARNING;
 	}
 
       if (stype == BGP_CLEAR_SOFT_NONE)
@@ -4068,7 +4068,7 @@ bgp_clear (struct vty *vty, struct bgp *bgp,  afi_t afi, safi_t safi,
       if (ret < 0)
 	bgp_clear_vty_error (vty, peer, afi, safi, ret);
 
-      return 0;
+      return CMD_SUCCESS;
     }
 
   /* Clear all peer-group members. */
@@ -4080,7 +4080,7 @@ bgp_clear (struct vty *vty, struct bgp *bgp,  afi_t afi, safi_t safi,
       if (! group)
 	{
 	  vty_out (vty, "%%BGP: No such peer-group %s%s", arg, VTY_NEWLINE);
-	  return -1; 
+	  return CMD_WARNING; 
 	}
 
       for (ALL_LIST_ELEMENTS (group->peer, node, nnode, peer))
@@ -4099,7 +4099,7 @@ bgp_clear (struct vty *vty, struct bgp *bgp,  afi_t afi, safi_t safi,
 	  if (ret < 0)
 	    bgp_clear_vty_error (vty, peer, afi, safi, ret);
 	}
-      return 0;
+      return CMD_SUCCESS;
     }
 
   if (sort == clear_external)
@@ -4117,22 +4117,21 @@ bgp_clear (struct vty *vty, struct bgp *bgp,  afi_t afi, safi_t safi,
 	  if (ret < 0)
 	    bgp_clear_vty_error (vty, peer, afi, safi, ret);
 	}
-      return 0;
+      return CMD_SUCCESS;
     }
 
   if (sort == clear_as)
     {
       as_t as;
       unsigned long as_ul;
-      char *endptr = NULL;
       int find = 0;
 
-      as_ul = strtoul(arg, &endptr, 10);
-
-      if ((as_ul == ULONG_MAX) || (*endptr != '\0') || (as_ul > USHRT_MAX))
+      VTY_GET_LONG ("AS", as_ul, arg);
+      
+      if (!as_ul)
 	{
 	  vty_out (vty, "Invalid AS number%s", VTY_NEWLINE); 
-	  return -1;
+	  return CMD_WARNING;
 	}
       as = (as_t) as_ul;
 
@@ -4153,10 +4152,10 @@ bgp_clear (struct vty *vty, struct bgp *bgp,  afi_t afi, safi_t safi,
       if (! find)
 	vty_out (vty, "%%BGP: No peer is configured with AS %s%s", arg,
 		 VTY_NEWLINE);
-      return 0;
+      return CMD_SUCCESS;
     }
 
-  return 0;
+  return CMD_SUCCESS;
 }
 
 static int
@@ -4187,11 +4186,7 @@ bgp_clear_vty (struct vty *vty, const char *name, afi_t afi, safi_t safi,
         }
     }
 
-  ret =  bgp_clear (vty, bgp, afi, safi, sort, stype, arg);
-  if (ret < 0)
-    return CMD_WARNING;
-
-  return CMD_SUCCESS;
+  return bgp_clear (vty, bgp, afi, safi, sort, stype, arg);
 }
   
 DEFUN (clear_ip_bgp_all,
@@ -4328,7 +4323,7 @@ ALIAS (clear_ip_bgp_external,
 
 DEFUN (clear_ip_bgp_as,
        clear_ip_bgp_as_cmd,
-       "clear ip bgp <1-65535>",
+       "clear ip bgp CMD_AS_RANGE",
        CLEAR_STR
        IP_STR
        BGP_STR
@@ -4339,14 +4334,14 @@ DEFUN (clear_ip_bgp_as,
 
 ALIAS (clear_ip_bgp_as,
        clear_bgp_as_cmd,
-       "clear bgp <1-65535>",
+       "clear bgp CMD_AS_RANGE",
        CLEAR_STR
        BGP_STR
        "Clear peers with the AS number\n")
 
 ALIAS (clear_ip_bgp_as,
        clear_bgp_ipv6_as_cmd,
-       "clear bgp ipv6 <1-65535>",
+       "clear bgp ipv6 CMD_AS_RANGE",
        CLEAR_STR
        BGP_STR
        "Address family\n"
@@ -4858,7 +4853,7 @@ ALIAS (clear_bgp_external_soft_out,
 
 DEFUN (clear_ip_bgp_as_soft_out,
        clear_ip_bgp_as_soft_out_cmd,
-       "clear ip bgp <1-65535> soft out",
+       "clear ip bgp CMD_AS_RANGE soft out",
        CLEAR_STR
        IP_STR
        BGP_STR
@@ -4872,7 +4867,7 @@ DEFUN (clear_ip_bgp_as_soft_out,
 
 ALIAS (clear_ip_bgp_as_soft_out,
        clear_ip_bgp_as_out_cmd,
-       "clear ip bgp <1-65535> out",
+       "clear ip bgp CMD_AS_RANGE out",
        CLEAR_STR
        IP_STR
        BGP_STR
@@ -4881,7 +4876,7 @@ ALIAS (clear_ip_bgp_as_soft_out,
 
 DEFUN (clear_ip_bgp_as_ipv4_soft_out,
        clear_ip_bgp_as_ipv4_soft_out_cmd,
-       "clear ip bgp <1-65535> ipv4 (unicast|multicast) soft out",
+       "clear ip bgp CMD_AS_RANGE ipv4 (unicast|multicast) soft out",
        CLEAR_STR
        IP_STR
        BGP_STR
@@ -4902,7 +4897,7 @@ DEFUN (clear_ip_bgp_as_ipv4_soft_out,
 
 ALIAS (clear_ip_bgp_as_ipv4_soft_out,
        clear_ip_bgp_as_ipv4_out_cmd,
-       "clear ip bgp <1-65535> ipv4 (unicast|multicast) out",
+       "clear ip bgp CMD_AS_RANGE ipv4 (unicast|multicast) out",
        CLEAR_STR
        IP_STR
        BGP_STR
@@ -4914,7 +4909,7 @@ ALIAS (clear_ip_bgp_as_ipv4_soft_out,
 
 DEFUN (clear_ip_bgp_as_vpnv4_soft_out,
        clear_ip_bgp_as_vpnv4_soft_out_cmd,
-       "clear ip bgp <1-65535> vpnv4 unicast soft out",
+       "clear ip bgp CMD_AS_RANGE vpnv4 unicast soft out",
        CLEAR_STR
        IP_STR
        BGP_STR
@@ -4930,7 +4925,7 @@ DEFUN (clear_ip_bgp_as_vpnv4_soft_out,
 
 ALIAS (clear_ip_bgp_as_vpnv4_soft_out,
        clear_ip_bgp_as_vpnv4_out_cmd,
-       "clear ip bgp <1-65535> vpnv4 unicast out",
+       "clear ip bgp CMD_AS_RANGE vpnv4 unicast out",
        CLEAR_STR
        IP_STR
        BGP_STR
@@ -4941,7 +4936,7 @@ ALIAS (clear_ip_bgp_as_vpnv4_soft_out,
 
 DEFUN (clear_bgp_as_soft_out,
        clear_bgp_as_soft_out_cmd,
-       "clear bgp <1-65535> soft out",
+       "clear bgp CMD_AS_RANGE soft out",
        CLEAR_STR
        BGP_STR
        "Clear peers with the AS number\n"
@@ -4954,7 +4949,7 @@ DEFUN (clear_bgp_as_soft_out,
 
 ALIAS (clear_bgp_as_soft_out,
        clear_bgp_ipv6_as_soft_out_cmd,
-       "clear bgp ipv6 <1-65535> soft out",
+       "clear bgp ipv6 CMD_AS_RANGE soft out",
        CLEAR_STR
        BGP_STR
        "Address family\n"
@@ -4964,7 +4959,7 @@ ALIAS (clear_bgp_as_soft_out,
 
 ALIAS (clear_bgp_as_soft_out,
        clear_bgp_as_out_cmd,
-       "clear bgp <1-65535> out",
+       "clear bgp CMD_AS_RANGE out",
        CLEAR_STR
        BGP_STR
        "Clear peers with the AS number\n"
@@ -4972,7 +4967,7 @@ ALIAS (clear_bgp_as_soft_out,
 
 ALIAS (clear_bgp_as_soft_out,
        clear_bgp_ipv6_as_out_cmd,
-       "clear bgp ipv6 <1-65535> out",
+       "clear bgp ipv6 CMD_AS_RANGE out",
        CLEAR_STR
        BGP_STR
        "Address family\n"
@@ -5762,7 +5757,7 @@ ALIAS (clear_bgp_external_in_prefix_filter,
 
 DEFUN (clear_ip_bgp_as_soft_in,
        clear_ip_bgp_as_soft_in_cmd,
-       "clear ip bgp <1-65535> soft in",
+       "clear ip bgp CMD_AS_RANGE soft in",
        CLEAR_STR
        IP_STR
        BGP_STR
@@ -5776,7 +5771,7 @@ DEFUN (clear_ip_bgp_as_soft_in,
 
 ALIAS (clear_ip_bgp_as_soft_in,
        clear_ip_bgp_as_in_cmd,
-       "clear ip bgp <1-65535> in",
+       "clear ip bgp CMD_AS_RANGE in",
        CLEAR_STR
        IP_STR
        BGP_STR
@@ -5785,7 +5780,7 @@ ALIAS (clear_ip_bgp_as_soft_in,
 
 DEFUN (clear_ip_bgp_as_in_prefix_filter,
        clear_ip_bgp_as_in_prefix_filter_cmd,
-       "clear ip bgp <1-65535> in prefix-filter",
+       "clear ip bgp CMD_AS_RANGE in prefix-filter",
        CLEAR_STR
        IP_STR
        BGP_STR
@@ -5799,7 +5794,7 @@ DEFUN (clear_ip_bgp_as_in_prefix_filter,
 
 DEFUN (clear_ip_bgp_as_ipv4_soft_in,
        clear_ip_bgp_as_ipv4_soft_in_cmd,
-       "clear ip bgp <1-65535> ipv4 (unicast|multicast) soft in",
+       "clear ip bgp CMD_AS_RANGE ipv4 (unicast|multicast) soft in",
        CLEAR_STR
        IP_STR
        BGP_STR
@@ -5820,7 +5815,7 @@ DEFUN (clear_ip_bgp_as_ipv4_soft_in,
 
 ALIAS (clear_ip_bgp_as_ipv4_soft_in,
        clear_ip_bgp_as_ipv4_in_cmd,
-       "clear ip bgp <1-65535> ipv4 (unicast|multicast) in",
+       "clear ip bgp CMD_AS_RANGE ipv4 (unicast|multicast) in",
        CLEAR_STR
        IP_STR
        BGP_STR
@@ -5832,7 +5827,7 @@ ALIAS (clear_ip_bgp_as_ipv4_soft_in,
 
 DEFUN (clear_ip_bgp_as_ipv4_in_prefix_filter,
        clear_ip_bgp_as_ipv4_in_prefix_filter_cmd,
-       "clear ip bgp <1-65535> ipv4 (unicast|multicast) in prefix-filter",
+       "clear ip bgp CMD_AS_RANGE ipv4 (unicast|multicast) in prefix-filter",
        CLEAR_STR
        IP_STR
        BGP_STR
@@ -5853,7 +5848,7 @@ DEFUN (clear_ip_bgp_as_ipv4_in_prefix_filter,
 
 DEFUN (clear_ip_bgp_as_vpnv4_soft_in,
        clear_ip_bgp_as_vpnv4_soft_in_cmd,
-       "clear ip bgp <1-65535> vpnv4 unicast soft in",
+       "clear ip bgp CMD_AS_RANGE vpnv4 unicast soft in",
        CLEAR_STR
        IP_STR
        BGP_STR
@@ -5869,7 +5864,7 @@ DEFUN (clear_ip_bgp_as_vpnv4_soft_in,
 
 ALIAS (clear_ip_bgp_as_vpnv4_soft_in,
        clear_ip_bgp_as_vpnv4_in_cmd,
-       "clear ip bgp <1-65535> vpnv4 unicast in",
+       "clear ip bgp CMD_AS_RANGE vpnv4 unicast in",
        CLEAR_STR
        IP_STR
        BGP_STR
@@ -5880,7 +5875,7 @@ ALIAS (clear_ip_bgp_as_vpnv4_soft_in,
 
 DEFUN (clear_bgp_as_soft_in,
        clear_bgp_as_soft_in_cmd,
-       "clear bgp <1-65535> soft in",
+       "clear bgp CMD_AS_RANGE soft in",
        CLEAR_STR
        BGP_STR
        "Clear peers with the AS number\n"
@@ -5893,7 +5888,7 @@ DEFUN (clear_bgp_as_soft_in,
 
 ALIAS (clear_bgp_as_soft_in,
        clear_bgp_ipv6_as_soft_in_cmd,
-       "clear bgp ipv6 <1-65535> soft in",
+       "clear bgp ipv6 CMD_AS_RANGE soft in",
        CLEAR_STR
        BGP_STR
        "Address family\n"
@@ -5903,7 +5898,7 @@ ALIAS (clear_bgp_as_soft_in,
 
 ALIAS (clear_bgp_as_soft_in,
        clear_bgp_as_in_cmd,
-       "clear bgp <1-65535> in",
+       "clear bgp CMD_AS_RANGE in",
        CLEAR_STR
        BGP_STR
        "Clear peers with the AS number\n"
@@ -5911,7 +5906,7 @@ ALIAS (clear_bgp_as_soft_in,
 
 ALIAS (clear_bgp_as_soft_in,
        clear_bgp_ipv6_as_in_cmd,
-       "clear bgp ipv6 <1-65535> in",
+       "clear bgp ipv6 CMD_AS_RANGE in",
        CLEAR_STR
        BGP_STR
        "Address family\n"
@@ -5920,7 +5915,7 @@ ALIAS (clear_bgp_as_soft_in,
 
 DEFUN (clear_bgp_as_in_prefix_filter,
        clear_bgp_as_in_prefix_filter_cmd,
-       "clear bgp <1-65535> in prefix-filter",
+       "clear bgp CMD_AS_RANGE in prefix-filter",
        CLEAR_STR
        BGP_STR
        "Clear peers with the AS number\n"
@@ -5933,7 +5928,7 @@ DEFUN (clear_bgp_as_in_prefix_filter,
 
 ALIAS (clear_bgp_as_in_prefix_filter,
        clear_bgp_ipv6_as_in_prefix_filter_cmd,
-       "clear bgp ipv6 <1-65535> in prefix-filter",
+       "clear bgp ipv6 CMD_AS_RANGE in prefix-filter",
        CLEAR_STR
        BGP_STR
        "Address family\n"
@@ -6248,7 +6243,7 @@ ALIAS (clear_bgp_external_soft,
 
 DEFUN (clear_ip_bgp_as_soft,
        clear_ip_bgp_as_soft_cmd,
-       "clear ip bgp <1-65535> soft",
+       "clear ip bgp CMD_AS_RANGE soft",
        CLEAR_STR
        IP_STR
        BGP_STR
@@ -6261,7 +6256,7 @@ DEFUN (clear_ip_bgp_as_soft,
 
 DEFUN (clear_ip_bgp_as_ipv4_soft,
        clear_ip_bgp_as_ipv4_soft_cmd,
-       "clear ip bgp <1-65535> ipv4 (unicast|multicast) soft",
+       "clear ip bgp CMD_AS_RANGE ipv4 (unicast|multicast) soft",
        CLEAR_STR
        IP_STR
        BGP_STR
@@ -6281,7 +6276,7 @@ DEFUN (clear_ip_bgp_as_ipv4_soft,
 
 DEFUN (clear_ip_bgp_as_vpnv4_soft,
        clear_ip_bgp_as_vpnv4_soft_cmd,
-       "clear ip bgp <1-65535> vpnv4 unicast soft",
+       "clear ip bgp CMD_AS_RANGE vpnv4 unicast soft",
        CLEAR_STR
        IP_STR
        BGP_STR
@@ -6296,7 +6291,7 @@ DEFUN (clear_ip_bgp_as_vpnv4_soft,
 
 DEFUN (clear_bgp_as_soft,
        clear_bgp_as_soft_cmd,
-       "clear bgp <1-65535> soft",
+       "clear bgp CMD_AS_RANGE soft",
        CLEAR_STR
        BGP_STR
        "Clear peers with the AS number\n"
@@ -6308,7 +6303,7 @@ DEFUN (clear_bgp_as_soft,
 
 ALIAS (clear_bgp_as_soft,
        clear_bgp_ipv6_as_soft_cmd,
-       "clear bgp ipv6 <1-65535> soft",
+       "clear bgp ipv6 CMD_AS_RANGE soft",
        CLEAR_STR
        BGP_STR
        "Address family\n"
@@ -6681,7 +6676,7 @@ bgp_show_summary (struct vty *vty, struct bgp *bgp, int afi, int safi)
 
 	  vty_out (vty, "4 ");
 
-	  vty_out (vty, "%5d %7d %7d %8d %4d %4ld ",
+	  vty_out (vty, "%6d %7d %7d %8d %4d %4ld ",
 		   peer->as,
 		   peer->open_in + peer->update_in + peer->keepalive_in
 		   + peer->notify_in + peer->refresh_in + peer->dynamic_cap_in,
@@ -7271,6 +7266,18 @@ bgp_show_peer (struct vty *vty, struct peer *p)
 	{
 	  vty_out (vty, "  Neighbor capabilities:%s", VTY_NEWLINE);
 
+	  /* AS4 */
+	  if (CHECK_FLAG (p->cap, PEER_CAP_AS4_RCV)
+	      || CHECK_FLAG (p->cap, PEER_CAP_AS4_ADV))
+	    {
+	      vty_out (vty, "    4 Byte AS:");
+	      if (CHECK_FLAG (p->cap, PEER_CAP_AS4_ADV))
+		vty_out (vty, " advertised");
+	      if (CHECK_FLAG (p->cap, PEER_CAP_AS4_RCV))
+		vty_out (vty, " %sreceived",
+			 CHECK_FLAG (p->cap, PEER_CAP_AS4_ADV) ? "and " : "");
+	      vty_out (vty, "%s", VTY_NEWLINE);
+	    }
 	  /* Dynamic */
 	  if (CHECK_FLAG (p->cap, PEER_CAP_DYNAMIC_RCV)
 	      || CHECK_FLAG (p->cap, PEER_CAP_DYNAMIC_ADV))
@@ -7907,7 +7914,7 @@ bgp_write_rsclient_summary (struct vty *vty, struct peer *rsclient,
 
   vty_out (vty, "4 ");
 
-  vty_out (vty, "%5d ", rsclient->as);
+  vty_out (vty, "%11d ", rsclient->as);
 
   rmname = ROUTE_MAP_EXPORT_NAME(&rsclient->filter[afi][safi]);
   if ( rmname && strlen (rmname) > 13 )
diff --git a/bgpd/bgp_vty.h b/bgpd/bgp_vty.h
index d3b4e2b..2df8aaa 100644
--- a/bgpd/bgp_vty.h
+++ b/bgpd/bgp_vty.h
@@ -21,6 +21,8 @@ Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
 #ifndef _QUAGGA_BGP_VTY_H
 #define _QUAGGA_BGP_VTY_H
 
+#define CMD_AS_RANGE "<1-4294967295>"
+
 extern void bgp_vty_init (void);
 extern const char *afi_safi_print (afi_t, safi_t);
 
diff --git a/bgpd/bgpd.c b/bgpd/bgpd.c
index 9852d65..e77014f 100644
--- a/bgpd/bgpd.c
+++ b/bgpd/bgpd.c
@@ -2104,10 +2104,22 @@ peer_lookup_with_open (union sockunion *su, as_t remote_as,
       if (sockunion_same (&peer->su, su)
 	  && ! CHECK_FLAG (peer->sflags, PEER_STATUS_ACCEPT_PEER))
 	{
-	  if (peer->as == remote_as
-	      && peer->remote_id.s_addr == remote_id->s_addr)
+	  /* XXX_AS4: Cack, why on earth are we storing protocol
+	   * details in struct peer?
+	   */
+	  if ( (peer->as == remote_as
+	        || (peer->as == BGP_AS_TRANS 
+	            && peer->as4cap == remote_as 
+	            && CHECK_FLAG (peer->cap, PEER_CAP_AS4_RCV)))
+	       && peer->remote_id.s_addr == remote_id->s_addr)
 	    return peer;
-	  if (peer->as == remote_as)
+	  if ( (peer->as == remote_as
+	        || (peer->as == BGP_AS_TRANS &&
+				peer->as4cap == remote_as &&
+				CHECK_FLAG (peer->cap, PEER_CAP_AS4_RCV))
+
+	       )
+			  )
 	    *as = 1;
 	}
     }
@@ -2116,10 +2128,19 @@ peer_lookup_with_open (union sockunion *su, as_t remote_as,
       if (sockunion_same (&peer->su, su)
 	  &&  ! CHECK_FLAG (peer->sflags, PEER_STATUS_ACCEPT_PEER))
 	{
-	  if (peer->as == remote_as
+	  /* XXX_AS4: as4cap rubbish again.. */
+	  if ( ( peer->as == remote_as
+			|| (peer->as == BGP_AS_TRANS &&
+				peer->as4cap == remote_as &&
+				CHECK_FLAG (peer->cap, PEER_CAP_AS4_RCV))
+	       )
 	      && peer->remote_id.s_addr == 0)
 	    return peer;
-	  if (peer->as == remote_as)
+	  if ( peer->as == remote_as
+			|| (peer->as == BGP_AS_TRANS &&
+				peer->as4cap == remote_as &&
+				CHECK_FLAG (peer->cap, PEER_CAP_AS4_RCV))
+			  )
 	    *as = 1;
 	}
     }
diff --git a/bgpd/bgpd.h b/bgpd/bgpd.h
index 8b180a4..35ed196 100644
--- a/bgpd/bgpd.h
+++ b/bgpd/bgpd.h
@@ -25,7 +25,8 @@ Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
 #include "sockunion.h"
 
 /* Typedef BGP specific types.  */
-typedef u_int16_t as_t;
+typedef u_int32_t as_t;
+typedef u_int16_t as16_t; /* we may still encounter 16 Bit asnums */
 typedef u_int16_t bgp_size_t;
 
 /* BGP master for system wide configurations and variables.  */
@@ -269,6 +270,9 @@ struct peer
   /* Peer's Change local AS number. */
   as_t change_local_as;
 
+  /* Peer's AS number as received with AS4Capability */
+  as_t as4cap;
+
   /* Remote router ID. */
   struct in_addr remote_id;
 
@@ -287,6 +291,9 @@ struct peer
   int status;
   int ostatus;
 
+  /* Peer index, used for dumping TABLE_DUMP_V2 format */
+  uint16_t table_dump_index;
+
   /* Peer information */
   int fd;			/* File descriptor */
   int ttl;			/* TTL of TCP connection to the peer. */
@@ -316,7 +323,7 @@ struct peer
   u_char afc_recv[AFI_MAX][SAFI_MAX];
 
   /* Capability flags (reset in bgp_stop) */
-  u_char cap;
+  u_int16_t cap;
 #define PEER_CAP_REFRESH_ADV                (1 << 0) /* refresh advertised */
 #define PEER_CAP_REFRESH_OLD_RCV            (1 << 1) /* refresh old received */
 #define PEER_CAP_REFRESH_NEW_RCV            (1 << 2) /* refresh rfc received */
@@ -324,6 +331,8 @@ struct peer
 #define PEER_CAP_DYNAMIC_RCV                (1 << 4) /* dynamic received */
 #define PEER_CAP_RESTART_ADV                (1 << 5) /* restart advertised */
 #define PEER_CAP_RESTART_RCV                (1 << 6) /* restart received */
+#define PEER_CAP_AS4_ADV                    (1 << 7) /* as4 advertised */
+#define PEER_CAP_AS4_RCV                    (1 << 8) /* as4 received */
 
   /* Capability flags (reset in bgp_stop) */
   u_int16_t af_cap[AFI_MAX][SAFI_MAX];
@@ -591,6 +600,8 @@ struct bgp_nlri
 #define BGP_ATTR_MP_REACH_NLRI                  14
 #define BGP_ATTR_MP_UNREACH_NLRI                15
 #define BGP_ATTR_EXT_COMMUNITIES                16
+#define BGP_ATTR_AS4_PATH                       17
+#define BGP_ATTR_AS4_AGGREGATOR                 18
 
 /* BGP update origin.  */
 #define BGP_ORIGIN_IGP                           0
diff --git a/doc/BGP-TypeCode b/doc/BGP-TypeCode
index 0904e19..92343e4 100644
--- a/doc/BGP-TypeCode
+++ b/doc/BGP-TypeCode
@@ -19,6 +19,8 @@
    14    MP_REACH_NLRI        [RFC 2283]
    15    MP_UNREACH_NLRI      [RFC 2283]
    16    EXT_COMMUNITIES      [draft-ramachandra-bgp-ext-communities-09.txt]
+   17    AS4_PATH             [draft-ietf-idr-as4bytes-13.txt]
+   18    AS4_AGGREGATOR       [draft-ietf-idr-as4bytes-13.txt]
   254    RCID_PATH            [RFC 1863]
   255    ADVERTISER           [RFC 1863]
 =========================================================================
diff --git a/tests/ChangeLog b/tests/ChangeLog
index 850fd54..32c9570 100644
--- a/tests/ChangeLog
+++ b/tests/ChangeLog
@@ -1,3 +1,7 @@
+2006-12-01 Juergen Kammer <j.kammer at eurodata.de>
+
+	* aspath_test.c: Support asn32 changes, call aspath_parse with 16 bit.
+
 2006-08-26 Paul Jakma <paul.jakma at sun.com>
 
 	* heavy-wq.c: (slow_func_del,slow_func) update to match workqueue
diff --git a/tests/aspath_test.c b/tests/aspath_test.c
index 1d28dbe..8a7c608 100644
--- a/tests/aspath_test.c
+++ b/tests/aspath_test.c
@@ -316,6 +316,43 @@ static struct test_segment {
       "8466 3 52737 4096 3456 {7099,8153,8153,8153}",
       6, 0, NOT_ALL_PRIVATE, 4096, 4, 8466 },
   },
+  { /* 18 */
+    "reconcile_lead_asp",
+    "seq(6435,59408,21665) set(23456,23456,23456), seq(23456,23456,23456)",
+    { 0x2,0x3, 0x19,0x23, 0xe8,0x10, 0x54,0xa1,
+      0x1,0x3, 0x5b,0xa0, 0x5b,0xa0, 0x5b,0xa0,
+      0x2,0x3, 0x5b,0xa0, 0x5b,0xa0, 0x5b,0xa0 },
+    24,
+    { "6435 59408 21665 {23456,23456,23456} 23456 23456 23456",
+      "6435 59408 21665 {23456,23456,23456} 23456 23456 23456",
+      7, 0, NOT_ALL_PRIVATE, 23456, 1, 6435 },
+  },
+  { /* 19 */
+    "reconcile_new_asp",
+    "set(2457,61697,4369), seq(1842,41591,51793)",
+    { 
+      0x1,0x3, 0x09,0x99, 0xf1,0x01, 0x11,0x11,
+      0x2,0x3, 0x07,0x32, 0xa2,0x77, 0xca,0x51 },
+    16,
+    { "{2457,4369,61697} 1842 41591 51793",
+      "{2457,4369,61697} 1842 41591 51793",
+      4, 0, NOT_ALL_PRIVATE, 51793, 1, 2457 },
+  },
+  { /* 20 */
+    "reconcile_confed",
+    "confseq(123,456,789) confset(456,124,788) seq(6435,59408,21665)"
+    " set(23456,23456,23456), seq(23456,23456,23456)",
+    { 0x3,0x3, 0x00,0x7b, 0x01,0xc8, 0x03,0x15,
+      0x4,0x3, 0x01,0xc8, 0x00,0x7c, 0x03,0x14,
+      0x2,0x3, 0x19,0x23, 0xe8,0x10, 0x54,0xa1,
+      0x1,0x3, 0x5b,0xa0, 0x5b,0xa0, 0x5b,0xa0,
+      0x2,0x3, 0x5b,0xa0, 0x5b,0xa0, 0x5b,0xa0 },
+    40,
+    { "(123 456 789) [124,456,788] 6435 59408 21665"
+      " {23456,23456,23456} 23456 23456 23456",
+      "6435 59408 21665 {23456,23456,23456} 23456 23456 23456",
+      7, 4, NOT_ALL_PRIVATE, 23456, 1, 6435 },
+  },
   { NULL, NULL, {0}, 0, { NULL, 0, 0 } }
 };
 
@@ -432,13 +469,32 @@ static struct tests {
   { NULL, NULL, { NULL, 0, 0, 0, 0, 0, 0, } },
 };
 
-struct tests aggregate_tests[] =
+struct tests reconcile_tests[] =
 {
-  { &test_segments[0], &test_segments[1],
-    { "{3,4,4096,8466,8722,52737}",
-      "{3,4,4096,8466,8722,52737}",
-      1, 0, NOT_ALL_PRIVATE, 52737, 1, NULL_ASN },
+  { &test_segments[18], &test_segments[19],
+    { "6435 59408 21665 {2457,4369,61697} 1842 41591 51793",
+      "6435 59408 21665 {2457,4369,61697} 1842 41591 51793",
+      7, 0, NOT_ALL_PRIVATE, 51793, 1, 6435 },
+  },
+  { &test_segments[19], &test_segments[18],
+    /* AS_PATH (19) has more hops than NEW_AS_PATH,
+     * so just AS_PATH should be used
+     */
+    { "{2457,4369,61697} 1842 41591 51793",
+      "{2457,4369,61697} 1842 41591 51793",
+      4, 0, NOT_ALL_PRIVATE, 51793, 1, 6435 },
   },
+  { &test_segments[20], &test_segments[19],
+    { "(123 456 789) [124,456,788] 6435 59408 21665"
+      " {2457,4369,61697} 1842 41591 51793",
+      "6435 59408 21665 {2457,4369,61697} 1842 41591 51793",
+      7, 4, NOT_ALL_PRIVATE, 51793, 1, 6435 },
+  },
+  { NULL, NULL, { NULL, 0, 0, 0, 0, 0, 0, } },
+};
+  
+struct tests aggregate_tests[] =
+{
   { &test_segments[0], &test_segments[2],
     { "8466 3 52737 4096 {4,8722}",
       "8466 3 52737 4096 {4,8722}",
@@ -459,6 +515,13 @@ struct tests aggregate_tests[] =
       "8466 {2,3,4,4096,8722,52737}",
       2, 0, NOT_ALL_PRIVATE, 2, 20000, 8466 },
   },
+
+  { &test_segments[5], &test_segments[18],
+    { "6435 59408 21665 {1842,2457,4369,23456,41590,51793,61697}",
+      "6435 59408 21665 {1842,2457,4369,23456,41590,51793,61697}",
+      4, 0, NOT_ALL_PRIVATE, 41590, 1, 6435 },
+  },
+
   { NULL, NULL, { NULL, 0, 0}  },
 };
 
@@ -508,7 +571,7 @@ make_aspath (const u_char *data, size_t len)
       s = stream_new (len);
       stream_put (s, data, len);
     }
-  as = aspath_parse (s, len);
+  as = aspath_parse (s, len, 0); /* jk: these are 16bit aspaths! */
   
   if (s)
     stream_free (s);
@@ -547,6 +610,8 @@ validate (struct aspath *as, const struct test_spec *sp)
   
   asconfeddel = aspath_delete_confed_seq (aspath_dup (asinout));
   
+  printf ("got: %s\n", aspath_print(as));
+  
   /* the parsed path should match the specified 'shouldbe' string.
    * We should pass the "eat our own dog food" test, be able to output
    * this path and then input it again. Ie the path resulting from:
@@ -580,7 +645,6 @@ validate (struct aspath *as, const struct test_spec *sp)
       failed++;
       fails++;
       printf ("shouldbe:\n%s\n", sp->shouldbe);
-      printf ("got:\n%s\n", aspath_print(as));
       printf ("hash keys: in: %d out->in: %d\n", 
               aspath_key_make (as), aspath_key_make (asinout));
       printf ("hops: %d, counted %d %d\n", sp->hops, 
@@ -639,7 +703,6 @@ validate (struct aspath *as, const struct test_spec *sp)
   aspath_free (asstr);
   
   return fails;
-  
 }
 
 static void
@@ -737,6 +800,32 @@ empty_prepend_test (struct test_segment *t)
   aspath_free (asp2);
 }
 
+/* as2+as4 reconciliation testing */
+static void
+as4_reconcile_test (struct tests *t)
+{
+  struct aspath *asp1, *asp2, *ascratch;
+  
+  printf ("reconciling %s:\n  %s\n", t->test1->name, t->test1->desc);
+  printf ("with %s:\n  %s\n", t->test2->name, t->test2->desc);
+  
+  asp1 = make_aspath (t->test1->asdata, t->test1->len);
+  asp2 = make_aspath (t->test2->asdata, t->test2->len);
+  
+  ascratch = aspath_reconcile_as4 (asp1, asp2);
+  
+  if (!validate (ascratch, &t->sp))
+    printf ("OK\n");
+  else
+    printf ("failed!\n");
+  
+  printf ("\n");
+  aspath_unintern (asp1);
+  aspath_unintern (asp2);
+  aspath_free (ascratch);
+}
+
+
 /* aggregation testing */
 static void
 aggregate_test (struct tests *t)
@@ -751,8 +840,6 @@ aggregate_test (struct tests *t)
   
   ascratch = aspath_aggregate (asp1, asp2);
   
-  printf ("aspath: %s\n", aspath_print (ascratch));
-  
   if (!validate (ascratch, &t->sp))
     printf ("OK\n");
   else
@@ -831,6 +918,13 @@ main (void)
   while (aggregate_tests[i].test1)
     aggregate_test (&aggregate_tests[i++]);
   
+  i = 0;
+  
+  while (reconcile_tests[i].test1)
+    as4_reconcile_test (&reconcile_tests[i++]);
+  
+  i = 0;
+  
   cmp_test();
   
   i = 0;
diff --git a/vtysh/ChangeLog b/vtysh/ChangeLog
index a582b95..178c459 100644
--- a/vtysh/ChangeLog
+++ b/vtysh/ChangeLog
@@ -12,6 +12,11 @@
 	  precision commands: send to all daemons.
 	  (vtysh_init_vty) Install new log timestamp precision commands.
 
+2007-02-12 Juergen Kammer <j.kammer at eurodata.de>
+	* extract.pl: AS4 compatibility for router bgp ASNUMBER
+	* extract.pl.in: AS4 compatibility for router bgp ASNUMBER
+	* vtysh.c: AS4 compatibility for router bgp ASNUMBER
+
 2006-07-27 Andrew J. Schorr <ajschorr at alumni.princeton.edu>
 
 	* vtysh_main.c: (usage) Add new -d and -E options.  And note that
diff --git a/vtysh/extract.pl.in b/vtysh/extract.pl.in
index 723fe8d..b4a7171 100755
--- a/vtysh/extract.pl.in
+++ b/vtysh/extract.pl.in
@@ -37,8 +37,8 @@ $ignore{'"router ripng"'} = "ignore";
 $ignore{'"router ospf"'} = "ignore";
 $ignore{'"router ospf <0-65535>"'} = "ignore";
 $ignore{'"router ospf6"'} = "ignore";
-$ignore{'"router bgp <1-65535>"'} = "ignore";
-$ignore{'"router bgp <1-65535> view WORD"'} = "ignore";
+$ignore{'"router bgp ASNUMBER"'} = "ignore";
+$ignore{'"router bgp ASNUMBER view WORD"'} = "ignore";
 $ignore{'"router isis WORD"'} = "ignore";
 $ignore{'"router zebra"'} = "ignore";
 $ignore{'"address-family ipv4"'} = "ignore";
diff --git a/vtysh/vtysh.c b/vtysh/vtysh.c
index 777a7ac..6193d88 100644
--- a/vtysh/vtysh.c
+++ b/vtysh/vtysh.c
@@ -838,7 +838,7 @@ DEFUNSH (VTYSH_ALL,
 DEFUNSH (VTYSH_BGPD,
 	 router_bgp,
 	 router_bgp_cmd,
-	 "router bgp <1-65535>",
+	 "router bgp ASNUMBER",
 	 ROUTER_STR
 	 BGP_STR
 	 AS_STR)


More information about the Quagga-dev mailing list