[PATCH 1/2] Add IP6_SADR support to Bird Core

Toke Høiland-Jørgensen toke at toke.dk
Sat Feb 3 21:40:56 CET 2018


This adds support for source-specific IPv6 routes to Bird core. This is
based on Dean Luga's original patch, with the review comments addressed.
Sadr support is added to network address parsing in confbase.Y and to
the kernel protocol on Linux.

A couple of issues remain with this patch:

- Had to change the default addr_type in rt_setup() to NET_SADR_IP6 to
  get the kernel 'learn' mechanism to work.

- Not sure if the logic in net_route_sadr_ip6() is right.

- The config parser barfs on sadr route literals in the static protocol
  with an "Integer expression expected" error message.

- There's no way to mix source-specific and non-source-specific routes
  (i.e., sadr tables cannot be connected to non-sadr tables).

Signed-off-by: Toke Høiland-Jørgensen <toke at toke.dk>
---
 conf/confbase.Y        | 22 ++++++++++++++++---
 lib/net.c              | 34 ++++++++++++++++++++++++++++++
 lib/net.h              | 57 ++++++++++++++++++++++++++++++++++++++++++++++----
 nest/config.Y          |  5 +++--
 nest/rt-fib.c          |  2 ++
 nest/rt-table.c        | 49 ++++++++++++++++++++++++++++++++++++++-----
 sysdep/linux/netlink.c | 31 +++++++++++++++++++++++++--
 sysdep/unix/krt.c      |  8 ++++---
 8 files changed, 189 insertions(+), 19 deletions(-)

diff --git a/conf/confbase.Y b/conf/confbase.Y
index 7e0537c5..246d6c3b 100644
--- a/conf/confbase.Y
+++ b/conf/confbase.Y
@@ -82,7 +82,7 @@ CF_DECLS
 %type <i> expr bool pxlen4
 %type <time> expr_us time
 %type <a> ipa
-%type <net> net_ip4_ net_ip6_ net_ip6 net_ip_ net_ip net_or_ipa
+%type <net> net_ip4_ net_ip6_ net_sadr_ip6_ net_ip6 net_ip_ net_ip net_or_ipa
 %type <net_ptr> net_ net_any net_vpn4_ net_vpn6_ net_vpn_ net_roa4_ net_roa6_ net_roa_ net_mpls_
 %type <mls> label_stack_start label_stack
 
@@ -96,7 +96,7 @@ CF_DECLS
 %left '!'
 %nonassoc '.'
 
-CF_KEYWORDS(DEFINE, ON, OFF, YES, NO, S, MS, US, PORT, VPN, MPLS)
+CF_KEYWORDS(DEFINE, ON, OFF, YES, NO, S, MS, US, PORT, VPN, MPLS, FROM)
 
 CF_GRAMMAR
 
@@ -206,6 +206,22 @@ net_ip6_: IP6 '/' NUM
 	     n->prefix, n->pxlen, ip6_and(n->prefix, ip6_mkmask(n->pxlen)), n->pxlen);
 };
 
+net_sadr_ip6_: IP6 '/' NUM FROM IP6 '/' NUM
+{
+  if ($3 > IP6_MAX_PREFIX_LENGTH)
+    cf_error("Invalid prefix length %u", $3);
+
+  if ($7 > IP6_MAX_PREFIX_LENGTH)
+    cf_error("Invalid prefix length %u", $7);
+
+  net_fill_sadr_ip6(&($$), $1, $3, $5, $7);
+
+  net_addr_sadr_ip6 *n = (void *) &($$);
+  if (!net_validate_sadr_ip6(n))
+    cf_error("Invalid SADR IPv6 prefix %I6/%d from %I6/%d",
+	    n->dst_prefix, n->dst_pxlen, n->src_prefix, n->src_pxlen);
+};
+
 net_vpn4_: VPN_RD net_ip4_
 {
   $$ = cfg_alloc(sizeof(net_addr_vpn4));
@@ -240,7 +256,7 @@ net_mpls_: MPLS NUM
   net_fill_mpls($$, $2);
 }
 
-net_ip_: net_ip4_ | net_ip6_ ;
+net_ip_: net_ip4_ | net_ip6_ | net_sadr_ip6_;
 net_vpn_: net_vpn4_ | net_vpn6_ ;
 net_roa_: net_roa4_ | net_roa6_ ;
 
diff --git a/lib/net.c b/lib/net.c
index 9335b78f..a2dad51d 100644
--- a/lib/net.c
+++ b/lib/net.c
@@ -14,6 +14,7 @@ const char * const net_label[] = {
   [NET_ROA6] 	= "roa6",
   [NET_FLOW4] 	= "flow4",
   [NET_FLOW6] 	= "flow6",
+  [NET_SADR_IP6] = "sadr6",
   [NET_MPLS]	= "mpls",
 };
 
@@ -26,6 +27,7 @@ const u16 net_addr_length[] = {
   [NET_ROA6] 	= sizeof(net_addr_roa6),
   [NET_FLOW4] 	= 0,
   [NET_FLOW6] 	= 0,
+  [NET_SADR_IP6]= sizeof(net_addr_sadr_ip6),
   [NET_MPLS]	= sizeof(net_addr_mpls),
 };
 
@@ -38,6 +40,7 @@ const u8 net_max_prefix_length[] = {
   [NET_ROA6] 	= IP6_MAX_PREFIX_LENGTH,
   [NET_FLOW4] 	= IP4_MAX_PREFIX_LENGTH,
   [NET_FLOW6] 	= IP6_MAX_PREFIX_LENGTH,
+  [NET_SADR_IP6]= IP6_MAX_PREFIX_LENGTH,
   [NET_MPLS]	= 0,
 };
 
@@ -50,6 +53,7 @@ const u16 net_max_text_length[] = {
   [NET_ROA6] 	= 60,	/* "ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff/128-128 AS4294967295" */
   [NET_FLOW4] 	= 0,	/* "flow4 { ... }" */
   [NET_FLOW6] 	= 0,	/* "flow6 { ... }" */
+  [NET_SADR_IP6]= 92, /* "ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff/128 from ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff/128" */
   [NET_MPLS]	= 7,	/* "1048575" */
 };
 
@@ -102,6 +106,8 @@ net_format(const net_addr *N, char *buf, int buflen)
     return flow4_net_format(buf, buflen, &n->flow4);
   case NET_FLOW6:
     return flow6_net_format(buf, buflen, &n->flow6);
+  case NET_SADR_IP6:
+    return bsnprintf(buf, buflen, "%I6/%d from %I6/%d", n->sadr_ip6.dst_prefix, n->sadr_ip6.dst_pxlen, n->sadr_ip6.src_prefix, n->sadr_ip6.src_pxlen);
   case NET_MPLS:
     return bsnprintf(buf, buflen, "%u", n->mpls.label);
   }
@@ -126,12 +132,25 @@ net_pxmask(const net_addr *a)
   case NET_FLOW6:
     return ipa_from_ip6(ip6_mkmask(net6_pxlen(a)));
 
+  case NET_SADR_IP6:
+    return ipa_from_ip6(ip6_mkmask(net6_pxlen(a)));
+
   case NET_MPLS:
   default:
     return IPA_NONE;
   }
 }
 
+int
+net_compare_sadr_ip6(const net_addr_sadr_ip6 *a, const net_addr_sadr_ip6 *b)
+{
+  int dst_cmp = ip6_compare(a->dst_prefix, b->dst_prefix) ?: uint_cmp(a->dst_pxlen, b->dst_pxlen);
+  if (dst_cmp)
+    return dst_cmp;
+  else
+    return ip6_compare(a->src_prefix, b->src_prefix) ?: uint_cmp(a->src_pxlen, b->src_pxlen);
+}
+
 int
 net_compare(const net_addr *a, const net_addr *b)
 {
@@ -158,6 +177,8 @@ net_compare(const net_addr *a, const net_addr *b)
     return net_compare_flow6((const net_addr_flow6 *) a, (const net_addr_flow6 *) b);
   case NET_MPLS:
     return net_compare_mpls((const net_addr_mpls *) a, (const net_addr_mpls *) b);
+  case NET_SADR_IP6:
+    return net_compare_sadr_ip6((const net_addr_sadr_ip6 *) a, (const net_addr_sadr_ip6 *) b);
   }
   return 0;
 }
@@ -177,6 +198,7 @@ net_hash(const net_addr *n)
   case NET_ROA6: return NET_HASH(n, roa6);
   case NET_FLOW4: return NET_HASH(n, flow4);
   case NET_FLOW6: return NET_HASH(n, flow6);
+  case NET_SADR_IP6: return NET_HASH(n, sadr_ip6);
   case NET_MPLS: return NET_HASH(n, mpls);
   default: bug("invalid type");
   }
@@ -199,6 +221,7 @@ net_validate(const net_addr *n)
   case NET_FLOW4: return NET_VALIDATE(n, flow4);
   case NET_FLOW6: return NET_VALIDATE(n, flow6);
   case NET_MPLS: return NET_VALIDATE(n, mpls);
+  case NET_SADR_IP6: return NET_VALIDATE(n, sadr_ip6);
   default: return 0;
   }
 }
@@ -224,6 +247,9 @@ net_normalize(net_addr *N)
 
   case NET_MPLS:
     return;
+
+  case NET_SADR_IP6:
+    return net_normalize_sadr_ip6(&n->sadr_ip6);
   }
 }
 
@@ -248,6 +274,9 @@ net_classify(const net_addr *N)
 
   case NET_MPLS:
     return IADDR_HOST | SCOPE_UNIVERSE;
+
+  case NET_SADR_IP6:
+    return ip6_zero(n->sadr_ip6.dst_prefix) ? (IADDR_HOST | SCOPE_UNIVERSE) : ip6_classify(&n->sadr_ip6.dst_prefix);
   }
 
   return IADDR_INVALID;
@@ -274,6 +303,11 @@ ipa_in_netX(const ip_addr a, const net_addr *n)
     return ip6_zero(ip6_and(ip6_xor(ipa_to_ip6(a), net6_prefix(n)),
 			    ip6_mkmask(net6_pxlen(n))));
 
+  case NET_SADR_IP6:
+    if (ipa_is_ip4(a)) return 0;
+    return ip6_zero(ip6_and(ip6_xor(ipa_to_ip6(a), net6_prefix(n)),
+          ip6_mkmask(net6_pxlen(n))));
+
   case NET_MPLS:
   default:
     return 0;
diff --git a/lib/net.h b/lib/net.h
index 69f00641..5204e6b5 100644
--- a/lib/net.h
+++ b/lib/net.h
@@ -12,7 +12,6 @@
 
 #include "lib/ip.h"
 
-
 #define NET_IP4		1
 #define NET_IP6		2
 #define NET_VPN4	3
@@ -21,8 +20,9 @@
 #define NET_ROA6	6
 #define NET_FLOW4	7
 #define NET_FLOW6	8
-#define NET_MPLS	9
-#define NET_MAX		10
+#define NET_SADR_IP6	9
+#define NET_MPLS	10
+#define NET_MAX		11
 
 #define NB_IP4		(1 << NET_IP4)
 #define NB_IP6		(1 << NET_IP6)
@@ -32,12 +32,13 @@
 #define NB_ROA6		(1 << NET_ROA6)
 #define NB_FLOW4	(1 << NET_FLOW4)
 #define NB_FLOW6	(1 << NET_FLOW6)
+#define NB_SADR_IP6	(1 << NET_SADR_IP6)
 #define NB_MPLS		(1 << NET_MPLS)
 
 #define NB_IP		(NB_IP4 | NB_IP6)
 #define NB_VPN		(NB_VPN4 | NB_VPN6)
 #define NB_FLOW		(NB_FLOW4 | NB_FLOW6)
-#define NB_DEST		(NB_IP | NB_VPN | NB_MPLS)
+#define NB_DEST		(NB_IP | NB_SADR_IP6 | NB_VPN | NB_MPLS)
 #define NB_ANY		0xffffffff
 
 
@@ -121,6 +122,15 @@ typedef struct net_addr_mpls {
   u32 label;
 } net_addr_mpls;
 
+typedef struct net_addr_sadr_ip6 {
+  u8 type;
+  u8 dst_pxlen;
+  u16 length;
+  ip6_addr dst_prefix;
+  u32 src_pxlen; // if u8, NET_ADDR_SADR_IP6 would leave non-zero padding
+  ip6_addr src_prefix;
+} net_addr_sadr_ip6;
+
 typedef union net_addr_union {
   net_addr n;
   net_addr_ip4 ip4;
@@ -132,6 +142,7 @@ typedef union net_addr_union {
   net_addr_flow4 flow4;
   net_addr_flow6 flow6;
   net_addr_mpls mpls;
+  net_addr_sadr_ip6 sadr_ip6;
 } net_addr_union;
 
 
@@ -170,6 +181,9 @@ extern const u16 net_max_text_length[];
 #define NET_ADDR_MPLS(label) \
   ((net_addr_mpls) { NET_MPLS, 20, sizeof(net_addr_mpls), label })
 
+#define NET_ADDR_SADR_IP6(dst_prefix,dst_pxlen,src_prefix,src_pxlen) \
+  ((net_addr_sadr_ip6) { NET_SADR_IP6, dst_pxlen, sizeof(net_addr_sadr_ip6), dst_prefix, src_pxlen, src_prefix })
+
 
 static inline void net_fill_ip4(net_addr *a, ip4_addr prefix, uint pxlen)
 { *(net_addr_ip4 *)a = NET_ADDR_IP4(prefix, pxlen); }
@@ -192,6 +206,9 @@ static inline void net_fill_roa6(net_addr *a, ip6_addr prefix, uint pxlen, uint
 static inline void net_fill_mpls(net_addr *a, u32 label)
 { *(net_addr_mpls *)a = NET_ADDR_MPLS(label); }
 
+static inline void net_fill_sadr_ip6(net_addr *a, ip6_addr dst_prefix, uint dst_pxlen, ip6_addr src_prefix, uint src_pxlen)
+{ *(net_addr_sadr_ip6 *)a = NET_ADDR_SADR_IP6(dst_prefix, dst_pxlen, src_prefix, src_pxlen); }
+
 static inline void net_fill_ipa(net_addr *a, ip_addr prefix, uint pxlen)
 {
   if (ipa_is_ip4(prefix))
@@ -240,6 +257,15 @@ static inline int net_is_roa(const net_addr *a)
 static inline int net_is_flow(const net_addr *a)
 { return (a->type == NET_FLOW4) || (a->type == NET_FLOW6); }
 
+static inline int net_is_sadr(const net_addr *a)
+{ return (a->type == NET_SADR_IP6); }
+
+static inline int net_is_sadr_set(const net_addr *a)
+{
+  net_addr_sadr_ip6 *sa = (void *) a;
+  return (a->type == NET_SADR_IP6 && sa->src_pxlen > 0);
+}
+
 
 static inline ip4_addr net4_prefix(const net_addr *a)
 { return ((net_addr_ip4 *) a)->prefix; }
@@ -261,6 +287,7 @@ static inline ip_addr net_prefix(const net_addr *a)
   case NET_VPN6:
   case NET_ROA6:
   case NET_FLOW6:
+  case NET_SADR_IP6:
     return ipa_from_ip6(net6_prefix(a));
 
   case NET_MPLS:
@@ -331,6 +358,13 @@ static inline int net_equal_flow6(const net_addr_flow6 *a, const net_addr_flow6
 static inline int net_equal_mpls(const net_addr_mpls *a, const net_addr_mpls *b)
 { return !memcmp(a, b, sizeof(net_addr_mpls)); }
 
+typedef net_addr_ip6 net_addr_sadr_dst;
+static inline int net_equal_sadr_dst(const net_addr_ip6 *a, const net_addr_ip6 *b)
+{ return net_equal_ip6(a, b); }
+
+static inline int net_equal_sadr_ip6(const net_addr_sadr_ip6 *a, const net_addr_sadr_ip6 *b)
+{ return !memcmp(a, b, sizeof(net_addr_sadr_ip6)); }
+
 
 static inline int net_equal_prefix_roa4(const net_addr_roa4 *a, const net_addr_roa4 *b)
 { return ip4_equal(a->prefix, b->prefix) && (a->pxlen == b->pxlen); }
@@ -394,6 +428,8 @@ static inline int net_compare_flow6(const net_addr_flow6 *a, const net_addr_flow
 static inline int net_compare_mpls(const net_addr_mpls *a, const net_addr_mpls *b)
 { return uint_cmp(a->label, b->label); }
 
+int net_compare_sadr_ip6(const net_addr_sadr_ip6 *a, const net_addr_sadr_ip6 *b);
+
 int net_compare(const net_addr *a, const net_addr *b);
 
 
@@ -427,6 +463,9 @@ static inline void net_copy_flow6(net_addr_flow6 *dst, const net_addr_flow6 *src
 static inline void net_copy_mpls(net_addr_mpls *dst, const net_addr_mpls *src)
 { memcpy(dst, src, sizeof(net_addr_mpls)); }
 
+static inline void net_copy_sadr_ip6(net_addr_sadr_ip6 *dst, const net_addr_sadr_ip6 *src)
+{ memcpy(dst, src, sizeof(net_addr_sadr_ip6)); }
+
 
 /* XXXX */
 static inline u32 u64_hash(u64 a)
@@ -438,6 +477,9 @@ static inline u32 net_hash_ip4(const net_addr_ip4 *n)
 static inline u32 net_hash_ip6(const net_addr_ip6 *n)
 { return ip6_hash(n->prefix) ^ ((u32) n->pxlen << 26); }
 
+static inline u32 net_hash_sadr_ip6(const net_addr_sadr_ip6 *n)
+{ return net_hash_ip6((net_addr_ip6 *)n); }
+
 static inline u32 net_hash_vpn4(const net_addr_vpn4 *n)
 { return ip4_hash(n->prefix) ^ ((u32) n->pxlen << 26) ^ u64_hash(n->rd); }
 
@@ -508,6 +550,9 @@ static inline int net_validate_flow6(const net_addr_flow6 *n)
 static inline int net_validate_mpls(const net_addr_mpls *n)
 { return n->label < (1 << 20); }
 
+static inline int net_validate_sadr_ip6(const net_addr_sadr_ip6 *n)
+{ return net_validate_px6(n->dst_prefix, n->dst_pxlen) && net_validate_px6(n->src_prefix, n->src_pxlen); }
+
 int net_validate(const net_addr *N);
 
 
@@ -523,6 +568,10 @@ static inline void net_normalize_vpn4(net_addr_vpn4 *n)
 static inline void net_normalize_vpn6(net_addr_vpn6 *n)
 { net_normalize_ip6((net_addr_ip6 *) n); }
 
+static inline void net_normalize_sadr_ip6(net_addr_sadr_ip6 *n)
+{ n->dst_prefix = ip6_and(n->dst_prefix, ip6_mkmask(n->dst_pxlen));
+  n->src_prefix = ip6_and(n->src_prefix, ip6_mkmask(n->src_pxlen)); }
+
 void net_normalize(net_addr *N);
 
 
diff --git a/nest/config.Y b/nest/config.Y
index 044aba2b..e8cb26e4 100644
--- a/nest/config.Y
+++ b/nest/config.Y
@@ -77,7 +77,7 @@ CF_KEYWORDS(TIMEFORMAT, ISO, SHORT, LONG, ROUTE, PROTOCOL, BASE, LOG, S, MS, US)
 CF_KEYWORDS(GRACEFUL, RESTART, WAIT, MAX, FLUSH, AS)
 
 /* For r_args_channel */
-CF_KEYWORDS(IPV4, IPV4_MC, IPV4_MPLS, IPV6, IPV6_MC, IPV6_MPLS, VPN4, VPN4_MC, VPN4_MPLS, VPN6, VPN6_MC, VPN6_MPLS, ROA4, ROA6, FLOW4, FLOW6, MPLS, PRI, SEC)
+CF_KEYWORDS(IPV4, IPV4_MC, IPV4_MPLS, IPV6, IPV6_MC, IPV6_MPLS, SADR, VPN4, VPN4_MC, VPN4_MPLS, VPN6, VPN6_MC, VPN6_MPLS, ROA4, ROA6, FLOW4, FLOW6, MPLS, PRI, SEC)
 
 CF_ENUM(T_ENUM_RTS, RTS_, DUMMY, STATIC, INHERIT, DEVICE, STATIC_DEVICE, REDIRECT,
 	RIP, OSPF, OSPF_IA, OSPF_EXT1, OSPF_EXT2, BGP, PIPE, BABEL)
@@ -134,6 +134,7 @@ gr_opts: GRACEFUL RESTART WAIT expr ';' { new_config->gr_wait = $4; } ;
 net_type:
    IPV4 { $$ = NET_IP4; }
  | IPV6 { $$ = NET_IP6; }
+ | IPV6 SADR { $$ = NET_SADR_IP6; }
  | VPN4 { $$ = NET_VPN4; }
  | VPN6 { $$ = NET_VPN6; }
  | ROA4 { $$ = NET_ROA4; }
@@ -143,7 +144,7 @@ net_type:
  | MPLS { $$ = NET_MPLS; }
  ;
 
-CF_ENUM(T_ENUM_NETTYPE, NET_, IP4, IP6, VPN4, VPN6, ROA4, ROA6, FLOW4, FLOW6)
+CF_ENUM(T_ENUM_NETTYPE, NET_, IP4, IP6, VPN4, VPN6, ROA4, ROA6, FLOW4, FLOW6, SADR_IP6)
 
 
 /* Creation of routing tables */
diff --git a/nest/rt-fib.c b/nest/rt-fib.c
index c09f2759..1046350d 100644
--- a/nest/rt-fib.c
+++ b/nest/rt-fib.c
@@ -236,6 +236,7 @@ fib_find(struct fib *f, const net_addr *a)
   case NET_ROA6: return FIB_FIND(f, a, roa6);
   case NET_FLOW4: return FIB_FIND(f, a, flow4);
   case NET_FLOW6: return FIB_FIND(f, a, flow6);
+  case NET_SADR_IP6: return FIB_FIND(f, a, sadr_ip6);
   case NET_MPLS: return FIB_FIND(f, a, mpls);
   default: bug("invalid type");
   }
@@ -256,6 +257,7 @@ fib_insert(struct fib *f, const net_addr *a, struct fib_node *e)
   case NET_ROA6: FIB_INSERT(f, a, e, roa6); return;
   case NET_FLOW4: FIB_INSERT(f, a, e, flow4); return;
   case NET_FLOW6: FIB_INSERT(f, a, e, flow6); return;
+  case NET_SADR_IP6: FIB_INSERT(f, a, e, sadr_ip6); return;
   case NET_MPLS: FIB_INSERT(f, a, e, mpls); return;
   default: bug("invalid type");
   }
diff --git a/nest/rt-table.c b/nest/rt-table.c
index 4640d6a9..c88dffef 100644
--- a/nest/rt-table.c
+++ b/nest/rt-table.c
@@ -85,6 +85,42 @@ net_route_ip6(rtable *t, net_addr_ip6 *n)
   return r;
 }
 
+static inline void *
+net_route_sadr_ip6(rtable *t, net_addr_sadr_ip6 *n)
+{
+  net *r;
+
+  net_addr_ip6 dst = NET_ADDR_IP6(n->dst_prefix, n->dst_pxlen);
+  dst.type = NET_SADR_IP6;
+
+  // search for matching dst
+  while (r = net_route_ip6(t, &dst))
+  {
+    /* FIXME: Is this right? */
+    net_addr_ip6 *n6 = (net_addr_ip6 *) r->n.addr;
+    dst.pxlen = n6->pxlen;
+
+    // found a matching dst, start search for entries that match both prefixes
+    net_addr_sadr_ip6 full_addr =
+      NET_ADDR_SADR_IP6(dst.prefix, dst.pxlen, n->src_prefix, n->src_pxlen);
+
+    while (r = net_find_valid(t, (net_addr *) &full_addr), (!r) && (full_addr.src_pxlen > 0))
+    {
+      full_addr.src_pxlen--;
+      ip6_clrbit(&full_addr.src_prefix, full_addr.src_pxlen);
+    }
+
+    if (r)
+      return r;
+
+    if (!dst.pxlen)
+      break;
+    dst.pxlen--;
+  }
+
+  return NULL;
+}
+
 void *
 net_route(rtable *tab, const net_addr *n)
 {
@@ -105,6 +141,9 @@ net_route(rtable *tab, const net_addr *n)
   case NET_ROA6:
     return net_route_ip6(tab, (net_addr_ip6 *) n0);
 
+  case NET_SADR_IP6:
+    return net_route_sadr_ip6(tab, (net_addr_sadr_ip6 *) n0);
+
   default:
     return NULL;
   }
@@ -614,9 +653,9 @@ rt_notify_accepted(struct channel *c, net *net, rte *new_changed, rte *old_chang
 	old_meet = 1;
     }
 
-  /* 
+  /*
    * Second, handle the feed case. That means we do not care for
-   * old_best. It is NULL for feed, and the new_best for refeed. 
+   * old_best. It is NULL for feed, and the new_best for refeed.
    * For refeed, there is a hack similar to one in rt_notify_basic()
    * to ensure withdraws in case of changed filters
    */
@@ -1387,7 +1426,7 @@ rte_update2(struct channel *c, const net_addr *n, rte *new, struct rte_src *src)
 
 /* Independent call to rte_announce(), used from next hop
    recalculation, outside of rte_update(). new must be non-NULL */
-static inline void 
+static inline void
 rte_announce_i(rtable *tab, unsigned type, net *net, rte *new, rte *old,
 	       rte *new_best, rte *old_best)
 {
@@ -1604,7 +1643,7 @@ rt_setup(pool *p, rtable *t, char *name, struct rtable_config *cf)
   bzero(t, sizeof(*t));
   t->name = name;
   t->config = cf;
-  t->addr_type = cf ? cf->addr_type : NET_IP4;
+  t->addr_type = cf ? cf->addr_type : NET_SADR_IP6; /* FIXME */
   fib_init(&t->fib, p, t->addr_type, sizeof(net), OFFSETOF(net, n), 0, NULL);
   init_list(&t->channels);
 
@@ -2360,7 +2399,7 @@ if_local_addr(ip_addr a, struct iface *i)
   return 0;
 }
 
-static u32 
+static u32
 rt_get_igp_metric(rte *rt)
 {
   eattr *ea = ea_find(rt->attrs->eattrs, EA_GEN_IGP_METRIC);
diff --git a/sysdep/linux/netlink.c b/sysdep/linux/netlink.c
index 4cb51519..e59b4910 100644
--- a/sysdep/linux/netlink.c
+++ b/sysdep/linux/netlink.c
@@ -374,6 +374,7 @@ static struct nl_want_attrs rtm_attr_want4[BIRD_RTA_MAX] = {
 
 static struct nl_want_attrs rtm_attr_want6[BIRD_RTA_MAX] = {
   [RTA_DST]	  = { 1, 1, sizeof(ip6_addr) },
+  [RTA_SRC]   = { 1, 1, sizeof(ip6_addr) },
   [RTA_IIF]	  = { 1, 1, sizeof(u32) },
   [RTA_OIF]	  = { 1, 1, sizeof(u32) },
   [RTA_GATEWAY]	  = { 1, 1, sizeof(ip6_addr) },
@@ -1221,7 +1222,16 @@ nl_send_route(struct krt_proto *p, rte *e, struct ea_list *eattrs, int op, int d
   }
   else
 #endif
+  {
     nl_add_attr_ipa(&r->h, rsize, RTA_DST, net_prefix(net->n.addr));
+    // add source address for SADR routes
+    if (net->n.addr->type == NET_SADR_IP6)
+    {
+      net_addr_sadr_ip6 *sadr_addr = (void *) &net->n.addr;
+      r->r.rtm_src_len = sadr_addr->src_pxlen;
+      nl_add_attr_ipa(&r->h, rsize, RTA_SRC, sadr_addr->src_prefix);
+    }
+  }
 
   /*
    * Strange behavior for RTM_DELROUTE:
@@ -1447,7 +1457,8 @@ nl_parse_route(struct nl_parse_state *s, struct nlmsghdr *h)
   struct rtattr *a[BIRD_RTA_MAX];
   int new = h->nlmsg_type == RTM_NEWROUTE;
 
-  net_addr dst;
+  net_addr dst, sadr_src;
+  net_fill_ip6(&sadr_src, IP6_NONE, 0);
   u32 oif = ~0;
   u32 table_id;
   u32 priority = 0;
@@ -1473,6 +1484,9 @@ nl_parse_route(struct nl_parse_state *s, struct nlmsghdr *h)
       if (!nl_parse_attrs(RTM_RTA(i), rtm_attr_want6, a, sizeof(a)))
 	return;
 
+      if (a[RTA_SRC])
+	net_fill_ip6(&sadr_src, rta_get_ip6(a[RTA_SRC]), i->rtm_src_len);
+
       if (a[RTA_DST])
 	net_fill_ip6(&dst, rta_get_ip6(a[RTA_DST]), i->rtm_dst_len);
       else
@@ -1551,7 +1565,20 @@ nl_parse_route(struct nl_parse_state *s, struct nlmsghdr *h)
       src = KRT_SRC_ALIEN;
     }
 
-  net *net = net_get(p->p.main_channel->table, &dst);
+  net *net;
+  if (p->p.main_channel->table->addr_type == NET_SADR_IP6)
+  {
+    net_addr_sadr_ip6 sadr_addr =
+      NET_ADDR_SADR_IP6(net_prefix(&dst), net_pxlen(&dst),
+			net_prefix(&sadr_src), net_pxlen(&sadr_src));
+    net = net_get(p->p.main_channel->table, (net_addr *)&sadr_addr);
+  }
+  else
+  {
+    if (a[RTA_SRC])
+      SKIP("sadr prefix for non-sadr channel\n");
+    net = net_get(p->p.main_channel->table, &dst);
+  }
 
   if (s->net && !nl_mergable_route(s, net, p, priority, i->rtm_type))
     nl_announce_route(s);
diff --git a/sysdep/unix/krt.c b/sysdep/unix/krt.c
index e7bd79e3..7166bbec 100644
--- a/sysdep/unix/krt.c
+++ b/sysdep/unix/krt.c
@@ -1103,7 +1103,9 @@ krt_start(struct proto *P)
   switch (p->p.net_type)
   {
   case NET_IP4:	p->af = AF_INET; break;
-  case NET_IP6:	p->af = AF_INET6; break;
+  case NET_IP6:
+  case NET_SADR_IP6:
+    p->af = AF_INET6; break;
 #ifdef AF_MPLS
   case NET_MPLS: p->af = AF_MPLS; break;
 #endif
@@ -1219,9 +1221,9 @@ struct protocol proto_unix_kernel = {
   .attr_class =		EAP_KRT,
   .preference =		DEF_PREF_INHERITED,
 #ifdef HAVE_MPLS_KERNEL
-  .channel_mask =	NB_IP | NB_MPLS,
+  .channel_mask =	NB_IP | NB_SADR_IP6 | NB_MPLS,
 #else
-  .channel_mask =	NB_IP,
+  .channel_mask =	NB_IP | NB_SADR_IP6,
 #endif
   .proto_size =		sizeof(struct krt_proto),
   .config_size =	sizeof(struct krt_config),
-- 
2.16.1



More information about the Bird-users mailing list