X-Git-Url: http://git.archive.openwrt.org/?p=project%2Fodhcpd.git;a=blobdiff_plain;f=src%2Fdhcpv6-ia.c;h=88686c9a79db127205f652567a003068d2ebb2dc;hp=c8786c3672c32d925e1da1d21d76ace66493de2c;hb=c5040fea540479b731b040986a9c3a62e34afc37;hpb=561890e1ce8aa467a83db5f05e563a00d4d9aa47 diff --git a/src/dhcpv6-ia.c b/src/dhcpv6-ia.c index c8786c3..88686c9 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}; @@ -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,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); @@ -259,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 : "-"), @@ -274,16 +284,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); + } inet_ntop(AF_INET6, &addr, ipbuf, sizeof(ipbuf) - 1); @@ -319,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 : "-"), @@ -384,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); @@ -450,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 & !(c->flags & OAF_STATIC)) + else if (first && !(c->flags & OAF_STATIC)) c->valid_until = now + 150; } @@ -741,7 +758,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)); @@ -757,11 +774,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)); @@ -925,6 +940,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) { @@ -933,10 +949,19 @@ 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); + } inet_ntop(AF_INET6, &addr, addrbuf, sizeof(addrbuf)); lbsize += snprintf(leasebuf + lbsize, sizeof(leasebuf) - lbsize, "%s/%d ", addrbuf, prefix); @@ -1037,12 +1062,12 @@ 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 + /* Reset state */ apply_lease(iface, a, false); memcpy(a->clid_data, clid_data, clid_len); a->clid_len = clid_len; @@ -1142,10 +1167,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 ||