/***
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 "sd-netlink.h"
#include "alloc-util.h"
#include "formats-util.h"
#include "missing.h"
#include "netlink-internal.h"
#include "netlink-types.h"
#include "netlink-util.h"
#include "refcnt.h"
#include "socket-util.h"
#include "util.h"
#define GET_CONTAINER(m, i) ((i) < (m)->n_containers ? (struct rtattr*)((uint8_t*)(m)->hdr + (m)->containers[i].offset) : NULL)
#define PUSH_CONTAINER(m, new) (m)->container_offsets[(m)->n_containers ++] = (uint8_t*)(new) - (uint8_t*)(m)->hdr;
/* Note that 'rtnl' is currently unused, if we start using it internally
we must take care to avoid problems due to mutual references between
buses 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;
return -EINVAL;
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);
-EINVAL);
if (dump)
else
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);
}
return NULL;
}
assert_return(m, -EINVAL);
return 0;
}
assert_return(m, -EINVAL);
return 0;
}
assert_return(m, -EINVAL);
return m->broadcast;
}
/* If successful the updated message will be correctly aligned, if
unsuccessful the old message is untouched. */
static int add_rtattr(sd_netlink_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_netlink_message *m, size_t *out_size, uint16_t attribute_type, uint16_t data_type) {
int r;
assert(m);
if (r < 0)
return r;
return -EINVAL;
if (out_size)
return 0;
}
int sd_netlink_message_append_string(sd_netlink_message *m, unsigned short type, const char *data) {
int r;
assert_return(m, -EINVAL);
if (r < 0)
return r;
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 r;
assert_return(m, -EINVAL);
if (r < 0)
return r;
if (r < 0)
return r;
return 0;
}
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);
if (r < 0)
return r;
return 0;
}
int sd_netlink_message_append_in_addr(sd_netlink_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_netlink_message_append_in6_addr(sd_netlink_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_netlink_message_append_ether_addr(sd_netlink_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_netlink_message_append_cache_info(sd_netlink_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) {
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->containers[m->n_containers].type_system, &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;
return 0;
}
int sd_netlink_message_open_container_union(sd_netlink_message *m, unsigned short type, const char *key) {
int r;
assert_return(m, -EINVAL);
r = type_system_get_type_system_union(m->containers[m->n_containers].type_system, &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;
}
static int netlink_message_read_internal(sd_netlink_message *m, unsigned short type, void **data, bool *net_byteorder) {
assert_return(m, -EINVAL);
return -ENODATA;
if (net_byteorder)
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;
}
void *attr_data;
bool net_byteorder;
int r;
assert_return(m, -EINVAL);
if (r < 0)
return r;
if (r < 0)
return r;
return -EIO;
if (data) {
if (net_byteorder)
else
}
return 0;
}
void *attr_data;
bool net_byteorder;
int r;
assert_return(m, -EINVAL);
if (r < 0)
return r;
if (r < 0)
return r;
return -EIO;
if (data) {
if (net_byteorder)
else
}
return 0;
}
int sd_netlink_message_read_ether_addr(sd_netlink_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_netlink_message_read_cache_info(sd_netlink_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 sd_netlink_message_read_in_addr(sd_netlink_message *m, unsigned short type, struct in_addr *data) {
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_netlink_message_read_in6_addr(sd_netlink_message *m, unsigned short type, struct in6_addr *data) {
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;
}
struct netlink_container *container,
int count,
unsigned int rt_len) {
if(!attributes)
return -ENOMEM;
unsigned short type;
/* 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");
}
attributes = NULL;
return 0;
}
void *container;
int r;
assert_return(m, -EINVAL);
&nl_type,
type_id);
if (r < 0)
return r;
if (type == NETLINK_TYPE_NESTED) {
type_id);
if (r < 0)
return r;
} else if (type == NETLINK_TYPE_UNION) {
type_id);
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-netlink: invalid type system union type");
}
} else
return -EINVAL;
if (r < 0)
return r;
else
m->n_containers ++;
r = netlink_container_parse(m,
&m->containers[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);
}
assert_return(m, 0);
assert_return(m->hdr, 0);
}
assert_return(m, -EINVAL);
if (!sd_netlink_message_is_error(m))
return 0;
}
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++)
m->n_containers = 0;
if (m->containers[0].attributes)
/* top-level attributes have already been parsed */
return 0;
if (r < 0)
return r;
if (type == NETLINK_TYPE_NESTED) {
r = netlink_container_parse(m,
&m->containers[m->n_containers],
if (r < 0)
return r;
}
return 0;
}
assert(m);
m->sealed = true;
}
assert_return(m, NULL);
return m->next;
}