X-Git-Url: http://git.archive.openwrt.org/?p=project%2Fodhcpd.git;a=blobdiff_plain;f=src%2Fdhcpv6-ia.c;h=4c077db8730e3f2c99260d42de47c9166186e3c6;hp=e1d0278e013c56a03e9d7ae2bd2d73ef0e92edff;hb=16cd87e8a76884802896495eb0e39461d8303c0f;hpb=60c396937959aff9b5358e7a5ab788f0f5f30ed4 diff --git a/src/dhcpv6-ia.c b/src/dhcpv6-ia.c index e1d0278..4c077db 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,25 @@ static void free_dhcpv6_assignment(struct dhcpv6_assignment *c) free(c); } +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 +264,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,18 +276,12 @@ 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) || + !ADDR_ENTRY_VALID_IA_ADDR(iface, i, m, addrs)) continue; addr = addrs[i].addr; @@ -306,7 +324,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 +389,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 +455,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 +465,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 +491,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 */ @@ -640,10 +663,9 @@ static void reconf_timer(struct uloop_timeout *event) list_for_each_entry_safe(a, n, &iface->ia_assignments, head) { if (!INFINITE_VALID(a->valid_until) && a->valid_until < now) { if ((a->length < 128 && a->clid_len > 0) || - (a->length == 128 && a->clid_len == 0)) { - list_del(&a->head); + (a->length == 128 && a->clid_len == 0)) free_dhcpv6_assignment(a); - } + } else if (a->reconf_cnt > 0 && a->reconf_cnt < 8 && now > a->reconf_sent + (1 << a->reconf_cnt)) { ++a->reconf_cnt; @@ -694,20 +716,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) @@ -747,11 +762,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)); @@ -798,8 +811,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; @@ -916,16 +928,22 @@ 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 addr.s6_addr32[1] |= htonl(a->assigned); @@ -1028,12 +1046,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; @@ -1133,10 +1151,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 ||