treewide: reflect managed mode is related to RA
[project/odhcpd.git] / src / router.c
index 4ab06b6..cb2afc9 100644 (file)
@@ -38,7 +38,6 @@ static void sigusr1_refresh(int signal);
 static struct odhcpd_event router_event = {.uloop = {.fd = -1}, .handle_dgram = handle_icmpv6, };
 
 static FILE *fp_route = NULL;
-#define RA_IOV_LEN 6
 
 #define TIME_LEFT(t1, now) ((t1) != UINT32_MAX ? (t1) - (now) : UINT32_MAX)
 
@@ -182,7 +181,9 @@ static bool parse_routes(struct odhcpd_ipaddr *n, ssize_t len)
 
        char line[512], ifname[16];
        bool found_default = false;
-       struct odhcpd_ipaddr p = {IN6ADDR_ANY_INIT, 0, 0, 0, 0};
+       struct odhcpd_ipaddr p = { .addr.in6 = IN6ADDR_ANY_INIT, .prefix = 0,
+                                       .dprefix = 0, .preferred = 0, .valid = 0};
+
        while (fgets(line, sizeof(line), fp_route)) {
                uint32_t rflags;
                if (sscanf(line, "00000000000000000000000000000000 00 "
@@ -191,15 +192,15 @@ static bool parse_routes(struct odhcpd_ipaddr *n, ssize_t len)
                        found_default = true;
                } else if (sscanf(line, "%8" SCNx32 "%8" SCNx32 "%*8" SCNx32 "%*8" SCNx32 " %hhx %*s "
                                "%*s 00000000000000000000000000000000 %*s %*s %*s %" SCNx32 " lo",
-                               &p.addr.s6_addr32[0], &p.addr.s6_addr32[1], &p.prefix, &rflags) &&
+                               &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.s6_addr32[0] = htonl(p.addr.s6_addr32[0]);
-                       p.addr.s6_addr32[1] = htonl(p.addr.s6_addr32[1]);
+                       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, &n[i].addr, p.prefix)) {
+                                               !odhcpd_bmemcmp(&p.addr.in6, &n[i].addr.in6, p.prefix)) {
                                        n[i].dprefix = p.prefix;
                                        break;
                                }
@@ -233,7 +234,8 @@ static int calc_adv_interval(struct interface *iface, uint32_t minvalid,
                minival = (*maxival >= 9 ? *maxival/3 : *maxival);
 
        odhcpd_urandom(&msecs, sizeof(msecs));
-       msecs = (labs(msecs) % ((*maxival - minival)*1000)) + minival*1000;
+       msecs = (labs(msecs) % ((*maxival != minival) ? (*maxival - minival)*1000 : 500)) +
+                       minival*1000;
 
        return msecs;
 }
@@ -253,21 +255,37 @@ static uint16_t calc_ra_lifetime(struct interface *iface, uint32_t maxival)
        return lifetime;
 }
 
+enum {
+       IOV_RA_ADV=0,
+       IOV_RA_PFXS,
+       IOV_RA_ROUTES,
+       IOV_RA_DNS,
+       IOV_RA_DNS_ADDR,
+       IOV_RA_SEARCH,
+       IOV_RA_ADV_INTERVAL,
+       IOV_RA_TOTAL,
+};
+
 // 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");
+       int mtu = iface->ra_mtu;
+       int hlim = iface->ra_hoplimit;
+
+       if (mtu == 0)
+               mtu = odhcpd_get_interface_config(iface->ifname, "mtu");
 
        if (mtu < 1280)
                mtu = 1280;
 
+       if (hlim == 0)
+               hlim = odhcpd_get_interface_config(iface->ifname, "hop_limit");
+
        struct {
                struct nd_router_advert h;
                struct icmpv6_opt lladdr;
                struct nd_opt_mtu mtu;
-               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}},
@@ -280,17 +298,21 @@ static uint64_t send_router_advert(struct interface *iface, const struct in6_add
        if (iface->dhcpv6)
                adv.h.nd_ra_flags_reserved = ND_RA_FLAG_OTHER;
 
-       if (iface->managed >= RELAYD_MANAGED_MFLAG)
+       if (iface->ra_managed >= RA_MANAGED_MFLAG)
                adv.h.nd_ra_flags_reserved |= ND_RA_FLAG_MANAGED;
 
        if (iface->route_preference < 0)
                adv.h.nd_ra_flags_reserved |= ND_RA_PREF_LOW;
        else if (iface->route_preference > 0)
                adv.h.nd_ra_flags_reserved |= ND_RA_PREF_HIGH;
+
+       adv.h.nd_ra_reachable = htonl(iface->ra_reachabletime);
+       adv.h.nd_ra_retransmit = htonl(iface->ra_retranstime);
+
        odhcpd_get_mac(iface, adv.lladdr.data);
 
        // If not currently shutting down
-       struct odhcpd_ipaddr addrs[RELAYD_MAX_ADDRS];
+       struct odhcpd_ipaddr *addrs = NULL;
        ssize_t ipcnt = 0;
        uint32_t minvalid = UINT32_MAX;
        bool default_route = false;
@@ -298,8 +320,11 @@ static uint64_t send_router_advert(struct interface *iface, const struct in6_add
 
        // If not shutdown
        if (iface->timer_rs.cb) {
+               size_t size = sizeof(*addrs) * iface->ia_addr_len;
+               addrs = alloca(size);
+               memcpy(addrs, iface->ia_addr, size);
+
                ipcnt = iface->ia_addr_len;
-               memcpy(addrs, iface->ia_addr, ipcnt * sizeof(*addrs));
 
                // Check default route
                if (iface->default_router) {
@@ -311,49 +336,71 @@ static uint64_t send_router_advert(struct interface *iface, const struct in6_add
                        default_route = true;
        }
 
-       // Construct Prefix Information options
-       size_t cnt = 0;
 
        struct in6_addr dns_pref, *dns_addr = &dns_pref;
        size_t dns_cnt = 1;
 
-       odhcpd_get_linklocal_interface_address(iface->ifindex, &dns_pref);
+       odhcpd_get_interface_dns_addr(iface, &dns_pref);
+
+       // Construct Prefix Information options
+       size_t pfxs_cnt = 0;
+       struct nd_opt_prefix_info *pfxs = NULL;
 
        for (ssize_t i = 0; i < ipcnt; ++i) {
                struct odhcpd_ipaddr *addr = &addrs[i];
+               uint32_t preferred = 0;
+               uint32_t valid = 0;
 
                if (addr->prefix > 96 || addr->valid <= (uint32_t)now) {
                        char namebuf[INET6_ADDRSTRLEN];
 
-                       inet_ntop(AF_INET6, addr, namebuf, sizeof(namebuf));
+                       inet_ntop(AF_INET6, &addr->addr.in6, namebuf, sizeof(namebuf));
                        syslog(LOG_INFO, "Address %s (prefix %d, valid %u) not suitable as RA prefix on %s",
                                        namebuf, addr->prefix, addr->valid, iface->ifname);
                        continue;
                }
 
                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 &&
-                                       !odhcpd_bmemcmp(&adv.prefix[i].nd_opt_pi_prefix,
-                                       &addr->addr, addr->prefix))
-                               p = &adv.prefix[i];
+               for (size_t i = 0; i < pfxs_cnt; ++i) {
+                       if (addr->prefix == pfxs[i].nd_opt_pi_prefix_len &&
+                                       !odhcpd_bmemcmp(&pfxs[i].nd_opt_pi_prefix,
+                                       &addr->addr.in6, addr->prefix))
+                               p = &pfxs[i];
                }
 
                if (!p) {
-                       if (cnt >= ARRAY_SIZE(adv.prefix))
-                               break;
+                       struct nd_opt_prefix_info *tmp;
+
+                       tmp = realloc(pfxs, sizeof(*pfxs) * (pfxs_cnt + 1));
+                       if (!tmp) {
+                               syslog(LOG_ERR, "Realloc failed for RA prefix option on interface %s", iface->ifname);
+                               continue;
+                       }
 
-                       p = &adv.prefix[cnt++];
+                       pfxs = tmp;
+                       p = &pfxs[pfxs_cnt++];
+                       memset(p, 0, sizeof(*p));
                }
 
-               if (addr->preferred > (uint32_t)now &&
-                               minvalid > TIME_LEFT(addr->valid, now))
-                       minvalid = TIME_LEFT(addr->valid, now);
+               if (addr->preferred > (uint32_t)now) {
+                       preferred = TIME_LEFT(addr->preferred, now);
 
-               if (!IN6_IS_ADDR_ULA(&addr->addr) || iface->default_router)
+                       if (iface->ra_useleasetime &&
+                                       preferred > iface->dhcpv4_leasetime)
+                               preferred = iface->dhcpv4_leasetime;
+               }
+
+               valid = TIME_LEFT(addr->valid, now);
+               if (iface->ra_useleasetime && valid > iface->dhcpv4_leasetime)
+                       valid = iface->dhcpv4_leasetime;
+
+               if (minvalid > valid)
+                       minvalid = valid;
+
+               if (!IN6_IS_ADDR_ULA(&addr->addr.in6) || iface->default_router)
                        valid_prefix = true;
 
-               odhcpd_bmemcpy(&p->nd_opt_pi_prefix, &addr->addr,
+               odhcpd_bmemcpy(&p->nd_opt_pi_prefix, &addr->addr.in6,
                                (iface->ra_advrouter) ? 128 : addr->prefix);
                p->nd_opt_pi_type = ND_OPT_PREFIX_INFORMATION;
                p->nd_opt_pi_len = 4;
@@ -361,15 +408,12 @@ static uint64_t send_router_advert(struct interface *iface, const struct in6_add
                p->nd_opt_pi_flags_reserved = 0;
                if (!iface->ra_not_onlink)
                        p->nd_opt_pi_flags_reserved |= ND_OPT_PI_FLAG_ONLINK;
-               if (iface->managed < RELAYD_MANAGED_NO_AFLAG && addr->prefix <= 64)
+               if (iface->ra_managed < RA_MANAGED_NO_AFLAG && addr->prefix <= 64)
                        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(TIME_LEFT(addr->valid, now));
-               if (addr->preferred > (uint32_t)now)
-                       p->nd_opt_pi_preferred_time = htonl(TIME_LEFT(addr->preferred, now));
-               else if (addr->valid - now < 7200)
-                       p->nd_opt_pi_valid_time = 0;
+               p->nd_opt_pi_preferred_time = htonl(preferred);
+               p->nd_opt_pi_valid_time = htonl(valid);
        }
 
        // Calculate periodic transmit
@@ -406,8 +450,6 @@ static uint64_t send_router_advert(struct interface *iface, const struct in6_add
                uint32_t lifetime;
        } dns = {ND_OPT_RECURSIVE_DNS, (1 + (2 * dns_cnt)), 0, 0, 0};
 
-
-
        // DNS Search options
        uint8_t search_buf[256], *search_domain = iface->search;
        size_t search_len = iface->search_len, search_padded = 0;
@@ -449,7 +491,7 @@ static uint64_t send_router_advert(struct interface *iface, const struct in6_add
                uint8_t flags;
                uint32_t lifetime;
                uint32_t addr[4];
-       } routes[RELAYD_MAX_PREFIXES];
+       } *tmp, *routes = NULL;
 
        for (ssize_t i = 0; i < ipcnt; ++i) {
                struct odhcpd_ipaddr *addr = &addrs[i];
@@ -457,12 +499,21 @@ static uint64_t send_router_advert(struct interface *iface, const struct in6_add
                                (addr->dprefix == 64 && addr->prefix == 64)) {
                        continue; // Address not suitable
                } else if (addr->dprefix > 32) {
-                       addr->addr.s6_addr32[1] &= htonl(~((1U << (64 - addr->dprefix)) - 1));
+                       addr->addr.in6.s6_addr32[1] &= htonl(~((1U << (64 - addr->dprefix)) - 1));
                } else if (addr->dprefix <= 32) {
-                       addr->addr.s6_addr32[0] &= htonl(~((1U << (32 - addr->dprefix)) - 1));
-                       addr->addr.s6_addr32[1] = 0;
+                       addr->addr.in6.s6_addr32[0] &= htonl(~((1U << (32 - addr->dprefix)) - 1));
+                       addr->addr.in6.s6_addr32[1] = 0;
                }
 
+               tmp = realloc(routes, sizeof(*routes) * (routes_cnt + 1));
+               if (!tmp) {
+                       syslog(LOG_ERR, "Realloc failed for RA route option on interface %s", iface->ifname);
+                       continue;
+               }
+
+               routes = tmp;
+
+               memset(&routes[routes_cnt], 0, sizeof(*routes));
                routes[routes_cnt].type = ND_OPT_ROUTE_INFO;
                routes[routes_cnt].len = sizeof(*routes) / 8;
                routes[routes_cnt].prefix = addr->dprefix;
@@ -472,8 +523,8 @@ static uint64_t send_router_advert(struct interface *iface, const struct in6_add
                else if (iface->route_preference > 0)
                        routes[routes_cnt].flags |= ND_RA_PREF_HIGH;
                routes[routes_cnt].lifetime = htonl(TIME_LEFT(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[0] = addr->addr.in6.s6_addr32[0];
+               routes[routes_cnt].addr[1] = addr->addr.in6.s6_addr32[1];
                routes[routes_cnt].addr[2] = 0;
                routes[routes_cnt].addr[3] = 0;
 
@@ -489,13 +540,14 @@ static uint64_t send_router_advert(struct interface *iface, const struct in6_add
                .data = {0, 0, (maxival*1000) >> 24, (maxival*1000) >> 16, (maxival*1000) >> 8, maxival*1000}
        };
 
-       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)},
-                       {search, search->len * 8},
-                       {&adv_interval, adv_interval.len * 8}};
+       struct iovec iov[IOV_RA_TOTAL] = {
+                       [IOV_RA_ADV] = {&adv, sizeof(adv)},
+                       [IOV_RA_PFXS] = {pfxs, pfxs_cnt * sizeof(*pfxs)},
+                       [IOV_RA_ROUTES] = {routes, routes_cnt * sizeof(*routes)},
+                       [IOV_RA_DNS] = {&dns, (dns_cnt) ? sizeof(dns) : 0},
+                       [IOV_RA_DNS_ADDR] = {dns_addr, dns_cnt * sizeof(*dns_addr)},
+                       [IOV_RA_SEARCH] = {search, search->len * 8},
+                       [IOV_RA_ADV_INTERVAL] = {&adv_interval, adv_interval.len * 8}};
        struct sockaddr_in6 dest = {AF_INET6, 0, 0, ALL_IPV6_NODES, 0};
 
        if (from && !IN6_IS_ADDR_UNSPECIFIED(from))
@@ -504,6 +556,9 @@ static uint64_t send_router_advert(struct interface *iface, const struct in6_add
        odhcpd_send(router_event.uloop.fd,
                        &dest, iov, ARRAY_SIZE(iov), iface);
 
+       free(pfxs);
+       free(routes);
+
        return msecs;
 }
 
@@ -605,7 +660,7 @@ static void forward_router_advertisement(uint8_t *data, size_t len)
                        size_t rewrite_cnt = iface->dns_cnt;
 
                        if (rewrite_cnt == 0) {
-                               if (odhcpd_get_linklocal_interface_address(iface->ifindex, &addr))
+                               if (odhcpd_get_interface_dns_addr(iface, &addr))
                                        continue; // Unable to comply
 
                                rewrite = &addr;