+static inline int
+radv_trigger_valid(struct radv_config *cf)
+{
+ return cf->trigger.type != 0;
+}
+
+static inline int
+radv_net_match_trigger(struct radv_config *cf, net *n)
+{
+ return radv_trigger_valid(cf) && net_equal(n->n.addr, &cf->trigger);
+}
+
+int
+radv_import_control(struct proto *P, rte **new, struct linpool *pool UNUSED)
+{
+ // struct radv_proto *p = (struct radv_proto *) P;
+ struct radv_config *cf = (struct radv_config *) (P->cf);
+
+ if (radv_net_match_trigger(cf, (*new)->net))
+ return RIC_PROCESS;
+
+ if (cf->propagate_routes)
+ return RIC_PROCESS;
+ else
+ return RIC_DROP;
+}
+
+static void
+radv_rt_notify(struct proto *P, struct channel *ch UNUSED, net *n, rte *new, rte *old UNUSED)
+{
+ struct radv_proto *p = (struct radv_proto *) P;
+ struct radv_config *cf = (struct radv_config *) (P->cf);
+ struct radv_route *rt;
+ eattr *ea;
+
+ if (radv_net_match_trigger(cf, n))
+ {
+ u8 old_active = p->active;
+ p->active = !!new;
+
+ if (p->active == old_active)
+ return;
+
+ if (p->active)
+ RADV_TRACE(D_EVENTS, "Triggered");
+ else
+ RADV_TRACE(D_EVENTS, "Suppressed");
+
+ radv_iface_notify_all(p, RA_EV_CHANGE);
+ return;
+ }
+
+ if (!cf->propagate_routes)
+ return;
+
+ /*
+ * Some other route we want to send (or stop sending). Update the cache,
+ * with marking a removed one as dead or creating a new one as needed.
+ *
+ * And yes, we exclude the trigger route on purpose.
+ */
+
+ if (new)
+ {
+ /* Update */
+
+ ea = ea_find(new->attrs->eattrs, EA_RA_PREFERENCE);
+ uint preference = ea ? ea->u.data : RA_PREF_MEDIUM;
+ uint preference_set = !!ea;
+
+ ea = ea_find(new->attrs->eattrs, EA_RA_LIFETIME);
+ uint lifetime = ea ? ea->u.data : 0;
+ uint lifetime_set = !!ea;
+
+ if ((preference != RA_PREF_LOW) &&
+ (preference != RA_PREF_MEDIUM) &&
+ (preference != RA_PREF_HIGH))
+ {
+ log(L_WARN "%s: Invalid ra_preference value %u on route %N",
+ p->p.name, preference, n->n.addr);
+ preference = RA_PREF_MEDIUM;
+ preference_set = 1;
+ lifetime = 0;
+ lifetime_set = 1;
+ }
+
+ rt = fib_get(&p->routes, n->n.addr);
+
+ /* Ignore update if nothing changed */
+ if (rt->valid &&
+ (rt->preference == preference) &&
+ (rt->preference_set == preference_set) &&
+ (rt->lifetime == lifetime) &&
+ (rt->lifetime_set == lifetime_set))
+ return;
+
+ if (p->routes.entries == 18)
+ log(L_WARN "%s: More than 17 routes exported to RAdv", p->p.name);
+
+ rt->valid = 1;
+ rt->changed = current_time();
+ rt->preference = preference;
+ rt->preference_set = preference_set;
+ rt->lifetime = lifetime;
+ rt->lifetime_set = lifetime_set;
+ }
+ else
+ {
+ /* Withdraw */
+ rt = fib_find(&p->routes, n->n.addr);
+
+ if (!rt || !rt->valid)
+ return;
+
+ /* Invalidate the route */
+ rt->valid = 0;
+ rt->changed = current_time();
+
+ /* Invalidated route will be pruned eventually */
+ btime expires = rt->changed + cf->max_linger_time S;
+ p->prune_time = MIN(p->prune_time, expires);
+ }
+
+ radv_iface_notify_all(p, RA_EV_CHANGE);
+}
+
+/*
+ * Cleans up all the dead routes that expired and schedules itself to be run
+ * again if there are more routes waiting for expiration.
+ */
+static void
+radv_prune_routes(struct radv_proto *p)
+{
+ struct radv_config *cf = (struct radv_config *) (p->p.cf);
+ btime now = current_time();
+ btime next = TIME_INFINITY;
+ btime expires = 0;
+
+ /* Should not happen */
+ if (!p->fib_up)
+ return;
+
+ struct fib_iterator fit;
+ FIB_ITERATE_INIT(&fit, &p->routes);
+
+again:
+ FIB_ITERATE_START(&p->routes, &fit, struct radv_route, rt)
+ {
+ if (!rt->valid)
+ {
+ expires = rt->changed + cf->max_linger_time S;
+
+ /* Delete expired nodes */
+ if (expires <= now)
+ {
+ FIB_ITERATE_PUT(&fit);
+ fib_delete(&p->routes, rt);
+ goto again;
+ }
+ else
+ next = MIN(next, expires);
+ }
+ }
+ FIB_ITERATE_END;
+
+ p->prune_time = next;
+}
+
+static int
+radv_check_active(struct radv_proto *p)
+{
+ struct radv_config *cf = (struct radv_config *) (p->p.cf);
+
+ if (!radv_trigger_valid(cf))
+ return 1;
+
+ struct channel *c = p->p.main_channel;
+ return rt_examine(c->table, &cf->trigger, &p->p, c->out_filter);
+}
+
+static void
+radv_postconfig(struct proto_config *CF)
+{
+ // struct radv_config *cf = (void *) CF;
+
+ /* Define default channel */
+ if (EMPTY_LIST(CF->channels))
+ channel_config_new(NULL, net_label[NET_IP6], NET_IP6, CF);
+}
+