router: add syslog debug tracing for trouble shooting
[project/odhcpd.git] / src / ndp.c
index d1683b9..d2fbe07 100644 (file)
--- 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,10 +38,11 @@ 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 void catch_rtnetlink(int error);
 
 static uint32_t rtnl_seqid = 0;
 static int ping_socket = -1;
-static struct odhcpd_event rtnl_event = {{.fd = -1}, handle_rtnetlink};
+static struct odhcpd_event rtnl_event = {{.fd = -1}, handle_rtnetlink, catch_rtnetlink};
 
 
 // Filter ICMPv6 messages of type neighbor soliciation
@@ -59,10 +61,15 @@ static const struct sock_fprog bpf_prog = {sizeof(bpf) / sizeof(*bpf), bpf};
 // Initialize NDP-proxy
 int init_ndp(void)
 {
+       int val = 256 * 1024;
+
        // Setup netlink socket
        if ((rtnl_event.uloop.fd = odhcpd_open_rtnl()) < 0)
                return -1;
 
+       if (setsockopt(rtnl_event.uloop.fd, SOL_SOCKET, SO_RCVBUF, &val, sizeof(val)))
+               setsockopt(rtnl_event.uloop.fd, SOL_SOCKET, SO_RCVBUFFORCE, &val, sizeof(val));
+
        // Receive netlink neighbor and ip-address events
        uint32_t group = RTNLGRP_IPV6_IFADDR;
        setsockopt(rtnl_event.uloop.fd, SOL_NETLINK,
@@ -80,7 +87,7 @@ int init_ndp(void)
                        return -1;
        }
 
-       int val = 2;
+       val = 2;
        setsockopt(ping_socket, IPPROTO_RAW, IPV6_CHECKSUM, &val, sizeof(val));
 
        // This is required by RFC 4861
@@ -272,6 +279,60 @@ static void setup_route(struct in6_addr *addr, struct interface *iface, bool 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[RELAYD_MAX_ADDRS] = {{IN6ADDR_ANY_INIT, 0, 0, 0, 0}};
+       time_t now = odhcpd_time();
+       ssize_t len = odhcpd_get_interface_addresses(iface->ifindex, addr, ARRAY_SIZE(addr));
+
+       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) {
+               syslog(LOG_DEBUG, "Raising SIGUSR1 due to address change");
+               raise(SIGUSR1);
+       }
+}
+
 
 // Handler for neighbor cache entries from the kernel. This is our source
 // to learn and unlearn hosts on interfaces.
@@ -299,11 +360,15 @@ static void handle_rtnetlink(_unused void *addr, void *data, size_t len,
                                || ndm->ndm_family != AF_INET6)
                        continue;
 
-               // Inform about a change in default route
-               if (is_route && rtm->rtm_dst_len == 0)
-                       raise(SIGUSR1);
-               else if (is_route)
+               if (is_route) {
+                       // Inform about a change in default route
+                       if (rtm->rtm_dst_len == 0) {
+                               syslog(LOG_DEBUG, "Raising SIGUSR1 due to default route change");
+                               raise(SIGUSR1);
+                       }
+
                        continue;
+               }
 
                // Data to retrieve
                size_t rta_offset = (is_addr) ? sizeof(struct ifaddrmsg) : sizeof(*ndm);
@@ -412,14 +477,44 @@ 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;
+
+                       if (iface->ndp == RELAYD_RELAY && iface->master) {
+                               // Replay address changes on all slave interfaces
+                               nh->nlmsg_flags = NLM_F_REQUEST;
+
+                               if (nh->nlmsg_type == RTM_NEWADDR)
+                                       nh->nlmsg_flags |= NLM_F_CREATE | NLM_F_REPLACE;
+
+                               struct interface *c;
+                               list_for_each_entry(c, &interfaces, head) {
+                                       if (c->ndp == RELAYD_RELAY && !c->master) {
+                                               ndm->ndm_ifindex = c->ifindex;
+                                               send(rtnl_event.uloop.fd, nh, nh->nlmsg_len, MSG_DONTWAIT);
+                                       }
+                               }
+                       }
                }
        }
 
        if (dump_neigh)
                dump_neigh_table(false);
 }
+
+static void catch_rtnetlink(int error)
+{
+       if (error == ENOBUFS) {
+               struct {
+                       struct nlmsghdr nh;
+                       struct ifaddrmsg ifa;
+               } req2 = {
+                       {sizeof(req2), RTM_GETADDR, NLM_F_REQUEST | NLM_F_DUMP,
+                                       ++rtnl_seqid, 0},
+                       {.ifa_family = AF_INET6}
+               };
+               send(rtnl_event.uloop.fd, &req2, sizeof(req2), MSG_DONTWAIT);
+       }
+}