HALF ECHO LEARN / REFERENCE COLLECTION
v262.2src/libsystemd/sd-netlink/netlink-message.c
Git blob: 0fff7cee1b215cdbc0786e193973f8706deacd80
Lines 1001–1363 of 1,363
int sd_netlink_message_read_in6_addr(sd_netlink_message *m, uint16_t attr_type, struct in6_addr *ret) {
assert_return(m, -EINVAL);
return netlink_message_read_impl(
m, attr_type, /* strict= */ true,
NETLINK_TYPE_IN_ADDR, sizeof(struct in6_addr),
ret, /* ret_net_byteorder= */ NULL);
}
int sd_netlink_message_has_flag(sd_netlink_message *m, uint16_t attr_type) {
void *attr_data;
int r;
assert_return(m, -EINVAL);
/* This returns 1 when the flag is set, 0 when not set, negative errno on error. */
r = message_attribute_has_type(m, NULL, attr_type, NETLINK_TYPE_FLAG);
if (r < 0)
return r;
r = netlink_message_read_internal(m, attr_type, &attr_data, NULL);
if (r == -ENODATA)
return 0;
if (r < 0)
return r;
return 1;
}
int sd_netlink_message_read_strv(sd_netlink_message *m, uint16_t container_type, uint16_t attr_type, char ***ret) {
_cleanup_strv_free_ char **s = NULL;
const NLAPolicySet *policy_set;
const NLAPolicy *policy;
struct rtattr *rta;
void *container;
size_t rt_len;
int r;
assert_return(m, -EINVAL);
assert_return(m->n_containers < NETLINK_CONTAINER_DEPTH, -EINVAL);
policy = policy_set_get_policy(
m->containers[m->n_containers].policy_set,
container_type);
if (!policy)
return -EOPNOTSUPP;
if (policy_get_type(policy) != NETLINK_TYPE_NESTED)
return -EINVAL;
policy_set = policy_set_get_policy_set(
m->containers[m->n_containers].policy_set,
container_type);
if (!policy_set)
return -EOPNOTSUPP;
policy = policy_set_get_policy(policy_set, attr_type);
if (!policy)
return -EOPNOTSUPP;
if (policy_get_type(policy) != NETLINK_TYPE_STRING)
return -EINVAL;
r = netlink_message_read_internal(m, container_type, &container, NULL);
if (r < 0)
return r;
rt_len = (size_t) r;
rta = container;
/* RTA_OK() macro compares with rta->rt_len, which is unsigned short, and
* LGTM.com analysis does not like the type difference. Hence, here we
* introduce an unsigned short variable as a workaround. */
unsigned short len = rt_len;
for (; RTA_OK(rta, len); rta = RTA_NEXT(rta, len)) {
uint16_t type;
type = RTA_TYPE(rta);
if (type != attr_type)
continue;
if (ret) {
r = strv_extend(&s, RTA_DATA(rta));
if (r < 0)
return r;
}
}
if (ret)
*ret = TAKE_PTR(s);
return 0;
}
static int netlink_container_parse(
sd_netlink_message *m,
struct netlink_container *container,
struct rtattr *rta,
size_t rt_len) {
_cleanup_free_ struct netlink_attribute *attributes = NULL;
uint16_t max_attr = 0;
/* RTA_OK() macro compares with rta->rt_len, which is unsigned short, and
* LGTM.com analysis does not like the type difference. Hence, here we
* introduce an unsigned short variable as a workaround. */
unsigned short len = rt_len;
for (; RTA_OK(rta, len); rta = RTA_NEXT(rta, len)) {
uint16_t attr;
attr = RTA_TYPE(rta);
max_attr = MAX(max_attr, attr);
if (!GREEDY_REALLOC0(attributes, (size_t) max_attr + 1))
return -ENOMEM;
if (attributes[attr].offset != 0)
log_debug("sd-netlink: message parse - overwriting repeated attribute");
attributes[attr].offset = (uint8_t *) rta - (uint8_t *) m->hdr;
attributes[attr].nested = RTA_FLAGS(rta) & NLA_F_NESTED;
attributes[attr].net_byteorder = RTA_FLAGS(rta) & NLA_F_NET_BYTEORDER;
}
container->attributes = TAKE_PTR(attributes);
container->max_attribute = max_attr;
return 0;
}
int sd_netlink_message_enter_container(sd_netlink_message *m, uint16_t attr_type) {
const NLAPolicy *policy;
const NLAPolicySet *policy_set;
void *container;
size_t size;
int r;
assert_return(m, -EINVAL);
assert_return(m->n_containers < (NETLINK_CONTAINER_DEPTH - 1), -EINVAL);
policy = policy_set_get_policy(
m->containers[m->n_containers].policy_set,
attr_type);
if (!policy)
return -EOPNOTSUPP;
switch (policy_get_type(policy)) {
case NETLINK_TYPE_NESTED:
policy_set = policy_set_get_policy_set(
m->containers[m->n_containers].policy_set,
attr_type);
break;
case NETLINK_TYPE_NESTED_UNION_BY_STRING: {
const NLAPolicySetUnion *policy_set_union;
const char *key;
policy_set_union = policy_get_policy_set_union(policy);
if (!policy_set_union)
return -EOPNOTSUPP;
r = sd_netlink_message_read_string(
m,
policy_set_union_get_match_attribute(policy_set_union),
&key);
if (r < 0)
return r;
policy_set = policy_set_union_get_policy_set_by_string(
policy_set_union,
key);
break;
}
case NETLINK_TYPE_NESTED_UNION_BY_FAMILY: {
const NLAPolicySetUnion *policy_set_union;
int family;
policy_set_union = policy_get_policy_set_union(policy);
if (!policy_set_union)
return -EOPNOTSUPP;
r = sd_rtnl_message_get_family(m, &family);
if (r < 0)
return r;
policy_set = policy_set_union_get_policy_set_by_family(
policy_set_union,
family);
break;
}
default:
assert_not_reached();
}
if (!policy_set)
return -EOPNOTSUPP;
r = netlink_message_read_internal(m, attr_type, &container, NULL);
if (r < 0)
return r;
size = (size_t) r;
m->n_containers++;
r = netlink_container_parse(m,
&m->containers[m->n_containers],
container,
size);
if (r < 0) {
m->n_containers--;
return r;
}
m->containers[m->n_containers].policy_set = policy_set;
return 0;
}
int sd_netlink_message_enter_array(sd_netlink_message *m, uint16_t attr_type) {
void *container;
size_t size;
int r;
assert_return(m, -EINVAL);
assert_return(m->n_containers < (NETLINK_CONTAINER_DEPTH - 1), -EINVAL);
r = netlink_message_read_internal(m, attr_type, &container, NULL);
if (r < 0)
return r;
size = (size_t) r;
m->n_containers++;
r = netlink_container_parse(m,
&m->containers[m->n_containers],
container,
size);
if (r < 0) {
m->n_containers--;
return r;
}
m->containers[m->n_containers].policy_set = m->containers[m->n_containers - 1].policy_set;
return 0;
}
int sd_netlink_message_exit_container(sd_netlink_message *m) {
assert_return(m, -EINVAL);
assert_return(m->sealed, -EINVAL);
assert_return(m->n_containers > 0, -EINVAL);
m->containers[m->n_containers].attributes = mfree(m->containers[m->n_containers].attributes);
m->containers[m->n_containers].max_attribute = 0;
m->containers[m->n_containers].policy_set = NULL;
m->n_containers--;
return 0;
}
int sd_netlink_message_get_max_attribute(sd_netlink_message *m, uint16_t *ret) {
assert_return(m, -EINVAL);
assert_return(m->sealed, -EINVAL);
assert_return(ret, -EINVAL);
*ret = m->containers[m->n_containers].max_attribute;
return 0;
}
int sd_netlink_message_is_error(sd_netlink_message *m) {
assert_return(m, 0);
assert_return(m->hdr, 0);
return m->hdr->nlmsg_type == NLMSG_ERROR;
}
int sd_netlink_message_get_errno(sd_netlink_message *m) {
struct nlmsgerr *err;
assert_return(m, -EINVAL);
assert_return(m->hdr, -EINVAL);
if (!sd_netlink_message_is_error(m))
return 0;
err = NLMSG_DATA(m->hdr);
return err->error;
}
static int netlink_message_parse_error(sd_netlink_message *m) {
struct nlmsgerr *err = NLMSG_DATA(m->hdr);
size_t hlen = sizeof(struct nlmsgerr);
/* no TLVs, nothing to do here */
if (!(m->hdr->nlmsg_flags & NLM_F_ACK_TLVS))
return 0;
/* if NLM_F_CAPPED is set then the inner err msg was capped */
if (!(m->hdr->nlmsg_flags & NLM_F_CAPPED))
hlen += err->msg.nlmsg_len - sizeof(struct nlmsghdr);
if (m->hdr->nlmsg_len <= NLMSG_SPACE(hlen))
return 0;
return netlink_container_parse(m,
&m->containers[m->n_containers],
(struct rtattr*)((uint8_t*) NLMSG_DATA(m->hdr) + hlen),
NLMSG_PAYLOAD(m->hdr, hlen));
}
int sd_netlink_message_rewind(sd_netlink_message *m, sd_netlink *nl) {
size_t size;
int r;
assert_return(m, -EINVAL);
assert_return(nl, -EINVAL);
/* don't allow appending to message once parsed */
message_seal(m);
for (unsigned i = 1; i <= m->n_containers; i++)
m->containers[i].attributes = mfree(m->containers[i].attributes);
m->n_containers = 0;
if (m->containers[0].attributes)
/* top-level attributes have already been parsed */
return 0;
assert(m->hdr);
r = netlink_get_policy_set_and_header_size(
nl,
m->hdr->nlmsg_type,
m->hdr->nlmsg_flags,
&m->containers[0].policy_set,
&size);
if (r < 0)
return r;
if (sd_netlink_message_is_error(m))
return netlink_message_parse_error(m);
return netlink_container_parse(m,
&m->containers[0],
(struct rtattr*)((uint8_t*) NLMSG_DATA(m->hdr) + NLMSG_ALIGN(size)),
NLMSG_PAYLOAD(m->hdr, size));
}
void message_seal(sd_netlink_message *m) {
assert(m);
m->sealed = true;
}
sd_netlink_message* sd_netlink_message_next(sd_netlink_message *m) {
assert_return(m, NULL);
return m->next;
}