diff options
author | Hans Dedecker <dedeckeh@gmail.com> | 2017-10-13 15:35:29 +0200 |
---|---|---|
committer | Hans Dedecker <dedeckeh@gmail.com> | 2017-10-16 14:01:15 +0200 |
commit | 24cdc1b59f00a065dd1cf0a04145ca6aaf6f23f1 (patch) | |
tree | 26872cb650fab1ca4cae0acc4b8cd60ad347237b | |
parent | 5dfb716333ad12b86312ef5d8c824ea1ed3df26b (diff) |
treewide: add netlink file
First step in moving netlink code to netlink.c by
moving netlink code from odhcpd.c to netlink.c
Signed-off-by: Hans Dedecker <dedeckeh@gmail.com>
-rw-r--r-- | CMakeLists.txt | 2 | ||||
-rw-r--r-- | src/config.c | 4 | ||||
-rw-r--r-- | src/dhcpv4.c | 4 | ||||
-rw-r--r-- | src/dhcpv6-ia.c | 2 | ||||
-rw-r--r-- | src/ndp.c | 16 | ||||
-rw-r--r-- | src/netlink.c | 392 | ||||
-rw-r--r-- | src/odhcpd.c | 356 | ||||
-rw-r--r-- | src/odhcpd.h | 24 |
8 files changed, 424 insertions, 376 deletions
diff --git a/CMakeLists.txt b/CMakeLists.txt index 0855458..839f47f 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -25,7 +25,7 @@ if(${UBUS}) set(EXT_LINK ${EXT_LINK} ubus) endif(${UBUS}) -add_executable(odhcpd src/odhcpd.c src/config.c src/router.c src/dhcpv6.c src/ndp.c src/dhcpv6-ia.c src/dhcpv4.c ${EXT_SRC}) +add_executable(odhcpd src/odhcpd.c src/config.c src/router.c src/dhcpv6.c src/ndp.c src/dhcpv6-ia.c src/dhcpv4.c src/netlink.c ${EXT_SRC}) target_link_libraries(odhcpd resolv ubox uci ${libnl} ${EXT_LINK}) # Installation diff --git a/src/config.c b/src/config.c index 597d5c5..2f51d33 100644 --- a/src/config.c +++ b/src/config.c @@ -449,13 +449,13 @@ int config_parse_interface(void *data, size_t len, const char *name, bool overwr } if (get_addrs) { - ssize_t len = odhcpd_get_interface_addresses(iface->ifindex, + ssize_t len = netlink_get_interface_addrs(iface->ifindex, true, &iface->ia_addr); if (len > 0) iface->ia_addr_len = len; - len = odhcpd_get_interface_addresses(iface->ifindex, + len = netlink_get_interface_addrs(iface->ifindex, false, &iface->addr4); if (len > 0) iface->addr4_len = len; diff --git a/src/dhcpv4.c b/src/dhcpv4.c index bfe70cb..c8be632 100644 --- a/src/dhcpv4.c +++ b/src/dhcpv4.c @@ -293,7 +293,7 @@ static void decr_ref_cnt_ip(struct odhcpd_ref_ip **ptr, struct interface *iface) struct odhcpd_ref_ip *ip = *ptr; if (--ip->ref_cnt == 0) { - odhcpd_setup_addr(&ip->addr, iface, false, false); + netlink_setup_addr(&ip->addr, iface, false, false); list_del(&ip->head); free(ip); @@ -373,7 +373,7 @@ void dhcpv4_addr_update(struct interface *iface) a = list_first_entry(&iface->dhcpv4_fr_ips, struct odhcpd_ref_ip, head); - if (odhcpd_setup_addr(&a->addr, iface, false, true)) { + if (netlink_setup_addr(&a->addr, iface, false, true)) { syslog(LOG_ERR, "Failed to add ip address"); return; } diff --git a/src/dhcpv6-ia.c b/src/dhcpv6-ia.c index 1a584c5..6568c69 100644 --- a/src/dhcpv6-ia.c +++ b/src/dhcpv6-ia.c @@ -454,7 +454,7 @@ static void apply_lease(struct interface *iface, struct dhcpv6_assignment *a, bo struct in6_addr prefix = addrs[i].addr.in6; prefix.s6_addr32[1] |= htonl(a->assigned); prefix.s6_addr32[2] = prefix.s6_addr32[3] = 0; - odhcpd_setup_route(&prefix, (a->managed_size) ? addrs[i].prefix : a->length, + netlink_setup_route(&prefix, (a->managed_size) ? addrs[i].prefix : a->length, iface, &a->peer.sin6_addr, 1024, add); } } @@ -78,7 +78,7 @@ int ndp_init(void) { int val = 2; - rtnl_event.sock = odhcpd_create_nl_socket(NETLINK_ROUTE); + rtnl_event.sock = netlink_create_socket(NETLINK_ROUTE); if (!rtnl_event.sock) goto err; @@ -265,9 +265,9 @@ static void ping6(struct in6_addr *addr, inet_ntop(AF_INET6, addr, ipbuf, sizeof(ipbuf)); syslog(LOG_NOTICE, "Pinging for %s%%%s", ipbuf, iface->ifname); - odhcpd_setup_route(addr, 128, iface, NULL, 128, true); + netlink_setup_route(addr, 128, iface, NULL, 128, true); odhcpd_send(ping_socket, &dest, &iov, 1, iface); - odhcpd_setup_route(addr, 128, iface, NULL, 128, false); + netlink_setup_route(addr, 128, iface, NULL, 128, false); } // Handle solicitations @@ -321,14 +321,14 @@ static void setup_route(struct in6_addr *addr, struct interface *iface, bool add (add) ? "Learned" : "Forgot", ipbuf, iface->ifname); if (iface->learn_routes) - odhcpd_setup_route(addr, 128, iface, NULL, 1024, add); + netlink_setup_route(addr, 128, iface, NULL, 1024, add); } // Check address update static void check_addr_updates(struct interface *iface) { struct odhcpd_ipaddr *addr = NULL; - ssize_t len = odhcpd_get_interface_addresses(iface->ifindex, false, &addr); + ssize_t len = netlink_get_interface_addrs(iface->ifindex, false, &addr); if (len < 0) return; @@ -350,7 +350,7 @@ static void check_addr_updates(struct interface *iface) static void check_addr6_updates(struct interface *iface) { struct odhcpd_ipaddr *addr = NULL; - ssize_t len = odhcpd_get_interface_addresses(iface->ifindex, true, &addr); + ssize_t len = netlink_get_interface_addrs(iface->ifindex, true, &addr); if (len < 0) return; @@ -390,7 +390,7 @@ static void setup_addr_for_relaying(struct in6_addr *addr, struct interface *ifa bool neigh_add = (c->ndp == MODE_RELAY ? add : false); - if (odhcpd_setup_proxy_neigh(addr, c, neigh_add)) + if (netlink_setup_proxy_neigh(addr, c, neigh_add)) syslog(LOG_DEBUG, "Failed to %s proxy neighbour entry %s%%%s", neigh_add ? "add" : "delete", ipbuf, c->ifname); else @@ -543,7 +543,7 @@ static int cb_rtnl_valid(struct nl_msg *msg, _unused void *arg) if (ndm->ndm_flags & NTF_PROXY) { /* Dump and flush proxy entries */ if (hdr->nlmsg_type == RTM_NEWNEIGH) { - odhcpd_setup_proxy_neigh(addr6, iface, false); + netlink_setup_proxy_neigh(addr6, iface, false); setup_route(addr6, iface, false); dump_neigh_table(false); } diff --git a/src/netlink.c b/src/netlink.c new file mode 100644 index 0000000..485fd57 --- /dev/null +++ b/src/netlink.c @@ -0,0 +1,392 @@ +/** + * Copyright (C) 2017 Hans Dedecker <dedeckeh@gmail.com> + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License v2 as published by + * the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + */ + +#include <errno.h> +#include <string.h> +#include <syslog.h> + +#include <linux/netlink.h> +#include <linux/if_addr.h> +#include <linux/neighbour.h> +#include <linux/rtnetlink.h> + +#include <netlink/msg.h> +#include <netlink/socket.h> +#include <netlink/attr.h> + +#include "odhcpd.h" + +static struct nl_sock *rtnl_socket = NULL; + + +int netlink_init(void) +{ + if (!(rtnl_socket = netlink_create_socket(NETLINK_ROUTE))) { + syslog(LOG_ERR, "Unable to open nl socket: %s", strerror(errno)); + return -1; + } + + return 0; +} + + +struct nl_sock *netlink_create_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; +} + + +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; + int ret = 0; + + if (a->prefix == b->prefix) { + ret = (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; + } else + ret = a->prefix < b->prefix ? 1 : -1; + + return ret; +} + + +// 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 netlink_get_interface_addrs(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; +} + + +int netlink_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 netlink_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); +} + + +int netlink_setup_addr(struct odhcpd_ipaddr *addr, + const struct interface *iface, const bool v6, + const bool add) +{ + struct nl_msg *msg; + struct ifaddrmsg ifa = { + .ifa_family = v6 ? AF_INET6 : AF_INET, + .ifa_prefixlen = addr->prefix, + .ifa_flags = 0, + .ifa_scope = 0, + .ifa_index = iface->ifindex, }; + int ret = 0, flags = NLM_F_REQUEST; + + if (add) + flags |= NLM_F_REPLACE | NLM_F_CREATE; + + msg = nlmsg_alloc_simple(add ? RTM_NEWADDR : RTM_DELADDR, 0); + if (!msg) + return -1; + + nlmsg_append(msg, &ifa, sizeof(ifa), flags); + nla_put(msg, IFA_LOCAL, v6 ? 16 : 4, &addr->addr); + if (v6) { + struct ifa_cacheinfo cinfo = { .ifa_prefered = 0xffffffffU, + .ifa_valid = 0xffffffffU, + .cstamp = 0, + .tstamp = 0 }; + time_t now = odhcpd_time(); + + if (addr->preferred) { + int64_t preferred = addr->preferred - now; + if (preferred < 0) + preferred = 0; + else if (preferred > UINT32_MAX) + preferred = UINT32_MAX; + + cinfo.ifa_prefered = preferred; + } + + if (addr->valid) { + int64_t valid = addr->valid - now; + if (valid <= 0) { + nlmsg_free(msg); + return -1; + } + else if (valid > UINT32_MAX) + valid = UINT32_MAX; + + cinfo.ifa_valid = valid; + } + + nla_put(msg, IFA_CACHEINFO, sizeof(cinfo), &cinfo); + + nla_put_u32(msg, IFA_FLAGS, IFA_F_NOPREFIXROUTE); + } else { + if (addr->broadcast.s_addr) + nla_put_u32(msg, IFA_BROADCAST, addr->broadcast.s_addr); + } + + ret = nl_send_auto_complete(rtnl_socket, msg); + nlmsg_free(msg); + + if (ret < 0) + return ret; + + return nl_wait_for_ack(rtnl_socket); +} diff --git a/src/odhcpd.c b/src/odhcpd.c index 259b623..5f87151 100644 --- a/src/odhcpd.c +++ b/src/odhcpd.c @@ -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> @@ -43,16 +40,12 @@ #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,6 +98,9 @@ int main(int argc, char **argv) signal(SIGINT, sighandler); signal(SIGTERM, sighandler); + if (netlink_init()) + return 4; + if (router_init()) return 4; @@ -124,26 +117,6 @@ int main(int argc, char **argv) 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,187 +193,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; - int ret = 0; - - if (a->prefix == b->prefix) { - ret = (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; - } else - ret = a->prefix < b->prefix ? 1 : -1; - - return ret; -} - -// 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) { @@ -465,144 +257,6 @@ int odhcpd_get_interface_dns_addr(const struct interface *iface, struct in6_addr 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); -} - -int odhcpd_setup_addr(struct odhcpd_ipaddr *addr, - const struct interface *iface, const bool v6, - const bool add) -{ - struct nl_msg *msg; - struct ifaddrmsg ifa = { - .ifa_family = v6 ? AF_INET6 : AF_INET, - .ifa_prefixlen = addr->prefix, - .ifa_flags = 0, - .ifa_scope = 0, - .ifa_index = iface->ifindex, }; - int ret = 0, flags = NLM_F_REQUEST; - - if (add) - flags |= NLM_F_REPLACE | NLM_F_CREATE; - - msg = nlmsg_alloc_simple(add ? RTM_NEWADDR : RTM_DELADDR, 0); - if (!msg) - return -1; - - nlmsg_append(msg, &ifa, sizeof(ifa), flags); - nla_put(msg, IFA_LOCAL, v6 ? 16 : 4, &addr->addr); - if (v6) { - struct ifa_cacheinfo cinfo = { .ifa_prefered = 0xffffffffU, - .ifa_valid = 0xffffffffU, - .cstamp = 0, - .tstamp = 0 }; - time_t now = odhcpd_time(); - - if (addr->preferred) { - int64_t preferred = addr->preferred - now; - if (preferred < 0) - preferred = 0; - else if (preferred > UINT32_MAX) - preferred = UINT32_MAX; - - cinfo.ifa_prefered = preferred; - } - - if (addr->valid) { - int64_t valid = addr->valid - now; - if (valid <= 0) { - nlmsg_free(msg); - return -1; - } - else if (valid > UINT32_MAX) - valid = UINT32_MAX; - - cinfo.ifa_valid = valid; - } - - nla_put(msg, IFA_CACHEINFO, sizeof(cinfo), &cinfo); - - nla_put_u32(msg, IFA_FLAGS, IFA_F_NOPREFIXROUTE); - } else { - if (addr->broadcast.s_addr) - nla_put_u32(msg, IFA_BROADCAST, addr->broadcast.s_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; diff --git a/src/odhcpd.h b/src/odhcpd.h index 2d9e474..0ae36c3 100644 --- a/src/odhcpd.h +++ b/src/odhcpd.h @@ -226,12 +226,9 @@ int odhcpd_register(struct odhcpd_event *event); int odhcpd_deregister(struct odhcpd_event *event); void odhcpd_process(struct odhcpd_event *event); -struct nl_sock *odhcpd_create_nl_socket(int protocol); ssize_t odhcpd_send(int socket, struct sockaddr_in6 *dest, struct iovec *iov, size_t iov_len, const struct interface *iface); -ssize_t odhcpd_get_interface_addresses(int ifindex, bool v6, - struct odhcpd_ipaddr **addrs); int odhcpd_get_interface_dns_addr(const struct interface *iface, struct in6_addr *addr); struct interface* odhcpd_get_interface_by_name(const char *name); @@ -240,14 +237,6 @@ int odhcpd_get_mac(const struct interface *iface, uint8_t mac[6]); struct interface* odhcpd_get_interface_by_index(int ifindex); struct interface* odhcpd_get_master_interface(void); int odhcpd_urandom(void *data, size_t len); -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); -int odhcpd_setup_proxy_neigh(const struct in6_addr *addr, - const struct interface *iface, const bool add); -int odhcpd_setup_addr(struct odhcpd_ipaddr *addr, - const struct interface *iface, const bool v6, - const bool add); void odhcpd_run(void); time_t odhcpd_time(void); @@ -271,8 +260,21 @@ void ubus_apply_network(void); bool ubus_has_prefix(const char *name, const char *ifname); #endif +struct nl_sock *netlink_create_socket(int protocol); +ssize_t netlink_get_interface_addrs(int ifindex, bool v6, + struct odhcpd_ipaddr **addrs); +int netlink_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); +int netlink_setup_proxy_neigh(const struct in6_addr *addr, + const struct interface *iface, const bool add); +int netlink_setup_addr(struct odhcpd_ipaddr *addr, + const struct interface *iface, const bool v6, + const bool add); + // Exported module initializers +int netlink_init(void); int router_init(void); int dhcpv6_init(void); int dhcpv4_init(void); |