summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/config.c5
-rw-r--r--src/dhcpv6-ia.c134
-rw-r--r--src/dhcpv6.c4
-rw-r--r--src/dhcpv6.h3
-rw-r--r--src/ndp.c160
-rw-r--r--src/odhcpd.c56
-rw-r--r--src/odhcpd.h4
-rw-r--r--src/router.c92
-rw-r--r--src/router.h6
9 files changed, 246 insertions, 218 deletions
diff --git a/src/config.c b/src/config.c
index f9ad3b8..7d870f1 100644
--- a/src/config.c
+++ b/src/config.c
@@ -40,6 +40,7 @@ enum {
IFACE_ATTR_RA_OFFLINK,
IFACE_ATTR_RA_PREFERENCE,
IFACE_ATTR_RA_ADVROUTER,
+ IFACE_ATTR_RA_MAXINTERVAL,
IFACE_ATTR_PD_MANAGER,
IFACE_ATTR_PD_CER,
IFACE_ATTR_NDPROXY_ROUTING,
@@ -74,6 +75,7 @@ static const struct blobmsg_policy iface_attrs[IFACE_ATTR_MAX] = {
[IFACE_ATTR_RA_OFFLINK] = { .name = "ra_offlink", .type = BLOBMSG_TYPE_BOOL },
[IFACE_ATTR_RA_PREFERENCE] = { .name = "ra_preference", .type = BLOBMSG_TYPE_STRING },
[IFACE_ATTR_RA_ADVROUTER] = { .name = "ra_advrouter", .type = BLOBMSG_TYPE_BOOL },
+ [IFACE_ATTR_RA_MAXINTERVAL] = { .name = "ra_maxinterval", .type = BLOBMSG_TYPE_INT32 },
[IFACE_ATTR_NDPROXY_ROUTING] = { .name = "ndproxy_routing", .type = BLOBMSG_TYPE_BOOL },
[IFACE_ATTR_NDPROXY_SLAVE] = { .name = "ndproxy_slave", .type = BLOBMSG_TYPE_BOOL },
};
@@ -515,6 +517,9 @@ int config_parse_interface(void *data, size_t len, const char *name, bool overwr
if ((c = tb[IFACE_ATTR_RA_ADVROUTER]))
iface->ra_advrouter = blobmsg_get_bool(c);
+ if ((c = tb[IFACE_ATTR_RA_MAXINTERVAL]))
+ iface->ra_maxinterval = blobmsg_get_u32(c);
+
if ((c = tb[IFACE_ATTR_RA_PREFERENCE])) {
const char *prio = blobmsg_get_string(c);
diff --git a/src/dhcpv6-ia.c b/src/dhcpv6-ia.c
index 0bcb453..c5f8783 100644
--- a/src/dhcpv6-ia.c
+++ b/src/dhcpv6-ia.c
@@ -34,7 +34,6 @@
#include <sys/timerfd.h>
-static void update(struct interface *iface);
static void reconf_timer(struct uloop_timeout *event);
static struct uloop_timeout reconf_event = {.cb = reconf_timer};
static uint32_t serial = 0;
@@ -89,8 +88,6 @@ int setup_dhcpv6_ia_interface(struct interface *iface, bool enable)
list_add(&border->head, &iface->ia_assignments);
}
- update(iface);
-
// Parse static entries
struct lease *lease;
list_for_each_entry(lease, &leases, head) {
@@ -363,7 +360,7 @@ static void apply_lease(struct interface *iface, struct dhcpv6_assignment *a, bo
struct in6_addr prefix = addrs[i].addr;
prefix.s6_addr32[1] |= htonl(a->assigned);
odhcpd_setup_route(&prefix, (a->managed_size) ? addrs[i].prefix : a->length,
- iface, &a->peer.sin6_addr, add);
+ iface, &a->peer.sin6_addr, 1024, add);
}
}
@@ -556,106 +553,73 @@ static bool assign_na(struct interface *iface, struct dhcpv6_assignment *assign)
return false;
}
-
-static int prefixcmp(const void *va, const void *vb)
+void dhcpv6_ia_preupdate(struct interface *iface)
{
- const struct odhcpd_ipaddr *a = va, *b = vb;
- uint32_t a_pref = ((a->addr.s6_addr[0] & 0xfe) != 0xfc) ? a->preferred : 1;
- uint32_t b_pref = ((b->addr.s6_addr[0] & 0xfe) != 0xfc) ? b->preferred : 1;
- return (a_pref < b_pref) ? 1 : (a_pref > b_pref) ? -1 : 0;
-}
+ if (iface->dhcpv6 != RELAYD_SERVER)
+ return;
+ struct dhcpv6_assignment *c, *border = list_last_entry(
+ &iface->ia_assignments, struct dhcpv6_assignment, head);
+ list_for_each_entry(c, &iface->ia_assignments, head)
+ if (c != border && !iface->managed)
+ apply_lease(iface, c, false);
+}
-static void update(struct interface *iface)
+void dhcpv6_ia_postupdate(struct interface *iface, time_t now)
{
- struct odhcpd_ipaddr addr[8];
- memset(addr, 0, sizeof(addr));
- int len = odhcpd_get_interface_addresses(iface->ifindex, addr, 8);
-
- if (len < 0)
+ if (iface->dhcpv6 != RELAYD_SERVER)
return;
- qsort(addr, len, sizeof(*addr), prefixcmp);
-
- time_t now = odhcpd_time();
int minprefix = -1;
-
- for (int i = 0; i < len; ++i) {
- if (addr[i].preferred > 0 && addr[i].prefix < 64 &&
- addr[i].prefix > minprefix)
- minprefix = addr[i].prefix;
-
- addr[i].addr.s6_addr32[3] = 0;
-
- if (addr[i].preferred < UINT32_MAX - now)
- addr[i].preferred += now;
-
- if (addr[i].valid < UINT32_MAX - now)
- addr[i].valid += now;
+ for (size_t i = 0; i < iface->ia_addr_len; ++i) {
+ if (iface->ia_addr[i].preferred > now &&
+ iface->ia_addr[i].prefix < 64 &&
+ iface->ia_addr[i].prefix > minprefix)
+ minprefix = iface->ia_addr[i].prefix;
}
- struct dhcpv6_assignment *border = list_last_entry(&iface->ia_assignments, struct dhcpv6_assignment, head);
+ struct dhcpv6_assignment *border = list_last_entry(
+ &iface->ia_assignments, struct dhcpv6_assignment, head);
if (minprefix > 32 && minprefix <= 64)
border->assigned = 1U << (64 - minprefix);
else
border->assigned = 0;
- bool change = len != (int)iface->ia_addr_len;
- for (int i = 0; !change && i < len; ++i)
- if (addr[i].addr.s6_addr32[0] != iface->ia_addr[i].addr.s6_addr32[0] ||
- addr[i].addr.s6_addr32[1] != iface->ia_addr[i].addr.s6_addr32[1] ||
- (addr[i].preferred > 0) != (iface->ia_addr[i].preferred > 0) ||
- (addr[i].valid > (uint32_t)now + 7200) !=
- (iface->ia_addr[i].valid > (uint32_t)now + 7200))
- change = true;
-
- if (change) {
- struct dhcpv6_assignment *c;
- list_for_each_entry(c, &iface->ia_assignments, head)
- if (c != border && !iface->managed)
- apply_lease(iface, c, false);
- }
-
- memcpy(iface->ia_addr, addr, len * sizeof(*addr));
- iface->ia_addr_len = len;
+ struct list_head reassign = LIST_HEAD_INIT(reassign);
+ struct dhcpv6_assignment *c, *d;
+ list_for_each_entry_safe(c, d, &iface->ia_assignments, head) {
+ if (c->clid_len == 0 || c->valid_until < now || c->managed_size)
+ continue;
- if (change) { // Addresses / prefixes have changed
- struct list_head reassign = LIST_HEAD_INIT(reassign);
- struct dhcpv6_assignment *c, *d;
- list_for_each_entry_safe(c, d, &iface->ia_assignments, head) {
- if (c->clid_len == 0 || c->valid_until < now || c->managed_size)
- continue;
+ if (c->length < 128 && c->assigned >= border->assigned && c != border)
+ list_move(&c->head, &reassign);
+ else if (c != border)
+ apply_lease(iface, c, true);
- if (c->length < 128 && c->assigned >= border->assigned && c != border)
- list_move(&c->head, &reassign);
- else if (c != border)
- apply_lease(iface, c, true);
-
- if (c->accept_reconf && c->reconf_cnt == 0) {
- c->reconf_cnt = 1;
- c->reconf_sent = now;
- send_reconf(iface, c);
-
- // Leave all other assignments of that client alone
- struct dhcpv6_assignment *a;
- list_for_each_entry(a, &iface->ia_assignments, head)
- if (a != c && a->clid_len == c->clid_len &&
- !memcmp(a->clid_data, c->clid_data, a->clid_len))
- c->reconf_cnt = INT_MAX;
- }
+ if (c->accept_reconf && c->reconf_cnt == 0) {
+ c->reconf_cnt = 1;
+ c->reconf_sent = now;
+ send_reconf(iface, c);
+
+ // Leave all other assignments of that client alone
+ struct dhcpv6_assignment *a;
+ list_for_each_entry(a, &iface->ia_assignments, head)
+ if (a != c && a->clid_len == c->clid_len &&
+ !memcmp(a->clid_data, c->clid_data, a->clid_len))
+ c->reconf_cnt = INT_MAX;
}
+ }
- while (!list_empty(&reassign)) {
- c = list_first_entry(&reassign, struct dhcpv6_assignment, head);
- list_del(&c->head);
- if (!assign_pd(iface, c)) {
- c->assigned = 0;
- list_add(&c->head, &iface->ia_assignments);
- }
+ while (!list_empty(&reassign)) {
+ c = list_first_entry(&reassign, struct dhcpv6_assignment, head);
+ list_del(&c->head);
+ if (!assign_pd(iface, c)) {
+ c->assigned = 0;
+ list_add(&c->head, &iface->ia_assignments);
}
-
- dhcpv6_write_statefile();
}
+
+ dhcpv6_write_statefile();
}
@@ -1009,8 +973,6 @@ ssize_t dhcpv6_handle_ia(uint8_t *buf, size_t buflen, struct interface *iface,
if (!clid_data || !clid_len || clid_len > 130)
goto out;
- update(iface);
-
struct dhcpv6_assignment *first = NULL;
dhcpv6_for_each_option(start, end, otype, olen, odata) {
bool is_pd = (otype == DHCPV6_OPT_IA_PD);
diff --git a/src/dhcpv6.c b/src/dhcpv6.c
index 7302d5d..c62a08f 100644
--- a/src/dhcpv6.c
+++ b/src/dhcpv6.c
@@ -51,7 +51,7 @@ int setup_dhcpv6_interface(struct interface *iface, bool enable)
}
// Configure multicast settings
- if (enable && iface->dhcpv6 && !iface->master) {
+ if (enable && iface->dhcpv6) {
int sock = socket(AF_INET6, SOCK_DGRAM | SOCK_CLOEXEC, IPPROTO_UDP);
if (sock < 0) {
syslog(LOG_ERR, "Failed to create DHCPv6 server socket: %s",
@@ -532,5 +532,5 @@ static void relay_client_request(struct sockaddr_in6 *source,
struct sockaddr_in6 dhcpv6_servers = {AF_INET6,
htons(DHCPV6_SERVER_PORT), 0, ALL_DHCPV6_SERVERS, 0};
struct iovec iov[2] = {{&hdr, sizeof(hdr)}, {(void*)data, len}};
- odhcpd_send(iface->dhcpv6_event.uloop.fd, &dhcpv6_servers, iov, 2, master);
+ odhcpd_send(master->dhcpv6_event.uloop.fd, &dhcpv6_servers, iov, 2, master);
}
diff --git a/src/dhcpv6.h b/src/dhcpv6.h
index 09aa6f1..d9f42c8 100644
--- a/src/dhcpv6.h
+++ b/src/dhcpv6.h
@@ -14,6 +14,7 @@
#pragma once
#include <libubox/ustream.h>
+#include "odhcpd.h"
#define ALL_DHCPV6_RELAYS {{{0xff, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,\
0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x02}}}
@@ -181,3 +182,5 @@ ssize_t dhcpv6_handle_ia(uint8_t *buf, size_t buflen, struct interface *iface,
int dhcpv6_ia_init(void);
int setup_dhcpv6_ia_interface(struct interface *iface, bool enable);
void dhcpv6_write_statefile(void);
+void dhcpv6_ia_preupdate(struct interface *iface);
+void dhcpv6_ia_postupdate(struct interface *iface, time_t now);
diff --git a/src/ndp.c b/src/ndp.c
index 5e8b5ac..7f63f96 100644
--- a/src/ndp.c
+++ b/src/ndp.c
@@ -29,6 +29,7 @@
#include <linux/rtnetlink.h>
#include <linux/filter.h>
#include "router.h"
+#include "dhcpv6.h"
#include "ndp.h"
@@ -37,8 +38,6 @@ static void handle_solicit(void *addr, void *data, size_t len,
struct interface *iface, void *dest);
static void handle_rtnetlink(void *addr, void *data, size_t len,
struct interface *iface, void *dest);
-static ssize_t ping6(struct in6_addr *addr,
- const struct interface *iface);
static uint32_t rtnl_seqid = 0;
static int ping_socket = -1;
@@ -209,17 +208,16 @@ int setup_ndp_interface(struct interface *iface, bool enable)
// Send an ICMP-ECHO. This is less for actually pinging but for the
// neighbor cache to be kept up-to-date.
-static ssize_t ping6(struct in6_addr *addr,
+static void ping6(struct in6_addr *addr,
const struct interface *iface)
{
- struct sockaddr_in6 dest = {AF_INET6, 0, 0, *addr, 0};
+ struct sockaddr_in6 dest = {AF_INET6, 0, 0, *addr, iface->ifindex};
struct icmp6_hdr echo = {.icmp6_type = ICMP6_ECHO_REQUEST};
struct iovec iov = {&echo, sizeof(echo)};
- // Linux seems to not honor IPV6_PKTINFO on raw-sockets, so work around
- setsockopt(ping_socket, SOL_SOCKET, SO_BINDTODEVICE,
- iface->ifname, sizeof(iface->ifname));
- return odhcpd_send(ping_socket, &dest, &iov, 1, iface);
+ odhcpd_setup_route(addr, 128, iface, NULL, 128, true);
+ odhcpd_send(ping_socket, &dest, &iov, 1, iface);
+ odhcpd_setup_route(addr, 128, iface, NULL, 128, false);
}
@@ -263,52 +261,6 @@ static void handle_solicit(void *addr, void *data, size_t len,
ping6(&req->nd_ns_target, c);
}
-
-void odhcpd_setup_route(const struct in6_addr *addr, int prefixlen,
- const struct interface *iface, const struct in6_addr *gw, bool add)
-{
- struct req {
- struct nlmsghdr nh;
- struct rtmsg rtm;
- struct rtattr rta_dst;
- struct in6_addr dst_addr;
- struct rtattr rta_oif;
- uint32_t ifindex;
- struct rtattr rta_table;
- uint32_t table;
- struct rtattr rta_gw;
- struct in6_addr gw;
- } req = {
- {sizeof(req), 0, NLM_F_REQUEST, ++rtnl_seqid, 0},
- {AF_INET6, prefixlen, 0, 0, 0, 0, 0, 0, 0},
- {sizeof(struct rtattr) + sizeof(struct in6_addr), RTA_DST},
- *addr,
- {sizeof(struct rtattr) + sizeof(uint32_t), RTA_OIF},
- iface->ifindex,
- {sizeof(struct rtattr) + sizeof(uint32_t), RTA_TABLE},
- RT_TABLE_MAIN,
- {sizeof(struct rtattr) + sizeof(struct in6_addr), RTA_GATEWAY},
- IN6ADDR_ANY_INIT,
- };
-
- if (gw)
- req.gw = *gw;
-
- if (add) {
- req.nh.nlmsg_type = RTM_NEWROUTE;
- req.nh.nlmsg_flags |= (NLM_F_CREATE | NLM_F_REPLACE);
- req.rtm.rtm_protocol = RTPROT_STATIC;
- req.rtm.rtm_scope = (gw) ? RT_SCOPE_UNIVERSE : RT_SCOPE_LINK;
- req.rtm.rtm_type = RTN_UNICAST;
- } else {
- req.nh.nlmsg_type = RTM_DELROUTE;
- req.rtm.rtm_scope = RT_SCOPE_NOWHERE;
- }
-
- req.nh.nlmsg_len = (gw) ? sizeof(req) : offsetof(struct req, rta_gw);
- send(rtnl_event.uloop.fd, &req, req.nh.nlmsg_len, MSG_DONTWAIT);
-}
-
// Use rtnetlink to modify kernel routes
static void setup_route(struct in6_addr *addr, struct interface *iface, bool add)
{
@@ -318,7 +270,59 @@ static void setup_route(struct in6_addr *addr, struct interface *iface, bool add
(add) ? "Learned" : "Forgot", namebuf, iface->ifname);
if (iface->learn_routes)
- odhcpd_setup_route(addr, 128, iface, NULL, add);
+ odhcpd_setup_route(addr, 128, iface, NULL, 1024, add);
+}
+
+// compare prefixes
+static int prefixcmp(const void *va, const void *vb)
+{
+ const struct odhcpd_ipaddr *a = va, *b = vb;
+ uint32_t a_pref = ((a->addr.s6_addr[0] & 0xfe) != 0xfc) ? a->preferred : 1;
+ uint32_t b_pref = ((b->addr.s6_addr[0] & 0xfe) != 0xfc) ? b->preferred : 1;
+ return (a_pref < b_pref) ? 1 : (a_pref > b_pref) ? -1 : 0;
+}
+
+// Check address update
+static void check_updates(struct interface *iface)
+{
+ struct odhcpd_ipaddr addr[8] = {{IN6ADDR_ANY_INIT, 0, 0, 0, 0}};
+ time_t now = odhcpd_time();
+ ssize_t len = odhcpd_get_interface_addresses(iface->ifindex, addr, 8);
+
+ if (len < 0)
+ return;
+
+ qsort(addr, len, sizeof(*addr), prefixcmp);
+
+ for (int i = 0; i < len; ++i) {
+ addr[i].addr.s6_addr32[3] = 0;
+
+ if (addr[i].preferred < UINT32_MAX - now)
+ addr[i].preferred += now;
+
+ if (addr[i].valid < UINT32_MAX - now)
+ addr[i].valid += now;
+ }
+
+ bool change = len != (ssize_t)iface->ia_addr_len;
+ for (ssize_t i = 0; !change && i < len; ++i)
+ if (!IN6_ARE_ADDR_EQUAL(&addr[i].addr, &iface->ia_addr[i].addr) ||
+ (addr[i].preferred > 0) != (iface->ia_addr[i].preferred > 0) ||
+ addr[i].valid < iface->ia_addr[i].valid ||
+ addr[i].preferred < iface->ia_addr[i].preferred)
+ change = true;
+
+ if (change)
+ dhcpv6_ia_preupdate(iface);
+
+ memcpy(iface->ia_addr, addr, len * sizeof(*addr));
+ iface->ia_addr_len = len;
+
+ if (change)
+ dhcpv6_ia_postupdate(iface, now);
+
+ if (change)
+ raise(SIGUSR1);
}
@@ -332,43 +336,46 @@ static void handle_rtnetlink(_unused void *addr, void *data, size_t len,
for (struct nlmsghdr *nh = data; NLMSG_OK(nh, len);
nh = NLMSG_NEXT(nh, len)) {
+ struct ndmsg *ndm = NLMSG_DATA(nh);
struct rtmsg *rtm = NLMSG_DATA(nh);
- if ((nh->nlmsg_type == RTM_NEWROUTE ||
- nh->nlmsg_type == RTM_DELROUTE) &&
- rtm->rtm_dst_len == 0)
- raise(SIGUSR1); // Inform about a change in default route
- struct ndmsg *ndm = NLMSG_DATA(nh);
- struct ifaddrmsg *ifa = NLMSG_DATA(nh);
- if (nh->nlmsg_type != RTM_NEWNEIGH
- && nh->nlmsg_type != RTM_DELNEIGH
- && nh->nlmsg_type != RTM_NEWADDR
- && nh->nlmsg_type != RTM_DELADDR)
- continue; // Unrelated message type
bool is_addr = (nh->nlmsg_type == RTM_NEWADDR
|| nh->nlmsg_type == RTM_DELADDR);
+ bool is_route = (nh->nlmsg_type == RTM_NEWROUTE
+ || nh->nlmsg_type == RTM_DELROUTE);
+ bool is_neigh = (nh->nlmsg_type == RTM_NEWNEIGH
+ || nh->nlmsg_type == RTM_DELNEIGH);
// Family and ifindex are on the same offset for NEIGH and ADDR
- if (NLMSG_PAYLOAD(nh, 0) < sizeof(*ndm)
+ if ((!is_addr && !is_route && !is_neigh)
+ || NLMSG_PAYLOAD(nh, 0) < sizeof(*ndm)
|| ndm->ndm_family != AF_INET6)
- continue; //
+ continue;
- // Lookup interface
- struct interface *iface;
- if (!(iface = odhcpd_get_interface_by_index(ndm->ndm_ifindex)))
+ // Inform about a change in default route
+ if (is_route && rtm->rtm_dst_len == 0)
+ raise(SIGUSR1);
+ else if (is_route)
continue;
// Data to retrieve
- size_t rta_offset = (is_addr) ? sizeof(*ifa) : sizeof(*ndm);
+ size_t rta_offset = (is_addr) ? sizeof(struct ifaddrmsg) : sizeof(*ndm);
uint16_t atype = (is_addr) ? IFA_ADDRESS : NDA_DST;
ssize_t alen = NLMSG_PAYLOAD(nh, rta_offset);
struct in6_addr *addr = NULL;
for (struct rtattr *rta = (void*)(((uint8_t*)ndm) + rta_offset);
- RTA_OK(rta, alen); rta = RTA_NEXT(rta, alen))
+ RTA_OK(rta, alen); rta = RTA_NEXT(rta, alen)) {
if (rta->rta_type == atype &&
- RTA_PAYLOAD(rta) >= sizeof(*addr))
+ RTA_PAYLOAD(rta) >= sizeof(*addr)) {
addr = RTA_DATA(rta);
+ }
+ }
+
+ // Lookup interface
+ struct interface *iface = odhcpd_get_interface_by_index(ndm->ndm_ifindex);
+ if (!iface)
+ continue;
// Address not specified or unrelated
if (!addr || IN6_IS_ADDR_LINKLOCAL(addr) ||
@@ -458,8 +465,7 @@ static void handle_rtnetlink(_unused void *addr, void *data, size_t len,
}
if (is_addr) {
- if (iface->ra == RELAYD_SERVER)
- raise(SIGUSR1); // Inform about a change in addresses
+ check_updates(iface);
if (iface->dhcpv6 == RELAYD_SERVER)
iface->ia_reconf = true;
@@ -474,7 +480,7 @@ static void handle_rtnetlink(_unused void *addr, void *data, size_t len,
struct interface *c;
list_for_each_entry(c, &interfaces, head) {
if (c->ndp == RELAYD_RELAY && !c->master) {
- ifa->ifa_index = c->ifindex;
+ ndm->ndm_ifindex = c->ifindex;
send(rtnl_event.uloop.fd, nh, nh->nlmsg_len, MSG_DONTWAIT);
}
}
diff --git a/src/odhcpd.c b/src/odhcpd.c
index aad5b37..5774a4f 100644
--- a/src/odhcpd.c
+++ b/src/odhcpd.c
@@ -174,12 +174,6 @@ ssize_t odhcpd_send(int socket, struct sockaddr_in6 *dest,
|| IN6_IS_ADDR_MC_LINKLOCAL(&dest->sin6_addr))
dest->sin6_scope_id = iface->ifindex;
- // IPV6_PKTINFO doesn't really work for IPv6-raw sockets (bug?)
- if (dest->sin6_port == 0) {
- msg.msg_control = NULL;
- msg.msg_controllen = 0;
- }
-
char ipbuf[INET6_ADDRSTRLEN];
inet_ntop(AF_INET6, &dest->sin6_addr, ipbuf, sizeof(ipbuf));
@@ -280,6 +274,56 @@ int odhcpd_get_preferred_interface_address(int ifindex, struct in6_addr *addr)
return ret;
}
+void odhcpd_setup_route(const struct in6_addr *addr, int prefixlen,
+ const struct interface *iface, const struct in6_addr *gw,
+ int metric, bool add)
+{
+ struct req {
+ struct nlmsghdr nh;
+ struct rtmsg rtm;
+ struct rtattr rta_dst;
+ struct in6_addr dst_addr;
+ struct rtattr rta_oif;
+ uint32_t ifindex;
+ struct rtattr rta_table;
+ uint32_t table;
+ struct rtattr rta_prio;
+ uint32_t prio;
+ struct rtattr rta_gw;
+ struct in6_addr gw;
+ } req = {
+ {sizeof(req), 0, NLM_F_REQUEST, ++rtnl_seq, 0},
+ {AF_INET6, prefixlen, 0, 0, 0, 0, 0, 0, 0},
+ {sizeof(struct rtattr) + sizeof(struct in6_addr), RTA_DST},
+ *addr,
+ {sizeof(struct rtattr) + sizeof(uint32_t), RTA_OIF},
+ iface->ifindex,
+ {sizeof(struct rtattr) + sizeof(uint32_t), RTA_TABLE},
+ RT_TABLE_MAIN,
+ {sizeof(struct rtattr) + sizeof(uint32_t), RTA_PRIORITY},
+ metric,
+ {sizeof(struct rtattr) + sizeof(struct in6_addr), RTA_GATEWAY},
+ IN6ADDR_ANY_INIT,
+ };
+
+ if (gw)
+ req.gw = *gw;
+
+ if (add) {
+ req.nh.nlmsg_type = RTM_NEWROUTE;
+ req.nh.nlmsg_flags |= (NLM_F_CREATE | NLM_F_REPLACE);
+ req.rtm.rtm_protocol = RTPROT_STATIC;
+ req.rtm.rtm_scope = (gw) ? RT_SCOPE_UNIVERSE : RT_SCOPE_LINK;
+ req.rtm.rtm_type = RTN_UNICAST;
+ } else {
+ req.nh.nlmsg_type = RTM_DELROUTE;
+ req.rtm.rtm_scope = RT_SCOPE_NOWHERE;
+ }
+
+ req.nh.nlmsg_len = (gw) ? sizeof(req) : offsetof(struct req, rta_gw);
+ send(rtnl_socket, &req, req.nh.nlmsg_len, MSG_DONTWAIT);
+}
+
struct interface* odhcpd_get_interface_by_index(int ifindex)
{
struct interface *iface;
diff --git a/src/odhcpd.h b/src/odhcpd.h
index d3a5ad6..759fb04 100644
--- a/src/odhcpd.h
+++ b/src/odhcpd.h
@@ -143,6 +143,7 @@ struct interface {
int default_router;
int managed;
int route_preference;
+ int ra_maxinterval;
// DHCPv4
struct in_addr dhcpv4_addr;
@@ -194,7 +195,8 @@ struct interface* odhcpd_get_interface_by_index(int ifindex);
struct interface* odhcpd_get_master_interface(void);
int odhcpd_urandom(void *data, size_t len);
void odhcpd_setup_route(const struct in6_addr *addr, int prefixlen,
- const struct interface *iface, const struct in6_addr *gw, bool add);
+ const struct interface *iface, const struct in6_addr *gw,
+ int metric, bool add);
void odhcpd_run(void);
time_t odhcpd_time(void);
diff --git a/src/router.c b/src/router.c
index f942d8f..36f6ae7 100644
--- a/src/router.c
+++ b/src/router.c
@@ -37,6 +37,7 @@ static void sigusr1_refresh(int signal);
static struct odhcpd_event router_event = {{.fd = -1}, handle_icmpv6};
static FILE *fp_route = NULL;
+#define RA_IOV_LEN 6
int init_router(void)
@@ -113,7 +114,7 @@ int setup_router_interface(struct interface *iface, bool enable)
forward_router_solicitation(iface);
} else if (iface->ra == RELAYD_SERVER && !iface->master) {
iface->timer_rs.cb = trigger_router_advert;
- trigger_router_advert(&iface->timer_rs);
+ uloop_timeout_set(&iface->timer_rs, 1000);
}
if (iface->ra == RELAYD_RELAY || (iface->ra == RELAYD_SERVER && !iface->master))
@@ -205,10 +206,10 @@ static bool parse_routes(struct odhcpd_ipaddr *n, ssize_t len)
return found_default;
}
-
// Router Advert server mode
static uint64_t send_router_advert(struct interface *iface, const struct in6_addr *from)
{
+ time_t now = odhcpd_time();
int mtu = odhcpd_get_interface_config(iface->ifname, "mtu");
int hlim = odhcpd_get_interface_config(iface->ifname, "hop_limit");
@@ -219,7 +220,7 @@ static uint64_t send_router_advert(struct interface *iface, const struct in6_add
struct nd_router_advert h;
struct icmpv6_opt lladdr;
struct nd_opt_mtu mtu;
- struct nd_opt_prefix_info prefix[RELAYD_MAX_PREFIXES];
+ struct nd_opt_prefix_info prefix[sizeof(iface->ia_addr) / sizeof(*iface->ia_addr)];
} adv = {
.h = {{.icmp6_type = ND_ROUTER_ADVERT, .icmp6_code = 0}, 0, 0},
.lladdr = {ND_OPT_SOURCE_LINKADDR, 1, {0}},
@@ -242,23 +243,24 @@ static uint64_t send_router_advert(struct interface *iface, const struct in6_add
odhcpd_get_mac(iface, adv.lladdr.data);
// If not currently shutting down
- struct odhcpd_ipaddr addrs[RELAYD_MAX_PREFIXES];
+ struct odhcpd_ipaddr *addrs = NULL;
ssize_t ipcnt = 0;
- uint64_t maxpreferred = 0;
+ int64_t minvalid = INT64_MAX;
+ int64_t maxvalid = 0;
// If not shutdown
if (iface->timer_rs.cb) {
- ipcnt = odhcpd_get_interface_addresses(iface->ifindex,
- addrs, ARRAY_SIZE(addrs));
+ addrs = iface->ia_addr;
+ ipcnt = iface->ia_addr_len;
// Check default route
- if (parse_routes(addrs, ipcnt) || iface->default_router > 1)
- adv.h.nd_ra_router_lifetime =
- htons(3 * MaxRtrAdvInterval);
+ if (parse_routes(addrs, ipcnt))
+ adv.h.nd_ra_router_lifetime = htons(1);
+ if (iface->default_router > 1)
+ adv.h.nd_ra_router_lifetime = htons(iface->default_router);
}
// Construct Prefix Information options
- bool have_public = false;
size_t cnt = 0;
struct in6_addr dns_pref = IN6ADDR_ANY_INIT, *dns_addr = &dns_pref;
@@ -267,15 +269,9 @@ static uint64_t send_router_advert(struct interface *iface, const struct in6_add
for (ssize_t i = 0; i < ipcnt; ++i) {
struct odhcpd_ipaddr *addr = &addrs[i];
- if (addr->prefix > 96)
+ if (addr->prefix > 96 || addr->valid <= now)
continue; // Address not suitable
- if (addr->preferred > MaxPreferredTime)
- addr->preferred = MaxPreferredTime;
-
- if (addr->valid > MaxValidTime)
- addr->valid = MaxValidTime;
-
struct nd_opt_prefix_info *p = NULL;
for (size_t i = 0; i < cnt; ++i) {
if (addr->prefix == adv.prefix[i].nd_opt_pi_prefix_len &&
@@ -291,12 +287,16 @@ static uint64_t send_router_advert(struct interface *iface, const struct in6_add
p = &adv.prefix[cnt++];
}
- if ((addr->addr.s6_addr[0] & 0xfe) != 0xfc && addr->preferred > 0) {
- have_public = true;
+ if (addr->preferred > now &&
+ minvalid > 1000LL * (addr->valid - now))
+ minvalid = 1000LL * (addr->valid - now);
- if (maxpreferred < 1000 * addr->preferred)
- maxpreferred = 1000 * addr->preferred;
- }
+ if (maxvalid < 1000LL * (addr->valid - now))
+ maxvalid = 1000LL * (addr->valid - now);
+
+ if (((addr->addr.s6_addr[0] & 0xfe) != 0xfc || iface->default_router)
+ && ntohs(adv.h.nd_ra_router_lifetime) < addr->valid - now)
+ adv.h.nd_ra_router_lifetime = htons(addr->valid - now);
odhcpd_bmemcpy(&p->nd_opt_pi_prefix, &addr->addr,
(iface->ra_advrouter) ? 128 : addr->prefix);
@@ -310,16 +310,18 @@ static uint64_t send_router_advert(struct interface *iface, const struct in6_add
p->nd_opt_pi_flags_reserved |= ND_OPT_PI_FLAG_AUTO;
if (iface->ra_advrouter)
p->nd_opt_pi_flags_reserved |= ND_OPT_PI_FLAG_RADDR;
- p->nd_opt_pi_valid_time = htonl(addr->valid);
- p->nd_opt_pi_preferred_time = htonl(addr->preferred);
+ p->nd_opt_pi_valid_time = htonl(addr->valid - now);
+ if (addr->preferred > now)
+ p->nd_opt_pi_preferred_time = htonl(addr->preferred - now);
- if (addr->preferred > dns_time) {
- dns_time = addr->preferred;
+
+ if (addr->preferred - now > dns_time) {
+ dns_time = addr->preferred - now;
dns_pref = addr->addr;
}
}
- if (!have_public && !iface->default_router && adv.h.nd_ra_router_lifetime) {
+ if (!iface->default_router && ntohs(adv.h.nd_ra_router_lifetime) == 1) {
syslog(LOG_WARNING, "A default route is present but there is no public prefix "
"on %s thus we don't announce a default route!", iface->ifname);
adv.h.nd_ra_router_lifetime = 0;
@@ -329,7 +331,7 @@ static uint64_t send_router_advert(struct interface *iface, const struct in6_add
if (iface->dns_cnt > 0) {
dns_addr = iface->dns;
dns_cnt = iface->dns_cnt;
- dns_time = 2 * MaxRtrAdvInterval;
+ dns_time = 0;
}
if (!dns_addr || IN6_IS_ADDR_UNSPECIFIED(dns_addr))
@@ -374,7 +376,6 @@ static uint64_t send_router_advert(struct interface *iface, const struct in6_add
search->len = search_len ? ((sizeof(*search) + search_padded) / 8) : 0;
search->pad = 0;
search->pad2 = 0;
- search->lifetime = htonl(2 * MaxRtrAdvInterval);;
memcpy(search->name, search_domain, search_len);
memset(&search->name[search_len], 0, search_padded - search_len);
@@ -391,7 +392,7 @@ static uint64_t send_router_advert(struct interface *iface, const struct in6_add
for (ssize_t i = 0; i < ipcnt; ++i) {
struct odhcpd_ipaddr *addr = &addrs[i];
- if (addr->dprefix > 64 || addr->dprefix == 0 ||
+ if (addr->dprefix > 64 || addr->dprefix == 0 || addr->valid <= now ||
(addr->dprefix == 64 && addr->prefix == 64)) {
continue; // Address not suitable
} else if (addr->dprefix > 32) {
@@ -409,7 +410,7 @@ static uint64_t send_router_advert(struct interface *iface, const struct in6_add
routes[routes_cnt].flags |= ND_RA_PREF_LOW;
else if (iface->route_preference > 0)
routes[routes_cnt].flags |= ND_RA_PREF_HIGH;
- routes[routes_cnt].lifetime = htonl(addr->valid);
+ routes[routes_cnt].lifetime = htonl(addr->valid - now);
routes[routes_cnt].addr[0] = addr->addr.s6_addr32[0];
routes[routes_cnt].addr[1] = addr->addr.s6_addr32[1];
routes[routes_cnt].addr[2] = 0;
@@ -420,20 +421,26 @@ static uint64_t send_router_advert(struct interface *iface, const struct in6_add
// Calculate periodic transmit
int msecs = 0;
- uint32_t maxival = MaxRtrAdvInterval * 1000;
- uint32_t minival = MinRtrAdvInterval * 1000;
+ uint32_t maxival = iface->ra_maxinterval * 1000;
+ uint32_t minival;
+
+ if (maxival < 4000 || maxival > MaxRtrAdvInterval * 1000)
+ maxival = MaxRtrAdvInterval * 1000;
+
+ if (maxival > minvalid / 3) {
+ maxival = minvalid / 3;
- if (maxpreferred > 0 && maxival > maxpreferred / 2) {
- maxival = maxpreferred / 2;
if (maxival < 4000)
maxival = 4000;
-
- if (maxival >= 9000)
- minival = maxival / 3;
- else
- minival = (maxival * 3) / 4;
}
+ minival = (maxival * 3) / 4;
+
+ search->lifetime = htonl(maxvalid / 1000);
+
+ if (!dns.lifetime)
+ dns.lifetime = search->lifetime;
+
odhcpd_urandom(&msecs, sizeof(msecs));
msecs = (labs(msecs) % (maxival - minival)) + minival;
@@ -443,7 +450,8 @@ static uint64_t send_router_advert(struct interface *iface, const struct in6_add
.data = {0, 0, maxival >> 24, maxival >> 16, maxival >> 8, maxival}
};
- struct iovec iov[] = {{&adv, (uint8_t*)&adv.prefix[cnt] - (uint8_t*)&adv},
+ struct iovec iov[RA_IOV_LEN] = {
+ {&adv, (uint8_t*)&adv.prefix[cnt] - (uint8_t*)&adv},
{&routes, routes_cnt * sizeof(*routes)},
{&dns, (dns_cnt) ? sizeof(dns) : 0},
{dns_addr, dns_cnt * sizeof(*dns_addr)},
diff --git a/src/router.h b/src/router.h
index 1e8649c..720490d 100644
--- a/src/router.h
+++ b/src/router.h
@@ -30,10 +30,8 @@ struct icmpv6_opt {
(void*)(opt + opt->len) <= (void*)(end); opt += opt->len)
-#define MaxRtrAdvInterval 600
-#define MinRtrAdvInterval (MaxRtrAdvInterval / 3)
-#define MaxValidTime 7200
-#define MaxPreferredTime (3 * MaxRtrAdvInterval)
+#define MaxValidTime 65535
+#define MaxRtrAdvInterval (MaxValidTime / 3)
#define ND_RA_FLAG_PROXY 0x4
#define ND_RA_PREF_HIGH (1 << 3)