summaryrefslogtreecommitdiff
path: root/src/libsystemd/sd-netlink/netlink-message.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/libsystemd/sd-netlink/netlink-message.c')
-rw-r--r--src/libsystemd/sd-netlink/netlink-message.c291
1 files changed, 157 insertions, 134 deletions
diff --git a/src/libsystemd/sd-netlink/netlink-message.c b/src/libsystemd/sd-netlink/netlink-message.c
index 87324fc2f7..b0ed2f2882 100644
--- a/src/libsystemd/sd-netlink/netlink-message.c
+++ b/src/libsystemd/sd-netlink/netlink-message.c
@@ -34,10 +34,11 @@
#include "netlink-internal.h"
#include "netlink-types.h"
-#define GET_CONTAINER(m, i) ((i) < (m)->n_containers ? (struct rtattr*)((uint8_t*)(m)->hdr + (m)->container_offsets[i]) : NULL)
+#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;
#define RTA_TYPE(rta) ((rta)->rta_type & NLA_TYPE_MASK)
+#define RTA_FLAGS(rta) ((rta)->rta_type & ~NLA_TYPE_MASK)
int message_new_empty(sd_netlink *rtnl, sd_netlink_message **ret) {
sd_netlink_message *m;
@@ -68,15 +69,18 @@ int message_new(sd_netlink *rtnl, sd_netlink_message **ret, uint16_t type) {
size_t size;
int r;
- r = type_system_get_type(NULL, &nl_type, type);
+ r = type_system_get_type(&type_system_root, &nl_type, type);
if (r < 0)
return r;
+ if (type_get_type(nl_type) != NETLINK_TYPE_NESTED)
+ return -EINVAL;
+
r = message_new_empty(rtnl, &m);
if (r < 0)
return r;
- size = NLMSG_SPACE(nl_type->size);
+ size = NLMSG_SPACE(type_get_size(nl_type));
assert(size >= sizeof(struct nlmsghdr));
m->hdr = malloc0(size);
@@ -85,7 +89,7 @@ int message_new(sd_netlink *rtnl, sd_netlink_message **ret, uint16_t type) {
m->hdr->nlmsg_flags = NLM_F_REQUEST | NLM_F_ACK;
- m->container_type_system[0] = nl_type->type_system;
+ type_get_type_system(nl_type, &m->containers[0].type_system);
m->hdr->nlmsg_len = size;
m->hdr->nlmsg_type = type;
@@ -126,7 +130,7 @@ sd_netlink_message *sd_netlink_message_unref(sd_netlink_message *m) {
free(m->hdr);
for (i = 0; i <= m->n_containers; i++)
- free(m->rta_offset_tb[i]);
+ free(m->containers[i].attributes);
sd_netlink_message_unref(m->next);
@@ -214,18 +218,22 @@ static int add_rtattr(sd_netlink_message *m, unsigned short type, const void *da
return offset;
}
-static int message_attribute_has_type(sd_netlink_message *m, uint16_t attribute_type, uint16_t data_type) {
+static int message_attribute_has_type(sd_netlink_message *m, size_t *out_size, 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);
+ assert(m);
+
+ r = type_system_get_type(m->containers[m->n_containers].type_system, &type, attribute_type);
if (r < 0)
return r;
- if (type->type != data_type)
+ if (type_get_type(type) != data_type)
return -EINVAL;
- return type->size;
+ if (out_size)
+ *out_size = type_get_size(type);
+ return 0;
}
int sd_netlink_message_append_string(sd_netlink_message *m, unsigned short type, const char *data) {
@@ -236,11 +244,9 @@ int sd_netlink_message_append_string(sd_netlink_message *m, unsigned short type,
assert_return(!m->sealed, -EPERM);
assert_return(data, -EINVAL);
- r = message_attribute_has_type(m, type, NLA_STRING);
+ r = message_attribute_has_type(m, &size, type, NETLINK_TYPE_STRING);
if (r < 0)
return r;
- else
- size = (size_t)r;
if (size) {
length = strnlen(data, size+1);
@@ -262,7 +268,7 @@ int sd_netlink_message_append_u8(sd_netlink_message *m, unsigned short type, uin
assert_return(m, -EINVAL);
assert_return(!m->sealed, -EPERM);
- r = message_attribute_has_type(m, type, NLA_U8);
+ r = message_attribute_has_type(m, NULL, type, NETLINK_TYPE_U8);
if (r < 0)
return r;
@@ -280,7 +286,7 @@ int sd_netlink_message_append_u16(sd_netlink_message *m, unsigned short type, ui
assert_return(m, -EINVAL);
assert_return(!m->sealed, -EPERM);
- r = message_attribute_has_type(m, type, NLA_U16);
+ r = message_attribute_has_type(m, NULL, type, NETLINK_TYPE_U16);
if (r < 0)
return r;
@@ -297,7 +303,7 @@ int sd_netlink_message_append_u32(sd_netlink_message *m, unsigned short type, ui
assert_return(m, -EINVAL);
assert_return(!m->sealed, -EPERM);
- r = message_attribute_has_type(m, type, NLA_U32);
+ r = message_attribute_has_type(m, NULL, type, NETLINK_TYPE_U32);
if (r < 0)
return r;
@@ -315,7 +321,7 @@ int sd_netlink_message_append_in_addr(sd_netlink_message *m, unsigned short type
assert_return(!m->sealed, -EPERM);
assert_return(data, -EINVAL);
- r = message_attribute_has_type(m, type, NLA_IN_ADDR);
+ r = message_attribute_has_type(m, NULL, type, NETLINK_TYPE_IN_ADDR);
if (r < 0)
return r;
@@ -333,7 +339,7 @@ int sd_netlink_message_append_in6_addr(sd_netlink_message *m, unsigned short typ
assert_return(!m->sealed, -EPERM);
assert_return(data, -EINVAL);
- r = message_attribute_has_type(m, type, NLA_IN_ADDR);
+ r = message_attribute_has_type(m, NULL, type, NETLINK_TYPE_IN_ADDR);
if (r < 0)
return r;
@@ -351,7 +357,7 @@ int sd_netlink_message_append_ether_addr(sd_netlink_message *m, unsigned short t
assert_return(!m->sealed, -EPERM);
assert_return(data, -EINVAL);
- r = message_attribute_has_type(m, type, NLA_ETHER_ADDR);
+ r = message_attribute_has_type(m, NULL, type, NETLINK_TYPE_ETHER_ADDR);
if (r < 0)
return r;
@@ -369,7 +375,7 @@ int sd_netlink_message_append_cache_info(sd_netlink_message *m, unsigned short t
assert_return(!m->sealed, -EPERM);
assert_return(info, -EINVAL);
- r = message_attribute_has_type(m, type, NLA_CACHE_INFO);
+ r = message_attribute_has_type(m, NULL, type, NETLINK_TYPE_CACHE_INFO);
if (r < 0)
return r;
@@ -388,34 +394,31 @@ int sd_netlink_message_open_container(sd_netlink_message *m, unsigned short type
assert_return(!m->sealed, -EPERM);
assert_return(m->n_containers < RTNL_CONTAINER_DEPTH, -ERANGE);
- r = message_attribute_has_type(m, type, NLA_NESTED);
+ r = message_attribute_has_type(m, &size, type, NETLINK_TYPE_NESTED);
if (r < 0) {
const NLTypeSystemUnion *type_system_union;
int family;
- r = message_attribute_has_type(m, type, NLA_UNION);
+ r = message_attribute_has_type(m, &size, type, NETLINK_TYPE_UNION);
if (r < 0)
return r;
- size = (size_t) r;
r = sd_rtnl_message_get_family(m, &family);
if (r < 0)
return r;
- r = type_system_get_type_system_union(m->container_type_system[m->n_containers], &type_system_union, type);
+ r = type_system_get_type_system_union(m->containers[m->n_containers].type_system, &type_system_union, type);
if (r < 0)
return r;
r = type_system_union_protocol_get_type_system(type_system_union,
- &m->container_type_system[m->n_containers + 1],
+ &m->containers[m->n_containers + 1].type_system,
family);
if (r < 0)
return r;
} else {
- size = (size_t)r;
-
- r = type_system_get_type_system(m->container_type_system[m->n_containers],
- &m->container_type_system[m->n_containers + 1],
+ r = type_system_get_type_system(m->containers[m->n_containers].type_system,
+ &m->containers[m->n_containers + 1].type_system,
type);
if (r < 0)
return r;
@@ -425,7 +428,7 @@ int sd_netlink_message_open_container(sd_netlink_message *m, unsigned short type
if (r < 0)
return r;
- m->container_offsets[m->n_containers ++] = r;
+ m->containers[m->n_containers ++].offset = r;
return 0;
}
@@ -437,12 +440,12 @@ int sd_netlink_message_open_container_union(sd_netlink_message *m, unsigned shor
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);
+ r = type_system_get_type_system_union(m->containers[m->n_containers].type_system, &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],
+ &m->containers[m->n_containers + 1].type_system,
key);
if (r < 0)
return r;
@@ -452,11 +455,11 @@ int sd_netlink_message_open_container_union(sd_netlink_message *m, unsigned shor
return r;
/* do we evere need non-null size */
- r = add_rtattr(m, type, NULL, 0);
+ r = add_rtattr(m, type | NLA_F_NESTED, NULL, 0);
if (r < 0)
return r;
- m->container_offsets[m->n_containers ++] = r;
+ m->containers[m->n_containers ++].offset = r;
return 0;
}
@@ -467,29 +470,35 @@ int sd_netlink_message_close_container(sd_netlink_message *m) {
assert_return(!m->sealed, -EPERM);
assert_return(m->n_containers > 0, -EINVAL);
- m->container_type_system[m->n_containers] = NULL;
+ m->containers[m->n_containers].type_system = NULL;
m->n_containers --;
return 0;
}
-int rtnl_message_read_internal(sd_netlink_message *m, unsigned short type, void **data) {
+static int netlink_message_read_internal(sd_netlink_message *m, unsigned short type, void **data, bool *net_byteorder) {
+ struct netlink_attribute *attribute;
struct rtattr *rta;
assert_return(m, -EINVAL);
assert_return(m->sealed, -EPERM);
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]);
+ assert(m->containers[m->n_containers].attributes);
+ assert(type < m->containers[m->n_containers].n_attributes);
+
+ attribute = &m->containers[m->n_containers].attributes[type];
- if(!m->rta_offset_tb[m->n_containers][type])
+ if(!attribute->offset)
return -ENODATA;
- rta = (struct rtattr*)((uint8_t *) m->hdr + m->rta_offset_tb[m->n_containers][type]);
+ rta = (struct rtattr*)((uint8_t *) m->hdr + attribute->offset);
*data = RTA_DATA(rta);
+ if (net_byteorder)
+ *net_byteorder = attribute->net_byteorder;
+
return RTA_PAYLOAD(rta);
}
@@ -499,11 +508,11 @@ int sd_netlink_message_read_string(sd_netlink_message *m, unsigned short type, c
assert_return(m, -EINVAL);
- r = message_attribute_has_type(m, type, NLA_STRING);
+ r = message_attribute_has_type(m, NULL, type, NETLINK_TYPE_STRING);
if (r < 0)
return r;
- r = rtnl_message_read_internal(m, type, &attr_data);
+ r = netlink_message_read_internal(m, type, &attr_data, NULL);
if (r < 0)
return r;
else if (strnlen(attr_data, r) >= (size_t) r)
@@ -521,11 +530,11 @@ int sd_netlink_message_read_u8(sd_netlink_message *m, unsigned short type, uint8
assert_return(m, -EINVAL);
- r = message_attribute_has_type(m, type, NLA_U8);
+ r = message_attribute_has_type(m, NULL, type, NETLINK_TYPE_U8);
if (r < 0)
return r;
- r = rtnl_message_read_internal(m, type, &attr_data);
+ r = netlink_message_read_internal(m, type, &attr_data, NULL);
if (r < 0)
return r;
else if ((size_t) r < sizeof(uint8_t))
@@ -538,45 +547,55 @@ int sd_netlink_message_read_u8(sd_netlink_message *m, unsigned short type, uint8
}
int sd_netlink_message_read_u16(sd_netlink_message *m, unsigned short type, uint16_t *data) {
- int r;
void *attr_data;
+ bool net_byteorder;
+ int r;
assert_return(m, -EINVAL);
- r = message_attribute_has_type(m, type, NLA_U16);
+ r = message_attribute_has_type(m, NULL, type, NETLINK_TYPE_U16);
if (r < 0)
return r;
- r = rtnl_message_read_internal(m, type, &attr_data);
+ r = netlink_message_read_internal(m, type, &attr_data, &net_byteorder);
if (r < 0)
return r;
else if ((size_t) r < sizeof(uint16_t))
return -EIO;
- if (data)
- *data = *(uint16_t *) attr_data;
+ if (data) {
+ if (net_byteorder)
+ *data = be16toh(*(uint16_t *) attr_data);
+ else
+ *data = *(uint16_t *) attr_data;
+ }
return 0;
}
int sd_netlink_message_read_u32(sd_netlink_message *m, unsigned short type, uint32_t *data) {
- int r;
void *attr_data;
+ bool net_byteorder;
+ int r;
assert_return(m, -EINVAL);
- r = message_attribute_has_type(m, type, NLA_U32);
+ r = message_attribute_has_type(m, NULL, type, NETLINK_TYPE_U32);
if (r < 0)
return r;
- r = rtnl_message_read_internal(m, type, &attr_data);
+ r = netlink_message_read_internal(m, type, &attr_data, &net_byteorder);
if (r < 0)
return r;
else if ((size_t)r < sizeof(uint32_t))
return -EIO;
- if (data)
- *data = *(uint32_t *) attr_data;
+ if (data) {
+ if (net_byteorder)
+ *data = be32toh(*(uint32_t *) attr_data);
+ else
+ *data = *(uint32_t *) attr_data;
+ }
return 0;
}
@@ -587,11 +606,11 @@ int sd_netlink_message_read_ether_addr(sd_netlink_message *m, unsigned short typ
assert_return(m, -EINVAL);
- r = message_attribute_has_type(m, type, NLA_ETHER_ADDR);
+ r = message_attribute_has_type(m, NULL, type, NETLINK_TYPE_ETHER_ADDR);
if (r < 0)
return r;
- r = rtnl_message_read_internal(m, type, &attr_data);
+ r = netlink_message_read_internal(m, type, &attr_data, NULL);
if (r < 0)
return r;
else if ((size_t)r < sizeof(struct ether_addr))
@@ -609,11 +628,11 @@ int sd_netlink_message_read_cache_info(sd_netlink_message *m, unsigned short typ
assert_return(m, -EINVAL);
- r = message_attribute_has_type(m, type, NLA_CACHE_INFO);
+ r = message_attribute_has_type(m, NULL, type, NETLINK_TYPE_CACHE_INFO);
if (r < 0)
return r;
- r = rtnl_message_read_internal(m, type, &attr_data);
+ r = netlink_message_read_internal(m, type, &attr_data, NULL);
if (r < 0)
return r;
else if ((size_t)r < sizeof(struct ifa_cacheinfo))
@@ -631,11 +650,11 @@ int sd_netlink_message_read_in_addr(sd_netlink_message *m, unsigned short type,
assert_return(m, -EINVAL);
- r = message_attribute_has_type(m, type, NLA_IN_ADDR);
+ r = message_attribute_has_type(m, NULL, type, NETLINK_TYPE_IN_ADDR);
if (r < 0)
return r;
- r = rtnl_message_read_internal(m, type, &attr_data);
+ r = netlink_message_read_internal(m, type, &attr_data, NULL);
if (r < 0)
return r;
else if ((size_t)r < sizeof(struct in_addr))
@@ -653,11 +672,11 @@ int sd_netlink_message_read_in6_addr(sd_netlink_message *m, unsigned short type,
assert_return(m, -EINVAL);
- r = message_attribute_has_type(m, type, NLA_IN_ADDR);
+ r = message_attribute_has_type(m, NULL, type, NETLINK_TYPE_IN_ADDR);
if (r < 0)
return r;
- r = rtnl_message_read_internal(m, type, &attr_data);
+ r = netlink_message_read_internal(m, type, &attr_data, NULL);
if (r < 0)
return r;
else if ((size_t)r < sizeof(struct in6_addr))
@@ -669,34 +688,73 @@ int sd_netlink_message_read_in6_addr(sd_netlink_message *m, unsigned short type,
return 0;
}
-int sd_netlink_message_enter_container(sd_netlink_message *m, unsigned short type) {
+static int netlink_container_parse(sd_netlink_message *m,
+ struct netlink_container *container,
+ int count,
+ struct rtattr *rta,
+ unsigned int rt_len) {
+ _cleanup_free_ struct netlink_attribute *attributes = NULL;
+
+ attributes = new0(struct netlink_attribute, count);
+ if(!attributes)
+ return -ENOMEM;
+
+ for (; RTA_OK(rta, rt_len); rta = RTA_NEXT(rta, rt_len)) {
+ unsigned short type;
+
+ type = RTA_TYPE(rta);
+
+ /* if the kernel is newer than the headers we used
+ when building, we ignore out-of-range attributes */
+ if (type >= count)
+ continue;
+
+ if (attributes[type].offset)
+ log_debug("rtnl: message parse - overwriting repeated attribute");
+
+ attributes[type].offset = (uint8_t *) rta - (uint8_t *) m->hdr;
+ attributes[type].nested = RTA_FLAGS(rta) & NLA_F_NESTED;
+ attributes[type].net_byteorder = RTA_FLAGS(rta) & NLA_F_NET_BYTEORDER;
+ }
+
+ container->attributes = attributes;
+ attributes = NULL;
+ container->n_attributes = count;
+
+ return 0;
+}
+
+int sd_netlink_message_enter_container(sd_netlink_message *m, unsigned short type_id) {
const NLType *nl_type;
const NLTypeSystem *type_system;
void *container;
+ uint16_t type;
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],
+ r = type_system_get_type(m->containers[m->n_containers].type_system,
&nl_type,
- type);
+ type_id);
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 = type_get_type(nl_type);
+
+ if (type == NETLINK_TYPE_NESTED) {
+ r = type_system_get_type_system(m->containers[m->n_containers].type_system,
&type_system,
- type);
+ type_id);
if (r < 0)
return r;
- } else if (nl_type->type == NLA_UNION) {
+ } else if (type == NETLINK_TYPE_UNION) {
const NLTypeSystemUnion *type_system_union;
- r = type_system_get_type_system_union(m->container_type_system[m->n_containers],
+ r = type_system_get_type_system_union(m->containers[m->n_containers].type_system,
&type_system_union,
- type);
+ type_id);
if (r < 0)
return r;
@@ -739,7 +797,7 @@ int sd_netlink_message_enter_container(sd_netlink_message *m, unsigned short typ
} else
return -EINVAL;
- r = rtnl_message_read_internal(m, type, &container);
+ r = netlink_message_read_internal(m, type_id, &container, NULL);
if (r < 0)
return r;
else
@@ -747,18 +805,17 @@ int sd_netlink_message_enter_container(sd_netlink_message *m, unsigned short typ
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);
+ r = netlink_container_parse(m,
+ &m->containers[m->n_containers],
+ type_system_get_count(type_system),
+ container,
+ size);
if (r < 0) {
m->n_containers --;
return r;
}
- m->container_type_system[m->n_containers] = type_system;
+ m->containers[m->n_containers].type_system = type_system;
return 0;
}
@@ -768,9 +825,9 @@ int sd_netlink_message_exit_container(sd_netlink_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;
+ free(m->containers[m->n_containers].attributes);
+ m->containers[m->n_containers].attributes = NULL;
+ m->containers[m->n_containers].type_system = NULL;
m->n_containers --;
@@ -805,43 +862,10 @@ int sd_netlink_message_get_errno(sd_netlink_message *m) {
return err->error;
}
-int rtnl_message_parse(sd_netlink_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_TYPE(rta);
-
- /* 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;
-}
-
int sd_netlink_message_rewind(sd_netlink_message *m) {
- const NLType *type;
+ const NLType *nl_type;
+ uint16_t type;
+ size_t size;
unsigned i;
int r;
@@ -852,39 +876,38 @@ int sd_netlink_message_rewind(sd_netlink_message *m) {
rtnl_message_seal(m);
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;
+ free(m->containers[i].attributes);
+ m->containers[i].attributes = NULL;
}
m->n_containers = 0;
- if (m->rta_offset_tb[0]) {
+ if (m->containers[0].attributes) {
/* top-level attributes have already been parsed */
return 0;
}
assert(m->hdr);
- r = type_system_get_type(NULL, &type, m->hdr->nlmsg_type);
+ r = type_system_get_type(&type_system_root, &nl_type, m->hdr->nlmsg_type);
if (r < 0)
return r;
- if (type->type == NLA_NESTED) {
- const NLTypeSystem *type_system = type->type_system;
+ type = type_get_type(nl_type);
+ size = type_get_size(nl_type);
+
+ if (type == NETLINK_TYPE_NESTED) {
+ const NLTypeSystem *type_system;
- assert(type_system);
+ type_get_type_system(nl_type, &type_system);
- m->container_type_system[0] = type_system;
+ m->containers[0].type_system = 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));
+ r = netlink_container_parse(m,
+ &m->containers[m->n_containers],
+ type_system_get_count(type_system),
+ (struct rtattr*)((uint8_t*)NLMSG_DATA(m->hdr) + NLMSG_ALIGN(size)),
+ NLMSG_PAYLOAD(m->hdr, size));
if (r < 0)
return r;
}