rtnl-message.c revision d595c5cc9e894c3608ed634052b0ba93aa94bf2f
/*-*- 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 <linux/rtnetlink.h>
#include <stdbool.h>
#include <unistd.h>
#include "util.h"
#include "refcnt.h"
#include "sd-rtnl.h"
#include "rtnl-util.h"
#include "rtnl-internal.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;
if (!m)
return -ENOMEM;
if (!m->hdr) {
free(m);
return -ENOMEM;
}
m->n_ref = REFCNT_INIT;
m->sealed = false;
*ret = m;
return 0;
}
assert_return(m, -EINVAL);
return -ERANGE;
return 0;
}
sd_rtnl_message **ret) {
int r;
if (r < 0)
return r;
if (nlmsg_type == RTM_NEWROUTE)
return 0;
}
assert_return(m, -EINVAL);
if (change)
else
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;
}
sd_rtnl_message **ret) {
int r;
if (r < 0)
return r;
return 0;
}
if (m)
return m;
}
if (m && REFCNT_DEC(m->n_ref) <= 0) {
free(m);
}
return NULL;
}
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;
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) {
/* 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;
}
int r;
assert_return(m, -EINVAL);
r = sd_rtnl_message_get_type(m, &rtm_type);
if (r < 0)
return r;
/* check that the type is correct */
switch (rtm_type) {
case RTM_NEWLINK:
case RTM_SETLINK:
case RTM_GETLINK:
case RTM_DELLINK:
if (m->n_containers == 1) {
type != IFLA_INFO_KIND)
return -ENOTSUP;
} else {
switch (type) {
case IFLA_IFNAME:
case IFLA_IFALIAS:
case IFLA_QDISC:
break;
default:
return -ENOTSUP;
}
}
break;
case RTM_NEWADDR:
case RTM_GETADDR:
case RTM_DELADDR:
return -ENOTSUP;
break;
default:
return -ENOTSUP;
}
if (r < 0)
return r;
return 0;
}
int r;
assert_return(m, -EINVAL);
r = sd_rtnl_message_get_type(m, &rtm_type);
if (r < 0)
return r;
switch (rtm_type) {
case RTM_NEWLINK:
case RTM_SETLINK:
case RTM_GETLINK:
case RTM_DELLINK:
switch (type) {
case IFLA_CARRIER:
case IFLA_OPERSTATE:
case IFLA_LINKMODE:
break;
default:
return -ENOTSUP;
}
break;
default:
return -ENOTSUP;
}
if (r < 0)
return r;
return 0;
}
int r;
assert_return(m, -EINVAL);
r = sd_rtnl_message_get_type(m, &rtm_type);
if (r < 0)
return r;
/* check that the type is correct */
switch (rtm_type) {
case RTM_NEWLINK:
case RTM_SETLINK:
case RTM_GETLINK:
case RTM_DELLINK:
if (m->n_containers == 2 &&
type == IFLA_VLAN_ID)
break;
else
return -ENOTSUP;
break;
default:
return -ENOTSUP;
}
if (r < 0)
return r;
return 0;
}
int r;
assert_return(m, -EINVAL);
r = sd_rtnl_message_get_type(m, &rtm_type);
if (r < 0)
return r;
/* check that the type is correct */
switch (rtm_type) {
case RTM_NEWLINK:
case RTM_SETLINK:
case RTM_GETLINK:
case RTM_DELLINK:
switch (type) {
case IFLA_MASTER:
case IFLA_MTU:
case IFLA_LINK:
case IFLA_GROUP:
case IFLA_TXQLEN:
case IFLA_WEIGHT:
case IFLA_NET_NS_FD:
case IFLA_NET_NS_PID:
case IFLA_PROMISCUITY:
case IFLA_NUM_TX_QUEUES:
case IFLA_NUM_RX_QUEUES:
break;
default:
return -ENOTSUP;
}
break;
case RTM_NEWROUTE:
case RTM_GETROUTE:
case RTM_DELROUTE:
switch (type) {
case RTA_TABLE:
case RTA_PRIORITY:
case RTA_IIF:
case RTA_OIF:
case RTA_MARK:
break;
default:
return -ENOTSUP;
}
break;
default:
return -ENOTSUP;
}
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);
r = sd_rtnl_message_get_type(m, &rtm_type);
if (r < 0)
return r;
/* check that the type is correct */
switch (rtm_type) {
case RTM_NEWADDR:
case RTM_GETADDR:
case RTM_DELADDR:
switch (type) {
case IFA_ADDRESS:
case IFA_LOCAL:
case IFA_BROADCAST:
case IFA_ANYCAST:
return -EINVAL;
break;
default:
return -ENOTSUP;
}
break;
case RTM_NEWROUTE:
case RTM_GETROUTE:
case RTM_DELROUTE:
switch (type) {
case RTA_DST:
case RTA_SRC:
case RTA_GATEWAY:
return -EINVAL;
break;
default:
return -ENOTSUP;
}
break;
default:
return -ENOTSUP;
}
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);
r = sd_rtnl_message_get_type(m, &rtm_type);
if (r < 0)
return r;
/* check that the type is correct */
switch (rtm_type) {
case RTM_NEWADDR:
case RTM_GETADDR:
case RTM_DELADDR:
switch (type) {
case IFA_ADDRESS:
case IFA_LOCAL:
case IFA_BROADCAST:
case IFA_ANYCAST:
return -EINVAL;
break;
default:
return -ENOTSUP;
}
break;
case RTM_NEWROUTE:
case RTM_GETROUTE:
case RTM_DELROUTE:
switch (type) {
case RTA_DST:
case RTA_SRC:
case RTA_GATEWAY:
return -EINVAL;
break;
default:
return -ENOTSUP;
}
default:
return -ENOTSUP;
}
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);
switch (rtm_type) {
case RTM_NEWLINK:
case RTM_SETLINK:
case RTM_DELLINK:
case RTM_GETLINK:
switch (type) {
case IFLA_ADDRESS:
case IFLA_BROADCAST:
break;
default:
return -ENOTSUP;
}
break;
default:
return -ENOTSUP;
}
if (r < 0)
return r;
return 0;
}
assert_return(m, -EINVAL);
if (rtnl_message_type_is_link(rtm_type)) {
else
return -ENOTSUP;
} else
return -ENOTSUP;
return 0;
}
assert_return(m, -EINVAL);
m->n_containers --;
return 0;
}
int r;
assert_return(m, -EINVAL);
/* only read until the end of the current container */
if (m->n_containers)
(m->next_rta_offset -
else
return 0;
/* if we read a container, enter it and return its type */
r = sd_rtnl_message_get_type(m, &rtm_type);
if (r < 0)
return r;
if (rtnl_message_type_is_link(rtm_type) &&
((m->n_containers == 0 &&
(m->n_containers == 1 &&
PUSH_CONTAINER(m, NEXT_RTA(m));
} else {
}
return 1;
}
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 r;
assert(m);
if (m->sealed)
return -EPERM;
if (nl)
m->sealed = true;
r = sd_rtnl_message_rewind(m);
if (r < 0)
return r;
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;
}
/* 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.
*/
sd_rtnl_message *m;
union {
struct sockaddr_nl nl;
} addr;
int r;
ssize_t k;
if (r < 0)
return r;
r = message_new(&m, need);
if (r < 0)
return r;
m->sealed = true;
if (k < 0)
else if (k == 0)
k = -ECONNRESET; /* connection was closed by the kernel */
k = -EIO; /* not a netlink message */
k = 0; /* not from the kernel */
k = -EIO; /* too small (we do accept too big though) */
k = 0; /* not broadcast and not for us */
if (k > 0)
switch (m->hdr->nlmsg_type) {
/* check that the size matches the message type */
case NLMSG_ERROR:
k = -EIO;
break;
case RTM_NEWLINK:
case RTM_SETLINK:
case RTM_DELLINK:
case RTM_GETLINK:
k = -EIO;
else {
}
break;
case RTM_NEWADDR:
case RTM_DELADDR:
case RTM_GETADDR:
k = -EIO;
else {
}
break;
case RTM_NEWROUTE:
case RTM_DELROUTE:
case RTM_GETROUTE:
k = -EIO;
else {
}
break;
case NLMSG_NOOP:
k = 0;
break;
default:
k = 0; /* ignoring message of unknown type */
}
if (k <= 0)
else {
/* we probably allocated way too much memory, give it back */
*ret = m;
}
return k;
}
int sd_rtnl_message_rewind(sd_rtnl_message *m) {
assert_return(m, -EINVAL);
switch(m->hdr->nlmsg_type) {
case RTM_NEWLINK:
case RTM_SETLINK:
case RTM_GETLINK:
case RTM_DELLINK:
break;
case RTM_NEWADDR:
case RTM_GETADDR:
case RTM_DELADDR:
break;
case RTM_NEWROUTE:
case RTM_GETROUTE:
case RTM_DELROUTE:
break;
default:
return -ENOTSUP;
}
m->n_containers = 0;
return 0;
}