+static struct radv_prefix_config default_prefix = {
+ .onlink = 1,
+ .autonomous = 1,
+ .valid_lifetime = DEFAULT_VALID_LIFETIME,
+ .preferred_lifetime = DEFAULT_PREFERRED_LIFETIME
+};
+
+static struct radv_prefix_config dead_prefix = {
+};
+
+/* Find a corresponding config for the given prefix */
+static struct radv_prefix_config *
+radv_prefix_match(struct radv_iface *ifa, net_addr_ip6 *px)
+{
+ struct radv_proto *p = ifa->ra;
+ struct radv_config *cf = (struct radv_config *) (p->p.cf);
+ struct radv_prefix_config *pc;
+
+ WALK_LIST(pc, ifa->cf->pref_list)
+ if (net_in_net_ip6(px, &pc->prefix))
+ return pc;
+
+ WALK_LIST(pc, cf->pref_list)
+ if (net_in_net_ip6(px, &pc->prefix))
+ return pc;
+
+ return &default_prefix;
+}
+
+/*
+ * Go through the list of prefixes, compare them with configs and decide if we
+ * want them or not.
+ */
+static void
+radv_prepare_prefixes(struct radv_iface *ifa)
+{
+ struct radv_proto *p = ifa->ra;
+ struct radv_prefix *pfx, *next;
+ btime now = current_time();
+
+ /* First mark all the prefixes as unused */
+ WALK_LIST(pfx, ifa->prefixes)
+ pfx->mark = 0;
+
+ /* Find all the prefixes we want to use and make sure they are in the list. */
+ struct ifa *addr;
+ WALK_LIST(addr, ifa->iface->addrs)
+ {
+ if ((addr->prefix.type != NET_IP6) ||
+ (addr->scope <= SCOPE_LINK))
+ continue;
+
+ net_addr_ip6 *prefix = (void *) &addr->prefix;
+ struct radv_prefix_config *pc = radv_prefix_match(ifa, prefix);
+
+ if (!pc || pc->skip)
+ continue;
+
+ /* Do we have it already? */
+ struct radv_prefix *existing = NULL;
+ WALK_LIST(pfx, ifa->prefixes)
+ if (net_equal_ip6(&pfx->prefix, prefix))
+ {
+ existing = pfx;
+ break;
+ }
+
+ if (!existing)
+ {
+ RADV_TRACE(D_EVENTS, "Adding new prefix %N on %s",
+ prefix, ifa->iface->name);
+
+ existing = mb_allocz(ifa->pool, sizeof *existing);
+ net_copy_ip6(&existing->prefix, prefix);
+ add_tail(&ifa->prefixes, NODE existing);
+ }
+
+ /*
+ * Update the information (it may have changed, or even bring a prefix back
+ * to life).
+ */
+ existing->valid = 1;
+ existing->changed = now;
+ existing->mark = 1;
+ existing->cf = pc;
+ }
+
+ WALK_LIST_DELSAFE(pfx, next, ifa->prefixes)
+ {
+ if (pfx->valid && !pfx->mark)
+ {
+ RADV_TRACE(D_EVENTS, "Invalidating prefix %N on %s",
+ &pfx->prefix, ifa->iface->name);
+
+ pfx->valid = 0;
+ pfx->changed = now;
+ pfx->cf = &dead_prefix;
+ }
+ }
+}
+
+static void
+radv_prune_prefixes(struct radv_iface *ifa)
+{
+ struct radv_proto *p = ifa->ra;
+ btime now = current_time();
+ btime next = TIME_INFINITY;
+ btime expires = 0;
+
+ struct radv_prefix *px, *pxn;
+ WALK_LIST_DELSAFE(px, pxn, ifa->prefixes)
+ {
+ if (!px->valid)
+ {
+ expires = px->changed + ifa->cf->prefix_linger_time S;
+
+ if (expires <= now)
+ {
+ RADV_TRACE(D_EVENTS, "Removing prefix %N on %s",
+ &px->prefix, ifa->iface->name);
+
+ rem_node(NODE px);
+ mb_free(px);
+ }
+ else
+ next = MIN(next, expires);
+ }
+ }
+
+ ifa->prune_time = next;