X-Git-Url: http://git.archive.openwrt.org/?p=project%2Fodhcpd.git;a=blobdiff_plain;f=src%2Fodhcpd.c;h=5f871518e218e2f875202876c4e43891842d6202;hp=2b0ea985c1c308c428af73cd352f0580b3289640;hb=24cdc1b59f00a065dd1cf0a04145ca6aaf6f23f1;hpb=ac70d28ed6ec96e6911cdf56b903f5c5ee3e67c5 diff --git a/src/odhcpd.c b/src/odhcpd.c index 2b0ea98..5f87151 100644 --- a/src/odhcpd.c +++ b/src/odhcpd.c @@ -24,15 +24,14 @@ #include #include #include +#include +#include #include #include #include #include #include -#include -#include -#include #include #include @@ -41,51 +40,48 @@ #include #include -#include -#include -#include #include #include "odhcpd.h" static int ioctl_sock; -static struct nl_sock *rtnl_socket = NULL; static int urandom_fd = -1; - static void sighandler(_unused int signal) { uloop_end(); } + static void print_usage(const char *app) { printf( "== %s Usage ==\n\n" " -h, --help Print this help\n" " -l level Specify log level 0..7 (default %d)\n", - app, LOG_WARNING + app, config.log_level ); } + int main(int argc, char **argv) { openlog("odhcpd", LOG_PERROR | LOG_PID, LOG_DAEMON); int opt; - int log_level = LOG_INFO; + while ((opt = getopt(argc, argv, "hl:")) != -1) { switch (opt) { case 'h': 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) { @@ -95,11 +91,6 @@ int main(int argc, char **argv) ioctl_sock = socket(AF_INET, SOCK_DGRAM | SOCK_CLOEXEC, 0); - if (!(rtnl_socket = odhcpd_create_nl_socket(NETLINK_ROUTE))) { - syslog(LOG_ERR, "Unable to open nl socket: %s", strerror(errno)); - return 2; - } - if ((urandom_fd = open("/dev/urandom", O_RDONLY | O_CLOEXEC)) < 0) return 4; @@ -107,42 +98,25 @@ int main(int argc, char **argv) signal(SIGINT, sighandler); signal(SIGTERM, sighandler); - if (init_router()) + if (netlink_init()) return 4; - if (init_dhcpv6()) + if (router_init()) return 4; - if (init_ndp()) + if (dhcpv6_init()) return 4; - if (init_dhcpv4()) + if (ndp_init()) + return 4; + + if (dhcpv4_init()) return 4; odhcpd_run(); return 0; } -struct nl_sock *odhcpd_create_nl_socket(int protocol) -{ - struct nl_sock *nl_sock; - - nl_sock = nl_socket_alloc(); - if (!nl_sock) - goto err; - - if (nl_connect(nl_sock, protocol) < 0) - goto err; - - return nl_sock; - -err: - if (nl_sock) - nl_socket_free(nl_sock); - - return NULL; -} - // Read IPv6 MTU for interface int odhcpd_get_interface_config(const char *ifname, const char *what) @@ -219,124 +193,8 @@ ssize_t odhcpd_send(int socket, struct sockaddr_in6 *dest, return sent; } -struct addr_info { - int ifindex; - struct odhcpd_ipaddr *addrs; - size_t addrs_sz; - 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 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) - return NL_SKIP; - - ifa = NLMSG_DATA(hdr); - if (ifa->ifa_scope != RT_SCOPE_UNIVERSE || - (ctxt->ifindex && ifa->ifa_index != (unsigned)ctxt->ifindex)) - return NL_SKIP; - - nlmsg_parse(hdr, sizeof(*ifa), nla, __IFA_MAX - 1, NULL); - 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; - - nla_memcpy(&ctxt->addrs[ctxt->ret].addr, nla[IFA_ADDRESS], - sizeof(ctxt->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; - } - - if (ifa->ifa_flags & IFA_F_DEPRECATED) - ctxt->addrs[ctxt->ret].preferred = 0; - - ctxt->ret++; - - return NL_OK; -} - -static int cb_finish_handler(_unused struct nl_msg *msg, void *arg) -{ - struct addr_info *ctxt = (struct addr_info *)arg; - - ctxt->pending = 0; - - return NL_STOP; -} -static int cb_error_handler(_unused struct sockaddr_nl *nla, struct nlmsgerr *err, - void *arg) -{ - struct addr_info *ctxt = (struct addr_info *)arg; - - ctxt->pending = 0; - ctxt->ret = err->error; - - return NL_STOP; -} - -// 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) -{ - struct nl_msg *msg; - struct ifaddrmsg ifa = { - .ifa_family = AF_INET6, - .ifa_prefixlen = 0, - .ifa_flags = 0, - .ifa_scope = 0, - .ifa_index = ifindex, }; - struct nl_cb *cb = nl_cb_alloc(NL_CB_DEFAULT); - struct addr_info ctxt = { - .ifindex = ifindex, - .addrs = addrs, - .addrs_sz = cnt, - .ret = 0, - .pending = 1, - }; - - if (!cb) { - ctxt.ret = -1; - goto out; - } - - msg = nlmsg_alloc_simple(RTM_GETADDR, NLM_F_REQUEST | NLM_F_DUMP); - - if (!msg) { - ctxt.ret = - 1; - goto out; - } - - nlmsg_append(msg, &ifa, sizeof(ifa), 0); - - nl_cb_set(cb, NL_CB_VALID, NL_CB_CUSTOM, cb_valid_handler, &ctxt); - nl_cb_set(cb, NL_CB_FINISH, NL_CB_CUSTOM, cb_finish_handler, &ctxt); - nl_cb_err(cb, NL_CB_CUSTOM, cb_error_handler, &ctxt); - - nl_send_auto_complete(rtnl_socket, msg); - while (ctxt.pending > 0) - nl_recvmsgs(rtnl_socket, cb); - - nlmsg_free(msg); -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,74 +212,49 @@ int odhcpd_get_linklocal_interface_address(int ifindex, struct in6_addr *lladdr) return status; } -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) -{ - struct nl_msg *msg; - struct rtmsg rtm = { - .rtm_family = AF_INET6, - .rtm_dst_len = prefixlen, - .rtm_src_len = 0, - .rtm_table = RT_TABLE_MAIN, - .rtm_protocol = (add ? RTPROT_STATIC : RTPROT_UNSPEC), - .rtm_scope = (add ? (gw ? RT_SCOPE_UNIVERSE : RT_SCOPE_LINK) : RT_SCOPE_NOWHERE), - .rtm_type = (add ? RTN_UNICAST : RTN_UNSPEC), - }; - int ret = 0; - - msg = nlmsg_alloc_simple(add ? RTM_NEWROUTE : RTM_DELROUTE, - add ? NLM_F_CREATE | NLM_F_REPLACE : 0); - if (!msg) - return -1; - - nlmsg_append(msg, &rtm, sizeof(rtm), 0); - - nla_put(msg, RTA_DST, sizeof(*addr), addr); - nla_put_u32(msg, RTA_OIF, iface->ifindex); - nla_put_u32(msg, RTA_PRIORITY, metric); - - if (gw) - nla_put(msg, RTA_GATEWAY, sizeof(*gw), gw); - - ret = nl_send_auto_complete(rtnl_socket, msg); - nlmsg_free(msg); - - if (ret < 0) - return ret; - - return nl_wait_for_ack(rtnl_socket); -} - -int odhcpd_setup_proxy_neigh(const struct in6_addr *addr, - const struct interface *iface, const bool add) +/* + * 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) { - struct nl_msg *msg; - struct ndmsg ndm = { - .ndm_family = AF_INET6, - .ndm_flags = NTF_PROXY, - .ndm_ifindex = iface->ifindex, - }; - int ret = 0, flags = NLM_F_REQUEST; + time_t now = odhcpd_time(); + ssize_t m = -1; - if (add) - flags |= NLM_F_REPLACE | NLM_F_CREATE; + for (size_t i = 0; i < iface->ia_addr_len; ++i) { + if (iface->ia_addr[i].valid <= (uint32_t)now) + continue; - msg = nlmsg_alloc_simple(add ? RTM_NEWNEIGH : RTM_DELNEIGH, flags); - if (!msg) - return -1; + if (m < 0) { + m = i; + continue; + } - nlmsg_append(msg, &ndm, sizeof(ndm), 0); + if (iface->ia_addr[m].preferred >= (uint32_t)now && + iface->ia_addr[i].preferred < (uint32_t)now) + continue; - nla_put(msg, NDA_DST, sizeof(*addr), addr); + 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; - ret = nl_send_auto_complete(rtnl_socket, msg); - nlmsg_free(msg); + if (iface->ia_addr[i].preferred > iface->ia_addr[m].preferred) + m = i; + } - if (ret < 0) - return ret; + if (m >= 0) { + *addr = iface->ia_addr[m].addr.in6; + return 0; + } - return nl_wait_for_ack(rtnl_socket); + return odhcpd_get_linklocal_interface_address(iface->ifindex, addr); } struct interface* odhcpd_get_interface_by_index(int ifindex) @@ -462,7 +295,7 @@ static void odhcpd_receive_packets(struct uloop_fd *u, _unused unsigned int even { struct odhcpd_event *e = container_of(u, struct odhcpd_event, uloop); - uint8_t data_buf[RELAYD_BUFFER_SIZE], cmsg_buf[128]; + uint8_t data_buf[8192], cmsg_buf[128]; union { struct sockaddr_in6 in6; struct sockaddr_in in; @@ -566,6 +399,12 @@ int odhcpd_register(struct odhcpd_event *event) ((event->handle_error) ? ULOOP_ERROR_CB : 0)); } +int odhcpd_deregister(struct odhcpd_event *event) +{ + event->uloop.cb = NULL; + return uloop_fd_delete(&event->uloop); +} + void odhcpd_process(struct odhcpd_event *event) { odhcpd_receive_packets(&event->uloop, 0); @@ -654,3 +493,54 @@ void odhcpd_bmemcpy(void *av, const void *bv, size_t bits) a[bytes] = (a[bytes] & mask) | ((~mask) & b[bytes]); } } + + +int odhcpd_netmask2bitlen(bool inet6, void *mask) +{ + int bits; + struct in_addr *v4; + struct in6_addr *v6; + + if (inet6) + for (bits = 0, v6 = mask; + bits < 128 && (v6->s6_addr[bits / 8] << (bits % 8)) & 128; + bits++); + else + for (bits = 0, v4 = mask; + bits < 32 && (ntohl(v4->s_addr) << bits) & 0x80000000; + bits++); + + return bits; +} + +bool odhcpd_bitlen2netmask(bool inet6, unsigned int bits, void *mask) +{ + uint8_t b; + struct in_addr *v4; + struct in6_addr *v6; + + if (inet6) + { + if (bits > 128) + return false; + + v6 = mask; + + for (unsigned int i = 0; i < sizeof(v6->s6_addr); i++) + { + b = (bits > 8) ? 8 : bits; + v6->s6_addr[i] = (uint8_t)(0xFF << (8 - b)); + bits -= b; + } + } + else + { + if (bits > 32) + return false; + + v4 = mask; + v4->s_addr = bits ? htonl(~((1 << (32 - bits)) - 1)) : 0; + } + + return true; +}