rtnl-message.c revision fc25d7f8050f262fa6cafeb2a1032e6eb3e7b412
/*-*- 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-internal.h"
struct sd_rtnl_message {
struct rtattr *current_container;
bool sealed:1;
};
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;
}
int r;
if (r < 0)
return r;
return 0;
}
switch (type) {
case RTM_NEWROUTE:
case RTM_GETROUTE:
case RTM_DELROUTE:
return true;
default:
return false;
}
}
switch (type) {
case RTM_NEWLINK:
case RTM_SETLINK:
case RTM_GETLINK:
case RTM_DELLINK:
return true;
default:
return false;
}
}
switch (type) {
case RTM_NEWADDR:
case RTM_GETADDR:
case RTM_DELADDR:
return true;
default:
return false;
}
}
unsigned char rtm_dst_len, unsigned char rtm_src_len,
unsigned char rtm_scope, unsigned char rtm_protocol,
int r;
if (r < 0)
return r;
if (nlmsg_type == RTM_NEWROUTE)
return 0;
}
return 0;
}
return 0;
}
int r;
if (r < 0)
return r;
if (nlmsg_type == RTM_NEWLINK)
return 0;
}
int sd_rtnl_message_addr_new(uint16_t nlmsg_type, int index, unsigned char family, unsigned char prefixlen, unsigned char flags, unsigned char scope, 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;
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 */
/* update message size */
/* we are inside a container, extend it */
if (m->current_container)
(unsigned char *) m->current_container;
/* fill in the attribute */
if (!data) {
/* this is a container, set pointer */
m->current_container = rta;
} 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 */
(unsigned char *) padding);
}
return 0;
}
assert_return(m, -EINVAL);
if (m->current_container) {
switch (rtm_type) {
case RTM_NEWLINK:
case RTM_SETLINK:
case RTM_GETLINK:
case RTM_DELLINK:
switch (m->current_container->rta_type) {
case IFLA_LINKINFO:
switch (type) {
case IFLA_INFO_KIND:
default:
return -ENOTSUP;
}
default:
return -ENOTSUP;
}
default:
return -ENOTSUP;
}
}
switch (rtm_type) {
case RTM_NEWLINK:
case RTM_SETLINK:
case RTM_DELLINK:
case RTM_GETLINK:
switch (type) {
case IFLA_IFNAME:
case IFLA_IFALIAS:
case IFLA_QDISC:
case IFLA_MASTER:
case IFLA_MTU:
case IFLA_LINK:
case IFLA_STATS:
case IFLA_ADDRESS:
case IFLA_BROADCAST:
default:
return -ENOTSUP;
}
case RTM_NEWADDR:
case RTM_DELADDR:
case RTM_GETADDR:
switch (type) {
case IFA_LABEL:
case IFA_ADDRESS:
case IFA_LOCAL:
case IFA_BROADCAST:
case IFA_ANYCAST:
switch (ifa->ifa_family) {
case AF_INET:
case AF_INET6:
default:
return -EINVAL;
}
default:
return -ENOTSUP;
}
case RTM_NEWROUTE:
case RTM_DELROUTE:
case RTM_GETROUTE:
switch (type) {
case RTA_DST:
case RTA_SRC:
case RTA_GATEWAY:
switch (rtm->rtm_family) {
case AF_INET:
case AF_INET6:
default:
return -EINVAL;
}
case RTA_TABLE:
case RTA_PRIORITY:
case RTA_IIF:
case RTA_OIF:
default:
return -ENOTSUP;
}
default:
return -ENOTSUP;
}
}
assert_return(m, -EINVAL);
if (message_type_is_link(rtm_type)) {
if (type == IFLA_LINKINFO)
else
return -ENOTSUP;
} else
return -ENOTSUP;
return 0;
}
assert_return(m, -EINVAL);
m->current_container = NULL;
return 0;
}
int r;
assert(m);
return 0;
/* make sure we don't try to read a container
* TODO: add support for entering containers for reading */
r = sd_rtnl_message_get_type(m, &rtm_type);
if (r < 0)
return r;
switch (rtm_type) {
case RTM_NEWLINK:
case RTM_GETLINK:
case RTM_SETLINK:
case RTM_DELLINK:
return -EINVAL;
}
}
return 1;
}
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_DELLINK:
case RTM_GETLINK:
if (!m->next_rta) {
}
break;
case RTM_NEWADDR:
case RTM_DELADDR:
case RTM_GETADDR:
if (!m->next_rta) {
}
break;
case RTM_NEWROUTE:
case RTM_DELROUTE:
case RTM_GETROUTE:
if (!m->next_rta) {
}
break;
default:
return -ENOTSUP;
}
}
assert(m);
}
int sd_rtnl_message_get_errno(sd_rtnl_message *m) {
assert_return(m, -EINVAL);
return 0;
}
assert(m);
if (m->sealed)
return -EPERM;
m->sealed = true;
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;
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;
break;
case RTM_NEWADDR:
case RTM_DELADDR:
case RTM_GETADDR:
k = -EIO;
break;
case RTM_NEWROUTE:
case RTM_DELROUTE:
case RTM_GETROUTE:
k = -EIO;
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;
}