X-Git-Url: http://git.archive.openwrt.org/?p=project%2Fodhcpd.git;a=blobdiff_plain;f=src%2Fdhcpv6-ia.c;h=09022a43a252cfa461c2de4e1b0f6fe2a49b3b22;hp=abcbe8904b83a2691cee929c485df650b24e3f98;hb=bc6c3ace738fd4eb28c06be533f6bbc75d0587a5;hpb=11ce6b5f0eaa9eac8cfcb77a89829275ebf04f96;ds=sidebyside diff --git a/src/dhcpv6-ia.c b/src/dhcpv6-ia.c index abcbe89..09022a4 100644 --- a/src/dhcpv6-ia.c +++ b/src/dhcpv6-ia.c @@ -34,6 +34,10 @@ #include #include +#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}; @@ -84,8 +88,7 @@ int setup_dhcpv6_ia_interface(struct interface *iface, bool enable) return -1; } - if (lease->dhcpv4_leasetime > 0) - a->leasetime = lease->dhcpv4_leasetime; + a->leasetime = lease->dhcpv4_leasetime; a->clid_len = duid_len; a->length = 128; @@ -142,6 +145,11 @@ 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); @@ -234,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"); @@ -275,16 +287,23 @@ void dhcpv6_write_statefile(void) size_t m = elect_addr(addrs, addrlen); for (size_t i = 0; i < addrlen; ++i) { - if (!valid_addr(&addrs[i], now) || (!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); + addr.s6_addr32[2] = addr.s6_addr32[3] = 0; + } inet_ntop(AF_INET6, &addr, ipbuf, sizeof(ipbuf) - 1); @@ -380,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); } @@ -451,7 +471,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 & !(c->flags & OAF_STATIC)) + else if (first && !(c->flags & OAF_STATIC)) c->valid_until = now + 150; } @@ -515,7 +535,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; } @@ -534,7 +557,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; } @@ -584,7 +610,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); } @@ -618,7 +644,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) { @@ -697,16 +723,11 @@ static size_t append_reply(uint8_t *buf, size_t buflen, uint16_t status, } else { if (a) { uint32_t leasetime; - if (a->leasetime > 0) + if (a->leasetime) leasetime = a->leasetime; else leasetime = iface->dhcpv4_leasetime; - if (leasetime == 0) - leasetime = 3600; - else if (leasetime < 60) - leasetime = 60; - uint32_t pref = leasetime; uint32_t valid = leasetime; @@ -737,12 +758,13 @@ 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; 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)); @@ -758,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)); @@ -815,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)) @@ -926,6 +947,7 @@ 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) { @@ -934,10 +956,20 @@ static void dhcpv6_log(uint8_t msgtype, struct interface *iface, time_t now, 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); + addr.s6_addr32[2] = addr.s6_addr32[3] = 0; + } inet_ntop(AF_INET6, &addr, addrbuf, sizeof(addrbuf)); lbsize += snprintf(leasebuf + lbsize, sizeof(leasebuf) - lbsize, "%s/%d ", addrbuf, prefix); @@ -1043,8 +1075,10 @@ ssize_t dhcpv6_handle_ia(uint8_t *buf, size_t buflen, struct interface *iface, ((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; @@ -1143,10 +1177,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 || @@ -1165,8 +1200,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;