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_routemap.c b/bgpd/bgp_routemap.c
index b74554d..857781f 100644
--- a/bgpd/bgp_routemap.c
+++ b/bgpd/bgp_routemap.c
@@ -1230,7 +1230,6 @@
 
   if (type == RMAP_BGP)
     {
-      aspath = rule;
       binfo = object;
     
       if (binfo->attr->aspath->refcnt)
@@ -1238,20 +1237,50 @@
       else
 	new = binfo->attr->aspath;
 
-      aspath_prepend (aspath, new);
+      if ((uintptr_t)rule > 10)
+      {
+	aspath = rule;
+	aspath_prepend (aspath, new);
+      }
+      else
+      {
+	as_t as = aspath_leftmost(new);
+	if (!as) as = binfo->peer->as;
+	new = aspath_add_seq_n (new, as, (uintptr_t) rule);
+      }
+
       binfo->attr->aspath = new;
     }
 
   return RMAP_OKAY;
 }
 
+static void *
+route_set_aspath_prepend_compile (const char *arg)
+{
+  unsigned int num;
+
+  if (sscanf(arg, "last-as %u", &num) == 1 && num > 0 && num < 10)
+    return (void*)(uintptr_t)num;
+
+  return route_aspath_compile(arg);
+}
+
+static void
+route_set_aspath_prepend_free (void *rule)
+{
+  if ((uintptr_t)rule > 10)
+    route_aspath_free(rule);
+}
+
+
 /* Set as-path prepend rule structure. */
 struct route_map_rule_cmd route_set_aspath_prepend_cmd = 
 {
   "as-path prepend",
   route_set_aspath_prepend,
-  route_aspath_compile,
-  route_aspath_free,
+  route_set_aspath_prepend_compile,
+  route_set_aspath_prepend_free,
 };
 
 /* `set as-path exclude ASn' */
@@ -3127,6 +3156,15 @@
   return ret;
 }
 
+ALIAS (set_aspath_prepend,
+       set_aspath_prepend_lastas_cmd,
+       "set as-path prepend (last-as) <1-10>",
+       SET_STR
+       "Transform BGP AS_PATH attribute\n"
+       "Prepend to the as-path\n"
+       "Use the peer's AS-number\n"
+       "Number of times to insert");
+
 DEFUN (no_set_aspath_prepend,
        no_set_aspath_prepend_cmd,
        "no set as-path prepend",
@@ -3983,6 +4021,7 @@
   install_element (RMAP_NODE, &no_set_metric_cmd);
   install_element (RMAP_NODE, &no_set_metric_val_cmd);
   install_element (RMAP_NODE, &set_aspath_prepend_cmd);
+  install_element (RMAP_NODE, &set_aspath_prepend_lastas_cmd);
   install_element (RMAP_NODE, &set_aspath_exclude_cmd);
   install_element (RMAP_NODE, &no_set_aspath_prepend_cmd);
   install_element (RMAP_NODE, &no_set_aspath_prepend_val_cmd);