rtnl-message.c revision 6e37cd2f4af8928d905203108a4331e375d7127c
/*-*- 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 <linux/if_tunnel.h>
#include <linux/if_bridge.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 curretly 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 0;
}
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;
}
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;
}
unsigned char 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;
}
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;
}
/* 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;
assert(m);
/* 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) {
//TODO: simply return this value rather than check for !data
/* this is the start of a new container */
} 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)
*/
/* make sure also the padding at the end of the message is initialized */
}
/* update message size */
return 0;
}
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)
return r;
else
type);
if (r < 0)
return r;
if (r < 0)
return 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;
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;
if (r < 0)
return r;
if (r < 0)
return r;
return -EIO;
return 0;
}
int r;
void *attr_data;
if (r < 0)
return r;
if (r < 0)
return r;
return -EIO;
return 0;
}
int r;
void *attr_data;
if (r < 0)
return r;
if (r < 0)
return r;
return -EIO;
return 0;
}
int r;
void *attr_data;
if (r < 0)
return r;
if (r < 0)
return r;
return -EIO;
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;
if (r < 0)
return r;
if (r < 0)
return r;
else if ((size_t)r < sizeof(struct ether_addr))
return -EIO;
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;
if (r < 0)
return r;
if (r < 0)
return r;
else if ((size_t)r < sizeof(struct ifa_cacheinfo))
return -EIO;
return 0;
}
int r;
void *attr_data;
if (r < 0)
return r;
if (r < 0)
return r;
return -EIO;
return 0;
}
int r;
void *attr_data;
if (r < 0)
return r;
if (r < 0)
return r;
return -EIO;
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;
char *key;
type);
if (r < 0)
return r;
if (r < 0)
return r;
key);
if (r < 0)
return r;
} 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_get_errno(sd_rtnl_message *m) {
assert_return(m, -EINVAL);
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;
}
/* 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.
*/
.msg_iovlen = 1,
.msg_controllen = sizeof(cred_buffer),
};
int r;
unsigned i = 0;
/* read nothing, just get the pending message size */
if (r < 0)
/* no data */
else if (r == 0)
/* connection was closed by the kernel */
return -ECONNRESET;
else
/* make room for the pending message */
return -ENOMEM;
if (r < 0)
/* no data */
else if (r == 0)
/* connection was closed by the kernel */
return -ECONNRESET;
else
/* message did not fit in read buffer */
return -EIO;
/* from the kernel */
auth = true;
break;
}
}
}
if (!auth)
/* not from the kernel, ignore */
return 0;
multi_part = true;
for (i = 0; i < rtnl->rqueue_partial_size; i++) {
break;
}
}
}
/* not broadcast and not for us */
continue;
/* silently drop noop messages */
continue;
/* finished reading multi-part message */
done = true;
break;
}
/* check that we support this message type */
if (r < 0) {
if (r == -ENOTSUP)
log_debug("sd-rtnl: ignored message with unknown type: %u",
continue;
}
/* check that the size matches the message type */
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;
}