rtnl-message.c revision aba496a58acf9d9c61314de71353550e579f85ee
/*-*- 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;
if (!m->hdr) {
free(m);
return -ENOMEM;
}
m->n_ref = REFCNT_INIT;
m->sealed = false;
*ret = m;
return 0;
}
int r;
if (r < 0)
return r;
if (r < 0)
return r;
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);
return -ERANGE;
return 0;
}
assert_return(m, -EINVAL);
return 0;
}
assert_return(m, -EINVAL);
return 0;
}
unsigned char family) {
int r;
if (r < 0)
return r;
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;
}
/* 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) */
if (*need > 8192UL)
*need = 8192UL;
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.
*/
union {
struct sockaddr_nl nl;
} addr;
int r;
if (r < 0)
return r;
if (r < 0)
return r;
if (r < 0)
else if (r == 0)
return -ECONNRESET; /* connection was closed by the kernel */
return -EIO; /* not a netlink message */
return 0; /* not from the kernel */
return -EIO; /* too small (we do accept too big though) */
return 0; /* not broadcast and not for us */
else
/* silently drop noop messages */
return 0;
/* check that we support this message type */
if (r < 0) {
if (r == -ENOTSUP)
log_debug("sd-rtnl: ignored message with unknown type: %u",
m->hdr->nlmsg_type);
return 0;
}
/* check that the size matches the message type */
return -EIO;
/* we probably allocated way too much memory, give it back */
if (!new_hdr)
return -ENOMEM;
/* seal and parse the top-level message */
r = sd_rtnl_message_rewind(m);
if (r < 0)
return r;
*ret = m;
m = NULL;
return len;
}
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;
}