2017-11-18 17:09:20 +01:00
|
|
|
/* SPDX-License-Identifier: LGPL-2.1+ */
|
2013-10-21 20:12:52 +02:00
|
|
|
/***
|
|
|
|
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 <netinet/in.h>
|
|
|
|
#include <stdbool.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
2015-10-24 22:58:24 +02:00
|
|
|
#include "sd-netlink.h"
|
|
|
|
|
2015-10-27 03:01:06 +01:00
|
|
|
#include "alloc-util.h"
|
2016-11-07 16:14:59 +01:00
|
|
|
#include "format-util.h"
|
2014-02-19 15:45:54 +01:00
|
|
|
#include "missing.h"
|
2015-06-12 16:31:33 +02:00
|
|
|
#include "netlink-internal.h"
|
|
|
|
#include "netlink-types.h"
|
2015-10-24 22:58:24 +02:00
|
|
|
#include "netlink-util.h"
|
|
|
|
#include "refcnt.h"
|
|
|
|
#include "socket-util.h"
|
|
|
|
#include "util.h"
|
2013-10-21 20:12:52 +02:00
|
|
|
|
2015-06-26 00:02:55 +02:00
|
|
|
#define GET_CONTAINER(m, i) ((i) < (m)->n_containers ? (struct rtattr*)((uint8_t*)(m)->hdr + (m)->containers[i].offset) : NULL)
|
2016-02-23 05:32:04 +01:00
|
|
|
#define PUSH_CONTAINER(m, new) (m)->container_offsets[(m)->n_containers++] = (uint8_t*)(new) - (uint8_t*)(m)->hdr;
|
2013-12-31 18:56:59 +01:00
|
|
|
|
2014-10-29 10:50:33 +01:00
|
|
|
#define RTA_TYPE(rta) ((rta)->rta_type & NLA_TYPE_MASK)
|
2015-06-23 13:13:20 +02:00
|
|
|
#define RTA_FLAGS(rta) ((rta)->rta_type & ~NLA_TYPE_MASK)
|
2014-10-29 10:50:33 +01:00
|
|
|
|
2015-06-13 21:10:39 +02:00
|
|
|
int message_new_empty(sd_netlink *rtnl, sd_netlink_message **ret) {
|
2015-06-12 16:31:33 +02:00
|
|
|
sd_netlink_message *m;
|
2013-10-21 20:12:52 +02:00
|
|
|
|
|
|
|
assert_return(ret, -EINVAL);
|
|
|
|
|
2014-12-29 10:45:58 +01:00
|
|
|
/* Note that 'rtnl' is currently unused, if we start using it internally
|
2014-03-23 15:33:24 +01:00
|
|
|
we must take care to avoid problems due to mutual references between
|
2015-05-11 13:49:29 +02:00
|
|
|
buses and their queued messages. See sd-bus.
|
2014-03-23 15:33:24 +01:00
|
|
|
*/
|
|
|
|
|
2015-06-12 16:31:33 +02:00
|
|
|
m = new0(sd_netlink_message, 1);
|
2013-10-21 20:12:52 +02:00
|
|
|
if (!m)
|
|
|
|
return -ENOMEM;
|
|
|
|
|
|
|
|
m->n_ref = REFCNT_INIT;
|
2017-12-18 15:17:06 +01:00
|
|
|
m->protocol = rtnl->protocol;
|
2013-10-21 20:12:52 +02:00
|
|
|
m->sealed = false;
|
|
|
|
|
|
|
|
*ret = m;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-06-12 16:31:33 +02:00
|
|
|
int message_new(sd_netlink *rtnl, sd_netlink_message **ret, uint16_t type) {
|
tree-wide: expose "p"-suffix unref calls in public APIs to make gcc cleanup easy
GLIB has recently started to officially support the gcc cleanup
attribute in its public API, hence let's do the same for our APIs.
With this patch we'll define an xyz_unrefp() call for each public
xyz_unref() call, to make it easy to use inside a
__attribute__((cleanup())) expression. Then, all code is ported over to
make use of this.
The new calls are also documented in the man pages, with examples how to
use them (well, I only added docs where the _unref() call itself already
had docs, and the examples, only cover sd_bus_unrefp() and
sd_event_unrefp()).
This also renames sd_lldp_free() to sd_lldp_unref(), since that's how we
tend to call our destructors these days.
Note that this defines no public macro that wraps gcc's attribute and
makes it easier to use. While I think it's our duty in the library to
make our stuff easy to use, I figure it's not our duty to make gcc's own
features easy to use on its own. Most likely, client code which wants to
make use of this should define its own:
#define _cleanup_(function) __attribute__((cleanup(function)))
Or similar, to make the gcc feature easier to use.
Making this logic public has the benefit that we can remove three header
files whose only purpose was to define these functions internally.
See #2008.
2015-11-27 19:13:45 +01:00
|
|
|
_cleanup_(sd_netlink_message_unrefp) sd_netlink_message *m = NULL;
|
2014-03-26 19:25:01 +01:00
|
|
|
const NLType *nl_type;
|
2017-12-18 15:17:06 +01:00
|
|
|
const NLTypeSystem *type_system_root;
|
2014-03-26 19:25:01 +01:00
|
|
|
size_t size;
|
|
|
|
int r;
|
|
|
|
|
2017-12-18 15:17:06 +01:00
|
|
|
assert_return(rtnl, -EINVAL);
|
|
|
|
|
|
|
|
type_system_root = type_system_get_root(rtnl->protocol);
|
|
|
|
|
|
|
|
r = type_system_get_type(type_system_root, &nl_type, type);
|
2014-03-26 19:25:01 +01:00
|
|
|
if (r < 0)
|
|
|
|
return r;
|
|
|
|
|
sd-netlink: make sure the root-level type is nested
In sd-netlink-message, we always guarantee that the currently selected
type-system is non-NULL. Otherwise, we would be unable to parse any types
in the current container level. Hence, this assertion must be true:
message->container_type_system[m->n_containers] != NULL
During message_new() we currently do not verify that this assertion is
true. Instead, we blindly access nl_type->type_system and use it (which
might be NULL for basic types and unions). Fix this, by explicitly
checking that the root-level type is nested.
Note that this is *not* a strict requirement of netlink, but it's a strict
requirement for all message types we currently support. Furthermore, all
the callers of message_new() already verify that only supported types are
passed, therefore, this is a pure cosmetic check. However, it might be
needed on the future, so make sure we don't trap into this once we change
the type-system.
2015-06-23 10:47:44 +02:00
|
|
|
if (type_get_type(nl_type) != NETLINK_TYPE_NESTED)
|
|
|
|
return -EINVAL;
|
|
|
|
|
2014-04-10 19:40:48 +02:00
|
|
|
r = message_new_empty(rtnl, &m);
|
2014-03-26 19:25:01 +01:00
|
|
|
if (r < 0)
|
|
|
|
return r;
|
|
|
|
|
2015-06-23 11:03:10 +02:00
|
|
|
size = NLMSG_SPACE(type_get_size(nl_type));
|
2014-04-10 19:40:48 +02:00
|
|
|
|
|
|
|
assert(size >= sizeof(struct nlmsghdr));
|
|
|
|
m->hdr = malloc0(size);
|
|
|
|
if (!m->hdr)
|
|
|
|
return -ENOMEM;
|
|
|
|
|
|
|
|
m->hdr->nlmsg_flags = NLM_F_REQUEST | NLM_F_ACK;
|
|
|
|
|
2015-06-26 00:02:55 +02:00
|
|
|
type_get_type_system(nl_type, &m->containers[0].type_system);
|
2014-04-10 19:40:48 +02:00
|
|
|
m->hdr->nlmsg_len = size;
|
|
|
|
m->hdr->nlmsg_type = type;
|
|
|
|
|
|
|
|
*ret = m;
|
|
|
|
m = NULL;
|
2014-03-26 19:25:01 +01:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-06-12 16:31:33 +02:00
|
|
|
int sd_netlink_message_request_dump(sd_netlink_message *m, int dump) {
|
2014-04-14 18:07:34 +02:00
|
|
|
assert_return(m, -EINVAL);
|
|
|
|
assert_return(m->hdr, -EINVAL);
|
2017-09-14 21:51:39 +02:00
|
|
|
|
|
|
|
assert_return(IN_SET(m->hdr->nlmsg_type, RTM_GETLINK, RTM_GETADDR, RTM_GETROUTE, RTM_GETNEIGH, RTM_GETRULE, RTM_GETADDRLABEL), -EINVAL);
|
2014-04-14 18:07:34 +02:00
|
|
|
|
2016-03-03 18:30:37 +01:00
|
|
|
SET_FLAG(m->hdr->nlmsg_flags, NLM_F_DUMP, dump);
|
2014-04-14 18:07:34 +02:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-06-12 16:31:33 +02:00
|
|
|
sd_netlink_message *sd_netlink_message_ref(sd_netlink_message *m) {
|
2018-01-23 15:47:18 +01:00
|
|
|
if (!m)
|
|
|
|
return NULL;
|
2013-10-21 20:12:52 +02:00
|
|
|
|
2018-01-23 15:47:18 +01:00
|
|
|
assert_se(REFCNT_INC(m->n_ref) >= 2);
|
2013-10-21 20:12:52 +02:00
|
|
|
return m;
|
|
|
|
}
|
|
|
|
|
2015-06-12 16:31:33 +02:00
|
|
|
sd_netlink_message *sd_netlink_message_unref(sd_netlink_message *m) {
|
2016-06-07 10:38:33 +02:00
|
|
|
sd_netlink_message *t;
|
|
|
|
|
|
|
|
while (m && REFCNT_DEC(m->n_ref) == 0) {
|
2014-03-23 21:45:46 +01:00
|
|
|
unsigned i;
|
|
|
|
|
2013-10-21 20:12:52 +02:00
|
|
|
free(m->hdr);
|
2014-03-23 21:45:46 +01:00
|
|
|
|
2014-03-28 00:46:45 +01:00
|
|
|
for (i = 0; i <= m->n_containers; i++)
|
2015-06-26 00:02:55 +02:00
|
|
|
free(m->containers[i].attributes);
|
2014-03-23 21:45:46 +01:00
|
|
|
|
2016-06-07 10:38:33 +02:00
|
|
|
t = m;
|
|
|
|
m = m->next;
|
|
|
|
free(t);
|
2013-10-21 20:12:52 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2015-06-12 16:31:33 +02:00
|
|
|
int sd_netlink_message_get_type(sd_netlink_message *m, uint16_t *type) {
|
2013-10-21 20:12:52 +02:00
|
|
|
assert_return(m, -EINVAL);
|
|
|
|
assert_return(type, -EINVAL);
|
|
|
|
|
|
|
|
*type = m->hdr->nlmsg_type;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-10-05 06:06:57 +02:00
|
|
|
int sd_netlink_message_set_flags(sd_netlink_message *m, uint16_t flags) {
|
|
|
|
assert_return(m, -EINVAL);
|
|
|
|
assert_return(flags, -EINVAL);
|
|
|
|
|
|
|
|
m->hdr->nlmsg_flags = flags;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-06-12 16:31:33 +02:00
|
|
|
int sd_netlink_message_is_broadcast(sd_netlink_message *m) {
|
2014-03-16 13:02:16 +01:00
|
|
|
assert_return(m, -EINVAL);
|
|
|
|
|
2015-03-13 16:12:57 +01:00
|
|
|
return m->broadcast;
|
2014-03-16 13:02:16 +01:00
|
|
|
}
|
|
|
|
|
2013-12-31 18:56:59 +01:00
|
|
|
/* If successful the updated message will be correctly aligned, if
|
|
|
|
unsuccessful the old message is untouched. */
|
2015-06-12 16:31:33 +02:00
|
|
|
static int add_rtattr(sd_netlink_message *m, unsigned short type, const void *data, size_t data_length) {
|
2014-05-02 22:29:18 +02:00
|
|
|
uint32_t rta_length;
|
|
|
|
size_t message_length, padding_length;
|
2013-10-21 20:12:52 +02:00
|
|
|
struct nlmsghdr *new_hdr;
|
|
|
|
struct rtattr *rta;
|
2013-11-08 12:14:28 +01:00
|
|
|
char *padding;
|
2014-01-24 18:44:03 +01:00
|
|
|
unsigned i;
|
2014-05-02 22:29:18 +02:00
|
|
|
int offset;
|
2013-10-21 20:12:52 +02:00
|
|
|
|
2013-11-21 20:44:29 +01:00
|
|
|
assert(m);
|
|
|
|
assert(m->hdr);
|
2014-01-24 21:04:05 +01:00
|
|
|
assert(!m->sealed);
|
2013-11-21 20:44:29 +01:00
|
|
|
assert(NLMSG_ALIGN(m->hdr->nlmsg_len) == m->hdr->nlmsg_len);
|
2014-05-02 22:29:18 +02:00
|
|
|
assert(!data || data_length);
|
|
|
|
|
|
|
|
/* get offset of the new attribute */
|
|
|
|
offset = m->hdr->nlmsg_len;
|
2013-10-21 20:12:52 +02:00
|
|
|
|
2013-11-08 12:14:28 +01:00
|
|
|
/* get the size of the new rta attribute (with padding at the end) */
|
2013-10-21 20:12:52 +02:00
|
|
|
rta_length = RTA_LENGTH(data_length);
|
2013-12-31 18:56:59 +01:00
|
|
|
|
|
|
|
/* get the new message size (with padding at the end) */
|
2014-05-02 22:29:18 +02:00
|
|
|
message_length = offset + RTA_ALIGN(rta_length);
|
2013-10-21 20:12:52 +02:00
|
|
|
|
2017-12-18 15:17:06 +01:00
|
|
|
/* buffer should be smaller than both one page or 8K to be accepted by the kernel */
|
|
|
|
if (message_length > MIN(page_size(), 8192UL))
|
|
|
|
return -ENOBUFS;
|
|
|
|
|
2013-10-21 20:12:52 +02:00
|
|
|
/* 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 */
|
2014-05-02 22:29:18 +02:00
|
|
|
rta = (struct rtattr *) ((uint8_t *) m->hdr + offset);
|
2013-10-21 20:12:52 +02:00
|
|
|
|
2014-01-24 18:44:03 +01:00
|
|
|
/* if we are inside containers, extend them */
|
|
|
|
for (i = 0; i < m->n_containers; i++)
|
2014-05-02 22:29:18 +02:00
|
|
|
GET_CONTAINER(m, i)->rta_len += message_length - offset;
|
2013-11-21 20:44:29 +01:00
|
|
|
|
2013-10-21 20:12:52 +02:00
|
|
|
/* fill in the attribute */
|
|
|
|
rta->rta_type = type;
|
|
|
|
rta->rta_len = rta_length;
|
2014-05-02 22:29:18 +02:00
|
|
|
if (data)
|
2013-11-21 20:44:29 +01:00
|
|
|
/* 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)
|
2014-05-02 22:29:18 +02:00
|
|
|
*/
|
2013-11-21 20:44:29 +01:00
|
|
|
padding = mempcpy(RTA_DATA(rta), data, data_length);
|
2016-05-03 19:16:50 +02:00
|
|
|
|
|
|
|
else
|
2014-05-02 22:29:18 +02:00
|
|
|
/* if no data was passed, make sure we still initialize the padding
|
|
|
|
note that we can have data_length > 0 (used by some containers) */
|
|
|
|
padding = RTA_DATA(rta);
|
2013-10-21 20:12:52 +02:00
|
|
|
|
2014-05-02 22:29:18 +02:00
|
|
|
/* make sure also the padding at the end of the message is initialized */
|
|
|
|
padding_length = (uint8_t*)m->hdr + message_length - (uint8_t*)padding;
|
|
|
|
memzero(padding, padding_length);
|
|
|
|
|
2013-12-31 18:56:59 +01:00
|
|
|
/* update message size */
|
|
|
|
m->hdr->nlmsg_len = message_length;
|
|
|
|
|
2014-05-02 22:29:18 +02:00
|
|
|
return offset;
|
2013-10-21 20:12:52 +02:00
|
|
|
}
|
|
|
|
|
2015-06-23 11:18:53 +02:00
|
|
|
static int message_attribute_has_type(sd_netlink_message *m, size_t *out_size, uint16_t attribute_type, uint16_t data_type) {
|
2014-03-26 19:25:01 +01:00
|
|
|
const NLType *type;
|
|
|
|
int r;
|
|
|
|
|
2015-06-23 11:18:53 +02:00
|
|
|
assert(m);
|
|
|
|
|
2015-06-26 00:02:55 +02:00
|
|
|
r = type_system_get_type(m->containers[m->n_containers].type_system, &type, attribute_type);
|
2014-03-26 19:25:01 +01:00
|
|
|
if (r < 0)
|
|
|
|
return r;
|
|
|
|
|
2015-06-23 11:03:10 +02:00
|
|
|
if (type_get_type(type) != data_type)
|
2014-03-26 19:25:01 +01:00
|
|
|
return -EINVAL;
|
|
|
|
|
2015-06-23 11:18:53 +02:00
|
|
|
if (out_size)
|
|
|
|
*out_size = type_get_size(type);
|
|
|
|
return 0;
|
2014-03-26 19:25:01 +01:00
|
|
|
}
|
|
|
|
|
2015-06-12 16:31:33 +02:00
|
|
|
int sd_netlink_message_append_string(sd_netlink_message *m, unsigned short type, const char *data) {
|
2014-03-26 19:25:01 +01:00
|
|
|
size_t length, size;
|
2013-12-15 14:00:20 +01:00
|
|
|
int r;
|
2013-10-21 20:12:52 +02:00
|
|
|
|
|
|
|
assert_return(m, -EINVAL);
|
2014-01-24 21:04:05 +01:00
|
|
|
assert_return(!m->sealed, -EPERM);
|
2013-10-21 20:12:52 +02:00
|
|
|
assert_return(data, -EINVAL);
|
|
|
|
|
2015-06-23 11:18:53 +02:00
|
|
|
r = message_attribute_has_type(m, &size, type, NETLINK_TYPE_STRING);
|
2013-12-15 14:00:20 +01:00
|
|
|
if (r < 0)
|
|
|
|
return r;
|
2013-10-21 20:12:52 +02:00
|
|
|
|
2014-03-26 19:25:01 +01:00
|
|
|
if (size) {
|
2014-12-09 01:09:21 +01:00
|
|
|
length = strnlen(data, size+1);
|
|
|
|
if (length > size)
|
2014-03-26 19:25:01 +01:00
|
|
|
return -EINVAL;
|
|
|
|
} else
|
|
|
|
length = strlen(data);
|
2013-11-21 20:44:29 +01:00
|
|
|
|
2014-03-26 19:25:01 +01:00
|
|
|
r = add_rtattr(m, type, data, length + 1);
|
2013-12-15 14:00:20 +01:00
|
|
|
if (r < 0)
|
|
|
|
return r;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-07-29 08:29:16 +02:00
|
|
|
int sd_netlink_message_append_flag(sd_netlink_message *m, unsigned short type) {
|
|
|
|
size_t size;
|
|
|
|
int r;
|
|
|
|
|
|
|
|
assert_return(m, -EINVAL);
|
|
|
|
assert_return(!m->sealed, -EPERM);
|
|
|
|
|
|
|
|
r = message_attribute_has_type(m, &size, type, NETLINK_TYPE_FLAG);
|
|
|
|
if (r < 0)
|
|
|
|
return r;
|
|
|
|
|
|
|
|
r = add_rtattr(m, type, NULL, 0);
|
|
|
|
if (r < 0)
|
|
|
|
return r;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-06-12 16:31:33 +02:00
|
|
|
int sd_netlink_message_append_u8(sd_netlink_message *m, unsigned short type, uint8_t data) {
|
2014-02-04 09:49:20 +01:00
|
|
|
int r;
|
|
|
|
|
|
|
|
assert_return(m, -EINVAL);
|
|
|
|
assert_return(!m->sealed, -EPERM);
|
|
|
|
|
2015-06-23 11:18:53 +02:00
|
|
|
r = message_attribute_has_type(m, NULL, type, NETLINK_TYPE_U8);
|
2014-02-04 09:49:20 +01:00
|
|
|
if (r < 0)
|
|
|
|
return r;
|
|
|
|
|
|
|
|
r = add_rtattr(m, type, &data, sizeof(uint8_t));
|
|
|
|
if (r < 0)
|
|
|
|
return r;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-06-12 16:31:33 +02:00
|
|
|
int sd_netlink_message_append_u16(sd_netlink_message *m, unsigned short type, uint16_t data) {
|
2014-01-22 14:16:01 +01:00
|
|
|
int r;
|
|
|
|
|
|
|
|
assert_return(m, -EINVAL);
|
2014-01-24 21:04:05 +01:00
|
|
|
assert_return(!m->sealed, -EPERM);
|
2014-01-22 14:16:01 +01:00
|
|
|
|
2015-06-23 11:18:53 +02:00
|
|
|
r = message_attribute_has_type(m, NULL, type, NETLINK_TYPE_U16);
|
2014-01-22 14:16:01 +01:00
|
|
|
if (r < 0)
|
|
|
|
return r;
|
|
|
|
|
|
|
|
r = add_rtattr(m, type, &data, sizeof(uint16_t));
|
|
|
|
if (r < 0)
|
|
|
|
return r;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-06-12 16:31:33 +02:00
|
|
|
int sd_netlink_message_append_u32(sd_netlink_message *m, unsigned short type, uint32_t data) {
|
2013-12-15 14:00:20 +01:00
|
|
|
int r;
|
|
|
|
|
|
|
|
assert_return(m, -EINVAL);
|
2014-01-24 21:04:05 +01:00
|
|
|
assert_return(!m->sealed, -EPERM);
|
2013-12-15 14:00:20 +01:00
|
|
|
|
2015-06-23 11:18:53 +02:00
|
|
|
r = message_attribute_has_type(m, NULL, type, NETLINK_TYPE_U32);
|
2013-12-15 14:00:20 +01:00
|
|
|
if (r < 0)
|
|
|
|
return r;
|
|
|
|
|
2013-12-19 21:28:50 +01:00
|
|
|
r = add_rtattr(m, type, &data, sizeof(uint32_t));
|
2013-12-15 14:00:20 +01:00
|
|
|
if (r < 0)
|
|
|
|
return r;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-11-28 03:31:06 +01:00
|
|
|
int sd_netlink_message_append_data(sd_netlink_message *m, unsigned short type, const void *data, size_t len) {
|
|
|
|
int r;
|
|
|
|
|
|
|
|
assert_return(m, -EINVAL);
|
|
|
|
assert_return(!m->sealed, -EPERM);
|
|
|
|
|
2016-05-03 19:16:50 +02:00
|
|
|
r = add_rtattr(m, type, data, len);
|
2015-11-28 03:31:06 +01:00
|
|
|
if (r < 0)
|
|
|
|
return r;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-06-12 16:31:33 +02:00
|
|
|
int sd_netlink_message_append_in_addr(sd_netlink_message *m, unsigned short type, const struct in_addr *data) {
|
2013-12-15 14:00:20 +01:00
|
|
|
int r;
|
|
|
|
|
|
|
|
assert_return(m, -EINVAL);
|
2014-01-24 21:04:05 +01:00
|
|
|
assert_return(!m->sealed, -EPERM);
|
2013-12-15 14:00:20 +01:00
|
|
|
assert_return(data, -EINVAL);
|
|
|
|
|
2015-06-23 11:18:53 +02:00
|
|
|
r = message_attribute_has_type(m, NULL, type, NETLINK_TYPE_IN_ADDR);
|
2013-12-15 14:00:20 +01:00
|
|
|
if (r < 0)
|
|
|
|
return r;
|
|
|
|
|
2013-12-19 21:28:50 +01:00
|
|
|
r = add_rtattr(m, type, data, sizeof(struct in_addr));
|
2013-12-15 14:00:20 +01:00
|
|
|
if (r < 0)
|
|
|
|
return r;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-06-12 16:31:33 +02:00
|
|
|
int sd_netlink_message_append_in6_addr(sd_netlink_message *m, unsigned short type, const struct in6_addr *data) {
|
2013-12-15 14:00:20 +01:00
|
|
|
int r;
|
|
|
|
|
|
|
|
assert_return(m, -EINVAL);
|
2014-01-24 21:04:05 +01:00
|
|
|
assert_return(!m->sealed, -EPERM);
|
2013-12-15 14:00:20 +01:00
|
|
|
assert_return(data, -EINVAL);
|
|
|
|
|
2015-06-23 11:18:53 +02:00
|
|
|
r = message_attribute_has_type(m, NULL, type, NETLINK_TYPE_IN_ADDR);
|
2013-12-15 14:00:20 +01:00
|
|
|
if (r < 0)
|
|
|
|
return r;
|
|
|
|
|
2013-12-19 21:28:50 +01:00
|
|
|
r = add_rtattr(m, type, data, sizeof(struct in6_addr));
|
2013-12-15 14:00:20 +01:00
|
|
|
if (r < 0)
|
|
|
|
return r;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-06-12 16:31:33 +02:00
|
|
|
int sd_netlink_message_append_ether_addr(sd_netlink_message *m, unsigned short type, const struct ether_addr *data) {
|
2013-12-15 14:00:20 +01:00
|
|
|
int r;
|
|
|
|
|
|
|
|
assert_return(m, -EINVAL);
|
2014-01-24 21:04:05 +01:00
|
|
|
assert_return(!m->sealed, -EPERM);
|
2013-12-15 14:00:20 +01:00
|
|
|
assert_return(data, -EINVAL);
|
|
|
|
|
2015-06-23 11:18:53 +02:00
|
|
|
r = message_attribute_has_type(m, NULL, type, NETLINK_TYPE_ETHER_ADDR);
|
2014-03-26 19:25:01 +01:00
|
|
|
if (r < 0)
|
|
|
|
return r;
|
2013-12-15 14:00:20 +01:00
|
|
|
|
2013-12-17 06:13:57 +01:00
|
|
|
r = add_rtattr(m, type, data, ETH_ALEN);
|
2013-12-15 14:00:20 +01:00
|
|
|
if (r < 0)
|
|
|
|
return r;
|
|
|
|
|
|
|
|
return 0;
|
2013-10-21 20:12:52 +02:00
|
|
|
}
|
|
|
|
|
2015-06-12 16:31:33 +02:00
|
|
|
int sd_netlink_message_append_cache_info(sd_netlink_message *m, unsigned short type, const struct ifa_cacheinfo *info) {
|
2014-04-02 21:31:12 +02:00
|
|
|
int r;
|
|
|
|
|
|
|
|
assert_return(m, -EINVAL);
|
|
|
|
assert_return(!m->sealed, -EPERM);
|
|
|
|
assert_return(info, -EINVAL);
|
|
|
|
|
2015-06-23 11:18:53 +02:00
|
|
|
r = message_attribute_has_type(m, NULL, type, NETLINK_TYPE_CACHE_INFO);
|
2014-04-02 21:31:12 +02:00
|
|
|
if (r < 0)
|
|
|
|
return r;
|
|
|
|
|
|
|
|
r = add_rtattr(m, type, info, sizeof(struct ifa_cacheinfo));
|
|
|
|
if (r < 0)
|
|
|
|
return r;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-06-12 16:31:33 +02:00
|
|
|
int sd_netlink_message_open_container(sd_netlink_message *m, unsigned short type) {
|
2014-03-26 19:25:01 +01:00
|
|
|
size_t size;
|
|
|
|
int r;
|
2013-11-21 20:44:29 +01:00
|
|
|
|
2013-10-21 20:12:52 +02:00
|
|
|
assert_return(m, -EINVAL);
|
2014-01-24 21:04:05 +01:00
|
|
|
assert_return(!m->sealed, -EPERM);
|
2014-05-02 22:29:18 +02:00
|
|
|
assert_return(m->n_containers < RTNL_CONTAINER_DEPTH, -ERANGE);
|
2013-11-21 20:44:29 +01:00
|
|
|
|
2015-06-23 11:18:53 +02:00
|
|
|
r = message_attribute_has_type(m, &size, type, NETLINK_TYPE_NESTED);
|
2015-02-08 12:37:05 +01:00
|
|
|
if (r < 0) {
|
|
|
|
const NLTypeSystemUnion *type_system_union;
|
|
|
|
int family;
|
|
|
|
|
2015-06-23 11:18:53 +02:00
|
|
|
r = message_attribute_has_type(m, &size, type, NETLINK_TYPE_UNION);
|
2015-02-08 12:37:05 +01:00
|
|
|
if (r < 0)
|
|
|
|
return r;
|
|
|
|
|
2015-06-13 21:10:39 +02:00
|
|
|
r = sd_rtnl_message_get_family(m, &family);
|
2015-02-08 12:37:05 +01:00
|
|
|
if (r < 0)
|
|
|
|
return r;
|
|
|
|
|
2015-06-26 00:02:55 +02:00
|
|
|
r = type_system_get_type_system_union(m->containers[m->n_containers].type_system, &type_system_union, type);
|
2015-02-08 12:37:05 +01:00
|
|
|
if (r < 0)
|
|
|
|
return r;
|
|
|
|
|
|
|
|
r = type_system_union_protocol_get_type_system(type_system_union,
|
2015-06-26 00:02:55 +02:00
|
|
|
&m->containers[m->n_containers + 1].type_system,
|
2015-02-08 12:37:05 +01:00
|
|
|
family);
|
|
|
|
if (r < 0)
|
|
|
|
return r;
|
|
|
|
} else {
|
2015-06-26 00:02:55 +02:00
|
|
|
r = type_system_get_type_system(m->containers[m->n_containers].type_system,
|
|
|
|
&m->containers[m->n_containers + 1].type_system,
|
2015-02-08 12:37:05 +01:00
|
|
|
type);
|
|
|
|
if (r < 0)
|
|
|
|
return r;
|
|
|
|
}
|
2014-02-13 18:46:48 +01:00
|
|
|
|
2014-10-29 10:52:07 +01:00
|
|
|
r = add_rtattr(m, type | NLA_F_NESTED, NULL, size);
|
2014-03-26 19:25:01 +01:00
|
|
|
if (r < 0)
|
|
|
|
return r;
|
|
|
|
|
2016-02-23 05:32:04 +01:00
|
|
|
m->containers[m->n_containers++].offset = r;
|
2014-05-02 22:29:18 +02:00
|
|
|
|
2014-03-26 19:25:01 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-06-12 16:31:33 +02:00
|
|
|
int sd_netlink_message_open_container_union(sd_netlink_message *m, unsigned short type, const char *key) {
|
2014-03-26 19:25:01 +01:00
|
|
|
const NLTypeSystemUnion *type_system_union;
|
|
|
|
int r;
|
|
|
|
|
|
|
|
assert_return(m, -EINVAL);
|
|
|
|
assert_return(!m->sealed, -EPERM);
|
|
|
|
|
2015-06-26 00:02:55 +02:00
|
|
|
r = type_system_get_type_system_union(m->containers[m->n_containers].type_system, &type_system_union, type);
|
2014-03-26 19:25:01 +01:00
|
|
|
if (r < 0)
|
|
|
|
return r;
|
|
|
|
|
|
|
|
r = type_system_union_get_type_system(type_system_union,
|
2015-06-26 00:02:55 +02:00
|
|
|
&m->containers[m->n_containers + 1].type_system,
|
2014-03-26 19:25:01 +01:00
|
|
|
key);
|
|
|
|
if (r < 0)
|
|
|
|
return r;
|
2013-11-21 20:44:29 +01:00
|
|
|
|
2015-06-12 16:31:33 +02:00
|
|
|
r = sd_netlink_message_append_string(m, type_system_union->match, key);
|
2014-03-26 19:25:01 +01:00
|
|
|
if (r < 0)
|
|
|
|
return r;
|
|
|
|
|
2017-12-18 15:17:06 +01:00
|
|
|
/* do we ever need non-null size */
|
2015-06-23 13:18:18 +02:00
|
|
|
r = add_rtattr(m, type | NLA_F_NESTED, NULL, 0);
|
2014-03-26 19:25:01 +01:00
|
|
|
if (r < 0)
|
|
|
|
return r;
|
|
|
|
|
2016-02-23 05:32:04 +01:00
|
|
|
m->containers[m->n_containers++].offset = r;
|
2014-05-02 22:29:18 +02:00
|
|
|
|
2014-03-26 19:25:01 +01:00
|
|
|
return 0;
|
2013-11-21 20:44:29 +01:00
|
|
|
}
|
|
|
|
|
2015-06-12 16:31:33 +02:00
|
|
|
int sd_netlink_message_close_container(sd_netlink_message *m) {
|
2013-11-21 20:44:29 +01:00
|
|
|
assert_return(m, -EINVAL);
|
2014-01-24 21:04:05 +01:00
|
|
|
assert_return(!m->sealed, -EPERM);
|
2014-01-24 18:44:03 +01:00
|
|
|
assert_return(m->n_containers > 0, -EINVAL);
|
2013-11-21 20:44:29 +01:00
|
|
|
|
2015-06-26 00:02:55 +02:00
|
|
|
m->containers[m->n_containers].type_system = NULL;
|
2017-12-18 15:17:06 +01:00
|
|
|
m->containers[m->n_containers].offset = 0;
|
|
|
|
m->n_containers--;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int sd_netlink_message_open_array(sd_netlink_message *m, uint16_t type) {
|
|
|
|
int r;
|
|
|
|
|
|
|
|
assert_return(m, -EINVAL);
|
|
|
|
assert_return(!m->sealed, -EPERM);
|
|
|
|
assert_return(m->n_containers > 0, -EINVAL);
|
|
|
|
|
|
|
|
r = add_rtattr(m, type | NLA_F_NESTED, NULL, 0);
|
|
|
|
if (r < 0)
|
|
|
|
return r;
|
|
|
|
|
|
|
|
m->containers[m->n_containers].offset = r;
|
|
|
|
m->n_containers++;
|
|
|
|
m->containers[m->n_containers].type_system = m->containers[m->n_containers - 1].type_system;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int sd_netlink_message_cancel_array(sd_netlink_message *m) {
|
|
|
|
unsigned i;
|
|
|
|
uint32_t rta_len;
|
|
|
|
|
|
|
|
assert_return(m, -EINVAL);
|
|
|
|
assert_return(!m->sealed, -EPERM);
|
|
|
|
assert_return(m->n_containers > 1, -EINVAL);
|
|
|
|
|
|
|
|
rta_len = GET_CONTAINER(m, (m->n_containers - 1))->rta_len;
|
|
|
|
|
|
|
|
for (i = 0; i < m->n_containers; i++)
|
|
|
|
GET_CONTAINER(m, i)->rta_len -= rta_len;
|
|
|
|
|
|
|
|
m->hdr->nlmsg_len -= rta_len;
|
|
|
|
|
2016-02-23 05:32:04 +01:00
|
|
|
m->n_containers--;
|
2017-12-18 15:17:06 +01:00
|
|
|
m->containers[m->n_containers].type_system = NULL;
|
2013-11-21 20:44:29 +01:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-06-23 13:13:20 +02:00
|
|
|
static int netlink_message_read_internal(sd_netlink_message *m, unsigned short type, void **data, bool *net_byteorder) {
|
2015-06-26 00:02:55 +02:00
|
|
|
struct netlink_attribute *attribute;
|
2014-03-23 21:44:47 +01:00
|
|
|
struct rtattr *rta;
|
|
|
|
|
2014-03-05 16:13:25 +01:00
|
|
|
assert_return(m, -EINVAL);
|
|
|
|
assert_return(m->sealed, -EPERM);
|
|
|
|
assert_return(data, -EINVAL);
|
2018-01-23 15:47:18 +01:00
|
|
|
|
2015-08-06 13:32:07 +02:00
|
|
|
assert(m->n_containers < RTNL_CONTAINER_DEPTH);
|
2015-06-26 00:02:55 +02:00
|
|
|
assert(m->containers[m->n_containers].attributes);
|
|
|
|
assert(type < m->containers[m->n_containers].n_attributes);
|
2014-03-05 16:13:25 +01:00
|
|
|
|
2015-06-26 00:02:55 +02:00
|
|
|
attribute = &m->containers[m->n_containers].attributes[type];
|
|
|
|
|
2018-01-23 15:47:18 +01:00
|
|
|
if (attribute->offset == 0)
|
2014-03-05 16:13:25 +01:00
|
|
|
return -ENODATA;
|
|
|
|
|
2015-06-26 00:02:55 +02:00
|
|
|
rta = (struct rtattr*)((uint8_t *) m->hdr + attribute->offset);
|
2014-03-05 16:13:25 +01:00
|
|
|
|
2014-03-23 21:44:47 +01:00
|
|
|
*data = RTA_DATA(rta);
|
|
|
|
|
2015-06-23 13:13:20 +02:00
|
|
|
if (net_byteorder)
|
|
|
|
*net_byteorder = attribute->net_byteorder;
|
|
|
|
|
2014-03-23 21:44:47 +01:00
|
|
|
return RTA_PAYLOAD(rta);
|
2014-03-05 16:13:25 +01:00
|
|
|
}
|
|
|
|
|
2015-06-12 16:31:33 +02:00
|
|
|
int sd_netlink_message_read_string(sd_netlink_message *m, unsigned short type, const char **data) {
|
2014-03-05 16:13:25 +01:00
|
|
|
int r;
|
|
|
|
void *attr_data;
|
|
|
|
|
2014-12-02 00:59:02 +01:00
|
|
|
assert_return(m, -EINVAL);
|
|
|
|
|
2015-06-23 11:18:53 +02:00
|
|
|
r = message_attribute_has_type(m, NULL, type, NETLINK_TYPE_STRING);
|
2014-03-26 19:25:01 +01:00
|
|
|
if (r < 0)
|
|
|
|
return r;
|
2014-03-05 16:13:25 +01:00
|
|
|
|
2015-06-23 13:13:20 +02:00
|
|
|
r = netlink_message_read_internal(m, type, &attr_data, NULL);
|
2014-03-23 21:44:47 +01:00
|
|
|
if (r < 0)
|
2014-03-05 16:13:25 +01:00
|
|
|
return r;
|
2014-03-23 21:44:47 +01:00
|
|
|
else if (strnlen(attr_data, r) >= (size_t) r)
|
|
|
|
return -EIO;
|
2014-03-05 16:13:25 +01:00
|
|
|
|
2014-12-02 00:59:02 +01:00
|
|
|
if (data)
|
|
|
|
*data = (const char *) attr_data;
|
2014-03-05 16:13:25 +01:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-06-12 16:31:33 +02:00
|
|
|
int sd_netlink_message_read_u8(sd_netlink_message *m, unsigned short type, uint8_t *data) {
|
2014-03-05 16:13:25 +01:00
|
|
|
int r;
|
|
|
|
void *attr_data;
|
|
|
|
|
2014-12-02 00:59:02 +01:00
|
|
|
assert_return(m, -EINVAL);
|
|
|
|
|
2015-06-23 11:18:53 +02:00
|
|
|
r = message_attribute_has_type(m, NULL, type, NETLINK_TYPE_U8);
|
2014-03-26 19:25:01 +01:00
|
|
|
if (r < 0)
|
|
|
|
return r;
|
2014-03-05 16:13:25 +01:00
|
|
|
|
2015-06-23 13:13:20 +02:00
|
|
|
r = netlink_message_read_internal(m, type, &attr_data, NULL);
|
2014-03-23 21:44:47 +01:00
|
|
|
if (r < 0)
|
2014-03-05 16:13:25 +01:00
|
|
|
return r;
|
2014-03-23 21:44:47 +01:00
|
|
|
else if ((size_t) r < sizeof(uint8_t))
|
|
|
|
return -EIO;
|
2014-03-05 16:13:25 +01:00
|
|
|
|
2014-12-02 00:59:02 +01:00
|
|
|
if (data)
|
|
|
|
*data = *(uint8_t *) attr_data;
|
2014-03-05 16:13:25 +01:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-06-12 16:31:33 +02:00
|
|
|
int sd_netlink_message_read_u16(sd_netlink_message *m, unsigned short type, uint16_t *data) {
|
2014-03-05 16:13:25 +01:00
|
|
|
void *attr_data;
|
2015-06-23 13:13:20 +02:00
|
|
|
bool net_byteorder;
|
|
|
|
int r;
|
2014-03-05 16:13:25 +01:00
|
|
|
|
2014-12-02 00:59:02 +01:00
|
|
|
assert_return(m, -EINVAL);
|
|
|
|
|
2015-06-23 11:18:53 +02:00
|
|
|
r = message_attribute_has_type(m, NULL, type, NETLINK_TYPE_U16);
|
2014-03-26 19:25:01 +01:00
|
|
|
if (r < 0)
|
|
|
|
return r;
|
2014-03-05 16:13:25 +01:00
|
|
|
|
2015-06-23 13:13:20 +02:00
|
|
|
r = netlink_message_read_internal(m, type, &attr_data, &net_byteorder);
|
2014-03-23 21:44:47 +01:00
|
|
|
if (r < 0)
|
2014-03-05 16:13:25 +01:00
|
|
|
return r;
|
2014-03-23 21:44:47 +01:00
|
|
|
else if ((size_t) r < sizeof(uint16_t))
|
|
|
|
return -EIO;
|
2014-03-05 16:13:25 +01:00
|
|
|
|
2015-06-23 13:13:20 +02:00
|
|
|
if (data) {
|
|
|
|
if (net_byteorder)
|
|
|
|
*data = be16toh(*(uint16_t *) attr_data);
|
|
|
|
else
|
|
|
|
*data = *(uint16_t *) attr_data;
|
|
|
|
}
|
2014-03-05 16:13:25 +01:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-06-12 16:31:33 +02:00
|
|
|
int sd_netlink_message_read_u32(sd_netlink_message *m, unsigned short type, uint32_t *data) {
|
2014-03-05 16:13:25 +01:00
|
|
|
void *attr_data;
|
2015-06-23 13:13:20 +02:00
|
|
|
bool net_byteorder;
|
|
|
|
int r;
|
2014-03-05 16:13:25 +01:00
|
|
|
|
2014-12-02 00:59:02 +01:00
|
|
|
assert_return(m, -EINVAL);
|
|
|
|
|
2015-06-23 11:18:53 +02:00
|
|
|
r = message_attribute_has_type(m, NULL, type, NETLINK_TYPE_U32);
|
2014-03-26 19:25:01 +01:00
|
|
|
if (r < 0)
|
|
|
|
return r;
|
2014-03-05 16:13:25 +01:00
|
|
|
|
2015-06-23 13:13:20 +02:00
|
|
|
r = netlink_message_read_internal(m, type, &attr_data, &net_byteorder);
|
2014-03-23 21:44:47 +01:00
|
|
|
if (r < 0)
|
2014-03-05 16:13:25 +01:00
|
|
|
return r;
|
2014-03-23 21:44:47 +01:00
|
|
|
else if ((size_t)r < sizeof(uint32_t))
|
|
|
|
return -EIO;
|
2014-03-05 16:13:25 +01:00
|
|
|
|
2015-06-23 13:13:20 +02:00
|
|
|
if (data) {
|
|
|
|
if (net_byteorder)
|
|
|
|
*data = be32toh(*(uint32_t *) attr_data);
|
|
|
|
else
|
|
|
|
*data = *(uint32_t *) attr_data;
|
|
|
|
}
|
2014-03-05 16:13:25 +01:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-06-12 16:31:33 +02:00
|
|
|
int sd_netlink_message_read_ether_addr(sd_netlink_message *m, unsigned short type, struct ether_addr *data) {
|
2014-03-07 16:08:02 +01:00
|
|
|
int r;
|
|
|
|
void *attr_data;
|
|
|
|
|
2014-12-02 00:59:02 +01:00
|
|
|
assert_return(m, -EINVAL);
|
|
|
|
|
2015-06-23 11:18:53 +02:00
|
|
|
r = message_attribute_has_type(m, NULL, type, NETLINK_TYPE_ETHER_ADDR);
|
2014-03-26 19:25:01 +01:00
|
|
|
if (r < 0)
|
|
|
|
return r;
|
2014-03-07 16:08:02 +01:00
|
|
|
|
2015-06-23 13:13:20 +02:00
|
|
|
r = netlink_message_read_internal(m, type, &attr_data, NULL);
|
2014-03-23 21:44:47 +01:00
|
|
|
if (r < 0)
|
2014-03-07 16:08:02 +01:00
|
|
|
return r;
|
2014-03-23 21:44:47 +01:00
|
|
|
else if ((size_t)r < sizeof(struct ether_addr))
|
|
|
|
return -EIO;
|
2014-03-07 16:08:02 +01:00
|
|
|
|
2014-12-02 00:59:02 +01:00
|
|
|
if (data)
|
|
|
|
memcpy(data, attr_data, sizeof(struct ether_addr));
|
2014-03-07 16:08:02 +01:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-06-12 16:31:33 +02:00
|
|
|
int sd_netlink_message_read_cache_info(sd_netlink_message *m, unsigned short type, struct ifa_cacheinfo *info) {
|
2014-04-02 21:31:12 +02:00
|
|
|
int r;
|
|
|
|
void *attr_data;
|
|
|
|
|
2014-12-02 00:59:02 +01:00
|
|
|
assert_return(m, -EINVAL);
|
|
|
|
|
2015-06-23 11:18:53 +02:00
|
|
|
r = message_attribute_has_type(m, NULL, type, NETLINK_TYPE_CACHE_INFO);
|
2014-04-02 21:31:12 +02:00
|
|
|
if (r < 0)
|
|
|
|
return r;
|
|
|
|
|
2015-06-23 13:13:20 +02:00
|
|
|
r = netlink_message_read_internal(m, type, &attr_data, NULL);
|
2014-04-02 21:31:12 +02:00
|
|
|
if (r < 0)
|
|
|
|
return r;
|
|
|
|
else if ((size_t)r < sizeof(struct ifa_cacheinfo))
|
|
|
|
return -EIO;
|
|
|
|
|
2014-12-02 00:59:02 +01:00
|
|
|
if (info)
|
|
|
|
memcpy(info, attr_data, sizeof(struct ifa_cacheinfo));
|
2014-04-02 21:31:12 +02:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-06-12 16:31:33 +02:00
|
|
|
int sd_netlink_message_read_in_addr(sd_netlink_message *m, unsigned short type, struct in_addr *data) {
|
2014-03-07 16:08:02 +01:00
|
|
|
int r;
|
|
|
|
void *attr_data;
|
|
|
|
|
2014-12-02 00:59:02 +01:00
|
|
|
assert_return(m, -EINVAL);
|
|
|
|
|
2015-06-23 11:18:53 +02:00
|
|
|
r = message_attribute_has_type(m, NULL, type, NETLINK_TYPE_IN_ADDR);
|
2014-03-26 19:25:01 +01:00
|
|
|
if (r < 0)
|
|
|
|
return r;
|
2014-03-07 16:08:02 +01:00
|
|
|
|
2015-06-23 13:13:20 +02:00
|
|
|
r = netlink_message_read_internal(m, type, &attr_data, NULL);
|
2014-03-23 21:44:47 +01:00
|
|
|
if (r < 0)
|
2014-03-07 16:08:02 +01:00
|
|
|
return r;
|
2014-03-23 21:44:47 +01:00
|
|
|
else if ((size_t)r < sizeof(struct in_addr))
|
|
|
|
return -EIO;
|
2014-03-07 16:08:02 +01:00
|
|
|
|
2014-12-02 00:59:02 +01:00
|
|
|
if (data)
|
|
|
|
memcpy(data, attr_data, sizeof(struct in_addr));
|
2014-03-07 16:08:02 +01:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-06-12 16:31:33 +02:00
|
|
|
int sd_netlink_message_read_in6_addr(sd_netlink_message *m, unsigned short type, struct in6_addr *data) {
|
2014-03-07 16:08:02 +01:00
|
|
|
int r;
|
|
|
|
void *attr_data;
|
|
|
|
|
2014-12-02 00:59:02 +01:00
|
|
|
assert_return(m, -EINVAL);
|
|
|
|
|
2015-06-23 11:18:53 +02:00
|
|
|
r = message_attribute_has_type(m, NULL, type, NETLINK_TYPE_IN_ADDR);
|
2014-03-26 19:25:01 +01:00
|
|
|
if (r < 0)
|
|
|
|
return r;
|
2014-03-07 16:08:02 +01:00
|
|
|
|
2015-06-23 13:13:20 +02:00
|
|
|
r = netlink_message_read_internal(m, type, &attr_data, NULL);
|
2014-03-23 21:45:46 +01:00
|
|
|
if (r < 0)
|
2014-03-07 16:08:02 +01:00
|
|
|
return r;
|
2014-03-23 21:45:46 +01:00
|
|
|
else if ((size_t)r < sizeof(struct in6_addr))
|
|
|
|
return -EIO;
|
2014-03-07 16:08:02 +01:00
|
|
|
|
2014-12-02 00:59:02 +01:00
|
|
|
if (data)
|
|
|
|
memcpy(data, attr_data, sizeof(struct in6_addr));
|
2014-03-07 16:08:02 +01:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-06-26 00:02:55 +02:00
|
|
|
static int netlink_container_parse(sd_netlink_message *m,
|
|
|
|
struct netlink_container *container,
|
|
|
|
int count,
|
|
|
|
struct rtattr *rta,
|
|
|
|
unsigned int rt_len) {
|
|
|
|
_cleanup_free_ struct netlink_attribute *attributes = NULL;
|
2015-06-26 00:07:25 +02:00
|
|
|
|
2015-06-26 00:02:55 +02:00
|
|
|
attributes = new0(struct netlink_attribute, count);
|
2016-02-23 18:52:52 +01:00
|
|
|
if (!attributes)
|
2015-06-26 00:07:25 +02:00
|
|
|
return -ENOMEM;
|
|
|
|
|
|
|
|
for (; RTA_OK(rta, rt_len); rta = RTA_NEXT(rta, rt_len)) {
|
2015-06-26 00:02:55 +02:00
|
|
|
unsigned short type;
|
|
|
|
|
2015-06-26 00:07:25 +02:00
|
|
|
type = RTA_TYPE(rta);
|
|
|
|
|
|
|
|
/* if the kernel is newer than the headers we used
|
2015-06-26 00:02:55 +02:00
|
|
|
when building, we ignore out-of-range attributes */
|
2015-06-26 00:07:25 +02:00
|
|
|
if (type >= count)
|
|
|
|
continue;
|
|
|
|
|
2018-01-23 15:47:18 +01:00
|
|
|
if (attributes[type].offset != 0)
|
2015-06-26 00:07:25 +02:00
|
|
|
log_debug("rtnl: message parse - overwriting repeated attribute");
|
|
|
|
|
2015-06-26 00:02:55 +02:00
|
|
|
attributes[type].offset = (uint8_t *) rta - (uint8_t *) m->hdr;
|
2015-06-23 13:13:20 +02:00
|
|
|
attributes[type].nested = RTA_FLAGS(rta) & NLA_F_NESTED;
|
|
|
|
attributes[type].net_byteorder = RTA_FLAGS(rta) & NLA_F_NET_BYTEORDER;
|
2015-06-26 00:07:25 +02:00
|
|
|
}
|
|
|
|
|
2018-03-22 16:53:26 +01:00
|
|
|
container->attributes = TAKE_PTR(attributes);
|
2015-06-26 00:02:55 +02:00
|
|
|
container->n_attributes = count;
|
2015-06-26 00:07:25 +02:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-06-23 11:03:10 +02:00
|
|
|
int sd_netlink_message_enter_container(sd_netlink_message *m, unsigned short type_id) {
|
2014-03-26 19:25:01 +01:00
|
|
|
const NLType *nl_type;
|
|
|
|
const NLTypeSystem *type_system;
|
2014-03-23 21:45:46 +01:00
|
|
|
void *container;
|
2015-06-23 11:03:10 +02:00
|
|
|
uint16_t type;
|
2014-03-26 19:25:01 +01:00
|
|
|
size_t size;
|
|
|
|
int r;
|
2014-03-23 21:45:46 +01:00
|
|
|
|
|
|
|
assert_return(m, -EINVAL);
|
|
|
|
assert_return(m->n_containers < RTNL_CONTAINER_DEPTH, -EINVAL);
|
|
|
|
|
2015-06-26 00:02:55 +02:00
|
|
|
r = type_system_get_type(m->containers[m->n_containers].type_system,
|
2014-03-26 19:25:01 +01:00
|
|
|
&nl_type,
|
2015-06-23 11:03:10 +02:00
|
|
|
type_id);
|
2014-03-23 21:45:46 +01:00
|
|
|
if (r < 0)
|
|
|
|
return r;
|
|
|
|
|
2015-06-23 11:03:10 +02:00
|
|
|
type = type_get_type(nl_type);
|
|
|
|
|
|
|
|
if (type == NETLINK_TYPE_NESTED) {
|
2015-06-26 00:02:55 +02:00
|
|
|
r = type_system_get_type_system(m->containers[m->n_containers].type_system,
|
2014-03-26 19:25:01 +01:00
|
|
|
&type_system,
|
2015-06-23 11:03:10 +02:00
|
|
|
type_id);
|
2014-03-26 19:25:01 +01:00
|
|
|
if (r < 0)
|
|
|
|
return r;
|
2015-06-23 11:03:10 +02:00
|
|
|
} else if (type == NETLINK_TYPE_UNION) {
|
2014-03-26 19:25:01 +01:00
|
|
|
const NLTypeSystemUnion *type_system_union;
|
|
|
|
|
2015-06-26 00:02:55 +02:00
|
|
|
r = type_system_get_type_system_union(m->containers[m->n_containers].type_system,
|
2014-03-26 19:25:01 +01:00
|
|
|
&type_system_union,
|
2015-06-23 11:03:10 +02:00
|
|
|
type_id);
|
2014-03-26 19:25:01 +01:00
|
|
|
if (r < 0)
|
|
|
|
return r;
|
|
|
|
|
2015-02-08 12:37:05 +01:00
|
|
|
switch (type_system_union->match_type) {
|
|
|
|
case NL_MATCH_SIBLING:
|
|
|
|
{
|
|
|
|
const char *key;
|
|
|
|
|
2015-06-12 16:31:33 +02:00
|
|
|
r = sd_netlink_message_read_string(m, type_system_union->match, &key);
|
2015-02-08 12:37:05 +01:00
|
|
|
if (r < 0)
|
|
|
|
return r;
|
|
|
|
|
|
|
|
r = type_system_union_get_type_system(type_system_union,
|
|
|
|
&type_system,
|
|
|
|
key);
|
|
|
|
if (r < 0)
|
|
|
|
return r;
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case NL_MATCH_PROTOCOL:
|
|
|
|
{
|
|
|
|
int family;
|
|
|
|
|
2015-06-13 21:10:39 +02:00
|
|
|
r = sd_rtnl_message_get_family(m, &family);
|
2015-02-08 12:37:05 +01:00
|
|
|
if (r < 0)
|
|
|
|
return r;
|
|
|
|
|
|
|
|
r = type_system_union_protocol_get_type_system(type_system_union,
|
|
|
|
&type_system,
|
|
|
|
family);
|
|
|
|
if (r < 0)
|
|
|
|
return r;
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
default:
|
2015-06-12 16:31:33 +02:00
|
|
|
assert_not_reached("sd-netlink: invalid type system union type");
|
2015-02-08 12:37:05 +01:00
|
|
|
}
|
2014-03-26 19:25:01 +01:00
|
|
|
} else
|
|
|
|
return -EINVAL;
|
|
|
|
|
2015-06-23 13:13:20 +02:00
|
|
|
r = netlink_message_read_internal(m, type_id, &container, NULL);
|
2014-03-23 21:45:46 +01:00
|
|
|
if (r < 0)
|
|
|
|
return r;
|
2014-03-26 19:25:01 +01:00
|
|
|
else
|
|
|
|
size = (size_t)r;
|
2014-03-23 21:45:46 +01:00
|
|
|
|
2016-02-23 05:32:04 +01:00
|
|
|
m->n_containers++;
|
2014-03-23 21:45:46 +01:00
|
|
|
|
2015-06-26 00:02:55 +02:00
|
|
|
r = netlink_container_parse(m,
|
|
|
|
&m->containers[m->n_containers],
|
|
|
|
type_system_get_count(type_system),
|
|
|
|
container,
|
|
|
|
size);
|
2014-03-26 19:25:01 +01:00
|
|
|
if (r < 0) {
|
2016-02-23 05:32:04 +01:00
|
|
|
m->n_containers--;
|
2014-03-23 21:45:46 +01:00
|
|
|
return r;
|
2014-03-26 19:25:01 +01:00
|
|
|
}
|
2014-03-23 21:45:46 +01:00
|
|
|
|
2015-06-26 00:02:55 +02:00
|
|
|
m->containers[m->n_containers].type_system = type_system;
|
2014-03-23 21:45:46 +01:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-06-12 16:31:33 +02:00
|
|
|
int sd_netlink_message_exit_container(sd_netlink_message *m) {
|
2014-01-24 21:04:05 +01:00
|
|
|
assert_return(m, -EINVAL);
|
|
|
|
assert_return(m->sealed, -EINVAL);
|
|
|
|
assert_return(m->n_containers > 0, -EINVAL);
|
|
|
|
|
2015-09-08 18:43:11 +02:00
|
|
|
m->containers[m->n_containers].attributes = mfree(m->containers[m->n_containers].attributes);
|
2015-06-26 00:02:55 +02:00
|
|
|
m->containers[m->n_containers].type_system = NULL;
|
2014-03-23 21:45:46 +01:00
|
|
|
|
2016-02-23 05:32:04 +01:00
|
|
|
m->n_containers--;
|
2014-01-24 21:04:05 +01:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-06-12 16:31:33 +02:00
|
|
|
uint32_t rtnl_message_get_serial(sd_netlink_message *m) {
|
2013-10-21 20:12:52 +02:00
|
|
|
assert(m);
|
2013-12-06 15:13:34 +01:00
|
|
|
assert(m->hdr);
|
2013-10-21 20:12:52 +02:00
|
|
|
|
|
|
|
return m->hdr->nlmsg_seq;
|
|
|
|
}
|
|
|
|
|
2015-06-12 16:31:33 +02:00
|
|
|
int sd_netlink_message_is_error(sd_netlink_message *m) {
|
2014-12-08 19:54:06 +01:00
|
|
|
assert_return(m, 0);
|
|
|
|
assert_return(m->hdr, 0);
|
|
|
|
|
|
|
|
return m->hdr->nlmsg_type == NLMSG_ERROR;
|
|
|
|
}
|
|
|
|
|
2015-06-12 16:31:33 +02:00
|
|
|
int sd_netlink_message_get_errno(sd_netlink_message *m) {
|
2013-10-21 20:12:52 +02:00
|
|
|
struct nlmsgerr *err;
|
|
|
|
|
2013-11-13 23:18:20 +01:00
|
|
|
assert_return(m, -EINVAL);
|
2013-12-06 15:13:34 +01:00
|
|
|
assert_return(m->hdr, -EINVAL);
|
2013-10-21 20:12:52 +02:00
|
|
|
|
2015-06-12 16:31:33 +02:00
|
|
|
if (!sd_netlink_message_is_error(m))
|
2013-10-21 20:12:52 +02:00
|
|
|
return 0;
|
|
|
|
|
|
|
|
err = NLMSG_DATA(m->hdr);
|
|
|
|
|
|
|
|
return err->error;
|
|
|
|
}
|
|
|
|
|
2015-06-12 16:31:33 +02:00
|
|
|
int sd_netlink_message_rewind(sd_netlink_message *m) {
|
2015-06-23 11:03:10 +02:00
|
|
|
const NLType *nl_type;
|
2017-12-18 15:17:06 +01:00
|
|
|
const NLTypeSystem *type_system_root;
|
2015-06-23 11:03:10 +02:00
|
|
|
uint16_t type;
|
|
|
|
size_t size;
|
2014-03-23 21:45:46 +01:00
|
|
|
unsigned i;
|
|
|
|
int r;
|
2013-12-06 17:19:55 +01:00
|
|
|
|
|
|
|
assert_return(m, -EINVAL);
|
|
|
|
|
2014-03-23 21:45:46 +01:00
|
|
|
/* don't allow appending to message once parsed */
|
|
|
|
if (!m->sealed)
|
|
|
|
rtnl_message_seal(m);
|
|
|
|
|
2017-12-18 15:17:06 +01:00
|
|
|
type_system_root = type_system_get_root(m->protocol);
|
|
|
|
|
2015-09-09 14:23:02 +02:00
|
|
|
for (i = 1; i <= m->n_containers; i++)
|
2015-09-08 18:43:11 +02:00
|
|
|
m->containers[i].attributes = mfree(m->containers[i].attributes);
|
2014-03-23 21:45:46 +01:00
|
|
|
|
|
|
|
m->n_containers = 0;
|
|
|
|
|
2015-09-08 23:03:38 +02:00
|
|
|
if (m->containers[0].attributes)
|
2014-03-23 21:45:46 +01:00
|
|
|
/* top-level attributes have already been parsed */
|
|
|
|
return 0;
|
|
|
|
|
2014-03-26 19:25:01 +01:00
|
|
|
assert(m->hdr);
|
|
|
|
|
2017-12-18 15:17:06 +01:00
|
|
|
r = type_system_get_type(type_system_root, &nl_type, m->hdr->nlmsg_type);
|
2014-03-26 19:25:01 +01:00
|
|
|
if (r < 0)
|
|
|
|
return r;
|
|
|
|
|
2015-06-23 11:03:10 +02:00
|
|
|
type = type_get_type(nl_type);
|
|
|
|
size = type_get_size(nl_type);
|
|
|
|
|
|
|
|
if (type == NETLINK_TYPE_NESTED) {
|
2015-06-23 10:51:25 +02:00
|
|
|
const NLTypeSystem *type_system;
|
2014-03-26 19:25:01 +01:00
|
|
|
|
2015-06-23 11:03:10 +02:00
|
|
|
type_get_type_system(nl_type, &type_system);
|
2014-03-26 19:25:01 +01:00
|
|
|
|
2015-06-26 00:02:55 +02:00
|
|
|
m->containers[0].type_system = type_system;
|
2014-03-26 19:25:01 +01:00
|
|
|
|
2015-06-26 00:02:55 +02:00
|
|
|
r = netlink_container_parse(m,
|
|
|
|
&m->containers[m->n_containers],
|
|
|
|
type_system_get_count(type_system),
|
|
|
|
(struct rtattr*)((uint8_t*)NLMSG_DATA(m->hdr) + NLMSG_ALIGN(size)),
|
|
|
|
NLMSG_PAYLOAD(m->hdr, size));
|
2014-03-26 19:25:01 +01:00
|
|
|
if (r < 0)
|
|
|
|
return r;
|
2013-12-06 17:19:55 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
2014-03-23 21:45:46 +01:00
|
|
|
|
2015-06-12 16:31:33 +02:00
|
|
|
void rtnl_message_seal(sd_netlink_message *m) {
|
2014-03-23 21:45:46 +01:00
|
|
|
assert(m);
|
|
|
|
assert(!m->sealed);
|
|
|
|
|
|
|
|
m->sealed = true;
|
|
|
|
}
|
2014-04-13 21:37:53 +02:00
|
|
|
|
2015-06-12 16:31:33 +02:00
|
|
|
sd_netlink_message *sd_netlink_message_next(sd_netlink_message *m) {
|
2014-04-13 21:37:53 +02:00
|
|
|
assert_return(m, NULL);
|
|
|
|
|
|
|
|
return m->next;
|
|
|
|
}
|