bgpd: Fix aggregation issues found via ANVL

There were various failures in ANVL's aggregation tests, this
patch fixes those issues found

Signed-off-by: Daniel Walton <dwalton@cumulusnetworks.com>
diff --git a/bgpd/bgp_aspath.c b/bgpd/bgp_aspath.c
index e5541f6..b7af5e8 100644
--- a/bgpd/bgp_aspath.c
+++ b/bgpd/bgp_aspath.c
@@ -1064,6 +1064,9 @@
       if (match != minlen || match != seg1->length 
 	  || seg1->length != seg2->length)
 	break;
+      /* We are moving on to the next segment to reset match */
+      else
+        match = 0;
       
       seg1 = seg1->next;
       seg2 = seg2->next;
diff --git a/bgpd/bgp_attr.c b/bgpd/bgp_attr.c
index 54ff544..616f779 100644
--- a/bgpd/bgp_attr.c
+++ b/bgpd/bgp_attr.c
@@ -717,6 +717,7 @@
   return new;
 }
 
+/* Create the attributes for an aggregate */
 struct attr *
 bgp_attr_aggregate_intern (struct bgp *bgp, u_char origin,
 			   struct aspath *aspath,
diff --git a/bgpd/bgp_route.c b/bgpd/bgp_route.c
index e5224dc..17b87f4 100644
--- a/bgpd/bgp_route.c
+++ b/bgpd/bgp_route.c
@@ -4731,6 +4731,7 @@
   XFREE (MTYPE_BGP_AGGREGATE, aggregate);
 }     
 
+/* Update an aggregate as routes are added/removed from the BGP table */
 static void
 bgp_aggregate_route (struct bgp *bgp, struct prefix *p, struct bgp_info *rinew,
 		     afi_t afi, safi_t safi, struct bgp_info *del, 
@@ -4803,11 +4804,11 @@
 
 		aggregate->count++;
 
+		if (origin < ri->attr->origin)
+		  origin = ri->attr->origin;
+
 		if (aggregate->as_set)
 		  {
-		    if (origin < ri->attr->origin)
-		      origin = ri->attr->origin;
-
 		    if (aspath)
 		      {
 			asmerge = aspath_aggregate (aspath, ri->attr->aspath);
@@ -4844,11 +4845,11 @@
       if (aggregate->summary_only)
         (bgp_info_extra_get (rinew))->suppress++;
 
+      if (origin < rinew->attr->origin)
+        origin = rinew->attr->origin;
+
       if (aggregate->as_set)
 	{
-	  if (origin < rinew->attr->origin)
-	    origin = rinew->attr->origin;
-
 	  if (aspath)
 	    {
 	      asmerge = aspath_aggregate (aspath, rinew->attr->aspath);
@@ -4968,6 +4969,7 @@
   bgp_unlock_node (child);
 }
 
+/* Called via bgp_aggregate_set when the user configures aggregate-address */
 static void
 bgp_aggregate_add (struct bgp *bgp, struct prefix *p, afi_t afi, safi_t safi,
 		   struct bgp_aggregate *aggregate)
@@ -5014,13 +5016,21 @@
 		    bgp_info_set_flag (rn, ri, BGP_INFO_ATTR_CHANGED);
 		    match++;
 		  }
+
+                /* If at least one route among routes that are aggregated has
+                 * ORIGIN with the value INCOMPLETE, then the aggregated route
+                 * MUST have the ORIGIN attribute with the value INCOMPLETE.
+                 * Otherwise, if at least one route among routes that are
+                 * aggregated has ORIGIN with the value EGP, then the aggregated
+                 * route MUST have the ORIGIN attribute with the value EGP.
+                 */
+                if (origin < ri->attr->origin)
+                    origin = ri->attr->origin;
+
 		/* as-set aggregate route generate origin, as path,
 		   community aggregation.  */
 		if (aggregate->as_set)
 		  {
-		    if (origin < ri->attr->origin)
-		      origin = ri->attr->origin;
-
 		    if (aspath)
 		      {
 			asmerge = aspath_aggregate (aspath, ri->attr->aspath);