+
+/*
+ * BFD automatic source selection
+ *
+ * This feature will use the next hop tracking (NHT) provided by zebra
+ * to find out the source address by looking at the output interface.
+ *
+ * When the interface address / routing table change we'll be notified
+ * and be able to update the source address accordingly.
+ *
+ * <daemon> zebra
+ * |
+ * +-----------------+
+ * | BFD session set |
+ * | to auto source |
+ * +-----------------+
+ * |
+ * \ +-----------------+
+ * --------------> | Resolves |
+ * | destination |
+ * | address |
+ * +-----------------+
+ * |
+ * +-----------------+ /
+ * | Sets resolved | <----------
+ * | source address |
+ * +-----------------+
+ */
+static bool
+bfd_source_cache_session_match(const struct bfd_source_cache *source,
+ const struct bfd_session_params *session)
+{
+ const struct in_addr *address;
+ const struct in6_addr *address_v6;
+
+ if (session->args.vrf_id != source->vrf_id)
+ return false;
+ if (session->args.family != source->address.family)
+ return false;
+
+ switch (session->args.family) {
+ case AF_INET:
+ address = (const struct in_addr *)&session->args.dst;
+ if (address->s_addr != source->address.u.prefix4.s_addr)
+ return false;
+ break;
+ case AF_INET6:
+ address_v6 = &session->args.dst;
+ if (memcmp(address_v6, &source->address.u.prefix6,
+ sizeof(struct in6_addr)))
+ return false;
+ break;
+ default:
+ return false;
+ }
+
+ return true;
+}
+
+static struct bfd_source_cache *
+bfd_source_cache_find(vrf_id_t vrf_id, const struct prefix *prefix)
+{
+ struct bfd_source_cache *source;
+
+ SLIST_FOREACH (source, &bsglobal.source_list, entry) {
+ if (source->vrf_id != vrf_id)
+ continue;
+ if (!prefix_same(&source->address, prefix))
+ continue;
+
+ return source;
+ }
+
+ return NULL;
+}
+
+static void bfd_source_cache_get(struct bfd_session_params *session)
+{
+ struct bfd_source_cache *source;
+ struct prefix target = {};
+
+ switch (session->args.family) {
+ case AF_INET:
+ target.family = AF_INET;
+ target.prefixlen = IPV4_MAX_BITLEN;
+ memcpy(&target.u.prefix4, &session->args.dst,
+ sizeof(struct in_addr));
+ break;
+ case AF_INET6:
+ target.family = AF_INET6;
+ target.prefixlen = IPV6_MAX_BITLEN;
+ memcpy(&target.u.prefix6, &session->args.dst,
+ sizeof(struct in6_addr));
+ break;
+ default:
+ return;
+ }
+
+ source = bfd_source_cache_find(session->args.vrf_id, &target);
+ if (source) {
+ if (session->source_cache == source)
+ return;
+
+ bfd_source_cache_put(session);
+ session->source_cache = source;
+ source->refcount++;
+ return;
+ }
+
+ source = XCALLOC(MTYPE_BFD_SOURCE, sizeof(*source));
+ prefix_copy(&source->address, &target);
+ source->vrf_id = session->args.vrf_id;
+ SLIST_INSERT_HEAD(&bsglobal.source_list, source, entry);
+
+ bfd_source_cache_put(session);
+ session->source_cache = source;
+ source->refcount = 1;
+
+ return;
+}
+
+static void bfd_source_cache_put(struct bfd_session_params *session)
+{
+ if (session->source_cache == NULL)
+ return;
+
+ session->source_cache->refcount--;
+ if (session->source_cache->refcount > 0) {
+ session->source_cache = NULL;
+ return;
+ }
+
+ SLIST_REMOVE(&bsglobal.source_list, session->source_cache,
+ bfd_source_cache, entry);
+ XFREE(MTYPE_BFD_SOURCE, session->source_cache);
+}
+
+/** Updates BFD running session if source address has changed. */
+static void
+bfd_source_cache_update_session(const struct bfd_source_cache *source,
+ struct bfd_session_params *session)
+{
+ const struct in_addr *address;
+ const struct in6_addr *address_v6;
+
+ switch (session->args.family) {
+ case AF_INET:
+ address = (const struct in_addr *)&session->args.src;
+ if (memcmp(address, &source->source.u.prefix4,
+ sizeof(struct in_addr)) == 0)
+ return;
+
+ _bfd_sess_remove(session);
+ memcpy(&session->args.src, &source->source.u.prefix4,
+ sizeof(struct in_addr));
+ break;
+ case AF_INET6:
+ address_v6 = &session->args.src;
+ if (memcmp(address_v6, &source->source.u.prefix6,
+ sizeof(struct in6_addr)) == 0)
+ return;
+
+ _bfd_sess_remove(session);
+ memcpy(&session->args.src, &source->source.u.prefix6,
+ sizeof(struct in6_addr));
+ break;
+ default:
+ return;
+ }
+
+ bfd_sess_install(session);
+}
+
+static void
+bfd_source_cache_update_sessions(const struct bfd_source_cache *source)
+{
+ struct bfd_session_params *session;
+
+ if (!source->valid)
+ return;
+
+ TAILQ_FOREACH (session, &bsglobal.bsplist, entry) {
+ if (!session->auto_source)
+ continue;
+ if (!bfd_source_cache_session_match(source, session))
+ continue;
+
+ bfd_source_cache_update_session(source, session);
+ }
+}
+
+/**
+ * Try to translate next hop information into source address.
+ *
+ * \returns `true` if source changed otherwise `false`.
+ */
+static bool bfd_source_cache_update(struct bfd_source_cache *source,
+ const struct zapi_route *route)
+{
+ size_t nh_index;
+
+ for (nh_index = 0; nh_index < route->nexthop_num; nh_index++) {
+ const struct zapi_nexthop *nh = &route->nexthops[nh_index];
+ const struct interface *interface;
+ const struct connected *connected;
+ const struct listnode *node;
+
+ interface = if_lookup_by_index(nh->ifindex, nh->vrf_id);
+ if (interface == NULL) {
+ zlog_err("next hop interface not found (index %d)",
+ nh->ifindex);
+ continue;
+ }
+
+ for (ALL_LIST_ELEMENTS_RO(interface->connected, node,
+ connected)) {
+ if (source->address.family !=
+ connected->address->family)
+ continue;
+ if (prefix_same(connected->address, &source->source))
+ return false;
+ /*
+ * Skip link-local as it is only useful for single hop
+ * and in that case no source is specified usually.
+ */
+ if (source->address.family == AF_INET6 &&
+ IN6_IS_ADDR_LINKLOCAL(
+ &connected->address->u.prefix6))
+ continue;
+
+ prefix_copy(&source->source, connected->address);
+ source->valid = true;
+ return true;
+ }
+ }
+
+ memset(&source->source, 0, sizeof(source->source));
+ source->valid = false;
+ return false;
+}
+
+int bfd_nht_update(const struct prefix *match, const struct zapi_route *route)
+{
+ struct bfd_source_cache *source;
+
+ if (bsglobal.debugging)
+ zlog_debug("BFD NHT update for %pFX", &route->prefix);
+
+ SLIST_FOREACH (source, &bsglobal.source_list, entry) {
+ if (source->vrf_id != route->vrf_id)
+ continue;
+ if (!prefix_same(match, &source->address))
+ continue;
+ if (bfd_source_cache_update(source, route))
+ bfd_source_cache_update_sessions(source);
+ }
+
+ return 0;
+}