in6: Add a helper function to compute expiry times

Tidy up a bunch of places that have the same duplicated logic.  Simplify
callers of in6_init_prefix_ltimes().  No functional change intended.

Reviewed by:	pouria, zlei, tuexen, glebius
MFC after:	2 weeks
Sponsored by:	OPNsense
Sponsored by:	Klara, Inc.
Differential Revision:	https://reviews.freebsd.org/D54561
This commit is contained in:
Mark Johnston
2026-01-12 13:49:34 +00:00
parent 7ff1d2be5d
commit fb08f80eaf
3 changed files with 22 additions and 41 deletions
+13 -10
View File
@@ -1029,6 +1029,15 @@ in6_alloc_ifa(struct ifnet *ifp, struct in6_aliasreq *ifra, int flags)
return (ia);
}
time_t
in6_expire_time(uint32_t ltime)
{
if (ltime == ND6_INFINITE_LIFETIME)
return (0);
else
return (time_uptime + ltime);
}
/*
* Update/configure interface address parameters:
*
@@ -1051,16 +1060,10 @@ in6_update_ifa_internal(struct ifnet *ifp, struct in6_aliasreq *ifra,
* these members for applications.
*/
ia->ia6_lifetime = ifra->ifra_lifetime;
if (ia->ia6_lifetime.ia6t_vltime != ND6_INFINITE_LIFETIME) {
ia->ia6_lifetime.ia6t_expire =
time_uptime + ia->ia6_lifetime.ia6t_vltime;
} else
ia->ia6_lifetime.ia6t_expire = 0;
if (ia->ia6_lifetime.ia6t_pltime != ND6_INFINITE_LIFETIME) {
ia->ia6_lifetime.ia6t_preferred =
time_uptime + ia->ia6_lifetime.ia6t_pltime;
} else
ia->ia6_lifetime.ia6t_preferred = 0;
ia->ia6_lifetime.ia6t_expire =
in6_expire_time(ifra->ifra_lifetime.ia6t_vltime);
ia->ia6_lifetime.ia6t_preferred =
in6_expire_time(ifra->ifra_lifetime.ia6t_pltime);
/*
* backward compatibility - if IN6_IFF_DEPRECATED is set from the
+2
View File
@@ -671,6 +671,8 @@ int in6_cksum_partial_l2(struct mbuf *m, uint8_t nxt, uint32_t off_l3,
uint32_t off_l4, uint32_t len, uint32_t cov);
int in6_cksum_pseudo(struct ip6_hdr *, uint32_t, uint8_t, uint16_t);
time_t in6_expire_time(uint32_t);
int in6_localaddr(struct in6_addr *);
int in6_localip(struct in6_addr *);
bool in6_localip_fib(struct in6_addr *, uint16_t);
+7 -31
View File
@@ -1146,39 +1146,18 @@ defrtrlist_update(struct nd_defrouter *new)
return (n);
}
static int
static void
in6_init_prefix_ltimes(struct nd_prefix *ndpr)
{
if (ndpr->ndpr_pltime == ND6_INFINITE_LIFETIME)
ndpr->ndpr_preferred = 0;
else
ndpr->ndpr_preferred = time_uptime + ndpr->ndpr_pltime;
if (ndpr->ndpr_vltime == ND6_INFINITE_LIFETIME)
ndpr->ndpr_expire = 0;
else
ndpr->ndpr_expire = time_uptime + ndpr->ndpr_vltime;
return 0;
ndpr->ndpr_preferred = in6_expire_time(ndpr->ndpr_pltime);
ndpr->ndpr_expire = in6_expire_time(ndpr->ndpr_vltime);
}
static void
in6_init_address_ltimes(struct nd_prefix *new, struct in6_addrlifetime *lt6)
{
/* init ia6t_expire */
if (lt6->ia6t_vltime == ND6_INFINITE_LIFETIME)
lt6->ia6t_expire = 0;
else {
lt6->ia6t_expire = time_uptime;
lt6->ia6t_expire += lt6->ia6t_vltime;
}
/* init ia6t_preferred */
if (lt6->ia6t_pltime == ND6_INFINITE_LIFETIME)
lt6->ia6t_preferred = 0;
else {
lt6->ia6t_preferred = time_uptime;
lt6->ia6t_preferred += lt6->ia6t_pltime;
}
lt6->ia6t_preferred = in6_expire_time(lt6->ia6t_pltime);
lt6->ia6t_expire = in6_expire_time(lt6->ia6t_vltime);
}
static struct in6_ifaddr *
@@ -1394,11 +1373,8 @@ nd6_prelist_add(struct nd_prefixctl *pr, struct nd_defrouter *dr,
new->ndpr_vltime = pr->ndpr_vltime;
new->ndpr_pltime = pr->ndpr_pltime;
new->ndpr_flags = pr->ndpr_flags;
if ((error = in6_init_prefix_ltimes(new)) != 0) {
free(new, M_IP6NDP);
return (error);
}
new->ndpr_lastupdate = time_uptime;
in6_init_prefix_ltimes(new);
/* initialization */
LIST_INIT(&new->ndpr_advrtrs);
@@ -1542,7 +1518,7 @@ prelist_update(struct nd_prefixctl *new, struct nd_defrouter *dr,
if (new->ndpr_raf_onlink) {
pr->ndpr_vltime = new->ndpr_vltime;
pr->ndpr_pltime = new->ndpr_pltime;
(void)in6_init_prefix_ltimes(pr); /* XXX error case? */
in6_init_prefix_ltimes(pr);
pr->ndpr_lastupdate = time_uptime;
}