dhcpv4: make DHCPv4 support compiletime configurable
[project/odhcpd.git] / src / odhcpd.c
index 837004b..13697b7 100644 (file)
@@ -32,9 +32,6 @@
 #include <netinet/ip6.h>
 #include <netpacket/packet.h>
 #include <linux/netlink.h>
-#include <linux/if_addr.h>
-#include <linux/neighbour.h>
-#include <linux/rtnetlink.h>
 
 #include <sys/socket.h>
 #include <sys/ioctl.h>
 #include <sys/wait.h>
 #include <sys/syscall.h>
 
-#include <netlink/msg.h>
-#include <netlink/socket.h>
-#include <netlink/attr.h>
 #include <libubox/uloop.h>
 #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)
@@ -60,6 +53,7 @@ static void sighandler(_unused int signal)
        uloop_end();
 }
 
+
 static void print_usage(const char *app)
 {
        printf(
@@ -70,6 +64,7 @@ static void print_usage(const char *app)
        );
 }
 
+
 int main(int argc, char **argv)
 {
        openlog("odhcpd", LOG_PERROR | LOG_PID, LOG_DAEMON);
@@ -96,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;
 
@@ -108,42 +98,27 @@ int main(int argc, char **argv)
        signal(SIGINT, sighandler);
        signal(SIGTERM, sighandler);
 
-       if (init_router())
+       if (netlink_init())
+               return 4;
+
+       if (router_init())
                return 4;
 
-       if (init_dhcpv6())
+       if (dhcpv6_init())
                return 4;
 
-       if (init_ndp())
+       if (ndp_init())
                return 4;
 
-       if (init_dhcpv4())
+#ifdef DHCPV4_SUPPORT
+       if (dhcpv4_init())
                return 4;
+#endif
 
        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)
@@ -220,180 +195,6 @@ ssize_t odhcpd_send(int socket, struct sockaddr_in6 *dest,
        return sent;
 }
 
-struct addr_info {
-       int ifindex;
-       int af;
-       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], *nla_addr = NULL;
-
-       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;
-
-       nlmsg_parse(hdr, sizeof(*ifa), nla, __IFA_MAX - 1, NULL);
-
-       switch (ifa->ifa_family) {
-       case AF_INET6:
-               if (nla[IFA_ADDRESS])
-                       nla_addr = nla[IFA_ADDRESS];
-               break;
-
-       case AF_INET:
-               if (nla[IFA_LOCAL])
-                       nla_addr = nla[IFA_LOCAL];
-               break;
-
-       default:
-               break;
-       }
-       if (!nla_addr)
-               return NL_SKIP;
-
-       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(&addrs[ctxt->ret].addr, nla_addr,
-                       sizeof(addrs[ctxt->ret].addr));
-
-       if (nla[IFA_BROADCAST])
-               nla_memcpy(&addrs[ctxt->ret].broadcast, nla[IFA_BROADCAST],
-                               sizeof(addrs[ctxt->ret].broadcast));
-
-       if (nla[IFA_CACHEINFO]) {
-               struct ifa_cacheinfo *ifc = nla_data(nla[IFA_CACHEINFO]);
-
-               addrs[ctxt->ret].preferred = ifc->ifa_prefered;
-               addrs[ctxt->ret].valid = ifc->ifa_valid;
-       }
-
-       if (ifa->ifa_flags & IFA_F_DEPRECATED)
-               addrs[ctxt->ret].preferred = 0;
-
-       ctxt->ret++;
-       *(ctxt->addrs) = addrs;
-
-       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;
-}
-
-static int prefix_cmp(const void *va, const void *vb)
-{
-       const struct odhcpd_ipaddr *a = va, *b = vb;
-       return (ntohl(a->addr.in.s_addr) < ntohl(b->addr.in.s_addr)) ? 1 :
-               (ntohl(a->addr.in.s_addr) > ntohl(b->addr.in.s_addr)) ? -1 : 0;
-}
-
-// compare IPv6 prefixes
-static int prefix6_cmp(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, bool v6, struct odhcpd_ipaddr **addrs)
-{
-       struct nl_msg *msg;
-       struct ifaddrmsg ifa = {
-               .ifa_family = v6? AF_INET6: AF_INET,
-               .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,
-               .af = v6? AF_INET6: AF_INET,
-               .addrs = addrs,
-               .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);
-
-       if (ctxt.ret <= 0)
-               goto out;
-
-       time_t now = odhcpd_time();
-       struct odhcpd_ipaddr *addr = *addrs;
-
-       qsort(addr, ctxt.ret, sizeof(*addr), v6 ? prefix6_cmp : prefix_cmp);
-
-       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;
-}
 
 static int odhcpd_get_linklocal_interface_address(int ifindex, struct in6_addr *lladdr)
 {
@@ -425,8 +226,8 @@ int odhcpd_get_interface_dns_addr(const struct interface *iface, struct in6_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)
+       for (size_t i = 0; i < iface->addr6_len; ++i) {
+               if (iface->addr6[i].valid <= (uint32_t)now)
                        continue;
 
                if (m < 0) {
@@ -434,100 +235,30 @@ int odhcpd_get_interface_dns_addr(const struct interface *iface, struct in6_addr
                        continue;
                }
 
-               if (iface->ia_addr[m].preferred >= (uint32_t)now &&
-                               iface->ia_addr[i].preferred < (uint32_t)now)
+               if (iface->addr6[m].preferred >= (uint32_t)now &&
+                               iface->addr6[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)) {
+               if (IN6_IS_ADDR_ULA(&iface->addr6[i].addr.in6)) {
+                       if (!IN6_IS_ADDR_ULA(&iface->addr6[m].addr.in6)) {
                                m = i;
                                continue;
                        }
-               } else if (IN6_IS_ADDR_ULA(&iface->ia_addr[m].addr.in6))
+               } else if (IN6_IS_ADDR_ULA(&iface->addr6[m].addr.in6))
                        continue;
 
-               if (iface->ia_addr[i].preferred > iface->ia_addr[m].preferred)
+               if (iface->addr6[i].preferred > iface->addr6[m].preferred)
                        m = i;
        }
 
        if (m >= 0) {
-               *addr = iface->ia_addr[m].addr.in6;
+               *addr = iface->addr6[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)
-{
-       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)
-{
-       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;
-
-       if (add)
-               flags |= NLM_F_REPLACE | NLM_F_CREATE;
-
-       msg = nlmsg_alloc_simple(add ? RTM_NEWNEIGH : RTM_DELNEIGH, flags);
-       if (!msg)
-               return -1;
-
-       nlmsg_append(msg, &ndm, sizeof(ndm), 0);
-
-       nla_put(msg, NDA_DST, sizeof(*addr), addr);
-
-       ret = nl_send_auto_complete(rtnl_socket, msg);
-       nlmsg_free(msg);
-
-       if (ret < 0)
-               return ret;
-
-       return nl_wait_for_ack(rtnl_socket);
-}
-
 struct interface* odhcpd_get_interface_by_index(int ifindex)
 {
        struct interface *iface;
@@ -764,3 +495,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;
+}