dhcpv4: add in_addr dhcpv4_mask and in_addr dhcpv4_addr to struct interface
[project/odhcpd.git] / src / router.c
index 77972ab..f942d8f 100644 (file)
@@ -176,7 +176,7 @@ static bool parse_routes(struct odhcpd_ipaddr *n, ssize_t len)
 
        char line[512], ifname[16];
        bool found_default = false;
 
        char line[512], ifname[16];
        bool found_default = false;
-       struct odhcpd_ipaddr p = {IN6ADDR_ANY_INIT, 0, 0, false, 0, 0, 0};
+       struct odhcpd_ipaddr p = {IN6ADDR_ANY_INIT, 0, 0, 0, 0};
        while (fgets(line, sizeof(line), fp_route)) {
                uint32_t rflags;
                if (sscanf(line, "00000000000000000000000000000000 00 "
        while (fgets(line, sizeof(line), fp_route)) {
                uint32_t rflags;
                if (sscanf(line, "00000000000000000000000000000000 00 "
@@ -209,9 +209,11 @@ static bool parse_routes(struct odhcpd_ipaddr *n, ssize_t len)
 // Router Advert server mode
 static uint64_t send_router_advert(struct interface *iface, const struct in6_addr *from)
 {
 // Router Advert server mode
 static uint64_t send_router_advert(struct interface *iface, const struct in6_addr *from)
 {
-       int mtu = odhcpd_get_interface_mtu(iface->ifname);
-       if (mtu < 0)
-               mtu = 1500;
+       int mtu = odhcpd_get_interface_config(iface->ifname, "mtu");
+       int hlim = odhcpd_get_interface_config(iface->ifname, "hop_limit");
+
+       if (mtu < 1280)
+               mtu = 1280;
 
        struct {
                struct nd_router_advert h;
 
        struct {
                struct nd_router_advert h;
@@ -224,6 +226,9 @@ static uint64_t send_router_advert(struct interface *iface, const struct in6_add
                .mtu = {ND_OPT_MTU, 1, 0, htonl(mtu)},
        };
 
                .mtu = {ND_OPT_MTU, 1, 0, htonl(mtu)},
        };
 
+       if (hlim > 0)
+               adv.h.nd_ra_curhoplimit = hlim;
+
        if (iface->dhcpv6)
                adv.h.nd_ra_flags_reserved = ND_RA_FLAG_OTHER;
 
        if (iface->dhcpv6)
                adv.h.nd_ra_flags_reserved = ND_RA_FLAG_OTHER;
 
@@ -262,7 +267,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];
 
        for (ssize_t i = 0; i < ipcnt; ++i) {
                struct odhcpd_ipaddr *addr = &addrs[i];
-               if (addr->prefix > 96 || addr->has_class)
+               if (addr->prefix > 96)
                        continue; // Address not suitable
 
                if (addr->preferred > MaxPreferredTime)
                        continue; // Address not suitable
 
                if (addr->preferred > MaxPreferredTime)
@@ -293,7 +298,8 @@ static uint64_t send_router_advert(struct interface *iface, const struct in6_add
                                maxpreferred = 1000 * addr->preferred;
                }
 
                                maxpreferred = 1000 * addr->preferred;
                }
 
-               odhcpd_bmemcpy(&p->nd_opt_pi_prefix, &addr->addr, addr->prefix);
+               odhcpd_bmemcpy(&p->nd_opt_pi_prefix, &addr->addr,
+                               (iface->ra_advrouter) ? 128 : addr->prefix);
                p->nd_opt_pi_type = ND_OPT_PREFIX_INFORMATION;
                p->nd_opt_pi_len = 4;
                p->nd_opt_pi_prefix_len = (addr->prefix < 64) ? 64 : addr->prefix;
                p->nd_opt_pi_type = ND_OPT_PREFIX_INFORMATION;
                p->nd_opt_pi_len = 4;
                p->nd_opt_pi_prefix_len = (addr->prefix < 64) ? 64 : addr->prefix;
@@ -302,6 +308,8 @@ static uint64_t send_router_advert(struct interface *iface, const struct in6_add
                        p->nd_opt_pi_flags_reserved |= ND_OPT_PI_FLAG_ONLINK;
                if (iface->managed < RELAYD_MANAGED_NO_AFLAG && addr->prefix <= 64)
                        p->nd_opt_pi_flags_reserved |= ND_OPT_PI_FLAG_AUTO;
                        p->nd_opt_pi_flags_reserved |= ND_OPT_PI_FLAG_ONLINK;
                if (iface->managed < RELAYD_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(addr->valid);
                p->nd_opt_pi_preferred_time = htonl(addr->preferred);
 
                p->nd_opt_pi_valid_time = htonl(addr->valid);
                p->nd_opt_pi_preferred_time = htonl(addr->preferred);
 
@@ -410,12 +418,37 @@ static uint64_t send_router_advert(struct interface *iface, const struct in6_add
                ++routes_cnt;
        }
 
                ++routes_cnt;
        }
 
+       // Calculate periodic transmit
+       int msecs = 0;
+       uint32_t maxival = MaxRtrAdvInterval * 1000;
+       uint32_t minival = MinRtrAdvInterval * 1000;
+
+       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;
+       }
+
+       odhcpd_urandom(&msecs, sizeof(msecs));
+       msecs = (labs(msecs) % (maxival - minival)) + minival;
+
+       struct icmpv6_opt adv_interval = {
+               .type = ND_OPT_RTR_ADV_INTERVAL,
+               .len = 1,
+               .data = {0, 0, maxival >> 24, maxival >> 16, maxival >> 8, maxival}
+       };
 
        struct iovec iov[] = {{&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)},
 
        struct iovec iov[] = {{&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}};
+                       {search, search->len * 8},
+                       {&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))
        struct sockaddr_in6 dest = {AF_INET6, 0, 0, ALL_IPV6_NODES, 0};
 
        if (from && !IN6_IS_ADDR_UNSPECIFIED(from))
@@ -424,36 +457,18 @@ 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);
 
        odhcpd_send(router_event.uloop.fd,
                        &dest, iov, ARRAY_SIZE(iov), iface);
 
-       return maxpreferred;
+       return msecs;
 }
 
 
 static void trigger_router_advert(struct uloop_timeout *event)
 {
        struct interface *iface = container_of(event, struct interface, timer_rs);
 }
 
 
 static void trigger_router_advert(struct uloop_timeout *event)
 {
        struct interface *iface = container_of(event, struct interface, timer_rs);
-       uint64_t maxpreferred = send_router_advert(iface, NULL);
+       int msecs = send_router_advert(iface, NULL);
 
        // Rearm timer if not shut down
 
        // Rearm timer if not shut down
-       if (event->cb) {
-               uint32_t maxinterval = MaxRtrAdvInterval * 1000;
-               uint32_t mininterval = MinRtrAdvInterval * 1000;
-
-               if (maxpreferred > 0 && maxinterval > maxpreferred / 2) {
-                       maxinterval = maxpreferred / 2;
-                       if (maxinterval < 4000)
-                               maxinterval = 4000;
-
-                       if (maxinterval >= 9000)
-                               mininterval = maxinterval / 3;
-                       else
-                               mininterval = (maxinterval * 3) / 4;
-               }
-
-               int msecs;
-               odhcpd_urandom(&msecs, sizeof(msecs));
-               msecs = (labs(msecs) % (maxinterval - mininterval)) + mininterval;
+       if (event->cb)
                uloop_timeout_set(event, msecs);
                uloop_timeout_set(event, msecs);
-       }
 }
 
 
 }
 
 
@@ -527,7 +542,6 @@ static void forward_router_advertisement(uint8_t *data, size_t len)
        struct sockaddr_in6 all_nodes = {AF_INET6, 0, 0, ALL_IPV6_NODES, 0};
        struct iovec iov = {data, len};
 
        struct sockaddr_in6 all_nodes = {AF_INET6, 0, 0, ALL_IPV6_NODES, 0};
        struct iovec iov = {data, len};
 
-       struct odhcpd_ipaddr addr;
        struct interface *iface;
        list_for_each_entry(iface, &interfaces, head) {
                if (iface->ra != RELAYD_RELAY || iface->master)
        struct interface *iface;
        list_for_each_entry(iface, &interfaces, head) {
                if (iface->ra != RELAYD_RELAY || iface->master)
@@ -540,13 +554,14 @@ static void forward_router_advertisement(uint8_t *data, size_t len)
                // If we have to rewrite DNS entries
                if (iface->always_rewrite_dns && dns_ptr && dns_count > 0) {
                        const struct in6_addr *rewrite = iface->dns;
                // If we have to rewrite DNS entries
                if (iface->always_rewrite_dns && dns_ptr && dns_count > 0) {
                        const struct in6_addr *rewrite = iface->dns;
+                       struct in6_addr addr;
                        size_t rewrite_cnt = iface->dns_cnt;
 
                        if (rewrite_cnt == 0) {
                        size_t rewrite_cnt = iface->dns_cnt;
 
                        if (rewrite_cnt == 0) {
-                               if (odhcpd_get_interface_addresses(iface->ifindex, &addr, 1) < 1)
+                               if (odhcpd_get_preferred_interface_address(iface->ifindex, &addr) < 1)
                                        continue; // Unable to comply
 
                                        continue; // Unable to comply
 
-                               rewrite = &addr.addr;
+                               rewrite = &addr;
                                rewrite_cnt = 1;
                        }
 
                                rewrite_cnt = 1;
                        }