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

router: implement RFC 7084 errata 7699 #211

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
85 changes: 84 additions & 1 deletion src/netlink.c
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
#include <netlink/attr.h>

#include <arpa/inet.h>
#include <net/route.h>
#include <libubox/list.h>

#include "odhcpd.h"
Expand Down Expand Up @@ -54,8 +55,15 @@ static struct event_socket rtnl_event = {
.sock_bufsize = 133120,
};

int netlink_init(void)
static FILE *fp_ipv6_route = NULL;

int netlink_init(bool ipv6)
{
if (ipv6 && !(fp_ipv6_route = fopen("/proc/net/ipv6_route", "r"))) {
syslog(LOG_ERR, "fopen(/proc/net/ipv6_route): %m");
goto err;
}

rtnl_socket = create_socket(NETLINK_ROUTE);
if (!rtnl_socket) {
syslog(LOG_ERR, "Unable to open nl socket: %m");
Expand Down Expand Up @@ -100,9 +108,67 @@ int netlink_init(void)
rtnl_event.ev.uloop.fd = -1;
}

if (fp_ipv6_route) {
fclose(fp_ipv6_route);
fp_ipv6_route = NULL;
}

return -1;
}

/* Detect whether a default IPv6 route exists */
bool netlink_default_ipv6_route_exists()
{
bool found_default = false;
char line[512], ifname[16];

if (!fp_ipv6_route)
return false;
rewind(fp_ipv6_route);

while (fgets(line, sizeof(line), fp_ipv6_route)) {
if (sscanf(line, "00000000000000000000000000000000 00 "
"%*s %*s %*s %*s %*s %*s %*s %15s", ifname) &&
strcmp(ifname, "lo")) {
found_default = true;
break;
}
}

return found_default;
}

/* Find the source prefixes of all IPv6 addresses */
static void find_addr6_dprefix(struct odhcpd_ipaddr *n, ssize_t len)
{
struct odhcpd_ipaddr p = { .addr.in6 = IN6ADDR_ANY_INIT, .prefix = 0,
.dprefix = 0, .preferred = 0, .valid = 0};
char line[512];
uint32_t rflags;

if (!fp_ipv6_route || len <= 0)
return;
rewind(fp_ipv6_route);

while (fgets(line, sizeof(line), fp_ipv6_route)) {
if (sscanf(line, "%8" SCNx32 "%8" SCNx32 "%*8" SCNx32 "%*8" SCNx32 " %hhx %*s "
"%*s 00000000000000000000000000000000 %*s %*s %*s %" SCNx32 " lo",
&p.addr.in6.s6_addr32[0], &p.addr.in6.s6_addr32[1], &p.prefix, &rflags) &&
p.prefix > 0 && (rflags & RTF_NONEXTHOP) && (rflags & RTF_REJECT)) {
// Find source prefixes by scanning through unreachable-routes
p.addr.in6.s6_addr32[0] = htonl(p.addr.in6.s6_addr32[0]);
p.addr.in6.s6_addr32[1] = htonl(p.addr.in6.s6_addr32[1]);

for (ssize_t i = 0; i < len; ++i) {
if (n[i].prefix <= 64 && n[i].prefix >= p.prefix &&
!odhcpd_bmemcmp(&p.addr.in6, &n[i].addr.in6, p.prefix)) {
n[i].dprefix = p.prefix;
break;
}
}
}
}
}

int netlink_add_netevent_handler(struct netevent_handler *handler)
{
Expand Down Expand Up @@ -212,6 +278,20 @@ static void refresh_iface_addr6(int ifindex)
}

if (change) {
/*
* Remove invalid prefixes that were added back to the interface.
*/
for (ssize_t i = 0; i < len; ++i) {
size_t j = 0;
while (j < iface->invalid_addr6_len) {
if (addr[i].prefix <= iface->invalid_addr6[j].prefix &&
odhcpd_bmemcmp(&addr[i].addr.in6, &iface->invalid_addr6[j].addr.in6, iface->invalid_addr6[j].prefix) == 0)
odhcpd_del_intf_invalid_addr6(iface, j);
else
++j;
}
}

/*
* Keep track on removed prefixes, so we could advertise them as invalid
* for at least a couple of times.
Expand Down Expand Up @@ -756,6 +836,9 @@ ssize_t netlink_get_interface_addrs(int ifindex, bool v6, struct odhcpd_ipaddr *
addr[i].valid += now;
}

if (v6)
find_addr6_dprefix(addr, ctxt.ret);

free:
nlmsg_free(msg);
out:
Expand Down
5 changes: 3 additions & 2 deletions src/odhcpd.c
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ int main(int argc, char **argv)
{
openlog("odhcpd", LOG_PERROR | LOG_PID, LOG_DAEMON);
int opt;
bool ipv6 = ipv6_enabled();

while ((opt = getopt(argc, argv, "hl:")) != -1) {
switch (opt) {
Expand Down Expand Up @@ -109,10 +110,10 @@ int main(int argc, char **argv)
signal(SIGINT, sighandler);
signal(SIGTERM, sighandler);

if (netlink_init())
if (netlink_init(ipv6))
return 4;

if (ipv6_enabled()) {
if (ipv6) {
if (router_init())
return 4;

Expand Down
23 changes: 22 additions & 1 deletion src/odhcpd.h
Original file line number Diff line number Diff line change
Expand Up @@ -399,6 +399,26 @@ inline static struct dhcp_assignment *alloc_assignment(size_t extra_len)
return a;
}

inline static void odhcpd_del_intf_invalid_addr6(struct interface *iface, size_t idx)
{
if (idx + 1 < iface->invalid_addr6_len)
memmove(&iface->invalid_addr6[idx], &iface->invalid_addr6[idx + 1],
sizeof(*iface->invalid_addr6) * (iface->invalid_addr6_len - idx - 1));

iface->invalid_addr6_len--;

if (iface->invalid_addr6_len) {
struct odhcpd_ipaddr *new_invalid_addr6 = realloc(iface->invalid_addr6,
sizeof(*iface->invalid_addr6) * iface->invalid_addr6_len);

if (new_invalid_addr6)
iface->invalid_addr6 = new_invalid_addr6;
} else {
free(iface->invalid_addr6);
iface->invalid_addr6 = NULL;
}
}

// Exported main functions
int odhcpd_register(struct odhcpd_event *event);
int odhcpd_deregister(struct odhcpd_event *event);
Expand Down Expand Up @@ -452,6 +472,7 @@ void dhcpv6_ia_enum_addrs(struct interface *iface, struct dhcp_assignment *c, ti
dhcpv6_binding_cb_handler_t func, void *arg);
void dhcpv6_ia_write_statefile(void);

bool netlink_default_ipv6_route_exists();
int netlink_add_netevent_handler(struct netevent_handler *hdlr);
ssize_t netlink_get_interface_addrs(const int ifindex, bool v6,
struct odhcpd_ipaddr **addrs);
Expand All @@ -468,7 +489,7 @@ void netlink_dump_neigh_table(const bool proxy);
void netlink_dump_addr_table(const bool v6);

// Exported module initializers
int netlink_init(void);
int netlink_init(bool ipv6);
int router_init(void);
int dhcpv6_init(void);
int ndp_init(void);
Expand Down
Loading