dhcpv6-ia: create assignment for unknown IA in rebind messages
[project/odhcpd.git] / src / dhcpv6-ia.c
index 888634f..0eaa886 100644 (file)
@@ -242,8 +242,12 @@ void dhcpv6_write_statefile(void)
                int fd = open(config.dhcp_statefile, O_CREAT | O_WRONLY | O_CLOEXEC, 0644);
                if (fd < 0)
                        return;
-
-               lockf(fd, F_LOCK, 0);
+               int ret;
+               ret = lockf(fd, F_LOCK, 0);
+               if (ret < 0) {
+                       close(fd);
+                       return;
+               }
                if (ftruncate(fd, 0) < 0) {}
 
                FILE *fp = fdopen(fd, "w");
@@ -293,12 +297,12 @@ void dhcpv6_write_statefile(void)
                                                                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);
+                                                       addr.s6_addr32[2] = addr.s6_addr32[3] = 0;
                                                }
 
                                                inet_ntop(AF_INET6, &addr, ipbuf, sizeof(ipbuf) - 1);
@@ -395,6 +399,7 @@ static void apply_lease(struct interface *iface, struct dhcpv6_assignment *a, bo
        for (size_t i = 0; i < addrlen; ++i) {
                struct in6_addr prefix = addrs[i].addr;
                prefix.s6_addr32[1] |= htonl(a->assigned);
+               prefix.s6_addr32[2] = prefix.s6_addr32[3] = 0;
                odhcpd_setup_route(&prefix, (a->managed_size) ? addrs[i].prefix : a->length,
                                iface, &a->peer.sin6_addr, 1024, add);
        }
@@ -753,6 +758,7 @@ static size_t append_reply(uint8_t *buf, size_t buflen, uint16_t status,
                                                .addr = addrs[i].addr
                                        };
                                        p.addr.s6_addr32[1] |= htonl(a->assigned);
+                                       p.addr.s6_addr32[2] = p.addr.s6_addr32[3] = 0;
 
                                        size_t entrlen = sizeof(p) - 4;
 
@@ -829,6 +835,7 @@ static size_t append_reply(uint8_t *buf, size_t buflen, uint16_t status,
                                                struct in6_addr addr = addrs[i].addr;
                                                if (ia->type == htons(DHCPV6_OPT_IA_PD)) {
                                                        addr.s6_addr32[1] |= htonl(a->assigned);
+                                                       addr.s6_addr32[2] = addr.s6_addr32[3] = 0;
 
                                                        if (!memcmp(&p->addr, &addr, sizeof(addr)) &&
                                                                        p->prefix == ((a->managed) ? addrs[i].prefix : a->length))
@@ -961,6 +968,7 @@ static void dhcpv6_log(uint8_t msgtype, struct interface *iface, time_t now,
                                        continue;
 
                                addr.s6_addr32[1] |= htonl(a->assigned);
+                               addr.s6_addr32[2] = addr.s6_addr32[3] = 0;
                        }
 
                        inet_ntop(AF_INET6, &addr, addrbuf, sizeof(addrbuf));
@@ -1086,7 +1094,9 @@ ssize_t dhcpv6_handle_ia(uint8_t *buf, size_t buflen, struct interface *iface,
                if (a && a->managed_size < 0)
                        return -1;
 
-               if (hdr->msg_type == DHCPV6_MSG_SOLICIT || hdr->msg_type == DHCPV6_MSG_REQUEST) {
+               if (hdr->msg_type == DHCPV6_MSG_SOLICIT ||
+                               hdr->msg_type == DHCPV6_MSG_REQUEST ||
+                               (hdr->msg_type == DHCPV6_MSG_REBIND && !a)) {
                        bool assigned = !!a;
 
                        if (!a && !iface->no_dynamic_dhcp) {
@@ -1122,7 +1132,7 @@ ssize_t dhcpv6_handle_ia(uint8_t *buf, size_t buflen, struct interface *iface,
                        if (!assigned || iface->ia_addr_len == 0)
                                /* Set error status */
                                status = (is_pd) ? DHCPV6_STATUS_NOPREFIXAVAIL : DHCPV6_STATUS_NOADDRSAVAIL;
-                       else if (assigned && !first) {
+                       else if (assigned && !first && hdr->msg_type != DHCPV6_MSG_REBIND) {
                                size_t handshake_len = 4;
                                buf[0] = 0;
                                buf[1] = DHCPV6_OPT_RECONF_ACCEPT;
@@ -1150,7 +1160,8 @@ ssize_t dhcpv6_handle_ia(uint8_t *buf, size_t buflen, struct interface *iface,
                                first = a;
                        }
 
-                       ia_response_len = append_reply(buf, buflen, status, ia, a, iface, true);
+                       ia_response_len = append_reply(buf, buflen, status, ia, a, iface,
+                                                       hdr->msg_type == DHCPV6_MSG_REBIND ? false : true);
 
                        /* Was only a solicitation: mark binding for removal */
                        if (assigned && hdr->msg_type == DHCPV6_MSG_SOLICIT) {
@@ -1158,7 +1169,9 @@ ssize_t dhcpv6_handle_ia(uint8_t *buf, size_t buflen, struct interface *iface,
 
                                if (!(a->flags & OAF_STATIC))
                                        a->valid_until = now;
-                       } else if (assigned && hdr->msg_type == DHCPV6_MSG_REQUEST) {
+                       } else if (assigned &&
+                                       (hdr->msg_type == DHCPV6_MSG_REQUEST ||
+                                        hdr->msg_type == DHCPV6_MSG_REBIND)) {
                                if (hostname_len > 0) {
                                        a->hostname = realloc(a->hostname, hostname_len + 1);
                                        if (a->hostname) {