X-Git-Url: http://git.archive.openwrt.org/?p=project%2Fodhcpd.git;a=blobdiff_plain;f=src%2Fodhcpd.c;h=6f38d5c1c06117aa182ea9862234ba76cff6e24e;hp=9a76e4d638ee54b3a0ffcd4592c6b383479e126e;hb=f4d38e0a853e933a4da54fde8ec2711b5dc5b741;hpb=51c756cfc15c63322df9fdb70d5c701cfb6b9a9f diff --git a/src/odhcpd.c b/src/odhcpd.c index 9a76e4d..6f38d5c 100644 --- a/src/odhcpd.c +++ b/src/odhcpd.c @@ -24,6 +24,8 @@ #include #include #include +#include +#include #include #include @@ -52,7 +54,6 @@ static int ioctl_sock; static struct nl_sock *rtnl_socket = NULL; static int urandom_fd = -1; -static int log_level = LOG_INFO; static void sighandler(_unused int signal) { @@ -65,7 +66,7 @@ static void print_usage(const char *app) "== %s Usage ==\n\n" " -h, --help Print this help\n" " -l level Specify log level 0..7 (default %d)\n", - app, log_level + app, config.log_level ); } @@ -80,12 +81,12 @@ int main(int argc, char **argv) print_usage(argv[0]); return 0; case 'l': - log_level = atoi(optarg); - fprintf(stderr, "Log level set to %d\n", log_level); + config.log_level = (atoi(optarg) & LOG_PRIMASK); + fprintf(stderr, "Log level set to %d\n", config.log_level); break; } } - setlogmask(LOG_UPTO(log_level)); + setlogmask(LOG_UPTO(config.log_level)); uloop_init(); if (getuid() != 0) { @@ -221,8 +222,8 @@ ssize_t odhcpd_send(int socket, struct sockaddr_in6 *dest, struct addr_info { int ifindex; - struct odhcpd_ipaddr *addrs; - size_t addrs_sz; + int af; + struct odhcpd_ipaddr **addrs; int pending; ssize_t ret; }; @@ -230,15 +231,17 @@ struct addr_info { 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 (ifa->ifa_scope != RT_SCOPE_UNIVERSE || + (ctxt->af != ifa->ifa_family) || (ctxt->ifindex && ifa->ifa_index != (unsigned)ctxt->ifindex)) return NL_SKIP; @@ -246,23 +249,28 @@ static int cb_valid_handler(struct nl_msg *msg, void *arg) 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; } @@ -287,13 +295,21 @@ static int cb_error_handler(_unused struct sockaddr_nl *nla, struct nlmsgerr *er return NL_STOP; } +// compare prefixes +static int prefixcmp(const void *va, const void *vb) +{ + const struct odhcpd_ipaddr *a = va, *b = vb; + uint32_t a_pref = IN6_IS_ADDR_ULA(&a->addr.in6) ? 1 : a->preferred; + uint32_t b_pref = IN6_IS_ADDR_ULA(&b->addr.in6) ? 1 : b->preferred; + return (a_pref < b_pref) ? 1 : (a_pref > b_pref) ? -1 : 0; +} + // 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, bool v6, struct odhcpd_ipaddr **addrs) { struct nl_msg *msg; struct ifaddrmsg ifa = { - .ifa_family = AF_INET6, + .ifa_family = v6? AF_INET6: AF_INET, .ifa_prefixlen = 0, .ifa_flags = 0, .ifa_scope = 0, @@ -301,8 +317,8 @@ ssize_t odhcpd_get_interface_addresses(int ifindex, struct nl_cb *cb = nl_cb_alloc(NL_CB_DEFAULT); struct addr_info ctxt = { .ifindex = ifindex, + .af = v6? AF_INET6: AF_INET, .addrs = addrs, - .addrs_sz = cnt, .ret = 0, .pending = 1, }; @@ -330,13 +346,30 @@ ssize_t odhcpd_get_interface_addresses(int ifindex, nl_recvmsgs(rtnl_socket, cb); nlmsg_free(msg); + + if (ctxt.ret <= 0) + goto out; + + time_t now = odhcpd_time(); + struct odhcpd_ipaddr *addr = *addrs; + + qsort(addr, ctxt.ret, sizeof(*addr), prefixcmp); + + for (ssize_t i = 0; i < ctxt.ret; ++i) { + if (addr[i].preferred < UINT32_MAX - now) + addr[i].preferred += now; + + if (addr[i].valid < UINT32_MAX - now) + addr[i].valid += now; + } + out: nl_cb_put(cb); 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}; @@ -354,6 +387,51 @@ int odhcpd_get_linklocal_interface_address(int ifindex, struct in6_addr *lladdr) 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.in6)) { + if (!IN6_IS_ADDR_ULA(&iface->ia_addr[m].addr.in6)) { + m = i; + continue; + } + } else if (IN6_IS_ADDR_ULA(&iface->ia_addr[m].addr.in6)) + continue; + + if (iface->ia_addr[i].preferred > iface->ia_addr[m].preferred) + m = i; + } + + if (m >= 0) { + *addr = iface->ia_addr[m].addr.in6; + 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)