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. */
diff --git a/bgpd/bgp_aspath.h b/bgpd/bgp_aspath.h
index e8764cc..1311f8a 100644
--- a/bgpd/bgp_aspath.h
+++ b/bgpd/bgp_aspath.h
@@ -71,6 +71,7 @@
extern struct aspath *aspath_aggregate (struct aspath *, struct aspath *);
extern struct aspath *aspath_prepend (struct aspath *, struct aspath *);
extern struct aspath *aspath_filter_exclude (struct aspath *, struct aspath *);
+extern struct aspath *aspath_add_seq_n (struct aspath *, as_t, unsigned);
extern struct aspath *aspath_add_seq (struct aspath *, as_t);
extern struct aspath *aspath_add_confed_seq (struct aspath *, as_t);
extern int aspath_cmp (const void *, const void *);
@@ -97,6 +98,7 @@
extern unsigned int aspath_count_confeds (struct aspath *);
extern unsigned int aspath_size (struct aspath *);
extern as_t aspath_highest (struct aspath *);
+extern as_t aspath_leftmost (struct aspath *);
extern size_t aspath_put (struct stream *, struct aspath *, int);
extern struct aspath *aspath_reconcile_as4 (struct aspath *, struct aspath *);
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);
diff --git a/doc/bgpd.texi b/doc/bgpd.texi
index de70970..5004cbf 100644
--- a/doc/bgpd.texi
+++ b/doc/bgpd.texi
@@ -496,6 +496,7 @@
@end deffn
@deffn {Route Map} {set as-path prepend @var{as-path}} {}
+@deffnx {Route Map} {set as-path prepend last-as @var{num}} {}
@end deffn
@node Private AS Numbers