X-Git-Url: http://git.archive.openwrt.org/?p=project%2Fodhcpd.git;a=blobdiff_plain;f=src%2Fdhcpv6.c;h=902bdbf6b501bc59ea40a7964407f011a2fe9a29;hp=7302d5d989018dd0908a33e17209461cdcc1129d;hb=f0e354befd6f787c1ed8ebc1ea514e9195f54433;hpb=ee243b09f3c81f824cc463c1c757e905f882350e diff --git a/src/dhcpv6.c b/src/dhcpv6.c index 7302d5d..902bdbf 100644 --- a/src/dhcpv6.c +++ b/src/dhcpv6.c @@ -18,6 +18,7 @@ #include #include #include +#include #include "odhcpd.h" #include "dhcpv6.h" @@ -51,7 +52,7 @@ int setup_dhcpv6_interface(struct interface *iface, bool enable) } // Configure multicast settings - if (enable && iface->dhcpv6 && !iface->master) { + if (enable && iface->dhcpv6) { int sock = socket(AF_INET6, SOCK_DGRAM | SOCK_CLOEXEC, IPPROTO_UDP); if (sock < 0) { syslog(LOG_ERR, "Failed to create DHCPv6 server socket: %s", @@ -86,7 +87,7 @@ int setup_dhcpv6_interface(struct interface *iface, bool enable) struct ipv6_mreq server = {ALL_DHCPV6_SERVERS, iface->ifindex}; setsockopt(sock, IPPROTO_IPV6, IPV6_ADD_MEMBERSHIP, &relay, sizeof(relay)); - if (iface->dhcpv6 == RELAYD_SERVER) + if (iface->dhcpv6 == MODE_SERVER) setsockopt(sock, IPPROTO_IPV6, IPV6_ADD_MEMBERSHIP, &server, sizeof(server)); iface->dhcpv6_event.uloop.fd = sock; @@ -228,7 +229,7 @@ static void handle_client_request(void *addr, void *data, size_t len, size_t dns_cnt = iface->dns_cnt; if ((dns_cnt == 0) && - odhcpd_get_preferred_interface_address(iface->ifindex, &dns_addr)) { + !odhcpd_get_interface_dns_addr(iface, &dns_addr)) { dns_addr_ptr = &dns_addr; dns_cnt = 1; } @@ -331,14 +332,15 @@ static void handle_client_request(void *addr, void *data, size_t len, iov[IOV_CERID].iov_len = sizeof(cerid); if (IN6_IS_ADDR_UNSPECIFIED(&cerid.addr)) { - struct odhcpd_ipaddr addrs[32]; - ssize_t len = odhcpd_get_interface_addresses(0, addrs, - ARRAY_SIZE(addrs)); + struct odhcpd_ipaddr *addrs; + ssize_t len = odhcpd_get_interface_addresses(0, true, &addrs); for (ssize_t i = 0; i < len; ++i) if (IN6_IS_ADDR_UNSPECIFIED(&cerid.addr) || memcmp(&addrs[i].addr, &cerid.addr, sizeof(cerid.addr)) < 0) cerid.addr = addrs[i].addr; + + free(addrs); } #endif } @@ -379,9 +381,9 @@ static void handle_client_request(void *addr, void *data, size_t len, static void handle_dhcpv6(void *addr, void *data, size_t len, struct interface *iface, void *dest_addr) { - if (iface->dhcpv6 == RELAYD_SERVER) { + if (iface->dhcpv6 == MODE_SERVER) { handle_client_request(addr, data, len, iface, dest_addr); - } else if (iface->dhcpv6 == RELAYD_RELAY) { + } else if (iface->dhcpv6 == MODE_RELAY) { if (iface->master) relay_server_response(data, len); else @@ -460,7 +462,7 @@ static void relay_server_response(uint8_t *data, size_t len) size_t rewrite_cnt = iface->dns_cnt; if (rewrite_cnt == 0) { - if (odhcpd_get_preferred_interface_address(iface->ifindex, &addr) < 1) + if (odhcpd_get_interface_dns_addr(iface, &addr)) return; // Unable to get interface address rewrite = &addr; @@ -478,6 +480,26 @@ static void relay_server_response(uint8_t *data, size_t len) odhcpd_send(iface->dhcpv6_event.uloop.fd, &target, &iov, 1, iface); } +static struct odhcpd_ipaddr *relay_link_address(struct interface *iface) +{ + struct odhcpd_ipaddr *addr = NULL; + time_t now = odhcpd_time(); + + for (size_t i = 0; i < iface->ia_addr_len; i++) { + if (iface->ia_addr[i].valid <= (uint32_t)now) + continue; + + if (iface->ia_addr[i].preferred > (uint32_t)now) { + addr = &iface->ia_addr[i]; + break; + } + + if (!addr || (iface->ia_addr[i].valid > addr->valid)) + addr = &iface->ia_addr[i]; + } + + return addr; +} // Relay client request (regular DHCPv6-relay) static void relay_client_request(struct sockaddr_in6 *source, @@ -485,7 +507,7 @@ static void relay_client_request(struct sockaddr_in6 *source, { struct interface *master = odhcpd_get_master_interface(); const struct dhcpv6_relay_header *h = data; - if (!master || master->dhcpv6 != RELAYD_RELAY || + if (!master || master->dhcpv6 != MODE_RELAY || h->msg_type == DHCPV6_MSG_RELAY_REPL || h->msg_type == DHCPV6_MSG_RECONFIGURE || h->msg_type == DHCPV6_MSG_REPLY || @@ -517,20 +539,22 @@ static void relay_client_request(struct sockaddr_in6 *source, memcpy(&hdr.interface_id_data, &ifindex, sizeof(ifindex)); // Detect public IP of slave interface to use as link-address - struct odhcpd_ipaddr ip; - if (odhcpd_get_interface_addresses(iface->ifindex, &ip, 1) < 1) { + struct odhcpd_ipaddr *ip = relay_link_address(iface); + if (!ip) { // No suitable address! Is the slave not configured yet? // Detect public IP of master interface and use it instead // This is WRONG and probably violates the RFC. However // otherwise we have a hen and egg problem because the // slave-interface cannot be auto-configured. - if (odhcpd_get_interface_addresses(master->ifindex, &ip, 1) < 1) + ip = relay_link_address(master); + if (!ip) return; // Could not obtain a suitable address } - memcpy(&hdr.link_address, &ip.addr, sizeof(hdr.link_address)); + + memcpy(&hdr.link_address, &ip->addr.in6, sizeof(hdr.link_address)); struct sockaddr_in6 dhcpv6_servers = {AF_INET6, htons(DHCPV6_SERVER_PORT), 0, ALL_DHCPV6_SERVERS, 0}; struct iovec iov[2] = {{&hdr, sizeof(hdr)}, {(void*)data, len}}; - odhcpd_send(iface->dhcpv6_event.uloop.fd, &dhcpv6_servers, iov, 2, master); + odhcpd_send(master->dhcpv6_event.uloop.fd, &dhcpv6_servers, iov, 2, master); }