#include <signal.h>
#include <stdbool.h>
#include <syslog.h>
+#include <alloca.h>
#include <arpa/inet.h>
#include <net/if.h>
struct addr_info {
int ifindex;
- struct odhcpd_ipaddr *addrs;
- size_t addrs_sz;
+ struct odhcpd_ipaddr **addrs;
int pending;
ssize_t ret;
};
static int cb_valid_handler(struct nl_msg *msg, void *arg)
{
struct addr_info *ctxt = (struct addr_info *)arg;
+ struct odhcpd_ipaddr *addrs = *(ctxt->addrs);
struct nlmsghdr *hdr = nlmsg_hdr(msg);
struct ifaddrmsg *ifa;
struct nlattr *nla[__IFA_MAX];
- if (hdr->nlmsg_type != RTM_NEWADDR || ctxt->ret >= (ssize_t)ctxt->addrs_sz)
+ if (hdr->nlmsg_type != RTM_NEWADDR)
return NL_SKIP;
ifa = NLMSG_DATA(hdr);
if (!nla[IFA_ADDRESS])
return NL_SKIP;
- memset(&ctxt->addrs[ctxt->ret], 0, sizeof(ctxt->addrs[ctxt->ret]));
- ctxt->addrs[ctxt->ret].prefix = ifa->ifa_prefixlen;
+ addrs = realloc(addrs, sizeof(*addrs)*(ctxt->ret + 1));
+ if (!addrs)
+ return NL_SKIP;
+
+ memset(&addrs[ctxt->ret], 0, sizeof(addrs[ctxt->ret]));
+ addrs[ctxt->ret].prefix = ifa->ifa_prefixlen;
- nla_memcpy(&ctxt->addrs[ctxt->ret].addr, nla[IFA_ADDRESS],
- sizeof(ctxt->addrs[ctxt->ret].addr));
+ nla_memcpy(&addrs[ctxt->ret].addr, nla[IFA_ADDRESS],
+ sizeof(addrs[ctxt->ret].addr));
if (nla[IFA_CACHEINFO]) {
struct ifa_cacheinfo *ifc = nla_data(nla[IFA_CACHEINFO]);
- ctxt->addrs[ctxt->ret].preferred = ifc->ifa_prefered;
- ctxt->addrs[ctxt->ret].valid = ifc->ifa_valid;
+ addrs[ctxt->ret].preferred = ifc->ifa_prefered;
+ addrs[ctxt->ret].valid = ifc->ifa_valid;
}
if (ifa->ifa_flags & IFA_F_DEPRECATED)
- ctxt->addrs[ctxt->ret].preferred = 0;
+ addrs[ctxt->ret].preferred = 0;
ctxt->ret++;
+ *(ctxt->addrs) = addrs;
return NL_OK;
}
}
// Detect an IPV6-address currently assigned to the given interface
-ssize_t odhcpd_get_interface_addresses(int ifindex,
- struct odhcpd_ipaddr *addrs, size_t cnt)
+ssize_t odhcpd_get_interface_addresses(int ifindex, struct odhcpd_ipaddr **addrs)
{
struct nl_msg *msg;
struct ifaddrmsg ifa = {
struct addr_info ctxt = {
.ifindex = ifindex,
.addrs = addrs,
- .addrs_sz = cnt,
.ret = 0,
.pending = 1,
};
return ctxt.ret;
}
-int odhcpd_get_linklocal_interface_address(int ifindex, struct in6_addr *lladdr)
+static int odhcpd_get_linklocal_interface_address(int ifindex, struct in6_addr *lladdr)
{
int status = -1;
struct sockaddr_in6 addr = {AF_INET6, 0, 0, ALL_IPV6_ROUTERS, ifindex};
return status;
}
+/*
+ * DNS address selection criteria order :
+ * - use IPv6 address with valid lifetime if none is yet selected
+ * - use IPv6 address with a preferred lifetime if the already selected IPv6 address is deprecated
+ * - use an IPv6 ULA address if the already selected IPv6 address is not an ULA address
+ * - use the IPv6 address with the longest preferred lifetime
+ */
+int odhcpd_get_interface_dns_addr(const struct interface *iface, struct in6_addr *addr)
+{
+ time_t now = odhcpd_time();
+ ssize_t m = -1;
+
+ for (size_t i = 0; i < iface->ia_addr_len; ++i) {
+ if (iface->ia_addr[i].valid <= (uint32_t)now)
+ continue;
+
+ if (m < 0) {
+ m = i;
+ continue;
+ }
+
+ if (iface->ia_addr[m].preferred >= (uint32_t)now &&
+ iface->ia_addr[i].preferred < (uint32_t)now)
+ continue;
+
+ if (IN6_IS_ADDR_ULA(&iface->ia_addr[i].addr)) {
+ if (!IN6_IS_ADDR_ULA(&iface->ia_addr[m].addr)) {
+ m = i;
+ continue;
+ }
+ } else if (IN6_IS_ADDR_ULA(&iface->ia_addr[m].addr))
+ continue;
+
+ if (iface->ia_addr[i].preferred > iface->ia_addr[m].preferred)
+ m = i;
+ }
+
+ if (m >= 0) {
+ *addr = iface->ia_addr[m].addr;
+ return 0;
+ }
+
+ return odhcpd_get_linklocal_interface_address(iface->ifindex, addr);
+}
+
int odhcpd_setup_route(const struct in6_addr *addr, const int prefixlen,
const struct interface *iface, const struct in6_addr *gw,
const uint32_t metric, const bool add)