Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add route lifetime from Router Advertisement #429

Open
wants to merge 13 commits into
base: master
Choose a base branch
from
15 changes: 15 additions & 0 deletions src/common.c
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@

#include "common.h"
#include "dhcpcd.h"
#include "eloop.h"
#include "if-options.h"

const char *
Expand Down Expand Up @@ -214,3 +215,17 @@ is_root_local(void)
return -1;
#endif
}

uint32_t
lifetime_left(uint32_t lifetime, const struct timespec *acquired, const struct timespec *now)
{
uint32_t elapsed;

if (lifetime == INFINITE_LIFETIME)
return lifetime;

elapsed = (uint32_t)eloop_timespec_diff(now, &acquired, NULL);
if (elapsed > lifetime)
return 0;
return lifetime - elapsed;
}
3 changes: 3 additions & 0 deletions src/common.h
Original file line number Diff line number Diff line change
Expand Up @@ -141,11 +141,14 @@
# endif
#endif

#define INFINITE_LIFETIME (~0U)

const char *hwaddr_ntoa(const void *, size_t, char *, size_t);
size_t hwaddr_aton(uint8_t *, const char *);
ssize_t readfile(const char *, void *, size_t);
ssize_t writefile(const char *, mode_t, const void *, size_t);
int filemtime(const char *, time_t *);
char *get_line(char ** __restrict, ssize_t * __restrict);
int is_root_local(void);
uint32_t lifetime_left(uint32_t, const struct timespec *, const struct timespec *);
#endif
9 changes: 9 additions & 0 deletions src/if-linux.c
Original file line number Diff line number Diff line change
Expand Up @@ -752,6 +752,11 @@ if_copyrt(struct dhcpcd_ctx *ctx, struct rt *rt, struct nlmsghdr *nlm)
}
break;
}
case RTA_EXPIRES:
{
rt->rt_expires = *(uint32_t *)RTA_DATA(rta);
break;
}
}

if (sa != NULL) {
Expand Down Expand Up @@ -1735,6 +1740,10 @@ if_route(unsigned char cmd, const struct rt *rt)
if (!sa_is_loopback(&rt->rt_gateway))
add_attr_32(&nlm.hdr, sizeof(nlm), RTA_OIF, rt->rt_ifp->index);

/* add route lifetime */
if (rt->rt_expires != 0)
add_attr_32(&nlm.hdr, sizeof(nlm), RTA_EXPIRES, rt->rt_expires);

if (rt->rt_metric != 0)
add_attr_32(&nlm.hdr, sizeof(nlm), RTA_PRIORITY,
rt->rt_metric);
Expand Down
8 changes: 8 additions & 0 deletions src/ipv6.c
Original file line number Diff line number Diff line change
Expand Up @@ -2305,6 +2305,9 @@ inet6_raroutes(rb_tree_t *routes, struct dhcpcd_ctx *ctx)
if (ctx->ra_routers == NULL)
return 0;

struct timespec now;
clock_gettime(CLOCK_MONOTONIC, &now);

TAILQ_FOREACH(rap, ctx->ra_routers, next) {
if (rap->expired)
continue;
Expand All @@ -2325,6 +2328,7 @@ inet6_raroutes(rb_tree_t *routes, struct dhcpcd_ctx *ctx)
#ifdef HAVE_ROUTE_PREF
rt->rt_pref = ipv6nd_rtpref(rinfo->flags);
#endif
rt->rt_expires = lifetime_left(rap->lifetime, &rinfo->acquired, &now);

rt_proto_add(routes, rt);
}
Expand All @@ -2339,6 +2343,8 @@ inet6_raroutes(rb_tree_t *routes, struct dhcpcd_ctx *ctx)
#ifdef HAVE_ROUTE_PREF
rt->rt_pref = ipv6nd_rtpref(rap->flags);
#endif
rt->rt_expires = lifetime_left(rap->lifetime, &addr->acquired, &now);

rt_proto_add(routes, rt);
}
}
Expand Down Expand Up @@ -2370,6 +2376,8 @@ inet6_raroutes(rb_tree_t *routes, struct dhcpcd_ctx *ctx)
#ifdef HAVE_ROUTE_PREF
rt->rt_pref = ipv6nd_rtpref(rap->flags);
#endif
rt->rt_expires = lifetime_left(rap->lifetime, &rap->acquired, &now);

rt_proto_add(routes, rt);
}
return 0;
Expand Down
1 change: 1 addition & 0 deletions src/route.h
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ struct rt {
#define RTDF_GATELINK 0x40 /* Gateway is on link */
size_t rt_order;
rb_node_t rt_tree;
uint32_t rt_expires; /* current lifetime of route */
};

extern const rb_tree_ops_t rt_compare_list_ops;
Expand Down
Loading