summaryrefslogtreecommitdiff
path: root/src/libsystemd-rtnl
diff options
context:
space:
mode:
authorTom Gundersen <teg@jklm.no>2014-01-13 20:14:44 +0100
committerTom Gundersen <teg@jklm.no>2014-01-13 21:06:13 +0100
commit0b54473e9b73d867ed7807507a0fb5adc8137b10 (patch)
tree0786f659f1b389adb5386636c4a1a9d49b7c2fab /src/libsystemd-rtnl
parentc813ca40c859ff8abc8bc6aabc3f1e896623eb67 (diff)
libsystemd-rtnl: merge into libsystemd
Diffstat (limited to 'src/libsystemd-rtnl')
l---------src/libsystemd-rtnl/Makefile1
-rw-r--r--src/libsystemd-rtnl/rtnl-internal.h99
-rw-r--r--src/libsystemd-rtnl/rtnl-message.c887
-rw-r--r--src/libsystemd-rtnl/rtnl-util.c100
-rw-r--r--src/libsystemd-rtnl/rtnl-util.h36
-rw-r--r--src/libsystemd-rtnl/sd-rtnl.c870
-rw-r--r--src/libsystemd-rtnl/test-rtnl.c330
7 files changed, 0 insertions, 2323 deletions
diff --git a/src/libsystemd-rtnl/Makefile b/src/libsystemd-rtnl/Makefile
deleted file mode 120000
index d0b0e8e008..0000000000
--- a/src/libsystemd-rtnl/Makefile
+++ /dev/null
@@ -1 +0,0 @@
-../Makefile \ No newline at end of file
diff --git a/src/libsystemd-rtnl/rtnl-internal.h b/src/libsystemd-rtnl/rtnl-internal.h
deleted file mode 100644
index 2e0b7b8f37..0000000000
--- a/src/libsystemd-rtnl/rtnl-internal.h
+++ /dev/null
@@ -1,99 +0,0 @@
-/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
-
-#pragma once
-
-/***
- This file is part of systemd.
-
- Copyright 2013 Tom Gundersen <teg@jklm.no>
-
- systemd is free software; you can redistribute it and/or modify it
- under the terms of the GNU Lesser General Public License as published by
- the Free Software Foundation; either version 2.1 of the License, or
- (at your option) any later version.
-
- systemd 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
- Lesser General Public License for more details.
-
- You should have received a copy of the GNU Lesser General Public License
- along with systemd; If not, see <http://www.gnu.org/licenses/>.
-***/
-
-#include <linux/netlink.h>
-
-#include "refcnt.h"
-#include "prioq.h"
-#include "list.h"
-
-#include "sd-rtnl.h"
-
-struct reply_callback {
- sd_rtnl_message_handler_t callback;
- void *userdata;
- usec_t timeout;
- uint64_t serial;
- unsigned prioq_idx;
-};
-
-struct match_callback {
- sd_rtnl_message_handler_t callback;
- uint16_t type;
- void *userdata;
-
- LIST_FIELDS(struct match_callback, match_callbacks);
-};
-
-struct sd_rtnl {
- RefCount n_ref;
-
- int fd;
-
- union {
- struct sockaddr sa;
- struct sockaddr_nl nl;
- } sockaddr;
-
- sd_rtnl_message **rqueue;
- unsigned rqueue_size;
-
- sd_rtnl_message **wqueue;
- unsigned wqueue_size;
-
- bool processing:1;
-
- uint32_t serial;
-
- struct Prioq *reply_callbacks_prioq;
- Hashmap *reply_callbacks;
-
- LIST_HEAD(struct match_callback, match_callbacks);
-
- pid_t original_pid;
-
- sd_event_source *io_event_source;
- sd_event_source *time_event_source;
- sd_event_source *exit_event_source;
- sd_event *event;
-};
-
-#define RTNL_DEFAULT_TIMEOUT ((usec_t) (10 * USEC_PER_SEC))
-
-#define RTNL_WQUEUE_MAX 1024
-#define RTNL_RQUEUE_MAX 64*1024
-
-int message_new_synthetic_error(int error, uint32_t serial, sd_rtnl_message **ret);
-uint32_t message_get_serial(sd_rtnl_message *m);
-int message_seal(sd_rtnl *nl, sd_rtnl_message *m);
-
-bool message_type_is_link(uint16_t type);
-bool message_type_is_addr(uint16_t type);
-bool message_type_is_route(uint16_t type);
-
-int socket_write_message(sd_rtnl *nl, sd_rtnl_message *m);
-int socket_read_message(sd_rtnl *nl, sd_rtnl_message **ret);
-
-/* Make sure callbacks don't destroy the rtnl connection */
-#define RTNL_DONT_DESTROY(rtnl) \
- _cleanup_sd_rtnl_unref_ _unused_ sd_rtnl *_dont_destroy_##rtnl = sd_rtnl_ref(rtnl)
diff --git a/src/libsystemd-rtnl/rtnl-message.c b/src/libsystemd-rtnl/rtnl-message.c
deleted file mode 100644
index 517df61156..0000000000
--- a/src/libsystemd-rtnl/rtnl-message.c
+++ /dev/null
@@ -1,887 +0,0 @@
-/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
-
-/***
- This file is part of systemd.
-
- Copyright 2013 Tom Gundersen <teg@jklm.no>
-
- systemd is free software; you can redistribute it and/or modify it
- under the terms of the GNU Lesser General Public License as published by
- the Free Software Foundation; either version 2.1 of the License, or
- (at your option) any later version.
-
- systemd 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
- Lesser General Public License for more details.
-
- You should have received a copy of the GNU Lesser General Public License
- along with systemd; If not, see <http://www.gnu.org/licenses/>.
-***/
-
-#include <linux/rtnetlink.h>
-#include <netinet/in.h>
-#include <netinet/ether.h>
-#include <stdbool.h>
-#include <unistd.h>
-
-#include "util.h"
-#include "refcnt.h"
-
-#include "sd-rtnl.h"
-#include "rtnl-internal.h"
-
-struct sd_rtnl_message {
- RefCount n_ref;
-
- struct nlmsghdr *hdr;
- size_t container_offset; /* offset from hdr to container start */
- size_t next_rta_offset; /* offset from hdr to next rta */
-
- bool sealed:1;
-};
-
-#define CURRENT_CONTAINER(m) ((m)->container_offset ? (struct rtattr*)((uint8_t*)(m)->hdr + (m)->container_offset) : NULL)
-#define NEXT_RTA(m) ((struct rtattr*)((uint8_t*)(m)->hdr + (m)->next_rta_offset))
-#define UPDATE_RTA(m, new) (m)->next_rta_offset = (uint8_t*)(new) - (uint8_t*)(m)->hdr;
-
-static int message_new(sd_rtnl_message **ret, size_t initial_size) {
- sd_rtnl_message *m;
-
- assert_return(ret, -EINVAL);
- assert_return(initial_size >= sizeof(struct nlmsghdr), -EINVAL);
-
- m = new0(sd_rtnl_message, 1);
- if (!m)
- return -ENOMEM;
-
- m->hdr = malloc0(initial_size);
- if (!m->hdr) {
- free(m);
- return -ENOMEM;
- }
-
- m->n_ref = REFCNT_INIT;
-
- m->hdr->nlmsg_flags = NLM_F_REQUEST | NLM_F_ACK;
- m->sealed = false;
-
- *ret = m;
-
- return 0;
-}
-
-int message_new_synthetic_error(int error, uint32_t serial, sd_rtnl_message **ret) {
- struct nlmsgerr *err;
- int r;
-
- assert(error <= 0);
-
- r = message_new(ret, NLMSG_SPACE(sizeof(struct nlmsgerr)));
- if (r < 0)
- return r;
-
- (*ret)->hdr->nlmsg_len = NLMSG_LENGTH(sizeof(struct nlmsgerr));
- (*ret)->hdr->nlmsg_type = NLMSG_ERROR;
- (*ret)->hdr->nlmsg_seq = serial;
-
- err = NLMSG_DATA((*ret)->hdr);
-
- err->error = error;
-
- return 0;
-}
-
-bool message_type_is_route(uint16_t type) {
- switch (type) {
- case RTM_NEWROUTE:
- case RTM_GETROUTE:
- case RTM_DELROUTE:
- return true;
- default:
- return false;
- }
-}
-
-bool message_type_is_link(uint16_t type) {
- switch (type) {
- case RTM_NEWLINK:
- case RTM_SETLINK:
- case RTM_GETLINK:
- case RTM_DELLINK:
- return true;
- default:
- return false;
- }
-}
-
-bool message_type_is_addr(uint16_t type) {
- switch (type) {
- case RTM_NEWADDR:
- case RTM_GETADDR:
- case RTM_DELADDR:
- return true;
- default:
- return false;
- }
-}
-
-int sd_rtnl_message_route_set_dst_prefixlen(sd_rtnl_message *m, unsigned char prefixlen) {
- struct rtmsg *rtm;
-
- rtm = NLMSG_DATA(m->hdr);
-
- rtm->rtm_dst_len = prefixlen;
-
- return 0;
-}
-
-int sd_rtnl_message_route_new(uint16_t nlmsg_type, unsigned char rtm_family,
- sd_rtnl_message **ret) {
- struct rtmsg *rtm;
- int r;
-
- assert_return(message_type_is_route(nlmsg_type), -EINVAL);
- assert_return(rtm_family == AF_INET || rtm_family == AF_INET6, -EINVAL);
- assert_return(ret, -EINVAL);
-
- r = message_new(ret, NLMSG_SPACE(sizeof(struct rtmsg)));
- if (r < 0)
- return r;
-
- (*ret)->hdr->nlmsg_len = NLMSG_LENGTH(sizeof(struct rtmsg));
- (*ret)->hdr->nlmsg_type = nlmsg_type;
- if (nlmsg_type == RTM_NEWROUTE)
- (*ret)->hdr->nlmsg_flags |= NLM_F_CREATE | NLM_F_EXCL;
-
- rtm = NLMSG_DATA((*ret)->hdr);
-
- UPDATE_RTA(*ret, RTM_RTA(rtm));
-
- rtm->rtm_family = rtm_family;
- rtm->rtm_scope = RT_SCOPE_UNIVERSE;
- rtm->rtm_type = RTN_UNICAST;
- rtm->rtm_table = RT_TABLE_MAIN;
- rtm->rtm_protocol = RTPROT_BOOT;
-
- return 0;
-}
-
-int sd_rtnl_message_link_set_flags(sd_rtnl_message *m, unsigned flags) {
- struct ifinfomsg *ifi;
-
- ifi = NLMSG_DATA(m->hdr);
-
- ifi->ifi_flags = flags;
-
- return 0;
-}
-
-int sd_rtnl_message_link_set_type(sd_rtnl_message *m, unsigned type) {
- struct ifinfomsg *ifi;
-
- ifi = NLMSG_DATA(m->hdr);
-
- ifi->ifi_type = type;
-
- return 0;
-}
-
-int sd_rtnl_message_link_new(uint16_t nlmsg_type, int index, sd_rtnl_message **ret) {
- struct ifinfomsg *ifi;
- int r;
-
- assert_return(message_type_is_link(nlmsg_type), -EINVAL);
- assert_return(nlmsg_type == RTM_NEWLINK || index > 0, -EINVAL);
- assert_return(ret, -EINVAL);
-
- r = message_new(ret, NLMSG_SPACE(sizeof(struct ifinfomsg)));
- if (r < 0)
- return r;
-
- (*ret)->hdr->nlmsg_len = NLMSG_LENGTH(sizeof(struct ifinfomsg));
- (*ret)->hdr->nlmsg_type = nlmsg_type;
- if (nlmsg_type == RTM_NEWLINK)
- (*ret)->hdr->nlmsg_flags |= NLM_F_CREATE;
-
- ifi = NLMSG_DATA((*ret)->hdr);
-
- ifi->ifi_family = AF_UNSPEC;
- ifi->ifi_index = index;
- ifi->ifi_change = 0xffffffff;
-
- UPDATE_RTA(*ret, IFLA_RTA(ifi));
-
- return 0;
-}
-
-int sd_rtnl_message_addr_new(uint16_t nlmsg_type, int index, unsigned char family, unsigned char prefixlen, unsigned char flags, unsigned char scope, sd_rtnl_message **ret) {
- struct ifaddrmsg *ifa;
- int r;
-
- assert_return(message_type_is_addr(nlmsg_type), -EINVAL);
- assert_return(index > 0, -EINVAL);
- assert_return(ret, -EINVAL);
-
- r = message_new(ret, NLMSG_SPACE(sizeof(struct ifaddrmsg)));
- if (r < 0)
- return r;
-
- (*ret)->hdr->nlmsg_len = NLMSG_LENGTH(sizeof(struct ifaddrmsg));
- (*ret)->hdr->nlmsg_type = nlmsg_type;
-
- ifa = NLMSG_DATA((*ret)->hdr);
-
- ifa->ifa_family = family;
- ifa->ifa_prefixlen = prefixlen;
- ifa->ifa_flags = flags;
- ifa->ifa_scope = scope;
- ifa->ifa_index = index;
-
- UPDATE_RTA(*ret, IFA_RTA(ifa));
-
- return 0;
-}
-
-sd_rtnl_message *sd_rtnl_message_ref(sd_rtnl_message *m) {
- if (m)
- assert_se(REFCNT_INC(m->n_ref) >= 2);
-
- return m;
-}
-
-sd_rtnl_message *sd_rtnl_message_unref(sd_rtnl_message *m) {
- if (m && REFCNT_DEC(m->n_ref) <= 0) {
- free(m->hdr);
- free(m);
- }
-
- return NULL;
-}
-
-int sd_rtnl_message_get_type(sd_rtnl_message *m, uint16_t *type) {
- assert_return(m, -EINVAL);
- assert_return(type, -EINVAL);
-
- *type = m->hdr->nlmsg_type;
-
- return 0;
-}
-
-int sd_rtnl_message_link_get_ifindex(sd_rtnl_message *m, int *ifindex) {
- struct ifinfomsg *ifi;
-
- assert_return(m, -EINVAL);
- assert_return(m->hdr, -EINVAL);
- assert_return(message_type_is_link(m->hdr->nlmsg_type), -EINVAL);
- assert_return(ifindex, -EINVAL);
-
- ifi = NLMSG_DATA(m->hdr);
-
- *ifindex = ifi->ifi_index;
-
- return 0;
-}
-
-int sd_rtnl_message_link_get_flags(sd_rtnl_message *m, unsigned *flags) {
- struct ifinfomsg *ifi;
-
- assert_return(m, -EINVAL);
- assert_return(m->hdr, -EINVAL);
- assert_return(message_type_is_link(m->hdr->nlmsg_type), -EINVAL);
- assert_return(flags, -EINVAL);
-
- ifi = NLMSG_DATA(m->hdr);
-
- *flags = ifi->ifi_flags;
-
- return 0;
-}
-
-/* If successful the updated message will be correctly aligned, if
- unsuccessful the old message is untouched. */
-static int add_rtattr(sd_rtnl_message *m, unsigned short type, const void *data, size_t data_length) {
- uint32_t rta_length, message_length;
- struct nlmsghdr *new_hdr;
- struct rtattr *rta;
- char *padding;
-
- assert(m);
- assert(m->hdr);
- assert(NLMSG_ALIGN(m->hdr->nlmsg_len) == m->hdr->nlmsg_len);
- assert(!data || data_length > 0);
-
- /* get the size of the new rta attribute (with padding at the end) */
- rta_length = RTA_LENGTH(data_length);
-
- /* get the new message size (with padding at the end) */
- message_length = m->hdr->nlmsg_len + RTA_ALIGN(rta_length);
-
- /* realloc to fit the new attribute */
- new_hdr = realloc(m->hdr, message_length);
- if (!new_hdr)
- return -ENOMEM;
- m->hdr = new_hdr;
-
- /* get pointer to the attribute we are about to add */
- rta = (struct rtattr *) ((uint8_t *) m->hdr + m->hdr->nlmsg_len);
-
- /* if we are inside a container, extend it */
- if (CURRENT_CONTAINER(m))
- CURRENT_CONTAINER(m)->rta_len += message_length - m->hdr->nlmsg_len;
-
- /* fill in the attribute */
- rta->rta_type = type;
- rta->rta_len = rta_length;
- if (!data) {
- /* this is the start of a new container */
- m->container_offset = m->hdr->nlmsg_len;
- } else {
- /* we don't deal with the case where the user lies about the type
- * and gives us too little data (so don't do that)
- */
- padding = mempcpy(RTA_DATA(rta), data, data_length);
- /* make sure also the padding at the end of the message is initialized */
- memzero(padding,
- (uint8_t *) m->hdr + message_length - (uint8_t *) padding);
- }
-
- /* update message size */
- m->hdr->nlmsg_len = message_length;
-
- return 0;
-}
-
-int sd_rtnl_message_append_string(sd_rtnl_message *m, unsigned short type, const char *data) {
- uint16_t rtm_type;
- int r;
-
- assert_return(m, -EINVAL);
- assert_return(data, -EINVAL);
-
- r = sd_rtnl_message_get_type(m, &rtm_type);
- if (r < 0)
- return r;
-
- /* check that the type is correct */
- switch (rtm_type) {
- case RTM_NEWLINK:
- case RTM_SETLINK:
- case RTM_GETLINK:
- case RTM_DELLINK:
- if (CURRENT_CONTAINER(m)) {
- if (CURRENT_CONTAINER(m)->rta_type != IFLA_LINKINFO ||
- type != IFLA_INFO_KIND)
- return -ENOTSUP;
- } else {
- switch (type) {
- case IFLA_IFNAME:
- case IFLA_IFALIAS:
- case IFLA_QDISC:
- break;
- default:
- return -ENOTSUP;
- }
- }
- break;
- case RTM_NEWADDR:
- case RTM_GETADDR:
- case RTM_DELADDR:
- if (type != IFA_LABEL)
- return -ENOTSUP;
- break;
- default:
- return -ENOTSUP;
- }
-
- r = add_rtattr(m, type, data, strlen(data) + 1);
- if (r < 0)
- return r;
-
- return 0;
-}
-
-int sd_rtnl_message_append_u32(sd_rtnl_message *m, unsigned short type, uint32_t data) {
- uint16_t rtm_type;
- int r;
-
- assert_return(m, -EINVAL);
-
- r = sd_rtnl_message_get_type(m, &rtm_type);
- if (r < 0)
- return r;
-
- /* check that the type is correct */
- switch (rtm_type) {
- case RTM_NEWLINK:
- case RTM_SETLINK:
- case RTM_GETLINK:
- case RTM_DELLINK:
- switch (type) {
- case IFLA_MASTER:
- case IFLA_MTU:
- case IFLA_LINK:
- break;
- default:
- return -ENOTSUP;
- }
- break;
- case RTM_NEWROUTE:
- case RTM_GETROUTE:
- case RTM_DELROUTE:
- switch (type) {
- case RTA_TABLE:
- case RTA_PRIORITY:
- case RTA_IIF:
- case RTA_OIF:
- break;
- default:
- return -ENOTSUP;
- }
- break;
- default:
- return -ENOTSUP;
- }
-
- r = add_rtattr(m, type, &data, sizeof(uint32_t));
- if (r < 0)
- return r;
-
- return 0;
-}
-
-int sd_rtnl_message_append_in_addr(sd_rtnl_message *m, unsigned short type, const struct in_addr *data) {
- struct ifaddrmsg *ifa;
- struct rtmsg *rtm;
- uint16_t rtm_type;
- int r;
-
- assert_return(m, -EINVAL);
- assert_return(data, -EINVAL);
-
- r = sd_rtnl_message_get_type(m, &rtm_type);
- if (r < 0)
- return r;
-
- /* check that the type is correct */
- switch (rtm_type) {
- case RTM_NEWADDR:
- case RTM_GETADDR:
- case RTM_DELADDR:
- switch (type) {
- case IFA_ADDRESS:
- case IFA_LOCAL:
- case IFA_BROADCAST:
- case IFA_ANYCAST:
- ifa = NLMSG_DATA(m->hdr);
-
- if (ifa->ifa_family != AF_INET)
- return -EINVAL;
-
- break;
- default:
- return -ENOTSUP;
- }
- break;
- case RTM_NEWROUTE:
- case RTM_GETROUTE:
- case RTM_DELROUTE:
- switch (type) {
- case RTA_DST:
- case RTA_SRC:
- case RTA_GATEWAY:
- rtm = NLMSG_DATA(m->hdr);
-
- if (rtm->rtm_family != AF_INET)
- return -EINVAL;
-
- break;
- default:
- return -ENOTSUP;
- }
- break;
- default:
- return -ENOTSUP;
- }
-
- r = add_rtattr(m, type, data, sizeof(struct in_addr));
- if (r < 0)
- return r;
-
- return 0;
-}
-
-int sd_rtnl_message_append_in6_addr(sd_rtnl_message *m, unsigned short type, const struct in6_addr *data) {
- struct ifaddrmsg *ifa;
- struct rtmsg *rtm;
- uint16_t rtm_type;
- int r;
-
- assert_return(m, -EINVAL);
- assert_return(data, -EINVAL);
-
- r = sd_rtnl_message_get_type(m, &rtm_type);
- if (r < 0)
- return r;
-
- /* check that the type is correct */
- switch (rtm_type) {
- case RTM_NEWADDR:
- case RTM_GETADDR:
- case RTM_DELADDR:
- switch (type) {
- case IFA_ADDRESS:
- case IFA_LOCAL:
- case IFA_BROADCAST:
- case IFA_ANYCAST:
- ifa = NLMSG_DATA(m->hdr);
-
- if (ifa->ifa_family != AF_INET6)
- return -EINVAL;
-
- break;
- default:
- return -ENOTSUP;
- }
- break;
- case RTM_NEWROUTE:
- case RTM_GETROUTE:
- case RTM_DELROUTE:
- switch (type) {
- case RTA_DST:
- case RTA_SRC:
- case RTA_GATEWAY:
- rtm = NLMSG_DATA(m->hdr);
-
- if (rtm->rtm_family != AF_INET6)
- return -EINVAL;
-
- break;
- default:
- return -ENOTSUP;
- }
- default:
- return -ENOTSUP;
- }
-
- r = add_rtattr(m, type, data, sizeof(struct in6_addr));
- if (r < 0)
- return r;
-
- return 0;
-}
-
-int sd_rtnl_message_append_ether_addr(sd_rtnl_message *m, unsigned short type, const struct ether_addr *data) {
- uint16_t rtm_type;
- int r;
-
- assert_return(m, -EINVAL);
- assert_return(data, -EINVAL);
-
- sd_rtnl_message_get_type(m, &rtm_type);
-
- switch (rtm_type) {
- case RTM_NEWLINK:
- case RTM_SETLINK:
- case RTM_DELLINK:
- case RTM_GETLINK:
- switch (type) {
- case IFLA_ADDRESS:
- case IFLA_BROADCAST:
- break;
- default:
- return -ENOTSUP;
- }
- break;
- default:
- return -ENOTSUP;
- }
-
- r = add_rtattr(m, type, data, ETH_ALEN);
- if (r < 0)
- return r;
-
- return 0;
-}
-
-int sd_rtnl_message_open_container(sd_rtnl_message *m, unsigned short type) {
- uint16_t rtm_type;
-
- assert_return(m, -EINVAL);
- assert_return(!CURRENT_CONTAINER(m), -EINVAL);
-
- sd_rtnl_message_get_type(m, &rtm_type);
-
- if (message_type_is_link(rtm_type)) {
- if (type == IFLA_LINKINFO)
- return add_rtattr(m, type, NULL, 0);
- else
- return -ENOTSUP;
- } else
- return -ENOTSUP;
-
- return 0;
-}
-
-int sd_rtnl_message_close_container(sd_rtnl_message *m) {
- assert_return(m, -EINVAL);
- assert_return(CURRENT_CONTAINER(m), -EINVAL);
-
- m->container_offset = 0;
-
- return 0;
-}
-
-int sd_rtnl_message_read(sd_rtnl_message *m, unsigned short *type, void **data) {
- size_t remaining_size;
- uint16_t rtm_type;
- int r;
-
- assert(m);
- assert(m->next_rta_offset);
- assert(type);
- assert(data);
-
- remaining_size = m->hdr->nlmsg_len - m->next_rta_offset;
-
- if (!RTA_OK(NEXT_RTA(m), remaining_size))
- return 0;
-
- /* make sure we don't try to read a container
- * TODO: add support for entering containers for reading */
- r = sd_rtnl_message_get_type(m, &rtm_type);
- if (r < 0)
- return r;
-
- if (message_type_is_link(rtm_type) &&
- NEXT_RTA(m)->rta_type == IFLA_LINKINFO)
- return -EINVAL;
-
- *data = RTA_DATA(NEXT_RTA(m));
- *type = NEXT_RTA(m)->rta_type;
-
- UPDATE_RTA(m, RTA_NEXT(NEXT_RTA(m), remaining_size));
-
- return 1;
-}
-
-uint32_t message_get_serial(sd_rtnl_message *m) {
- assert(m);
- assert(m->hdr);
-
- return m->hdr->nlmsg_seq;
-}
-
-int sd_rtnl_message_get_errno(sd_rtnl_message *m) {
- struct nlmsgerr *err;
-
- assert_return(m, -EINVAL);
- assert_return(m->hdr, -EINVAL);
-
- if (m->hdr->nlmsg_type != NLMSG_ERROR)
- return 0;
-
- err = NLMSG_DATA(m->hdr);
-
- return err->error;
-}
-
-int message_seal(sd_rtnl *nl, sd_rtnl_message *m) {
- assert(nl);
- assert(m);
- assert(m->hdr);
-
- if (m->sealed)
- return -EPERM;
-
- m->hdr->nlmsg_seq = nl->serial++;
- m->sealed = true;
-
- return 0;
-}
-
-static int message_receive_need(sd_rtnl *rtnl, size_t *need) {
- assert(rtnl);
- assert(need);
-
- /* ioctl(rtnl->fd, FIONREAD, &need)
- Does not appear to work on netlink sockets. libnl uses
- MSG_PEEK instead. I don't know if that is worth the
- extra roundtrip.
-
- For now we simply use the maximum message size the kernel
- may use (NLMSG_GOODSIZE), and then realloc to the actual
- size after reading the message (hence avoiding huge memory
- usage in case many small messages are kept around) */
- *need = page_size();
- if (*need > 8192UL)
- *need = 8192UL;
-
- return 0;
-}
-
-/* returns the number of bytes sent, or a negative error code */
-int socket_write_message(sd_rtnl *nl, sd_rtnl_message *m) {
- union {
- struct sockaddr sa;
- struct sockaddr_nl nl;
- } addr = {
- .nl.nl_family = AF_NETLINK,
- };
- ssize_t k;
-
- assert(nl);
- assert(m);
- assert(m->hdr);
-
- k = sendto(nl->fd, m->hdr, m->hdr->nlmsg_len,
- 0, &addr.sa, sizeof(addr));
- if (k < 0)
- return (errno == EAGAIN) ? 0 : -errno;
-
- return k;
-}
-
-/* On success, the number of bytes received is returned and *ret points to the received message
- * which has a valid header and the correct size.
- * If nothing useful was received 0 is returned.
- * On failure, a negative error code is returned.
- */
-int socket_read_message(sd_rtnl *nl, sd_rtnl_message **ret) {
- sd_rtnl_message *m;
- union {
- struct sockaddr sa;
- struct sockaddr_nl nl;
- } addr;
- socklen_t addr_len;
- int r;
- ssize_t k;
- size_t need;
-
- assert(nl);
- assert(ret);
-
- r = message_receive_need(nl, &need);
- if (r < 0)
- return r;
-
- r = message_new(&m, need);
- if (r < 0)
- return r;
-
- addr_len = sizeof(addr);
-
- k = recvfrom(nl->fd, m->hdr, need,
- 0, &addr.sa, &addr_len);
- if (k < 0)
- k = (errno == EAGAIN) ? 0 : -errno; /* no data */
- else if (k == 0)
- k = -ECONNRESET; /* connection was closed by the kernel */
- else if (addr_len != sizeof(addr.nl) ||
- addr.nl.nl_family != AF_NETLINK)
- k = -EIO; /* not a netlink message */
- else if (addr.nl.nl_pid != 0)
- k = 0; /* not from the kernel */
- else if ((size_t) k < sizeof(struct nlmsghdr) ||
- (size_t) k < m->hdr->nlmsg_len)
- k = -EIO; /* too small (we do accept too big though) */
- else if (m->hdr->nlmsg_pid && m->hdr->nlmsg_pid != nl->sockaddr.nl.nl_pid)
- k = 0; /* not broadcast and not for us */
-
- if (k > 0)
- switch (m->hdr->nlmsg_type) {
- struct ifinfomsg *ifi;
- struct ifaddrmsg *ifa;
- struct rtmsg *rtm;
-
- /* check that the size matches the message type */
- case NLMSG_ERROR:
- if (m->hdr->nlmsg_len < NLMSG_LENGTH(sizeof(struct nlmsgerr)))
- k = -EIO;
- break;
- case RTM_NEWLINK:
- case RTM_SETLINK:
- case RTM_DELLINK:
- case RTM_GETLINK:
- if (m->hdr->nlmsg_len < NLMSG_LENGTH(sizeof(struct ifinfomsg)))
- k = -EIO;
- else {
- ifi = NLMSG_DATA(m->hdr);
- UPDATE_RTA(m, IFLA_RTA(ifi));
- }
- break;
- case RTM_NEWADDR:
- case RTM_DELADDR:
- case RTM_GETADDR:
- if (m->hdr->nlmsg_len < NLMSG_LENGTH(sizeof(struct ifaddrmsg)))
- k = -EIO;
- else {
- ifa = NLMSG_DATA(m->hdr);
- UPDATE_RTA(m, IFA_RTA(ifa));
- }
- break;
- case RTM_NEWROUTE:
- case RTM_DELROUTE:
- case RTM_GETROUTE:
- if (m->hdr->nlmsg_len < NLMSG_LENGTH(sizeof(struct rtmsg)))
- k = -EIO;
- else {
- rtm = NLMSG_DATA(m->hdr);
- UPDATE_RTA(m, RTM_RTA(rtm));
- }
- break;
- case NLMSG_NOOP:
- k = 0;
- break;
- default:
- k = 0; /* ignoring message of unknown type */
- }
-
- if (k <= 0)
- sd_rtnl_message_unref(m);
- else {
- /* we probably allocated way too much memory, give it back */
- m->hdr = realloc(m->hdr, m->hdr->nlmsg_len);
- *ret = m;
- }
-
- return k;
-}
-
-int sd_rtnl_message_rewind(sd_rtnl_message *m) {
- struct ifinfomsg *ifi;
- struct ifaddrmsg *ifa;
- struct rtmsg *rtm;
-
- assert_return(m, -EINVAL);
- assert_return(m->hdr, -EINVAL);
-
- switch(m->hdr->nlmsg_type) {
- case RTM_NEWLINK:
- case RTM_SETLINK:
- case RTM_GETLINK:
- case RTM_DELLINK:
- ifi = NLMSG_DATA(m->hdr);
- UPDATE_RTA(m, IFLA_RTA(ifi));
-
- break;
- case RTM_NEWADDR:
- case RTM_GETADDR:
- case RTM_DELADDR:
- ifa = NLMSG_DATA(m->hdr);
- UPDATE_RTA(m, IFA_RTA(ifa));
-
- break;
- case RTM_NEWROUTE:
- case RTM_GETROUTE:
- case RTM_DELROUTE:
- rtm = NLMSG_DATA(m->hdr);
- UPDATE_RTA(m, RTM_RTA(rtm));
-
- break;
- default:
- return -ENOTSUP;
- }
-
- return 0;
-}
diff --git a/src/libsystemd-rtnl/rtnl-util.c b/src/libsystemd-rtnl/rtnl-util.c
deleted file mode 100644
index dfc0050def..0000000000
--- a/src/libsystemd-rtnl/rtnl-util.c
+++ /dev/null
@@ -1,100 +0,0 @@
-/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
-
-/***
- This file is part of systemd.
-
- Copyright (C) 2013 Tom Gundersen <teg@jklm.no>
-
- systemd is free software; you can redistribute it and/or modify it
- under the terms of the GNU Lesser General Public License as published by
- the Free Software Foundation; either version 2.1 of the License, or
- (at your option) any later version.
-
- systemd 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
- Lesser General Public License for more details.
-
- You should have received a copy of the GNU Lesser General Public License
- along with systemd; If not, see <http://www.gnu.org/licenses/>.
-***/
-
-#include <linux/rtnetlink.h>
-#include <netinet/ether.h>
-
-#include "sd-rtnl.h"
-
-#include "rtnl-util.h"
-
-int rtnl_set_link_name(sd_rtnl *rtnl, int ifindex, const char *name) {
- _cleanup_sd_rtnl_message_unref_ sd_rtnl_message *message = NULL;
- int r;
-
- assert(rtnl);
- assert(ifindex > 0);
- assert(name);
-
- r = sd_rtnl_message_link_new(RTM_SETLINK, ifindex, &message);
- if (r < 0)
- return r;
-
- r = sd_rtnl_message_append_string(message, IFLA_IFNAME, name);
- if (r < 0)
- return r;
-
- r = sd_rtnl_call(rtnl, message, 0, NULL);
- if (r < 0)
- return r;
-
- return 0;
-}
-
-int rtnl_set_link_properties(sd_rtnl *rtnl, int ifindex, const char *alias,
- const struct ether_addr *mac, unsigned mtu) {
- _cleanup_sd_rtnl_message_unref_ sd_rtnl_message *message = NULL;
- bool need_update = false;
- int r;
-
- assert(rtnl);
- assert(ifindex > 0);
-
- if (!alias && !mac && mtu == 0)
- return 0;
-
- r = sd_rtnl_message_link_new(RTM_SETLINK, ifindex, &message);
- if (r < 0)
- return r;
-
- if (alias) {
- r = sd_rtnl_message_append_string(message, IFLA_IFALIAS, alias);
- if (r < 0)
- return r;
-
- need_update = true;
-
- }
-
- if (mac) {
- r = sd_rtnl_message_append_ether_addr(message, IFLA_ADDRESS, mac);
- if (r < 0)
- return r;
-
- need_update = true;
- }
-
- if (mtu > 0) {
- r = sd_rtnl_message_append_u32(message, IFLA_MTU, mtu);
- if (r < 0)
- return r;
-
- need_update = true;
- }
-
- if (need_update) {
- r = sd_rtnl_call(rtnl, message, 0, NULL);
- if (r < 0)
- return r;
- }
-
- return 0;
-}
diff --git a/src/libsystemd-rtnl/rtnl-util.h b/src/libsystemd-rtnl/rtnl-util.h
deleted file mode 100644
index 013002dd60..0000000000
--- a/src/libsystemd-rtnl/rtnl-util.h
+++ /dev/null
@@ -1,36 +0,0 @@
-/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
-
-#pragma once
-
-/***
- This file is part of systemd.
-
- Copyright (C) 2013 Tom Gundersen <teg@jklm.no>
-
- systemd is free software; you can redistribute it and/or modify it
- under the terms of the GNU Lesser General Public License as published by
- the Free Software Foundation; either version 2.1 of the License, or
- (at your option) any later version.
-
- systemd 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
- Lesser General Public License for more details.
-
- You should have received a copy of the GNU Lesser General Public License
- along with systemd; If not, see <http://www.gnu.org/licenses/>.
-***/
-
-#include <netinet/ether.h>
-
-#include "util.h"
-#include "sd-rtnl.h"
-
-int rtnl_set_link_name(sd_rtnl *rtnl, int ifindex, const char *name);
-int rtnl_set_link_properties(sd_rtnl *rtnl, int ifindex, const char *alias, const struct ether_addr *mac, unsigned mtu);
-
-DEFINE_TRIVIAL_CLEANUP_FUNC(sd_rtnl*, sd_rtnl_unref);
-DEFINE_TRIVIAL_CLEANUP_FUNC(sd_rtnl_message*, sd_rtnl_message_unref);
-
-#define _cleanup_sd_rtnl_unref_ _cleanup_(sd_rtnl_unrefp)
-#define _cleanup_sd_rtnl_message_unref_ _cleanup_(sd_rtnl_message_unrefp)
diff --git a/src/libsystemd-rtnl/sd-rtnl.c b/src/libsystemd-rtnl/sd-rtnl.c
deleted file mode 100644
index 08b82ab2a9..0000000000
--- a/src/libsystemd-rtnl/sd-rtnl.c
+++ /dev/null
@@ -1,870 +0,0 @@
-/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
-
-/***
- This file is part of systemd.
-
- Copyright 2013 Tom Gundersen <teg@jklm.no>
-
- systemd is free software; you can redistribute it and/or modify it
- under the terms of the GNU Lesser General Public License as published by
- the Free Software Foundation; either version 2.1 of the License, or
- (at your option) any later version.
-
- systemd 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
- Lesser General Public License for more details.
-
- You should have received a copy of the GNU Lesser General Public License
- along with systemd; If not, see <http://www.gnu.org/licenses/>.
-***/
-
-#include <sys/socket.h>
-#include <poll.h>
-
-#include "macro.h"
-#include "util.h"
-#include "hashmap.h"
-
-#include "sd-rtnl.h"
-#include "rtnl-internal.h"
-#include "rtnl-util.h"
-
-static int sd_rtnl_new(sd_rtnl **ret) {
- sd_rtnl *rtnl;
-
- assert_return(ret, -EINVAL);
-
- rtnl = new0(sd_rtnl, 1);
- if (!rtnl)
- return -ENOMEM;
-
- rtnl->n_ref = REFCNT_INIT;
-
- rtnl->fd = -1;
-
- rtnl->sockaddr.nl.nl_family = AF_NETLINK;
-
- rtnl->original_pid = getpid();
-
- LIST_HEAD_INIT(rtnl->match_callbacks);
-
- /* We guarantee that wqueue always has space for at least
- * one entry */
- rtnl->wqueue = new(sd_rtnl_message*, 1);
- if (!rtnl->wqueue) {
- free(rtnl);
- return -ENOMEM;
- }
-
- *ret = rtnl;
- return 0;
-}
-
-static bool rtnl_pid_changed(sd_rtnl *rtnl) {
- assert(rtnl);
-
- /* We don't support people creating an rtnl connection and
- * keeping it around over a fork(). Let's complain. */
-
- return rtnl->original_pid != getpid();
-}
-
-int sd_rtnl_open(uint32_t groups, sd_rtnl **ret) {
- _cleanup_sd_rtnl_unref_ sd_rtnl *rtnl = NULL;
- socklen_t addrlen;
- int r;
-
- assert_return(ret, -EINVAL);
-
- r = sd_rtnl_new(&rtnl);
- if (r < 0)
- return r;
-
- rtnl->fd = socket(PF_NETLINK, SOCK_RAW|SOCK_CLOEXEC|SOCK_NONBLOCK, NETLINK_ROUTE);
- if (rtnl->fd < 0)
- return -errno;
-
- rtnl->sockaddr.nl.nl_groups = groups;
-
- addrlen = sizeof(rtnl->sockaddr);
-
- r = bind(rtnl->fd, &rtnl->sockaddr.sa, addrlen);
- if (r < 0)
- return -errno;
-
- r = getsockname(rtnl->fd, &rtnl->sockaddr.sa, &addrlen);
- if (r < 0)
- return r;
-
- *ret = rtnl;
- rtnl = NULL;
-
- return 0;
-}
-
-sd_rtnl *sd_rtnl_ref(sd_rtnl *rtnl) {
- if (rtnl)
- assert_se(REFCNT_INC(rtnl->n_ref) >= 2);
-
- return rtnl;
-}
-
-sd_rtnl *sd_rtnl_unref(sd_rtnl *rtnl) {
-
- if (rtnl && REFCNT_DEC(rtnl->n_ref) <= 0) {
- struct match_callback *f;
- unsigned i;
-
- for (i = 0; i < rtnl->rqueue_size; i++)
- sd_rtnl_message_unref(rtnl->rqueue[i]);
- free(rtnl->rqueue);
-
- for (i = 0; i < rtnl->wqueue_size; i++)
- sd_rtnl_message_unref(rtnl->wqueue[i]);
- free(rtnl->wqueue);
-
- hashmap_free_free(rtnl->reply_callbacks);
- prioq_free(rtnl->reply_callbacks_prioq);
-
- while ((f = rtnl->match_callbacks)) {
- LIST_REMOVE(match_callbacks, rtnl->match_callbacks, f);
- free(f);
- }
-
- if (rtnl->fd >= 0)
- close_nointr_nofail(rtnl->fd);
-
- free(rtnl);
- }
-
- return NULL;
-}
-
-int sd_rtnl_send(sd_rtnl *nl,
- sd_rtnl_message *message,
- uint32_t *serial) {
- int r;
-
- assert_return(nl, -EINVAL);
- assert_return(!rtnl_pid_changed(nl), -ECHILD);
- assert_return(message, -EINVAL);
-
- r = message_seal(nl, message);
- if (r < 0)
- return r;
-
- if (nl->wqueue_size <= 0) {
- /* send directly */
- r = socket_write_message(nl, message);
- if (r < 0)
- return r;
- else if (r == 0) {
- /* nothing was sent, so let's put it on
- * the queue */
- nl->wqueue[0] = sd_rtnl_message_ref(message);
- nl->wqueue_size = 1;
- }
- } else {
- sd_rtnl_message **q;
-
- /* append to queue */
- if (nl->wqueue_size >= RTNL_WQUEUE_MAX)
- return -ENOBUFS;
-
- q = realloc(nl->wqueue, sizeof(sd_rtnl_message*) * (nl->wqueue_size + 1));
- if (!q)
- return -ENOMEM;
-
- nl->wqueue = q;
- q[nl->wqueue_size ++] = sd_rtnl_message_ref(message);
- }
-
- if (serial)
- *serial = message_get_serial(message);
-
- return 1;
-}
-
-static int dispatch_rqueue(sd_rtnl *rtnl, sd_rtnl_message **message) {
- sd_rtnl_message *z = NULL;
- int r;
-
- assert(rtnl);
- assert(message);
-
- if (rtnl->rqueue_size > 0) {
- /* Dispatch a queued message */
-
- *message = rtnl->rqueue[0];
- rtnl->rqueue_size --;
- memmove(rtnl->rqueue, rtnl->rqueue + 1, sizeof(sd_rtnl_message*) * rtnl->rqueue_size);
-
- return 1;
- }
-
- /* Try to read a new message */
- r = socket_read_message(rtnl, &z);
- if (r < 0)
- return r;
- if (r == 0)
- return 0;
-
- *message = z;
-
- return 1;
-}
-
-static int dispatch_wqueue(sd_rtnl *rtnl) {
- int r, ret = 0;
-
- assert(rtnl);
-
- while (rtnl->wqueue_size > 0) {
- r = socket_write_message(rtnl, rtnl->wqueue[0]);
- if (r < 0)
- return r;
- else if (r == 0)
- /* Didn't do anything this time */
- return ret;
- else {
- /* see equivalent in sd-bus.c */
- sd_rtnl_message_unref(rtnl->wqueue[0]);
- rtnl->wqueue_size --;
- memmove(rtnl->wqueue, rtnl->wqueue + 1, sizeof(sd_rtnl_message*) * rtnl->wqueue_size);
-
- ret = 1;
- }
- }
-
- return ret;
-}
-
-static int process_timeout(sd_rtnl *rtnl) {
- _cleanup_sd_rtnl_message_unref_ sd_rtnl_message *m = NULL;
- struct reply_callback *c;
- usec_t n;
- int r;
-
- assert(rtnl);
-
- c = prioq_peek(rtnl->reply_callbacks_prioq);
- if (!c)
- return 0;
-
- n = now(CLOCK_MONOTONIC);
- if (c->timeout > n)
- return 0;
-
- r = message_new_synthetic_error(-ETIMEDOUT, c->serial, &m);
- if (r < 0)
- return r;
-
- assert_se(prioq_pop(rtnl->reply_callbacks_prioq) == c);
- hashmap_remove(rtnl->reply_callbacks, &c->serial);
-
- r = c->callback(rtnl, m, c->userdata);
- free(c);
-
- return r < 0 ? r : 1;
-}
-
-static int process_reply(sd_rtnl *rtnl, sd_rtnl_message *m) {
- struct reply_callback *c;
- uint64_t serial;
- int r;
-
- assert(rtnl);
- assert(m);
-
- serial = message_get_serial(m);
- c = hashmap_remove(rtnl->reply_callbacks, &serial);
- if (!c)
- return 0;
-
- if (c->timeout != 0)
- prioq_remove(rtnl->reply_callbacks_prioq, c, &c->prioq_idx);
-
- r = c->callback(rtnl, m, c->userdata);
- free(c);
-
- return r;
-}
-
-static int process_match(sd_rtnl *rtnl, sd_rtnl_message *m) {
- struct match_callback *c;
- uint16_t type;
- int r;
-
- assert(rtnl);
- assert(m);
-
- r = sd_rtnl_message_get_type(m, &type);
- if (r < 0)
- return r;
-
- LIST_FOREACH(match_callbacks, c, rtnl->match_callbacks) {
- if (type == c->type) {
- r = c->callback(rtnl, m, c->userdata);
- if (r != 0)
- return r;
- }
- }
-
- return 0;
-}
-
-static int process_running(sd_rtnl *rtnl, sd_rtnl_message **ret) {
- _cleanup_sd_rtnl_message_unref_ sd_rtnl_message *m = NULL;
- int r;
-
- assert(rtnl);
-
- r = process_timeout(rtnl);
- if (r != 0)
- goto null_message;
-
- r = dispatch_wqueue(rtnl);
- if (r != 0)
- goto null_message;
-
- r = dispatch_rqueue(rtnl, &m);
- if (r < 0)
- return r;
- if (!m)
- goto null_message;
-
- r = process_reply(rtnl, m);
- if (r != 0)
- goto null_message;
-
- r = process_match(rtnl, m);
- if (r != 0)
- goto null_message;
-
- if (ret) {
- *ret = m;
- m = NULL;
-
- return 1;
- }
-
- return 1;
-
-null_message:
- if (r >= 0 && ret)
- *ret = NULL;
-
- return r;
-}
-
-int sd_rtnl_process(sd_rtnl *rtnl, sd_rtnl_message **ret) {
- RTNL_DONT_DESTROY(rtnl);
- int r;
-
- assert_return(rtnl, -EINVAL);
- assert_return(!rtnl_pid_changed(rtnl), -ECHILD);
- assert_return(!rtnl->processing, -EBUSY);
-
- rtnl->processing = true;
- r = process_running(rtnl, ret);
- rtnl->processing = false;
-
- return r;
-}
-
-static usec_t calc_elapse(uint64_t usec) {
- if (usec == (uint64_t) -1)
- return 0;
-
- if (usec == 0)
- usec = RTNL_DEFAULT_TIMEOUT;
-
- return now(CLOCK_MONOTONIC) + usec;
-}
-
-static int rtnl_poll(sd_rtnl *rtnl, bool need_more, uint64_t timeout_usec) {
- struct pollfd p[1] = {};
- struct timespec ts;
- usec_t m = (usec_t) -1;
- int r, e;
-
- assert(rtnl);
-
- e = sd_rtnl_get_events(rtnl);
- if (e < 0)
- return e;
-
- if (need_more)
- /* Caller wants more data, and doesn't care about
- * what's been read or any other timeouts. */
- return e |= POLLIN;
- else {
- usec_t until;
- /* Caller wants to process if there is something to
- * process, but doesn't care otherwise */
-
- r = sd_rtnl_get_timeout(rtnl, &until);
- if (r < 0)
- return r;
- if (r > 0) {
- usec_t nw;
- nw = now(CLOCK_MONOTONIC);
- m = until > nw ? until - nw : 0;
- }
- }
-
- if (timeout_usec != (uint64_t) -1 && (m == (uint64_t) -1 || timeout_usec < m))
- m = timeout_usec;
-
- p[0].fd = rtnl->fd;
- p[0].events = e;
-
- r = ppoll(p, 1, m == (uint64_t) -1 ? NULL : timespec_store(&ts, m), NULL);
- if (r < 0)
- return -errno;
-
- return r > 0 ? 1 : 0;
-}
-
-int sd_rtnl_wait(sd_rtnl *nl, uint64_t timeout_usec) {
- assert_return(nl, -EINVAL);
- assert_return(!rtnl_pid_changed(nl), -ECHILD);
-
- if (nl->rqueue_size > 0)
- return 0;
-
- return rtnl_poll(nl, false, timeout_usec);
-}
-
-static int timeout_compare(const void *a, const void *b) {
- const struct reply_callback *x = a, *y = b;
-
- if (x->timeout != 0 && y->timeout == 0)
- return -1;
-
- if (x->timeout == 0 && y->timeout != 0)
- return 1;
-
- if (x->timeout < y->timeout)
- return -1;
-
- if (x->timeout > y->timeout)
- return 1;
-
- return 0;
-}
-
-int sd_rtnl_call_async(sd_rtnl *nl,
- sd_rtnl_message *m,
- sd_rtnl_message_handler_t callback,
- void *userdata,
- uint64_t usec,
- uint32_t *serial) {
- struct reply_callback *c;
- uint32_t s;
- int r, k;
-
- assert_return(nl, -EINVAL);
- assert_return(m, -EINVAL);
- assert_return(callback, -EINVAL);
- assert_return(!rtnl_pid_changed(nl), -ECHILD);
-
- r = hashmap_ensure_allocated(&nl->reply_callbacks, uint64_hash_func, uint64_compare_func);
- if (r < 0)
- return r;
-
- if (usec != (uint64_t) -1) {
- r = prioq_ensure_allocated(&nl->reply_callbacks_prioq, timeout_compare);
- if (r < 0)
- return r;
- }
-
- c = new0(struct reply_callback, 1);
- if (!c)
- return -ENOMEM;
-
- c->callback = callback;
- c->userdata = userdata;
- c->timeout = calc_elapse(usec);
-
- k = sd_rtnl_send(nl, m, &s);
- if (k < 0) {
- free(c);
- return k;
- }
-
- c->serial = s;
-
- r = hashmap_put(nl->reply_callbacks, &c->serial, c);
- if (r < 0) {
- free(c);
- return r;
- }
-
- if (c->timeout != 0) {
- r = prioq_put(nl->reply_callbacks_prioq, c, &c->prioq_idx);
- if (r > 0) {
- c->timeout = 0;
- sd_rtnl_call_async_cancel(nl, c->serial);
- return r;
- }
- }
-
- if (serial)
- *serial = s;
-
- return k;
-}
-
-int sd_rtnl_call_async_cancel(sd_rtnl *nl, uint32_t serial) {
- struct reply_callback *c;
- uint64_t s = serial;
-
- assert_return(nl, -EINVAL);
- assert_return(serial != 0, -EINVAL);
- assert_return(!rtnl_pid_changed(nl), -ECHILD);
-
- c = hashmap_remove(nl->reply_callbacks, &s);
- if (!c)
- return 0;
-
- if (c->timeout != 0)
- prioq_remove(nl->reply_callbacks_prioq, c, &c->prioq_idx);
-
- free(c);
- return 1;
-}
-
-int sd_rtnl_call(sd_rtnl *nl,
- sd_rtnl_message *message,
- uint64_t usec,
- sd_rtnl_message **ret) {
- usec_t timeout;
- uint32_t serial;
- bool room = false;
- int r;
-
- assert_return(nl, -EINVAL);
- assert_return(!rtnl_pid_changed(nl), -ECHILD);
- assert_return(message, -EINVAL);
-
- r = sd_rtnl_send(nl, message, &serial);
- if (r < 0)
- return r;
-
- timeout = calc_elapse(usec);
-
- for (;;) {
- usec_t left;
- _cleanup_sd_rtnl_message_unref_ sd_rtnl_message *incoming = NULL;
-
- if (!room) {
- sd_rtnl_message **q;
-
- if (nl->rqueue_size >= RTNL_RQUEUE_MAX)
- return -ENOBUFS;
-
- /* Make sure there's room for queueing this
- * locally, before we read the message */
-
- q = realloc(nl->rqueue, (nl->rqueue_size + 1) * sizeof(sd_rtnl_message*));
- if (!q)
- return -ENOMEM;
-
- nl->rqueue = q;
- room = true;
- }
-
- r = socket_read_message(nl, &incoming);
- if (r < 0)
- return r;
- if (incoming) {
- uint32_t received_serial = message_get_serial(incoming);
-
- if (received_serial == serial) {
- r = sd_rtnl_message_get_errno(incoming);
- if (r < 0)
- return r;
-
- if (ret) {
- *ret = incoming;
- incoming = NULL;
- }
-
- return 1;
- }
-
- /* Room was allocated on the queue above */
- nl->rqueue[nl->rqueue_size ++] = incoming;
- incoming = NULL;
- room = false;
-
- /* Try to read more, right away */
- continue;
- }
- if (r != 0)
- continue;
-
- if (timeout > 0) {
- usec_t n;
-
- n = now(CLOCK_MONOTONIC);
- if (n >= timeout)
- return -ETIMEDOUT;
-
- left = timeout - n;
- } else
- left = (uint64_t) -1;
-
- r = rtnl_poll(nl, true, left);
- if (r < 0)
- return r;
-
- r = dispatch_wqueue(nl);
- if (r < 0)
- return r;
- }
-}
-
-int sd_rtnl_flush(sd_rtnl *rtnl) {
- int r;
-
- assert_return(rtnl, -EINVAL);
- assert_return(!rtnl_pid_changed(rtnl), -ECHILD);
-
- if (rtnl->wqueue_size <= 0)
- return 0;
-
- for (;;) {
- r = dispatch_wqueue(rtnl);
- if (r < 0)
- return r;
-
- if (rtnl->wqueue_size <= 0)
- return 0;
-
- r = rtnl_poll(rtnl, false, (uint64_t) -1);
- if (r < 0)
- return r;
- }
-}
-
-int sd_rtnl_get_events(sd_rtnl *rtnl) {
- int flags = 0;
-
- assert_return(rtnl, -EINVAL);
- assert_return(!rtnl_pid_changed(rtnl), -ECHILD);
-
- if (rtnl->rqueue_size <= 0)
- flags |= POLLIN;
- if (rtnl->wqueue_size > 0)
- flags |= POLLOUT;
-
- return flags;
-}
-
-int sd_rtnl_get_timeout(sd_rtnl *rtnl, uint64_t *timeout_usec) {
- struct reply_callback *c;
-
- assert_return(rtnl, -EINVAL);
- assert_return(timeout_usec, -EINVAL);
- assert_return(!rtnl_pid_changed(rtnl), -ECHILD);
-
- if (rtnl->rqueue_size > 0) {
- *timeout_usec = 0;
- return 1;
- }
-
- c = prioq_peek(rtnl->reply_callbacks_prioq);
- if (!c) {
- *timeout_usec = (uint64_t) -1;
- return 0;
- }
-
- *timeout_usec = c->timeout;
-
- return 1;
-}
-
-static int io_callback(sd_event_source *s, int fd, uint32_t revents, void *userdata) {
- sd_rtnl *rtnl = userdata;
- int r;
-
- assert(rtnl);
-
- r = sd_rtnl_process(rtnl, NULL);
- if (r < 0)
- return r;
-
- return 1;
-}
-
-static int time_callback(sd_event_source *s, uint64_t usec, void *userdata) {
- sd_rtnl *rtnl = userdata;
- int r;
-
- assert(rtnl);
-
- r = sd_rtnl_process(rtnl, NULL);
- if (r < 0)
- return r;
-
- return 1;
-}
-
-static int prepare_callback(sd_event_source *s, void *userdata) {
- sd_rtnl *rtnl = userdata;
- int r, e;
- usec_t until;
-
- assert(s);
- assert(rtnl);
-
- e = sd_rtnl_get_events(rtnl);
- if (e < 0)
- return e;
-
- r = sd_event_source_set_io_events(rtnl->io_event_source, e);
- if (r < 0)
- return r;
-
- r = sd_rtnl_get_timeout(rtnl, &until);
- if (r < 0)
- return r;
- if (r > 0) {
- int j;
-
- j = sd_event_source_set_time(rtnl->time_event_source, until);
- if (j < 0)
- return j;
- }
-
- r = sd_event_source_set_enabled(rtnl->time_event_source, r > 0);
- if (r < 0)
- return r;
-
- return 1;
-}
-
-static int exit_callback(sd_event_source *event, void *userdata) {
- sd_rtnl *rtnl = userdata;
-
- assert(event);
-
- sd_rtnl_flush(rtnl);
-
- return 1;
-}
-
-int sd_rtnl_attach_event(sd_rtnl *rtnl, sd_event *event, int priority) {
- int r;
-
- assert_return(rtnl, -EINVAL);
- assert_return(!rtnl->event, -EBUSY);
-
- assert(!rtnl->io_event_source);
- assert(!rtnl->time_event_source);
-
- if (event)
- rtnl->event = sd_event_ref(event);
- else {
- r = sd_event_default(&rtnl->event);
- if (r < 0)
- return r;
- }
-
- r = sd_event_add_io(rtnl->event, rtnl->fd, 0, io_callback, rtnl, &rtnl->io_event_source);
- if (r < 0)
- goto fail;
-
- r = sd_event_source_set_priority(rtnl->io_event_source, priority);
- if (r < 0)
- goto fail;
-
- r = sd_event_source_set_prepare(rtnl->io_event_source, prepare_callback);
- if (r < 0)
- goto fail;
-
- r = sd_event_add_monotonic(rtnl->event, 0, 0, time_callback, rtnl, &rtnl->time_event_source);
- if (r < 0)
- goto fail;
-
- r = sd_event_source_set_priority(rtnl->time_event_source, priority);
- if (r < 0)
- goto fail;
-
- r = sd_event_add_exit(rtnl->event, exit_callback, rtnl, &rtnl->exit_event_source);
- if (r < 0)
- goto fail;
-
- return 0;
-
-fail:
- sd_rtnl_detach_event(rtnl);
- return r;
-}
-
-int sd_rtnl_detach_event(sd_rtnl *rtnl) {
- assert_return(rtnl, -EINVAL);
- assert_return(rtnl->event, -ENXIO);
-
- if (rtnl->io_event_source)
- rtnl->io_event_source = sd_event_source_unref(rtnl->io_event_source);
-
- if (rtnl->time_event_source)
- rtnl->time_event_source = sd_event_source_unref(rtnl->time_event_source);
-
- if (rtnl->exit_event_source)
- rtnl->exit_event_source = sd_event_source_unref(rtnl->exit_event_source);
-
- if (rtnl->event)
- rtnl->event = sd_event_unref(rtnl->event);
-
- return 0;
-}
-
-int sd_rtnl_add_match(sd_rtnl *rtnl,
- uint16_t type,
- sd_rtnl_message_handler_t callback,
- void *userdata) {
- struct match_callback *c;
-
- assert_return(rtnl, -EINVAL);
- assert_return(callback, -EINVAL);
- assert_return(!rtnl_pid_changed(rtnl), -ECHILD);
- assert_return(message_type_is_link(type) || message_type_is_addr(type) || message_type_is_route(type), -ENOTSUP);
-
- c = new0(struct match_callback, 1);
- if (!c)
- return -ENOMEM;
-
- c->callback = callback;
- c->type = type;
- c->userdata = userdata;
-
- LIST_PREPEND(match_callbacks, rtnl->match_callbacks, c);
-
- return 0;
-}
-
-int sd_rtnl_remove_match(sd_rtnl *rtnl,
- uint16_t type,
- sd_rtnl_message_handler_t callback,
- void *userdata) {
- struct match_callback *c;
-
- assert_return(rtnl, -EINVAL);
- assert_return(callback, -EINVAL);
- assert_return(!rtnl_pid_changed(rtnl), -ECHILD);
-
- LIST_FOREACH(match_callbacks, c, rtnl->match_callbacks)
- if (c->callback == callback && c->type == type && c->userdata == userdata) {
- LIST_REMOVE(match_callbacks, rtnl->match_callbacks, c);
- free(c);
-
- return 1;
- }
-
- return 0;
-}
diff --git a/src/libsystemd-rtnl/test-rtnl.c b/src/libsystemd-rtnl/test-rtnl.c
deleted file mode 100644
index 58654e990a..0000000000
--- a/src/libsystemd-rtnl/test-rtnl.c
+++ /dev/null
@@ -1,330 +0,0 @@
-/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
-
-/***
- This file is part of systemd.
-
- Copyright 2013 Tom Gundersen <teg@jklm.no>
-
- systemd is free software; you can redistribute it and/or modify it
- under the terms of the GNU Lesser General Public License as published by
- the Free Software Foundation; either version 2.1 of the License, or
- (at your option) any later version.
-
- systemd 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
- Lesser General Public License for more details.
-
- You should have received a copy of the GNU Lesser General Public License
- along with systemd; If not, see <http://www.gnu.org/licenses/>.
-***/
-
-#include <linux/rtnetlink.h>
-#include <netinet/ether.h>
-
-#include "util.h"
-#include "macro.h"
-#include "sd-rtnl.h"
-#include "socket-util.h"
-#include "rtnl-util.h"
-#include "event-util.h"
-
-static void test_link_configure(sd_rtnl *rtnl, int ifindex) {
- _cleanup_sd_rtnl_message_unref_ sd_rtnl_message *message;
- uint16_t type;
- const char *mac = "98:fe:94:3f:c6:18", *name = "test";
- unsigned int mtu = 1450;
- void *data;
-
- /* we'd really like to test NEWLINK, but let's not mess with the running kernel */
- assert(sd_rtnl_message_link_new(RTM_GETLINK, ifindex, &message) >= 0);
- assert(sd_rtnl_message_append_string(message, IFLA_IFNAME, name) >= 0);
- assert(sd_rtnl_message_append_ether_addr(message, IFLA_ADDRESS, ether_aton(mac)) >= 0);
- assert(sd_rtnl_message_append_u32(message, IFLA_MTU, mtu) >= 0);
-
- assert(sd_rtnl_message_read(message, &type, &data) > 0);
- assert(type == IFLA_IFNAME);
- assert(streq(name, (char *) data));
-
- assert(sd_rtnl_message_read(message, &type, &data) > 0);
- assert(type == IFLA_ADDRESS);
- assert(streq(mac, ether_ntoa(data)));
-
- assert(sd_rtnl_message_read(message, &type, &data) > 0);
- assert(type == IFLA_MTU);
- assert(mtu == *(unsigned int *) data);
-
- assert(sd_rtnl_call(rtnl, message, 0, NULL) == 1);
-}
-
-static void test_route(void) {
- _cleanup_sd_rtnl_message_unref_ sd_rtnl_message *req;
- struct in_addr addr;
- uint32_t index = 2;
- uint16_t type;
- void *data;
- int r;
-
- r = sd_rtnl_message_route_new(RTM_NEWROUTE, AF_INET, &req);
- if (r < 0) {
- log_error("Could not create RTM_NEWROUTE message: %s", strerror(-r));
- return;
- }
-
- addr.s_addr = htonl(INADDR_LOOPBACK);
-
- r = sd_rtnl_message_append_in_addr(req, RTA_GATEWAY, &addr);
- if (r < 0) {
- log_error("Could not append RTA_GATEWAY attribute: %s", strerror(-r));
- return;
- }
-
- r = sd_rtnl_message_append_u32(req, RTA_OIF, index);
- if (r < 0) {
- log_error("Could not append RTA_OIF attribute: %s", strerror(-r));
- return;
- }
-
- assert(sd_rtnl_message_read(req, &type, &data) > 0);
- assert(type == RTA_GATEWAY);
- assert(((struct in_addr *)data)->s_addr == addr.s_addr);
-
- assert(sd_rtnl_message_read(req, &type, &data) > 0);
- assert(type == RTA_OIF);
- assert(*(uint32_t *) data == index);
-}
-
-static void test_multiple(void) {
- sd_rtnl *rtnl1, *rtnl2;
-
- assert(sd_rtnl_open(0, &rtnl1) >= 0);
- assert(sd_rtnl_open(0, &rtnl2) >= 0);
-
- rtnl1 = sd_rtnl_unref(rtnl1);
- rtnl2 = sd_rtnl_unref(rtnl2);
-}
-
-static int link_handler(sd_rtnl *rtnl, sd_rtnl_message *m, void *userdata) {
- void *data;
- uint16_t type;
- char *ifname = userdata;
-
- assert(rtnl);
- assert(m);
-
- log_info("got link info about %s", ifname);
- free(ifname);
-
- while (sd_rtnl_message_read(m, &type, &data) > 0) {
- switch (type) {
-// case IFLA_MTU:
-// assert(*(unsigned int *) data == 65536);
-// break;
-// case IFLA_QDISC:
-// assert(streq((char *) data, "noqueue"));
-// break;
- case IFLA_IFNAME:
- assert(streq((char *) data, "lo"));
- break;
- }
- }
-
- return 1;
-}
-
-static void test_event_loop(int ifindex) {
- _cleanup_event_unref_ sd_event *event = NULL;
- _cleanup_sd_rtnl_unref_ sd_rtnl *rtnl = NULL;
- _cleanup_sd_rtnl_message_unref_ sd_rtnl_message *m = NULL;
- char *ifname;
-
- ifname = strdup("lo2");
- assert(ifname);
-
- assert(sd_rtnl_open(0, &rtnl) >= 0);
- assert(sd_rtnl_message_link_new(RTM_GETLINK, ifindex, &m) >= 0);
-
- assert(sd_rtnl_call_async(rtnl, m, &link_handler, ifname, 0, NULL) >= 0);
-
- assert(sd_event_default(&event) >= 0);
-
- assert(sd_rtnl_attach_event(rtnl, event, 0) >= 0);
-
- assert(sd_event_run(event, 0) >= 0);
-
- assert(sd_rtnl_detach_event(rtnl) >= 0);
-}
-
-static int pipe_handler(sd_rtnl *rtnl, sd_rtnl_message *m, void *userdata) {
- int *counter = userdata;
-
- (*counter) --;
-
- log_info("got reply, %d left in pipe", *counter);
-
- return sd_rtnl_message_get_errno(m);
-}
-
-static void test_async(int ifindex) {
- _cleanup_sd_rtnl_unref_ sd_rtnl *rtnl = NULL;
- _cleanup_sd_rtnl_message_unref_ sd_rtnl_message *m = NULL, *r = NULL;
- uint32_t serial;
- char *ifname;
-
- ifname = strdup("lo");
- assert(ifname);
-
- assert(sd_rtnl_open(0, &rtnl) >= 0);
-
- assert(sd_rtnl_message_link_new(RTM_GETLINK, ifindex, &m) >= 0);
-
- assert(sd_rtnl_call_async(rtnl, m, &link_handler, ifname, 0, &serial) >= 0);
-
- assert(sd_rtnl_wait(rtnl, 0) >= 0);
- assert(sd_rtnl_process(rtnl, &r) >= 0);
-}
-
-static void test_pipe(int ifindex) {
- _cleanup_sd_rtnl_unref_ sd_rtnl *rtnl = NULL;
- _cleanup_sd_rtnl_message_unref_ sd_rtnl_message *m1 = NULL, *m2 = NULL;
- int counter = 0;
-
- assert(sd_rtnl_open(0, &rtnl) >= 0);
-
- assert(sd_rtnl_message_link_new(RTM_GETLINK, ifindex, &m1) >= 0);
- assert(sd_rtnl_message_link_new(RTM_GETLINK, ifindex, &m2) >= 0);
-
- counter ++;
- assert(sd_rtnl_call_async(rtnl, m1, &pipe_handler, &counter, 0, NULL) >= 0);
-
- counter ++;
- assert(sd_rtnl_call_async(rtnl, m2, &pipe_handler, &counter, 0, NULL) >= 0);
-
- while (counter > 0) {
- assert(sd_rtnl_wait(rtnl, 0) >= 0);
- assert(sd_rtnl_process(rtnl, NULL) >= 0);
- }
-}
-
-static void test_container(void) {
- _cleanup_sd_rtnl_message_unref_ sd_rtnl_message *m = NULL;
- uint16_t type;
- void *data;
-
- assert(sd_rtnl_message_link_new(RTM_NEWLINK, 0, &m) >= 0);
-
- assert(sd_rtnl_message_open_container(m, IFLA_LINKINFO) >= 0);
- assert(sd_rtnl_message_open_container(m, IFLA_LINKINFO) == -EINVAL);
- assert(sd_rtnl_message_append_string(m, IFLA_INFO_KIND, "kind") >= 0);
- assert(sd_rtnl_message_close_container(m) >= 0);
- assert(sd_rtnl_message_close_container(m) == -EINVAL);
-
- assert(sd_rtnl_message_read(m, &type, &data) == -EINVAL);
-
-/* TODO: add support for entering containers
- assert(sd_rtnl_message_read(m, &type, &data) > 0);
- assert(type == IFLA_INFO_KIND);
- assert(streq("kind", (char *) data));
-
- assert(sd_rtnl_message_read(m, &type, &data) == 0);
-*/
-}
-
-static void test_match(void) {
- _cleanup_sd_rtnl_unref_ sd_rtnl *rtnl = NULL;
-
- assert(sd_rtnl_open(0, &rtnl) >= 0);
-
- assert(sd_rtnl_add_match(rtnl, RTM_NEWLINK, &link_handler, NULL) >= 0);
- assert(sd_rtnl_add_match(rtnl, RTM_NEWLINK, &link_handler, NULL) >= 0);
-
- assert(sd_rtnl_remove_match(rtnl, RTM_NEWLINK, &link_handler, NULL) == 1);
- assert(sd_rtnl_remove_match(rtnl, RTM_NEWLINK, &link_handler, NULL) == 1);
- assert(sd_rtnl_remove_match(rtnl, RTM_NEWLINK, &link_handler, NULL) == 0);
-}
-
-int main(void) {
- sd_rtnl *rtnl;
- sd_rtnl_message *m;
- sd_rtnl_message *r;
- void *data;
- int if_loopback;
- uint16_t type;
- unsigned int mtu = 0;
- unsigned int *mtu_reply;
-
- test_match();
-
- test_multiple();
-
- test_route();
-
- test_container();
-
- assert(sd_rtnl_open(0, &rtnl) >= 0);
- assert(rtnl);
-
- if_loopback = (int) if_nametoindex("lo");
- assert(if_loopback > 0);
-
- test_async(if_loopback);
-
- test_pipe(if_loopback);
-
- test_event_loop(if_loopback);
-
- test_link_configure(rtnl, if_loopback);
-
- assert(sd_rtnl_message_link_new(RTM_GETLINK, if_loopback, &m) >= 0);
- assert(m);
-
- assert(sd_rtnl_message_get_type(m, &type) >= 0);
- assert(type == RTM_GETLINK);
-
- assert(sd_rtnl_message_read(m, &type, &data) == 0);
-
- assert(sd_rtnl_call(rtnl, m, 0, &r) == 1);
- assert(sd_rtnl_message_get_type(r, &type) >= 0);
- assert(type == RTM_NEWLINK);
-
- assert(sd_rtnl_message_read(m, &type, &data) == 0);
- assert((r = sd_rtnl_message_unref(r)) == NULL);
-
- assert(sd_rtnl_call(rtnl, m, -1, &r) == -EPERM);
- assert((m = sd_rtnl_message_unref(m)) == NULL);
- assert((r = sd_rtnl_message_unref(r)) == NULL);
-
- assert(sd_rtnl_message_link_new(RTM_GETLINK, if_loopback, &m) >= 0);
- assert(m);
-
- assert(sd_rtnl_message_append_u32(m, IFLA_MTU, mtu) >= 0);
- assert(sd_rtnl_message_read(m, &type, (void **) &mtu_reply) == 1);
-
- assert(type == IFLA_MTU);
- assert(*mtu_reply == 0);
-
- assert(sd_rtnl_message_read(m, &type, &data) == 0);
-
- assert(sd_rtnl_call(rtnl, m, -1, &r) == 1);
- while (sd_rtnl_message_read(r, &type, &data) > 0) {
- switch (type) {
-// case IFLA_MTU:
-// assert(*(unsigned int *) data == 65536);
-// break;
-// case IFLA_QDISC:
-// assert(streq((char *) data, "noqueue"));
-// break;
- case IFLA_IFNAME:
- assert(streq((char *) data, "lo"));
- break;
- }
- }
-
- assert(sd_rtnl_flush(rtnl) >= 0);
-
- assert((m = sd_rtnl_message_unref(m)) == NULL);
- assert((r = sd_rtnl_message_unref(r)) == NULL);
- assert((rtnl = sd_rtnl_unref(rtnl)) == NULL);
-
- return EXIT_SUCCESS;
-}