bgpd: implement route-map set as-path prepend last-as

It picks up the AS to add from the aspath, or uses the peers
AS number. Useful mostly in iBGP setups.

Signed-off-by: Timo Teräs <timo.teras@iki.fi>
Reviewed-by: Paul Jakma <paul@opensourcerouting.org>
diff --git a/bgpd/bgp_aspath.c b/bgpd/bgp_aspath.c
index e8559be..cfa9bc1 100644
--- a/bgpd/bgp_aspath.c
+++ b/bgpd/bgp_aspath.c
@@ -476,6 +476,19 @@
   return highest;
 }
 
+/* Return the left-most ASN in path */
+as_t
+aspath_leftmost (struct aspath *aspath)
+{
+  struct assegment *seg = aspath->segments;
+  as_t leftmost = 0;
+
+  if (seg && seg->length && seg->type == AS_SEQUENCE)
+    leftmost = seg->as[0];
+
+  return leftmost;
+}
+
 /* Return 1 if there are any 4-byte ASes in the path */
 unsigned int
 aspath_has_as4 (struct aspath *aspath)
@@ -1362,46 +1375,50 @@
 
 /* Add specified AS to the leftmost of aspath. */
 static struct aspath *
-aspath_add_one_as (struct aspath *aspath, as_t asno, u_char type)
+aspath_add_asns (struct aspath *aspath, as_t asno, u_char type, unsigned num)
 {
   struct assegment *assegment = aspath->segments;
+  int i;
 
-  /* In case of empty aspath. */
-  if (assegment == NULL || assegment->length == 0)
+  if (assegment && assegment->type == type)
     {
-      aspath->segments = assegment_new (type, 1);
-      aspath->segments->as[0] = asno;
-      
-      if (assegment)
-	assegment_free (assegment);
-
-      return aspath;
+      /* extend existing segment */
+      aspath->segments = assegment_prepend_asns (aspath->segments, asno, num);
     }
-
-  if (assegment->type == type)
-    aspath->segments = assegment_prepend_asns (aspath->segments, asno, 1);
   else 
     {
-      /* create new segment
-       * push it onto head of aspath's segment chain 
-       */
-      struct assegment *newsegment;
-      
-      newsegment = assegment_new (type, 1);
-      newsegment->as[0] = asno;
-      
-      newsegment->next = assegment;
+      /* prepend with new segment */
+      struct assegment *newsegment = assegment_new (type, num);
+      for (i = 0; i < num; i++)
+	newsegment->as[i] = asno;
+
+      /* insert potentially replacing empty segment */
+      if (assegment && assegment->length == 0)
+	{
+	  newsegment->next = assegment->next;
+	  assegment_free (assegment);
+	}
+       else
+	  newsegment->next = assegment;
       aspath->segments = newsegment;
     }
 
+  aspath_str_update (aspath);
   return aspath;
 }
 
+/* Add specified AS to the leftmost of aspath num times. */
+struct aspath *
+aspath_add_seq_n (struct aspath *aspath, as_t asno, unsigned num)
+{
+  return aspath_add_asns (aspath, asno, AS_SEQUENCE, num);
+}
+
 /* Add specified AS to the leftmost of aspath. */
 struct aspath *
 aspath_add_seq (struct aspath *aspath, as_t asno)
 {
-  return aspath_add_one_as (aspath, asno, AS_SEQUENCE);
+  return aspath_add_asns (aspath, asno, AS_SEQUENCE, 1);
 }
 
 /* Compare leftmost AS value for MED check.  If as1's leftmost AS and
@@ -1600,7 +1617,7 @@
 struct aspath*
 aspath_add_confed_seq (struct aspath *aspath, as_t asno)
 {
-  return aspath_add_one_as (aspath, asno, AS_CONFED_SEQUENCE);
+  return aspath_add_asns (aspath, asno, AS_CONFED_SEQUENCE, 1);
 }
 
 /* Add new as value to as path structure. */