dhcpv6-ia: apply lease delete based on assignment bound state
[project/odhcpd.git] / src / dhcpv6-ia.c
index 17294f0..84f368e 100644 (file)
 #include <arpa/inet.h>
 #include <sys/timerfd.h>
 
+#define ADDR_ENTRY_VALID_IA_ADDR(iface, i, m, addrs) \
+    ((iface)->managed == RELAYD_MANAGED_NO_AFLAG || (i) == (m) || \
+     (addrs)[(i)].prefix > 64)
+
 static void free_dhcpv6_assignment(struct dhcpv6_assignment *c);
 static void reconf_timer(struct uloop_timeout *event);
 static struct uloop_timeout reconf_event = {.cb = reconf_timer};
@@ -50,6 +54,7 @@ int setup_dhcpv6_ia_interface(struct interface *iface, bool enable)
 {
        if (!enable && iface->ia_assignments.next) {
                struct dhcpv6_assignment *c;
+
                while (!list_empty(&iface->ia_assignments)) {
                        c = list_first_entry(&iface->ia_assignments, struct dhcpv6_assignment, head);
                        free_dhcpv6_assignment(c);
@@ -103,7 +108,7 @@ int setup_dhcpv6_ia_interface(struct interface *iface, bool enable)
                        /* Infinite valid */
                        a->valid_until = 0;
 
-                       // Assign to all interfaces
+                       /* Assign to all interfaces */
                        struct dhcpv6_assignment *c;
                        list_for_each_entry(c, &iface->ia_assignments, head) {
                                if (c->length != 128 || c->assigned > a->assigned) {
@@ -141,6 +146,30 @@ static void free_dhcpv6_assignment(struct dhcpv6_assignment *c)
        free(c);
 }
 
+static inline bool valid_prefix_length(const struct dhcpv6_assignment *a, const uint8_t prefix_length)
+{
+       return (a->managed_size || a->length > prefix_length);
+}
+
+static inline bool valid_addr(const struct odhcpd_ipaddr *addr, time_t now)
+{
+       return (addr->prefix <= 96 && addr->preferred > (uint32_t)now);
+}
+
+static size_t elect_addr(const struct odhcpd_ipaddr *addrs, const size_t addrlen)
+{
+       size_t i, m;
+
+       for (i = 0, m = 0; i < addrlen; ++i) {
+               if (addrs[i].preferred > addrs[m].preferred ||
+                       (addrs[i].preferred == addrs[m].preferred &&
+                        memcmp(&addrs[i].addr, &addrs[m].addr, 16) > 0))
+                       m = i;
+       }
+
+       return m;
+}
+
 static int send_reconf(struct interface *iface, struct dhcpv6_assignment *assign)
 {
        struct {
@@ -240,7 +269,7 @@ void dhcpv6_write_statefile(void)
                                        char duidbuf[264];
                                        odhcpd_hexlify(duidbuf, c->clid_data, c->clid_len);
 
-                                       // iface DUID iaid hostname lifetime assigned length [addrs...]
+                                       /* iface DUID iaid hostname lifetime assigned length [addrs...] */
                                        int l = snprintf(leasebuf, sizeof(leasebuf), "# %s %s %x %s %ld %x %u ",
                                                        iface->ifname, duidbuf, ntohl(c->iaid),
                                                        (c->hostname ? c->hostname : "-"),
@@ -252,25 +281,26 @@ void dhcpv6_write_statefile(void)
                                        struct in6_addr addr;
                                        struct odhcpd_ipaddr *addrs = (c->managed) ? c->managed : iface->ia_addr;
                                        size_t addrlen = (c->managed) ? (size_t)c->managed_size : iface->ia_addr_len;
-                                       size_t m = 0;
-
-                                       for (size_t i = 0; i < addrlen; ++i)
-                                               if (addrs[i].preferred > addrs[m].preferred ||
-                                                               (addrs[i].preferred == addrs[m].preferred &&
-                                                                               memcmp(&addrs[i].addr, &addrs[m].addr, 16) > 0))
-                                                       m = i;
+                                       size_t m = elect_addr(addrs, addrlen);
 
                                        for (size_t i = 0; i < addrlen; ++i) {
-                                               if (addrs[i].prefix > 96 || (!INFINITE_VALID(c->valid_until) && c->valid_until <= now) ||
-                                                               (iface->managed < RELAYD_MANAGED_NO_AFLAG && i != m &&
-                                                                               addrs[i].prefix == 64))
+                                               if (!valid_addr(&addrs[i], now) ||
+                                                           (!INFINITE_VALID(c->valid_until) && c->valid_until <= now))
                                                        continue;
 
                                                addr = addrs[i].addr;
-                                               if (c->length == 128)
+                                               if (c->length == 128) {
+                                                       if (!ADDR_ENTRY_VALID_IA_ADDR(iface, i, m, addrs))
+                                                               continue;
+
                                                        addr.s6_addr32[3] = htonl(c->assigned);
-                                               else
+                                               }
+                                               else {
+                                                       if (!valid_prefix_length(c, addrs[i].prefix))
+                                                               continue;
+
                                                        addr.s6_addr32[1] |= htonl(c->assigned);
+                                               }
 
                                                inet_ntop(AF_INET6, &addr, ipbuf, sizeof(ipbuf) - 1);
 
@@ -306,7 +336,7 @@ void dhcpv6_write_statefile(void)
                                        char duidbuf[16];
                                        odhcpd_hexlify(duidbuf, c->hwaddr, sizeof(c->hwaddr));
 
-                                       // iface DUID iaid hostname lifetime assigned length [addrs...]
+                                       /* iface DUID iaid hostname lifetime assigned length [addrs...] */
                                        int l = snprintf(leasebuf, sizeof(leasebuf), "# %s %s ipv4 %s %ld %x 32 ",
                                                        iface->ifname, duidbuf,
                                                        (c->hostname ? c->hostname : "-"),
@@ -371,7 +401,7 @@ static void apply_lease(struct interface *iface, struct dhcpv6_assignment *a, bo
        }
 }
 
-// More data was received from TCP connection
+/* More data was received from TCP connection */
 static void managed_handle_pd_data(struct ustream *s, _unused int bytes_new)
 {
        struct dhcpv6_assignment *c = container_of(s, struct dhcpv6_assignment, managed_sock);
@@ -437,7 +467,7 @@ static void managed_handle_pd_data(struct ustream *s, _unused int bytes_new)
 
        if (first && c->managed_size == 0)
                free_dhcpv6_assignment(c);
-       else if (first)
+       else if (first && !(c->flags & OAF_STATIC))
                c->valid_until = now + 150;
 }
 
@@ -447,7 +477,9 @@ static void managed_handle_pd_done(struct ustream *s)
 {
        struct dhcpv6_assignment *c = container_of(s, struct dhcpv6_assignment, managed_sock);
 
-       c->valid_until = odhcpd_time() + 15;
+       if (!(c->flags & OAF_STATIC))
+               c->valid_until = odhcpd_time() + 15;
+
        c->managed_size = 0;
 
        if (c->accept_reconf)
@@ -471,7 +503,10 @@ static bool assign_pd(struct interface *iface, struct dhcpv6_assignment *assign)
                                        iaidbuf, assign->iaid, assign->length);
                        ustream_write_pending(&assign->managed_sock.stream);
                        assign->managed_size = -1;
-                       assign->valid_until = odhcpd_time() + 15;
+
+                       if (!(assign->flags & OAF_STATIC))
+                               assign->valid_until = odhcpd_time() + 15;
+
                        list_add(&assign->head, &iface->ia_assignments);
 
                        /* Wait initial period of up to 250ms for immediate assignment */
@@ -496,7 +531,10 @@ static bool assign_pd(struct interface *iface, struct dhcpv6_assignment *assign)
 
                        if (assign->assigned >= current && assign->assigned + asize < c->assigned) {
                                list_add_tail(&assign->head, &c->head);
-                               apply_lease(iface, assign, true);
+
+                               if (assign->flags & OAF_BOUND)
+                                       apply_lease(iface, assign, true);
+
                                return true;
                        }
 
@@ -515,7 +553,10 @@ static bool assign_pd(struct interface *iface, struct dhcpv6_assignment *assign)
                if (current + asize < c->assigned) {
                        assign->assigned = current;
                        list_add_tail(&assign->head, &c->head);
-                       apply_lease(iface, assign, true);
+
+                       if (assign->flags & OAF_BOUND)
+                               apply_lease(iface, assign, true);
+
                        return true;
                }
 
@@ -565,7 +606,7 @@ void dhcpv6_ia_preupdate(struct interface *iface)
                        &iface->ia_assignments, struct dhcpv6_assignment, head);
 
        list_for_each_entry(c, &iface->ia_assignments, head)
-               if (c != border && !iface->managed)
+               if (c != border && !iface->managed && (c->flags & OAF_BOUND))
                        apply_lease(iface, c, false);
 }
 
@@ -599,7 +640,7 @@ void dhcpv6_ia_postupdate(struct interface *iface, time_t now)
 
                if (c->length < 128 && c->assigned >= border->assigned && c != border)
                        list_move(&c->head, &reassign);
-               else if (c != border)
+               else if (c != border && (c->flags & OAF_BOUND))
                        apply_lease(iface, c, true);
 
                if (c->accept_reconf && c->reconf_cnt == 0) {
@@ -693,20 +734,13 @@ static size_t append_reply(uint8_t *buf, size_t buflen, uint16_t status,
 
                        struct odhcpd_ipaddr *addrs = (a->managed) ? a->managed : iface->ia_addr;
                        size_t addrlen = (a->managed) ? (size_t)a->managed_size : iface->ia_addr_len;
-                       size_t m = 0;
-
-                       for (size_t i = 0; i < addrlen; ++i)
-                               if (addrs[i].preferred > addrs[m].preferred ||
-                                               (addrs[i].preferred == addrs[m].preferred &&
-                                                               memcmp(&addrs[i].addr, &addrs[m].addr, 16) > 0))
-                                       m = i;
+                       size_t m = elect_addr(addrs, addrlen);
 
                        for (size_t i = 0; i < addrlen; ++i) {
                                uint32_t prefix_pref = addrs[i].preferred;
                                uint32_t prefix_valid = addrs[i].valid;
 
-                               if (addrs[i].prefix > 96 ||
-                                               addrs[i].preferred <= (uint32_t)now)
+                               if (!valid_addr(&addrs[i], now))
                                        continue;
 
                                if (prefix_pref != UINT32_MAX)
@@ -730,7 +764,7 @@ static size_t append_reply(uint8_t *buf, size_t buflen, uint16_t status,
 
                                        if (datalen + entrlen + 4 > buflen ||
                                                        (a->assigned == 0 && a->managed_size == 0) ||
-                                                       (!a->managed_size && a->length <= addrs[i].prefix))
+                                                       !valid_prefix_length(a, addrs[i].prefix))
                                                continue;
 
                                        memcpy(buf + datalen, &p, sizeof(p));
@@ -746,11 +780,9 @@ static size_t append_reply(uint8_t *buf, size_t buflen, uint16_t status,
                                        n.addr.s6_addr32[3] = htonl(a->assigned);
                                        size_t entrlen = sizeof(n) - 4;
 
-                                       if (iface->managed < RELAYD_MANAGED_NO_AFLAG && i != m &&
-                                                       addrs[i].prefix <= 64)
-                                               continue;
-
-                                       if (datalen + entrlen + 4 > buflen || a->assigned == 0)
+                                       if (!ADDR_ENTRY_VALID_IA_ADDR(iface, i, m, addrs) ||
+                                                       a->assigned == 0 ||
+                                                       datalen + entrlen + 4 > buflen)
                                                continue;
 
                                        memcpy(buf + datalen, &n, sizeof(n));
@@ -797,8 +829,7 @@ static size_t append_reply(uint8_t *buf, size_t buflen, uint16_t status,
                                        size_t addrlen = (a->managed) ? (size_t)a->managed_size : iface->ia_addr_len;
 
                                        for (size_t i = 0; i < addrlen; ++i) {
-                                               if (addrs[i].prefix > 96 ||
-                                                               addrs[i].preferred <= (uint32_t)now)
+                                               if (!valid_addr(&addrs[i], now))
                                                        continue;
 
                                                struct in6_addr addr = addrs[i].addr;
@@ -915,18 +946,28 @@ static void dhcpv6_log(uint8_t msgtype, struct interface *iface, time_t now,
                struct odhcpd_ipaddr *addrs = (a->managed) ? a->managed : iface->ia_addr;
                size_t addrlen = (a->managed) ? (size_t)a->managed_size : iface->ia_addr_len;
                size_t lbsize = 0;
+               size_t m = elect_addr(addrs, addrlen);
                char addrbuf[INET6_ADDRSTRLEN];
 
                for (size_t i = 0; i < addrlen; ++i) {
-                       if (addrs[i].prefix > 96 || addrs[i].preferred <= (uint32_t)now)
+                       if (!valid_addr(&addrs[i], now))
                                continue;
 
                        struct in6_addr addr = addrs[i].addr;
                        int prefix = a->managed ? addrs[i].prefix : a->length;
-                       if (prefix == 128)
+                       if (prefix == 128) {
+                               if (!ADDR_ENTRY_VALID_IA_ADDR(iface, i, m, addrs) ||
+                                               a->assigned == 0)
+                                       continue;
+
                                addr.s6_addr32[3] = htonl(a->assigned);
-                       else
+                       }
+                       else {
+                               if (!valid_prefix_length(a, addrs[i].prefix))
+                                       continue;
+
                                addr.s6_addr32[1] |= htonl(a->assigned);
+                       }
 
                        inet_ntop(AF_INET6, &addr, addrbuf, sizeof(addrbuf));
                        lbsize += snprintf(leasebuf + lbsize, sizeof(leasebuf) - lbsize, "%s/%d ", addrbuf, prefix);
@@ -1027,13 +1068,15 @@ ssize_t dhcpv6_handle_ia(uint8_t *buf, size_t buflen, struct interface *iface,
                list_for_each_entry(c, &iface->ia_assignments, head) {
                        if (((c->clid_len == clid_len && !memcmp(c->clid_data, clid_data, clid_len)) ||
                                        (c->clid_len >= clid_len && !c->clid_data[0] && !c->clid_data[1]
-                                               && !memcmp(c->mac, mac, sizeof(mac)))) &&
+                                               && !memcmp(c->mac, mac, sizeof(mac)))) &&
                                        (c->iaid == ia->iaid || INFINITE_VALID(c->valid_until) || now < c->valid_until) &&
                                        ((is_pd && c->length <= 64) || (is_na && c->length == 128))) {
                                a = c;
 
-                               // Reset state
-                               apply_lease(iface, a, false);
+                               /* Reset state */
+                               if (a->flags & OAF_BOUND)
+                                       apply_lease(iface, a, false);
+
                                memcpy(a->clid_data, clid_data, clid_len);
                                a->clid_len = clid_len;
                                a->iaid = ia->iaid;
@@ -1132,10 +1175,11 @@ ssize_t dhcpv6_handle_ia(uint8_t *buf, size_t buflen, struct interface *iface,
                                a->accept_reconf = accept_reconf;
                                a->flags |= OAF_BOUND;
                                apply_lease(iface, a, true);
-                       } else if (!assigned && a && a->managed_size == 0)
+                       } else if (!assigned && a && a->managed_size == 0) {
                                /* Cleanup failed assignment */
                                free_dhcpv6_assignment(a);
-
+                               a = NULL;
+                       }
                } else if (hdr->msg_type == DHCPV6_MSG_RENEW ||
                                hdr->msg_type == DHCPV6_MSG_RELEASE ||
                                hdr->msg_type == DHCPV6_MSG_REBIND ||
@@ -1154,8 +1198,10 @@ ssize_t dhcpv6_handle_ia(uint8_t *buf, size_t buflen, struct interface *iface,
                                if (!(a->flags & OAF_STATIC))
                                        a->valid_until = now - 1;
 
-                               a->flags &= ~OAF_BOUND;
-                               apply_lease(iface, a, false);
+                               if (a->flags & OAF_BOUND) {
+                                       apply_lease(iface, a, false);
+                                       a->flags &= ~OAF_BOUND;
+                               }
                        } else if (hdr->msg_type == DHCPV6_MSG_DECLINE && a->length == 128) {
                                a->flags &= ~OAF_BOUND;