along with systemd; If not, see <http://www.gnu.org/licenses/>.
***/
-#include <linux/rtnetlink.h>
#include <netinet/in.h>
#include <netinet/ether.h>
#include <stdbool.h>
#include <unistd.h>
+#include <linux/netlink.h>
+#include <linux/veth.h>
+#include <linux/if.h>
+#include <linux/ip.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 NEXT_RTA(m) ((struct rtattr*)((uint8_t*)(m)->hdr + (m)->next_rta_offset))
-#define UPDATE_RTA(m, new) (m)->next_rta_offset = (uint8_t*)(new) - (uint8_t*)(m)->hdr;
+#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;
-int message_new(sd_rtnl_message **ret, size_t initial_size) {
+static int message_new_empty(sd_rtnl *rtnl, sd_rtnl_message **ret) {
sd_rtnl_message *m;
assert_return(ret, -EINVAL);
- assert_return(initial_size >= sizeof(struct nlmsghdr), -EINVAL);
+
+ /* 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.
+ */
m = new0(sd_rtnl_message, 1);
if (!m)
return -ENOMEM;
- m->hdr = malloc0(initial_size);
- if (!m->hdr) {
- free(m);
- return -ENOMEM;
- }
-
m->n_ref = REFCNT_INIT;
- m->hdr->nlmsg_flags = NLM_F_REQUEST | NLM_F_ACK;
m->sealed = false;
*ret = m;
return 0;
}
+int message_new(sd_rtnl *rtnl, sd_rtnl_message **ret, uint16_t type) {
+ _cleanup_rtnl_message_unref_ sd_rtnl_message *m = NULL;
+ const NLType *nl_type;
+ size_t size;
+ int r;
+
+ r = type_system_get_type(NULL, &nl_type, type);
+ if (r < 0)
+ return r;
+
+ assert(nl_type->type == NLA_NESTED);
+
+ r = message_new_empty(rtnl, &m);
+ if (r < 0)
+ return r;
+
+ size = NLMSG_SPACE(nl_type->size);
+
+ assert(size >= sizeof(struct nlmsghdr));
+ m->hdr = malloc0(size);
+ if (!m->hdr)
+ return -ENOMEM;
+
+ m->hdr->nlmsg_flags = NLM_F_REQUEST | NLM_F_ACK;
+
+ m->container_type_system[0] = nl_type->type_system;
+ m->hdr->nlmsg_len = size;
+ m->hdr->nlmsg_type = type;
+
+ *ret = m;
+ m = NULL;
+
+ return 0;
+}
+
int sd_rtnl_message_route_set_dst_prefixlen(sd_rtnl_message *m, unsigned char prefixlen) {
struct rtmsg *rtm;
return 0;
}
-int sd_rtnl_message_new_route(uint16_t nlmsg_type, unsigned char rtm_family,
- sd_rtnl_message **ret) {
+int sd_rtnl_message_route_set_scope(sd_rtnl_message *m, unsigned char scope) {
+ struct rtmsg *rtm;
+
+ assert_return(m, -EINVAL);
+ assert_return(m->hdr, -EINVAL);
+ assert_return(rtnl_message_type_is_route(m->hdr->nlmsg_type), -EINVAL);
+
+ rtm = NLMSG_DATA(m->hdr);
+
+ rtm->rtm_scope = scope;
+
+ return 0;
+}
+
+int sd_rtnl_message_new_route(sd_rtnl *rtnl, sd_rtnl_message **ret,
+ uint16_t nlmsg_type, unsigned char rtm_family) {
struct rtmsg *rtm;
int r;
assert_return(rtm_family == AF_INET || rtm_family == AF_INET6, -EINVAL);
assert_return(ret, -EINVAL);
- r = message_new(ret, NLMSG_SPACE(sizeof(struct rtmsg)));
+ r = message_new(rtnl, ret, nlmsg_type);
if (r < 0)
return r;
- (*ret)->hdr->nlmsg_len = NLMSG_LENGTH(sizeof(struct rtmsg));
- (*ret)->hdr->nlmsg_type = nlmsg_type;
if (nlmsg_type == RTM_NEWROUTE)
(*ret)->hdr->nlmsg_flags |= NLM_F_CREATE | NLM_F_EXCL;
rtm = NLMSG_DATA((*ret)->hdr);
- UPDATE_RTA(*ret, RTM_RTA(rtm));
-
rtm->rtm_family = rtm_family;
rtm->rtm_scope = RT_SCOPE_UNIVERSE;
rtm->rtm_type = RTN_UNICAST;
assert_return(m, -EINVAL);
assert_return(m->hdr, -EINVAL);
assert_return(rtnl_message_type_is_link(m->hdr->nlmsg_type), -EINVAL);
+ assert_return(change, -EINVAL);
ifi = NLMSG_DATA(m->hdr);
ifi->ifi_flags = flags;
- if (change)
- ifi->ifi_change = change;
- else
- ifi->ifi_change = 0xffffffff;
+ ifi->ifi_change = change;
return 0;
}
return 0;
}
-int sd_rtnl_message_new_link(uint16_t nlmsg_type, int index, sd_rtnl_message **ret) {
+int sd_rtnl_message_new_link(sd_rtnl *rtnl, sd_rtnl_message **ret,
+ uint16_t nlmsg_type, int index) {
struct ifinfomsg *ifi;
int r;
assert_return(rtnl_message_type_is_link(nlmsg_type), -EINVAL);
- assert_return(nlmsg_type == RTM_NEWLINK || index > 0, -EINVAL);
+ assert_return(nlmsg_type != RTM_DELLINK || index > 0, -EINVAL);
assert_return(ret, -EINVAL);
- r = message_new(ret, NLMSG_SPACE(sizeof(struct ifinfomsg)));
+ r = message_new(rtnl, ret, nlmsg_type);
if (r < 0)
return r;
- (*ret)->hdr->nlmsg_len = NLMSG_LENGTH(sizeof(struct ifinfomsg));
- (*ret)->hdr->nlmsg_type = nlmsg_type;
if (nlmsg_type == RTM_NEWLINK)
- (*ret)->hdr->nlmsg_flags |= NLM_F_CREATE;
+ (*ret)->hdr->nlmsg_flags |= NLM_F_CREATE | NLM_F_EXCL;
ifi = NLMSG_DATA((*ret)->hdr);
ifi->ifi_family = AF_UNSPEC;
ifi->ifi_index = index;
- UPDATE_RTA(*ret, IFLA_RTA(ifi));
-
return 0;
}
return 0;
}
-int sd_rtnl_message_new_addr(uint16_t nlmsg_type, int index, unsigned char family,
- sd_rtnl_message **ret) {
+int sd_rtnl_message_addr_get_family(sd_rtnl_message *m, unsigned char *family) {
+ struct ifaddrmsg *ifa;
+
+ assert_return(m, -EINVAL);
+ assert_return(m->hdr, -EINVAL);
+ assert_return(rtnl_message_type_is_addr(m->hdr->nlmsg_type), -EINVAL);
+ assert_return(family, -EINVAL);
+
+ ifa = NLMSG_DATA(m->hdr);
+
+ *family = ifa->ifa_family;
+
+ return 0;
+}
+
+int sd_rtnl_message_addr_get_scope(sd_rtnl_message *m, unsigned char *scope) {
+ struct ifaddrmsg *ifa;
+
+ assert_return(m, -EINVAL);
+ assert_return(m->hdr, -EINVAL);
+ assert_return(rtnl_message_type_is_addr(m->hdr->nlmsg_type), -EINVAL);
+ assert_return(scope, -EINVAL);
+
+ ifa = NLMSG_DATA(m->hdr);
+
+ *scope = ifa->ifa_scope;
+
+ return 0;
+}
+
+int sd_rtnl_message_addr_get_flags(sd_rtnl_message *m, unsigned char *flags) {
+ struct ifaddrmsg *ifa;
+
+ assert_return(m, -EINVAL);
+ assert_return(m->hdr, -EINVAL);
+ assert_return(rtnl_message_type_is_addr(m->hdr->nlmsg_type), -EINVAL);
+ assert_return(flags, -EINVAL);
+
+ ifa = NLMSG_DATA(m->hdr);
+
+ *flags = ifa->ifa_flags;
+
+ return 0;
+}
+
+int sd_rtnl_message_addr_get_ifindex(sd_rtnl_message *m, int *ifindex) {
+ struct ifaddrmsg *ifa;
+
+ assert_return(m, -EINVAL);
+ assert_return(m->hdr, -EINVAL);
+ assert_return(rtnl_message_type_is_addr(m->hdr->nlmsg_type), -EINVAL);
+ assert_return(ifindex, -EINVAL);
+
+ ifa = NLMSG_DATA(m->hdr);
+
+ *ifindex = ifa->ifa_index;
+
+ return 0;
+}
+
+int sd_rtnl_message_new_addr(sd_rtnl *rtnl, sd_rtnl_message **ret,
+ uint16_t nlmsg_type, int index,
+ unsigned char family) {
struct ifaddrmsg *ifa;
int r;
assert_return(family == AF_INET || family == AF_INET6, -EINVAL);
assert_return(ret, -EINVAL);
- r = message_new(ret, NLMSG_SPACE(sizeof(struct ifaddrmsg)));
+ r = message_new(rtnl, ret, nlmsg_type);
if (r < 0)
return r;
- (*ret)->hdr->nlmsg_len = NLMSG_LENGTH(sizeof(struct ifaddrmsg));
- (*ret)->hdr->nlmsg_type = nlmsg_type;
+ if (nlmsg_type == RTM_GETADDR && family == AF_INET)
+ (*ret)->hdr->nlmsg_flags |= NLM_F_DUMP;
ifa = NLMSG_DATA((*ret)->hdr);
else if (family == AF_INET6)
ifa->ifa_prefixlen = 128;
- UPDATE_RTA(*ret, IFA_RTA(ifa));
+ return 0;
+}
+
+int sd_rtnl_message_new_addr_update(sd_rtnl *rtnl, sd_rtnl_message **ret,
+ int index, unsigned char family) {
+ int r;
+
+ r = sd_rtnl_message_new_addr(rtnl, ret, RTM_NEWADDR, index, family);
+ if (r < 0)
+ return r;
+
+ (*ret)->hdr->nlmsg_flags |= NLM_F_REPLACE;
return 0;
}
sd_rtnl_message *sd_rtnl_message_unref(sd_rtnl_message *m) {
if (m && REFCNT_DEC(m->n_ref) <= 0) {
+ unsigned i;
+
free(m->hdr);
+
+ for (i = 0; i <= m->n_containers; i++)
+ free(m->rta_offset_tb[i]);
+
+ sd_rtnl_message_unref(m->next);
+
free(m);
}
return 0;
}
+int sd_rtnl_message_is_broadcast(sd_rtnl_message *m) {
+ assert_return(m, -EINVAL);
+
+ return !m->hdr->nlmsg_pid;
+}
+
int sd_rtnl_message_link_get_ifindex(sd_rtnl_message *m, int *ifindex) {
struct ifinfomsg *ifi;
rta->rta_type = type;
rta->rta_len = rta_length;
if (!data) {
+ //TODO: simply return this value rather than check for !data
/* this is the start of a new container */
m->container_offsets[m->n_containers ++] = m->hdr->nlmsg_len;
} else {
return 0;
}
+static int message_attribute_has_type(sd_rtnl_message *m, uint16_t attribute_type, uint16_t data_type) {
+ const NLType *type;
+ int r;
+
+ r = type_system_get_type(m->container_type_system[m->n_containers], &type, attribute_type);
+ if (r < 0)
+ return r;
+
+ if (type->type != data_type)
+ return -EINVAL;
+
+ return type->size;
+}
+
int sd_rtnl_message_append_string(sd_rtnl_message *m, unsigned short type, const char *data) {
- uint16_t rtm_type;
+ size_t length, size;
int r;
assert_return(m, -EINVAL);
assert_return(!m->sealed, -EPERM);
assert_return(data, -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) {
- if (GET_CONTAINER(m, 0)->rta_type != IFLA_LINKINFO ||
- 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:
- if (type != IFA_LABEL)
- return -ENOTSUP;
- break;
- default:
- return -ENOTSUP;
- }
+ r = message_attribute_has_type(m, type, NLA_STRING);
+ if (r < 0)
+ return r;
+ else
+ size = (size_t)r;
- r = add_rtattr(m, type, data, strlen(data) + 1);
+ if (size) {
+ length = strnlen(data, size);
+ if (length >= size)
+ return -EINVAL;
+ } else
+ length = strlen(data);
+
+ r = add_rtattr(m, type, data, length + 1);
if (r < 0)
return r;
}
int sd_rtnl_message_append_u8(sd_rtnl_message *m, unsigned short type, uint8_t data) {
- uint16_t rtm_type;
int r;
assert_return(m, -EINVAL);
assert_return(!m->sealed, -EPERM);
- r = sd_rtnl_message_get_type(m, &rtm_type);
+ r = message_attribute_has_type(m, type, NLA_U8);
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;
- }
-
r = add_rtattr(m, type, &data, sizeof(uint8_t));
if (r < 0)
return r;
int sd_rtnl_message_append_u16(sd_rtnl_message *m, unsigned short type, uint16_t data) {
- uint16_t rtm_type;
int r;
assert_return(m, -EINVAL);
assert_return(!m->sealed, -EPERM);
- r = sd_rtnl_message_get_type(m, &rtm_type);
+ r = message_attribute_has_type(m, type, NLA_U16);
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 &&
- GET_CONTAINER(m, 0)->rta_type == IFLA_LINKINFO &&
- GET_CONTAINER(m, 1)->rta_type == IFLA_INFO_DATA &&
- type == IFLA_VLAN_ID)
- break;
- else
- return -ENOTSUP;
- break;
- default:
- return -ENOTSUP;
- }
-
r = add_rtattr(m, type, &data, sizeof(uint16_t));
if (r < 0)
return r;
}
int sd_rtnl_message_append_u32(sd_rtnl_message *m, unsigned short type, uint32_t data) {
- uint16_t rtm_type;
int r;
assert_return(m, -EINVAL);
assert_return(!m->sealed, -EPERM);
- 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;
- }
+ r = message_attribute_has_type(m, type, NLA_U32);
+ if (r < 0)
+ return r;
r = add_rtattr(m, type, &data, sizeof(uint32_t));
if (r < 0)
}
int sd_rtnl_message_append_in_addr(sd_rtnl_message *m, unsigned short type, const struct in_addr *data) {
- struct ifaddrmsg *ifa;
- struct rtmsg *rtm;
- uint16_t rtm_type;
int r;
assert_return(m, -EINVAL);
assert_return(!m->sealed, -EPERM);
assert_return(data, -EINVAL);
- r = sd_rtnl_message_get_type(m, &rtm_type);
+ r = message_attribute_has_type(m, type, NLA_IN_ADDR);
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:
- ifa = NLMSG_DATA(m->hdr);
-
- if (ifa->ifa_family != AF_INET)
- 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:
- rtm = NLMSG_DATA(m->hdr);
-
- if (rtm->rtm_family != AF_INET)
- return -EINVAL;
-
- break;
- default:
- return -ENOTSUP;
- }
- break;
- default:
- return -ENOTSUP;
- }
-
r = add_rtattr(m, type, data, sizeof(struct in_addr));
if (r < 0)
return r;
}
int sd_rtnl_message_append_in6_addr(sd_rtnl_message *m, unsigned short type, const struct in6_addr *data) {
- struct ifaddrmsg *ifa;
- struct rtmsg *rtm;
- uint16_t rtm_type;
int r;
assert_return(m, -EINVAL);
assert_return(!m->sealed, -EPERM);
assert_return(data, -EINVAL);
- r = sd_rtnl_message_get_type(m, &rtm_type);
+ r = message_attribute_has_type(m, type, NLA_IN_ADDR);
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:
- ifa = NLMSG_DATA(m->hdr);
-
- if (ifa->ifa_family != AF_INET6)
- 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:
- rtm = NLMSG_DATA(m->hdr);
-
- if (rtm->rtm_family != AF_INET6)
- return -EINVAL;
-
- break;
- default:
- return -ENOTSUP;
- }
- default:
- return -ENOTSUP;
- }
-
r = add_rtattr(m, type, data, sizeof(struct in6_addr));
if (r < 0)
return r;
}
int sd_rtnl_message_append_ether_addr(sd_rtnl_message *m, unsigned short type, const struct ether_addr *data) {
- uint16_t rtm_type;
int r;
assert_return(m, -EINVAL);
assert_return(!m->sealed, -EPERM);
assert_return(data, -EINVAL);
- sd_rtnl_message_get_type(m, &rtm_type);
-
- 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;
- }
+ r = message_attribute_has_type(m, type, NLA_ETHER_ADDR);
+ if (r < 0)
+ return r;
r = add_rtattr(m, type, data, ETH_ALEN);
if (r < 0)
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);
+ assert_return(!m->sealed, -EPERM);
+ assert_return(info, -EINVAL);
+
+ r = message_attribute_has_type(m, type, NLA_CACHE_INFO);
+ if (r < 0)
+ return r;
+
+ r = add_rtattr(m, type, info, sizeof(struct ifa_cacheinfo));
+ if (r < 0)
+ return r;
+
+ return 0;
+}
+
int sd_rtnl_message_open_container(sd_rtnl_message *m, unsigned short type) {
- uint16_t rtm_type;
+ size_t size;
+ int r;
assert_return(m, -EINVAL);
assert_return(!m->sealed, -EPERM);
- sd_rtnl_message_get_type(m, &rtm_type);
+ r = message_attribute_has_type(m, type, NLA_NESTED);
+ if (r < 0)
+ return r;
+ else
+ size = (size_t)r;
- if (rtnl_message_type_is_link(rtm_type)) {
- if ((type == IFLA_LINKINFO && m->n_containers == 0) ||
- (type == IFLA_INFO_DATA && m->n_containers == 1 &&
- GET_CONTAINER(m, 0)->rta_type == IFLA_LINKINFO))
- return add_rtattr(m, type, NULL, 0);
- else
- return -ENOTSUP;
- } else
- return -ENOTSUP;
+ r = type_system_get_type_system(m->container_type_system[m->n_containers],
+ &m->container_type_system[m->n_containers + 1],
+ type);
+ if (r < 0)
+ return r;
+
+ r = add_rtattr(m, type, NULL, size);
+ 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);
+ assert_return(!m->sealed, -EPERM);
+
+ r = type_system_get_type_system_union(m->container_type_system[m->n_containers], &type_system_union, type);
+ if (r < 0)
+ return r;
+
+ r = type_system_union_get_type_system(type_system_union,
+ &m->container_type_system[m->n_containers + 1],
+ key);
+ if (r < 0)
+ return r;
+
+ r = sd_rtnl_message_append_string(m, type_system_union->match, key);
+ if (r < 0)
+ return r;
+
+ /* do we evere need non-null size */
+ r = add_rtattr(m, type, NULL, 0);
+ if (r < 0)
+ return r;
return 0;
}
+
int sd_rtnl_message_close_container(sd_rtnl_message *m) {
assert_return(m, -EINVAL);
assert_return(!m->sealed, -EPERM);
assert_return(m->n_containers > 0, -EINVAL);
+ m->container_type_system[m->n_containers] = NULL;
m->n_containers --;
return 0;
}
-int sd_rtnl_message_read(sd_rtnl_message *m, unsigned short *type, void **data) {
- size_t remaining_size;
- uint16_t rtm_type;
- int r;
+int rtnl_message_read_internal(sd_rtnl_message *m, unsigned short type, void **data) {
+ struct rtattr *rta;
assert_return(m, -EINVAL);
assert_return(m->sealed, -EPERM);
- assert_return(m->next_rta_offset, -EINVAL);
- assert_return(type, -EINVAL);
assert_return(data, -EINVAL);
+ assert(m->n_containers <= RTNL_CONTAINER_DEPTH);
+ assert(m->rta_offset_tb[m->n_containers]);
+ assert(type < m->rta_tb_size[m->n_containers]);
- /* only read until the end of the current container */
- if (m->n_containers)
- remaining_size = GET_CONTAINER(m, m->n_containers - 1)->rta_len -
- (m->next_rta_offset -
- m->container_offsets[m->n_containers - 1]);
- else
- remaining_size = m->hdr->nlmsg_len - m->next_rta_offset;
+ if(!m->rta_offset_tb[m->n_containers][type])
+ return -ENODATA;
- if (!RTA_OK(NEXT_RTA(m), remaining_size))
- return 0;
+ rta = (struct rtattr*)((uint8_t *) m->hdr + m->rta_offset_tb[m->n_containers][type]);
+
+ *data = RTA_DATA(rta);
- /* if we read a container, enter it and return its type */
- r = sd_rtnl_message_get_type(m, &rtm_type);
+ return RTA_PAYLOAD(rta);
+}
+
+int sd_rtnl_message_read_string(sd_rtnl_message *m, unsigned short type, char **data) {
+ int r;
+ void *attr_data;
+
+ r = message_attribute_has_type(m, type, NLA_STRING);
if (r < 0)
return r;
- *type = NEXT_RTA(m)->rta_type;
+ r = rtnl_message_read_internal(m, type, &attr_data);
+ if (r < 0)
+ return r;
+ else if (strnlen(attr_data, r) >= (size_t) r)
+ return -EIO;
- if (rtnl_message_type_is_link(rtm_type) &&
- ((m->n_containers == 0 &&
- NEXT_RTA(m)->rta_type == IFLA_LINKINFO) ||
- (m->n_containers == 1 &&
- GET_CONTAINER(m, 0)->rta_type == IFLA_LINKINFO &&
- NEXT_RTA(m)->rta_type == IFLA_INFO_DATA))) {
- *data = NULL;
- PUSH_CONTAINER(m, NEXT_RTA(m));
- UPDATE_RTA(m, RTA_DATA(NEXT_RTA(m)));
- } else {
- *data = RTA_DATA(NEXT_RTA(m));
- UPDATE_RTA(m, RTA_NEXT(NEXT_RTA(m), remaining_size));
+ *data = (char *) attr_data;
+
+ return 0;
+}
+
+int sd_rtnl_message_read_u8(sd_rtnl_message *m, unsigned short type, uint8_t *data) {
+ int r;
+ void *attr_data;
+
+ r = message_attribute_has_type(m, type, NLA_U8);
+ if (r < 0)
+ return r;
+
+ r = rtnl_message_read_internal(m, type, &attr_data);
+ if (r < 0)
+ return r;
+ else if ((size_t) r < sizeof(uint8_t))
+ return -EIO;
+
+ *data = *(uint8_t *) attr_data;
+
+ return 0;
+}
+
+int sd_rtnl_message_read_u16(sd_rtnl_message *m, unsigned short type, uint16_t *data) {
+ int r;
+ void *attr_data;
+
+ r = message_attribute_has_type(m, type, NLA_U16);
+ if (r < 0)
+ return r;
+
+ r = rtnl_message_read_internal(m, type, &attr_data);
+ if (r < 0)
+ return r;
+ else if ((size_t) r < sizeof(uint16_t))
+ return -EIO;
+
+ *data = *(uint16_t *) attr_data;
+
+ return 0;
+}
+
+int sd_rtnl_message_read_u32(sd_rtnl_message *m, unsigned short type, uint32_t *data) {
+ int r;
+ void *attr_data;
+
+ r = message_attribute_has_type(m, type, NLA_U32);
+ if (r < 0)
+ return r;
+
+ r = rtnl_message_read_internal(m, type, &attr_data);
+ if (r < 0)
+ return r;
+ else if ((size_t)r < sizeof(uint32_t))
+ return -EIO;
+
+ *data = *(uint32_t *) attr_data;
+
+ 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;
+
+ r = message_attribute_has_type(m, type, NLA_ETHER_ADDR);
+ if (r < 0)
+ return r;
+
+ r = rtnl_message_read_internal(m, type, &attr_data);
+ if (r < 0)
+ return r;
+ else if ((size_t)r < sizeof(struct ether_addr))
+ return -EIO;
+
+ memcpy(data, attr_data, sizeof(struct ether_addr));
+
+ 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;
+
+ r = message_attribute_has_type(m, type, NLA_CACHE_INFO);
+ if (r < 0)
+ return r;
+
+ r = rtnl_message_read_internal(m, type, &attr_data);
+ if (r < 0)
+ return r;
+ else if ((size_t)r < sizeof(struct ifa_cacheinfo))
+ return -EIO;
+
+ memcpy(info, attr_data, sizeof(struct ifa_cacheinfo));
+
+ return 0;
+}
+
+int sd_rtnl_message_read_in_addr(sd_rtnl_message *m, unsigned short type, struct in_addr *data) {
+ int r;
+ void *attr_data;
+
+ r = message_attribute_has_type(m, type, NLA_IN_ADDR);
+ if (r < 0)
+ return r;
+
+ r = rtnl_message_read_internal(m, type, &attr_data);
+ if (r < 0)
+ return r;
+ else if ((size_t)r < sizeof(struct in_addr))
+ return -EIO;
+
+ memcpy(data, attr_data, sizeof(struct in_addr));
+
+ return 0;
+}
+
+int sd_rtnl_message_read_in6_addr(sd_rtnl_message *m, unsigned short type, struct in6_addr *data) {
+ int r;
+ void *attr_data;
+
+ r = message_attribute_has_type(m, type, NLA_IN_ADDR);
+ if (r < 0)
+ return r;
+
+ r = rtnl_message_read_internal(m, type, &attr_data);
+ if (r < 0)
+ return r;
+ else if ((size_t)r < sizeof(struct in6_addr))
+ return -EIO;
+
+ memcpy(data, attr_data, sizeof(struct in6_addr));
+
+ return 0;
+}
+
+int sd_rtnl_message_enter_container(sd_rtnl_message *m, unsigned short type) {
+ const NLType *nl_type;
+ const NLTypeSystem *type_system;
+ void *container;
+ size_t size;
+ int r;
+
+ assert_return(m, -EINVAL);
+ assert_return(m->n_containers < RTNL_CONTAINER_DEPTH, -EINVAL);
+
+ r = type_system_get_type(m->container_type_system[m->n_containers],
+ &nl_type,
+ type);
+ if (r < 0)
+ return r;
+
+ if (nl_type->type == NLA_NESTED) {
+ r = type_system_get_type_system(m->container_type_system[m->n_containers],
+ &type_system,
+ type);
+ if (r < 0)
+ return r;
+ } else if (nl_type->type == NLA_UNION) {
+ const NLTypeSystemUnion *type_system_union;
+ char *key;
+
+ r = type_system_get_type_system_union(m->container_type_system[m->n_containers],
+ &type_system_union,
+ type);
+ if (r < 0)
+ return r;
+
+ r = sd_rtnl_message_read_string(m, type_system_union->match, &key);
+ if (r < 0)
+ return r;
+
+ r = type_system_union_get_type_system(type_system_union,
+ &type_system,
+ key);
+ if (r < 0)
+ return r;
+ } else
+ return -EINVAL;
+
+ r = rtnl_message_read_internal(m, type, &container);
+ if (r < 0)
+ return r;
+ else
+ size = (size_t)r;
+
+ m->n_containers ++;
+
+ r = rtnl_message_parse(m,
+ &m->rta_offset_tb[m->n_containers],
+ &m->rta_tb_size[m->n_containers],
+ type_system->max,
+ container,
+ size);
+ if (r < 0) {
+ m->n_containers --;
+ return r;
}
- return 1;
+ m->container_type_system[m->n_containers] = type_system;
+
+ return 0;
}
int sd_rtnl_message_exit_container(sd_rtnl_message *m) {
assert_return(m->sealed, -EINVAL);
assert_return(m->n_containers > 0, -EINVAL);
+ free(m->rta_offset_tb[m->n_containers]);
+ m->rta_offset_tb[m->n_containers] = NULL;
+ m->container_type_system[m->n_containers] = NULL;
+
m->n_containers --;
return 0;
return err->error;
}
-int rtnl_message_seal(sd_rtnl *nl, sd_rtnl_message *m) {
- int r;
-
- assert(m);
- assert(m->hdr);
-
- if (m->sealed)
- return -EPERM;
-
- if (nl)
- m->hdr->nlmsg_seq = nl->serial++;
-
- m->sealed = true;
-
- r = sd_rtnl_message_rewind(m);
- if (r < 0)
- return r;
-
- return 0;
-}
-
static int message_receive_need(sd_rtnl *rtnl, size_t *need) {
assert(rtnl);
assert(need);
return 0;
}
+int rtnl_message_parse(sd_rtnl_message *m,
+ size_t **rta_offset_tb,
+ unsigned short *rta_tb_size,
+ int max,
+ struct rtattr *rta,
+ unsigned int rt_len) {
+ unsigned short type;
+ size_t *tb;
+
+ tb = new0(size_t, max + 1);
+ if(!tb)
+ return -ENOMEM;
+
+ *rta_tb_size = max + 1;
+
+ for (; RTA_OK(rta, rt_len); rta = RTA_NEXT(rta, rt_len)) {
+ type = rta->rta_type;
+
+ /* if the kernel is newer than the headers we used
+ when building, we ignore out-of-range attributes
+ */
+ if (type > max)
+ continue;
+
+ if (tb[type])
+ log_debug("rtnl: message parse - overwriting repeated attribute");
+
+ tb[type] = (uint8_t *) rta - (uint8_t *) m->hdr;
+ }
+
+ *rta_offset_tb = tb;
+
+ return 0;
+}
+
/* returns the number of bytes sent, or a negative error code */
int socket_write_message(sd_rtnl *nl, sd_rtnl_message *m) {
union {
* If nothing useful was received 0 is returned.
* On failure, a negative error code is returned.
*/
-int socket_read_message(sd_rtnl *nl, sd_rtnl_message **ret) {
- sd_rtnl_message *m;
- union {
- struct sockaddr sa;
- struct sockaddr_nl nl;
- } addr;
- socklen_t addr_len;
- int r;
- ssize_t k;
- size_t need;
+int socket_read_message(sd_rtnl *rtnl) {
+ _cleanup_rtnl_message_unref_ sd_rtnl_message *first = NULL;
+ sd_rtnl_message *previous = NULL;
+ _cleanup_free_ void *buffer = NULL;
+ uint8_t cred_buffer[CMSG_SPACE(sizeof(struct ucred))];
+ struct iovec iov = {};
+ struct msghdr msg = {
+ .msg_iov = &iov,
+ .msg_iovlen = 1,
+ .msg_control = cred_buffer,
+ .msg_controllen = sizeof(cred_buffer),
+ };
+ struct cmsghdr *cmsg;
+ bool auth = false;
+ struct nlmsghdr *new_msg;
+ size_t need, len;
+ int r, ret = 0;
- assert(nl);
- assert(ret);
+ assert(rtnl);
- r = message_receive_need(nl, &need);
+ r = message_receive_need(rtnl, &need);
if (r < 0)
return r;
- r = message_new(&m, need);
- if (r < 0)
- return r;
+ buffer = malloc0(need);
+ if (!buffer)
+ return -ENOMEM;
- /* don't allow sealing/appending to received messages */
- m->sealed = true;
+ iov.iov_base = buffer;
+ iov.iov_len = need;
- addr_len = sizeof(addr);
+ r = recvmsg(rtnl->fd, &msg, 0);
+ if (r < 0)
+ /* no data */
+ return (errno == EAGAIN) ? 0 : -errno;
+ else if (r == 0)
+ /* connection was closed by the kernel */
+ return -ECONNRESET;
+ else
+ len = (size_t)r;
- k = recvfrom(nl->fd, m->hdr, need,
- 0, &addr.sa, &addr_len);
- if (k < 0)
- k = (errno == EAGAIN) ? 0 : -errno; /* no data */
- else if (k == 0)
- k = -ECONNRESET; /* connection was closed by the kernel */
- else if (addr_len != sizeof(addr.nl) ||
- addr.nl.nl_family != AF_NETLINK)
- k = -EIO; /* not a netlink message */
- else if (addr.nl.nl_pid != 0)
- k = 0; /* not from the kernel */
- else if ((size_t) k < sizeof(struct nlmsghdr) ||
- (size_t) k < m->hdr->nlmsg_len)
- k = -EIO; /* too small (we do accept too big though) */
- else if (m->hdr->nlmsg_pid && m->hdr->nlmsg_pid != nl->sockaddr.nl.nl_pid)
- k = 0; /* not broadcast and not for us */
-
- if (k > 0)
- switch (m->hdr->nlmsg_type) {
- struct ifinfomsg *ifi;
- struct ifaddrmsg *ifa;
- struct rtmsg *rtm;
-
- /* check that the size matches the message type */
- case NLMSG_ERROR:
- if (m->hdr->nlmsg_len < NLMSG_LENGTH(sizeof(struct nlmsgerr)))
- k = -EIO;
- break;
- case RTM_NEWLINK:
- case RTM_SETLINK:
- case RTM_DELLINK:
- case RTM_GETLINK:
- if (m->hdr->nlmsg_len < NLMSG_LENGTH(sizeof(struct ifinfomsg)))
- k = -EIO;
- else {
- ifi = NLMSG_DATA(m->hdr);
- UPDATE_RTA(m, IFLA_RTA(ifi));
- }
- break;
- case RTM_NEWADDR:
- case RTM_DELADDR:
- case RTM_GETADDR:
- if (m->hdr->nlmsg_len < NLMSG_LENGTH(sizeof(struct ifaddrmsg)))
- k = -EIO;
- else {
- ifa = NLMSG_DATA(m->hdr);
- UPDATE_RTA(m, IFA_RTA(ifa));
- }
- break;
- case RTM_NEWROUTE:
- case RTM_DELROUTE:
- case RTM_GETROUTE:
- if (m->hdr->nlmsg_len < NLMSG_LENGTH(sizeof(struct rtmsg)))
- k = -EIO;
- else {
- rtm = NLMSG_DATA(m->hdr);
- UPDATE_RTA(m, RTM_RTA(rtm));
- }
- break;
- case NLMSG_NOOP:
- k = 0;
+ for (cmsg = CMSG_FIRSTHDR(&msg); cmsg; cmsg = CMSG_NXTHDR(&msg, cmsg)) {
+ if (cmsg->cmsg_level == SOL_SOCKET &&
+ cmsg->cmsg_type == SCM_CREDENTIALS &&
+ cmsg->cmsg_len == CMSG_LEN(sizeof(struct ucred))) {
+ struct ucred *ucred = (void *)CMSG_DATA(cmsg);
+
+ /* from the kernel */
+ if (ucred->uid == 0 && ucred->pid == 0) {
+ auth = true;
break;
- default:
- k = 0; /* ignoring message of unknown type */
+ }
+ }
+ }
+
+ if (!auth)
+ /* not from the kernel, ignore */
+ return 0;
+
+ for (new_msg = buffer; NLMSG_OK(new_msg, len); new_msg = NLMSG_NEXT(new_msg, len)) {
+ _cleanup_rtnl_message_unref_ sd_rtnl_message *m = NULL;
+ const NLType *nl_type;
+
+ if (new_msg->nlmsg_pid && new_msg->nlmsg_pid != rtnl->sockaddr.nl.nl_pid)
+ /* not broadcast and not for us */
+ continue;
+
+ /* silently drop noop messages */
+ if (new_msg->nlmsg_type == NLMSG_NOOP)
+ continue;
+
+ /* finished reading multi-part message */
+ if (new_msg->nlmsg_type == NLMSG_DONE)
+ break;
+
+ /* check that we support this message type */
+ r = type_system_get_type(NULL, &nl_type, new_msg->nlmsg_type);
+ if (r < 0) {
+ if (r == -ENOTSUP)
+ log_debug("sd-rtnl: ignored message with unknown type: %u",
+ new_msg->nlmsg_type);
+
+ continue;
}
- if (k <= 0)
- sd_rtnl_message_unref(m);
- else {
- /* we probably allocated way too much memory, give it back */
- m->hdr = realloc(m->hdr, m->hdr->nlmsg_len);
- *ret = m;
+ /* check that the size matches the message type */
+ if (new_msg->nlmsg_len < NLMSG_LENGTH(nl_type->size))
+ continue;
+
+ r = message_new_empty(rtnl, &m);
+ if (r < 0)
+ return r;
+
+ m->hdr = memdup(new_msg, new_msg->nlmsg_len);
+ if (!m->hdr)
+ return -ENOMEM;
+
+ /* seal and parse the top-level message */
+ r = sd_rtnl_message_rewind(m);
+ if (r < 0)
+ return r;
+
+ if (!first)
+ first = m;
+ else {
+ assert(previous);
+
+ previous->next = m;
+ }
+ previous = m;
+ m = NULL;
+
+ ret += new_msg->nlmsg_len;
+
+ /* not a multi-part message, so stop reading*/
+ if (!(new_msg->nlmsg_flags & NLM_F_MULTI))
+ break;
}
- return k;
+ r = rtnl_rqueue_make_room(rtnl);
+ if (r < 0)
+ return r;
+
+ rtnl->rqueue[rtnl->rqueue_size ++] = first;
+ first = NULL;
+
+ return ret;
}
int sd_rtnl_message_rewind(sd_rtnl_message *m) {
- struct ifinfomsg *ifi;
- struct ifaddrmsg *ifa;
- struct rtmsg *rtm;
+ const NLType *type;
+ unsigned i;
+ int r;
assert_return(m, -EINVAL);
- assert_return(m->sealed, -EPERM);
- assert_return(m->hdr, -EINVAL);
- switch(m->hdr->nlmsg_type) {
- case RTM_NEWLINK:
- case RTM_SETLINK:
- case RTM_GETLINK:
- case RTM_DELLINK:
- ifi = NLMSG_DATA(m->hdr);
- UPDATE_RTA(m, IFLA_RTA(ifi));
+ /* don't allow appending to message once parsed */
+ if (!m->sealed)
+ rtnl_message_seal(m);
- break;
- case RTM_NEWADDR:
- case RTM_GETADDR:
- case RTM_DELADDR:
- ifa = NLMSG_DATA(m->hdr);
- UPDATE_RTA(m, IFA_RTA(ifa));
+ 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;
+ }
- break;
- case RTM_NEWROUTE:
- case RTM_GETROUTE:
- case RTM_DELROUTE:
- rtm = NLMSG_DATA(m->hdr);
- UPDATE_RTA(m, RTM_RTA(rtm));
+ m->n_containers = 0;
- break;
- default:
- return -ENOTSUP;
+ if (m->rta_offset_tb[0]) {
+ /* top-level attributes have already been parsed */
+ return 0;
}
- m->n_containers = 0;
+ assert(m->hdr);
+
+ r = type_system_get_type(NULL, &type, m->hdr->nlmsg_type);
+ if (r < 0)
+ return r;
+
+ if (type->type == NLA_NESTED) {
+ const NLTypeSystem *type_system = type->type_system;
+
+ assert(type_system);
+
+ 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],
+ type_system->max,
+ (struct rtattr*)((uint8_t*)NLMSG_DATA(m->hdr) +
+ NLMSG_ALIGN(type->size)),
+ NLMSG_PAYLOAD(m->hdr, type->size));
+ if (r < 0)
+ return r;
+ }
return 0;
}
+
+void rtnl_message_seal(sd_rtnl_message *m) {
+ assert(m);
+ assert(!m->sealed);
+
+ m->sealed = true;
+}
+
+sd_rtnl_message *sd_rtnl_message_next(sd_rtnl_message *m) {
+ assert_return(m, NULL);
+
+ return m->next;
+}