rtnl-message.c revision 15411c0cb1192799b37ec8f25d6f30e8d7292fc6
/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
/***
This file is part of systemd.
Copyright 2013 Tom Gundersen <teg@jklm.no>
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 <stdbool.h>
#include <unistd.h>
#include "util.h"
#include "refcnt.h"
#include "missing.h"
#include "sd-rtnl.h"
#include "rtnl-util.h"
#include "rtnl-internal.h"
#include "rtnl-types.h"
#define GET_CONTAINER(m, i) ((i) < (m)->n_containers ? (struct rtattr*)((uint8_t*)(m)->hdr + (m)->container_offsets[i]) : NULL)
#define PUSH_CONTAINER(m, new) (m)->container_offsets[(m)->n_containers ++] = (uint8_t*)(new) - (uint8_t*)(m)->hdr;
sd_rtnl_message *m;
/* Note that 'rtnl' is currently unused, if we start using it internally
we must take care to avoid problems due to mutual references between
busses and their queued messages. See sd-bus.
*/
if (!m)
return -ENOMEM;
m->n_ref = REFCNT_INIT;
m->sealed = false;
*ret = m;
return 0;
}
int r;
if (r < 0)
return r;
r = message_new_empty(rtnl, &m);
if (r < 0)
return r;
if (!m->hdr)
return -ENOMEM;
*ret = m;
m = NULL;
return 0;
}
assert_return(m, -EINVAL);
return -ERANGE;
return 0;
}
assert_return(m, -EINVAL);
return -ERANGE;
return 0;
}
assert_return(m, -EINVAL);
return 0;
}
assert_return(m, -EINVAL);
return 0;
}
assert_return(m, -EINVAL);
return 0;
}
assert_return(m, -EINVAL);
return 0;
}
unsigned char rtm_protocol) {
int r;
if (r < 0)
return r;
if (nlmsg_type == RTM_NEWROUTE)
return 0;
}
assert_return(m, -EINVAL);
return 0;
}
assert_return(m, -EINVAL);
return 0;
}
assert_return(m, -EINVAL);
return 0;
}
assert_return(m, -EINVAL);
return 0;
}
assert_return(m, -EINVAL);
return 0;
}
assert_return(m, -EINVAL);
return 0;
}
int sd_rtnl_message_new_neigh(sd_rtnl *rtnl, sd_rtnl_message **ret, uint16_t nlmsg_type, int index, int ndm_family) {
int r;
ndm_family == AF_INET6 ||
if (r < 0)
return r;
if (nlmsg_type == RTM_NEWNEIGH)
return 0;
}
assert_return(m, -EINVAL);
return 0;
}
assert_return(m, -EINVAL);
return 0;
}
assert_return(m, -EINVAL);
return 0;
}
int r;
if (r < 0)
return r;
if (nlmsg_type == RTM_NEWLINK)
return 0;
}
assert_return(m, -EINVAL);
-EINVAL);
if (dump)
else
return 0;
}
assert_return(m, -EINVAL);
return -ERANGE;
return 0;
}
assert_return(m, -EINVAL);
return 0;
}
assert_return(m, -EINVAL);
return 0;
}
assert_return(m, -EINVAL);
return 0;
}
assert_return(m, -EINVAL);
return 0;
}
assert_return(m, -EINVAL);
return 0;
}
assert_return(m, -EINVAL);
return 0;
}
assert_return(m, -EINVAL);
return 0;
}
int family) {
int r;
if (r < 0)
return r;
if (nlmsg_type == RTM_GETADDR)
return 0;
}
int r;
if (r < 0)
return r;
return 0;
}
if (m)
return m;
}
if (m && REFCNT_DEC(m->n_ref) == 0) {
unsigned i;
for (i = 0; i <= m->n_containers; i++)
free(m->rta_offset_tb[i]);
free(m);
}
return NULL;
}
assert_return(m, -EINVAL);
return 0;
}
assert_return(m, -EINVAL);
return 0;
return 0;
return 0;
return 0;
}
return -EOPNOTSUPP;
}
int sd_rtnl_message_is_broadcast(sd_rtnl_message *m) {
assert_return(m, -EINVAL);
}
assert_return(m, -EINVAL);
return 0;
}
assert_return(m, -EINVAL);
return 0;
}
assert_return(m, -EINVAL);
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) {
char *padding;
unsigned i;
int offset;
assert(m);
/* get offset of the new attribute */
/* get the size of the new rta attribute (with padding at the end) */
/* get the new message size (with padding at the end) */
/* realloc to fit the new attribute */
if (!new_hdr)
return -ENOMEM;
/* get pointer to the attribute we are about to add */
/* if we are inside containers, extend them */
for (i = 0; i < m->n_containers; i++)
/* fill in the attribute */
if (data)
/* 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)
*/
else {
/* if no data was passed, make sure we still initialize the padding
note that we can have data_length > 0 (used by some containers) */
}
/* make sure also the padding at the end of the message is initialized */
/* update message size */
return offset;
}
static int message_attribute_has_type(sd_rtnl_message *m, uint16_t attribute_type, uint16_t data_type) {
int r;
if (r < 0)
return r;
return -EINVAL;
}
int r;
assert_return(m, -EINVAL);
if (r < 0)
return r;
else
if (size) {
return -EINVAL;
} else
if (r < 0)
return r;
return 0;
}
int r;
assert_return(m, -EINVAL);
if (r < 0)
return r;
if (r < 0)
return r;
return 0;
}
int r;
assert_return(m, -EINVAL);
if (r < 0)
return r;
if (r < 0)
return r;
return 0;
}
int r;
assert_return(m, -EINVAL);
if (r < 0)
return r;
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) {
int r;
assert_return(m, -EINVAL);
if (r < 0)
return r;
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) {
int r;
assert_return(m, -EINVAL);
if (r < 0)
return r;
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) {
int r;
assert_return(m, -EINVAL);
if (r < 0)
return r;
if (r < 0)
return r;
return 0;
}
int sd_rtnl_message_append_cache_info(sd_rtnl_message *m, unsigned short type, const struct ifa_cacheinfo *info) {
int r;
assert_return(m, -EINVAL);
if (r < 0)
return r;
if (r < 0)
return r;
return 0;
}
int r;
assert_return(m, -EINVAL);
if (r < 0) {
const NLTypeSystemUnion *type_system_union;
int family;
if (r < 0)
return r;
r = sd_rtnl_message_get_family(m, &family);
if (r < 0)
return r;
r = type_system_get_type_system_union(m->container_type_system[m->n_containers], &type_system_union, type);
if (r < 0)
return r;
family);
if (r < 0)
return r;
} else {
type);
if (r < 0)
return r;
}
if (r < 0)
return r;
m->container_offsets[m->n_containers ++] = r;
return 0;
}
int sd_rtnl_message_open_container_union(sd_rtnl_message *m, unsigned short type, const char *key) {
const NLTypeSystemUnion *type_system_union;
int r;
assert_return(m, -EINVAL);
r = type_system_get_type_system_union(m->container_type_system[m->n_containers], &type_system_union, type);
if (r < 0)
return r;
key);
if (r < 0)
return r;
if (r < 0)
return r;
/* do we evere need non-null size */
if (r < 0)
return r;
m->container_offsets[m->n_containers ++] = r;
return 0;
}
assert_return(m, -EINVAL);
m->n_containers --;
return 0;
}
assert_return(m, -EINVAL);
return -ENODATA;
return RTA_PAYLOAD(rta);
}
int r;
void *attr_data;
assert_return(m, -EINVAL);
if (r < 0)
return r;
if (r < 0)
return r;
return -EIO;
if (data)
return 0;
}
int r;
void *attr_data;
assert_return(m, -EINVAL);
if (r < 0)
return r;
if (r < 0)
return r;
return -EIO;
if (data)
return 0;
}
int r;
void *attr_data;
assert_return(m, -EINVAL);
if (r < 0)
return r;
if (r < 0)
return r;
return -EIO;
if (data)
return 0;
}
int r;
void *attr_data;
assert_return(m, -EINVAL);
if (r < 0)
return r;
if (r < 0)
return r;
return -EIO;
if (data)
return 0;
}
int sd_rtnl_message_read_ether_addr(sd_rtnl_message *m, unsigned short type, struct ether_addr *data) {
int r;
void *attr_data;
assert_return(m, -EINVAL);
if (r < 0)
return r;
if (r < 0)
return r;
else if ((size_t)r < sizeof(struct ether_addr))
return -EIO;
if (data)
return 0;
}
int sd_rtnl_message_read_cache_info(sd_rtnl_message *m, unsigned short type, struct ifa_cacheinfo *info) {
int r;
void *attr_data;
assert_return(m, -EINVAL);
if (r < 0)
return r;
if (r < 0)
return r;
else if ((size_t)r < sizeof(struct ifa_cacheinfo))
return -EIO;
if (info)
return 0;
}
int r;
void *attr_data;
assert_return(m, -EINVAL);
if (r < 0)
return r;
if (r < 0)
return r;
return -EIO;
if (data)
return 0;
}
int r;
void *attr_data;
assert_return(m, -EINVAL);
if (r < 0)
return r;
if (r < 0)
return r;
return -EIO;
if (data)
return 0;
}
const NLTypeSystem *type_system;
void *container;
int r;
assert_return(m, -EINVAL);
&nl_type,
type);
if (r < 0)
return r;
type);
if (r < 0)
return r;
const NLTypeSystemUnion *type_system_union;
type);
if (r < 0)
return r;
switch (type_system_union->match_type) {
case NL_MATCH_SIBLING:
{
const char *key;
if (r < 0)
return r;
key);
if (r < 0)
return r;
break;
}
case NL_MATCH_PROTOCOL:
{
int family;
r = sd_rtnl_message_get_family(m, &family);
if (r < 0)
return r;
family);
if (r < 0)
return r;
break;
}
default:
assert_not_reached("sd-rtnl: invalid type system union type");
}
} else
return -EINVAL;
if (r < 0)
return r;
else
m->n_containers ++;
r = rtnl_message_parse(m,
&m->rta_offset_tb[m->n_containers],
&m->rta_tb_size[m->n_containers],
size);
if (r < 0) {
m->n_containers --;
return r;
}
return 0;
}
assert_return(m, -EINVAL);
m->n_containers --;
return 0;
}
assert(m);
}
int sd_rtnl_message_is_error(sd_rtnl_message *m) {
assert_return(m, 0);
assert_return(m->hdr, 0);
}
int sd_rtnl_message_get_errno(sd_rtnl_message *m) {
assert_return(m, -EINVAL);
if (!sd_rtnl_message_is_error(m))
return 0;
}
int rtnl_message_parse(sd_rtnl_message *m,
unsigned short *rta_tb_size,
int max,
unsigned int rt_len) {
unsigned short type;
if(!tb)
return -ENOMEM;
/* if the kernel is newer than the headers we used
when building, we ignore out-of-range attributes
*/
continue;
log_debug("rtnl: message parse - overwriting repeated attribute");
}
*rta_offset_tb = tb;
return 0;
}
/* returns the number of bytes sent, or a negative error code */
union {
struct sockaddr_nl nl;
} addr = {
};
ssize_t k;
assert(m);
if (k < 0)
return k;
}
CMSG_SPACE(sizeof(struct nl_pktinfo))];
.msg_iovlen = 1,
.msg_controllen = sizeof(cred_buffer),
};
bool auth = false;
int r;
if (r < 0) {
/* no data */
log_debug("rtnl: kernel receive buffer overrun");
log_debug("rtnl: no data in socket");
}
/* from the kernel */
auth = true;
else
/* multi-cast group */
}
}
if (!auth) {
/* not from the kernel, ignore */
if (peek) {
/* drop the message */
if (r < 0)
}
return 0;
}
if (group)
return r;
}
/* 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.
*/
bool multi_part = false, done = false;
int r;
unsigned i = 0;
/* read nothing, just get the pending message size */
if (r <= 0)
return r;
else
/* make room for the pending message */
return -ENOMEM;
/* read the pending message */
if (r <= 0)
return r;
else
/* message did not fit in read buffer */
return -EIO;
multi_part = true;
for (i = 0; i < rtnl->rqueue_partial_size; i++) {
break;
}
}
}
for (new_msg = rtnl->rbuffer; NLMSG_OK(new_msg, len) && !done; new_msg = NLMSG_NEXT(new_msg, len)) {
/* not broadcast and not for us */
continue;
/* silently drop noop messages */
continue;
/* finished reading multi-part message */
done = true;
continue;
}
/* check that we support this message type */
if (r < 0) {
if (r == -EOPNOTSUPP)
log_debug("sd-rtnl: ignored message with unknown type: %i",
continue;
}
/* check that the size matches the message type */
log_debug("sd-rtnl: message larger than expected, dropping");
continue;
}
r = message_new_empty(rtnl, &m);
if (r < 0)
return r;
if (!m->hdr)
return -ENOMEM;
/* seal and parse the top-level message */
r = sd_rtnl_message_rewind(m);
if (r < 0)
return r;
/* push the message onto the multi-part message stack */
if (first)
first = m;
m = NULL;
}
if (len)
if (!first)
return 0;
if (!multi_part || done) {
/* we got a complete message, push it on the read queue */
r = rtnl_rqueue_make_room(rtnl);
if (r < 0)
return r;
/* remove the message form the partial read queue */
rtnl->rqueue_partial_size --;
}
return 1;
} else {
/* we only got a partial multi-part message, push it on the
partial read queue */
if (i < rtnl->rqueue_partial_size) {
} else {
if (r < 0)
return r;
}
return 0;
}
}
int sd_rtnl_message_rewind(sd_rtnl_message *m) {
unsigned i;
int r;
assert_return(m, -EINVAL);
/* don't allow appending to message once parsed */
if (!m->sealed)
for (i = 1; i <= m->n_containers; i++) {
free(m->rta_offset_tb[i]);
m->rta_offset_tb[i] = NULL;
m->rta_tb_size[i] = 0;
m->container_type_system[i] = NULL;
}
m->n_containers = 0;
if (m->rta_offset_tb[0]) {
/* top-level attributes have already been parsed */
return 0;
}
if (r < 0)
return r;
m->container_type_system[0] = type_system;
r = rtnl_message_parse(m,
&m->rta_offset_tb[m->n_containers],
&m->rta_tb_size[m->n_containers],
if (r < 0)
return r;
}
return 0;
}
void rtnl_message_seal(sd_rtnl_message *m) {
assert(m);
m->sealed = true;
}
assert_return(m, NULL);
return m->next;
}