router: add support for router address
authorSteven Barth <steven@midlink.org>
Thu, 16 Oct 2014 15:12:26 +0000 (17:12 +0200)
committerSteven Barth <steven@midlink.org>
Thu, 16 Oct 2014 15:12:26 +0000 (17:12 +0200)
src/config.c
src/odhcpd.h
src/router.c

index 2c0bc900173e13841d8efe486004d25279af9750..dc1dcdcaa0d9e12d628540479929f272b8552c6e 100644 (file)
@@ -39,6 +39,7 @@ enum {
        IFACE_ATTR_RA_MANAGEMENT,
        IFACE_ATTR_RA_OFFLINK,
        IFACE_ATTR_RA_PREFERENCE,
+       IFACE_ATTR_RA_ADVROUTER,
        IFACE_ATTR_PD_MANAGER,
        IFACE_ATTR_PD_CER,
        IFACE_ATTR_NDPROXY_ROUTING,
@@ -73,6 +74,7 @@ static const struct blobmsg_policy iface_attrs[IFACE_ATTR_MAX] = {
        [IFACE_ATTR_RA_MANAGEMENT] = { .name = "ra_management", .type = BLOBMSG_TYPE_INT32 },
        [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_NDPROXY_ROUTING] = { .name = "ndproxy_routing", .type = BLOBMSG_TYPE_BOOL },
        [IFACE_ATTR_NDPROXY_SLAVE] = { .name = "ndproxy_slave", .type = BLOBMSG_TYPE_BOOL },
        [IFACE_ATTR_NDPROXY_STATIC] = { .name = "ndproxy_static", .type = BLOBMSG_TYPE_ARRAY },
@@ -514,6 +516,9 @@ int config_parse_interface(void *data, size_t len, const char *name, bool overwr
        if ((c = tb[IFACE_ATTR_RA_OFFLINK]))
                iface->ra_not_onlink = blobmsg_get_bool(c);
 
+       if ((c = tb[IFACE_ATTR_RA_ADVROUTER]))
+               iface->ra_advrouter = blobmsg_get_bool(c);
+
        if ((c = tb[IFACE_ATTR_RA_PREFERENCE])) {
                const char *prio = blobmsg_get_string(c);
 
index 4c0c28ca166b8ae971b251f81fc8d477a1624c15..2cadccb253a6e7fb7bad0c91bdc0d2d5dd577512 100644 (file)
@@ -137,6 +137,7 @@ struct interface {
        bool ignore;
        bool always_rewrite_dns;
        bool ra_not_onlink;
+       bool ra_advrouter;
        bool no_dynamic_dhcp;
 
        int learn_routes;
index 77972ab210ab705197f0fcefac637578c3b9dfaf..0b15dacc16dea859e30a596a78d2ad6b54d1a570 100644 (file)
@@ -293,7 +293,8 @@ static uint64_t send_router_advert(struct interface *iface, const struct in6_add
                                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;
@@ -302,6 +303,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;
+               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);
 
@@ -410,12 +413,37 @@ static uint64_t send_router_advert(struct interface *iface, const struct in6_add
                ++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)},
-                       {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))
@@ -424,36 +452,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);
 
-       return maxpreferred;
+       return msecs;
 }
 
 
 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
-       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);
-       }
 }