| 1 |
/* SPDX-License-Identifier: LGPL-2.1-or-later */ |
| 2 |
|
| 3 |
#include <linux/netlink.h> |
| 4 |
|
| 5 |
#include "netlink-internal.h" |
| 6 |
#include "netlink-types-internal.h" |
| 7 |
#include "string-util.h" |
| 8 |
|
| 9 |
static const NLAPolicy empty_policies[1] = { |
| 10 |
/* fake array to avoid .types==NULL, which denotes invalid type-systems */ |
| 11 |
}; |
| 12 |
|
| 13 |
DEFINE_POLICY_SET(empty); |
| 14 |
|
| 15 |
static const NLAPolicy error_policies[] = { |
| 16 |
[NLMSGERR_ATTR_MSG] = BUILD_POLICY(STRING), |
| 17 |
[NLMSGERR_ATTR_OFFS] = BUILD_POLICY(U32), |
| 18 |
}; |
| 19 |
|
| 20 |
DEFINE_POLICY_SET(error); |
| 21 |
|
| 22 |
static const NLAPolicy basic_policies[] = { |
| 23 |
[NLMSG_DONE] = BUILD_POLICY_NESTED(empty), |
| 24 |
[NLMSG_ERROR] = BUILD_POLICY_NESTED_WITH_SIZE(error, sizeof(struct nlmsgerr)), |
| 25 |
}; |
| 26 |
|
| 27 |
DEFINE_POLICY_SET(basic); |
| 28 |
|
| 29 |
NLAType policy_get_type(const NLAPolicy *policy) { |
| 30 |
return ASSERT_PTR(policy)->type; |
| 31 |
} |
| 32 |
|
| 33 |
size_t policy_get_size(const NLAPolicy *policy) { |
| 34 |
return ASSERT_PTR(policy)->size; |
| 35 |
} |
| 36 |
|
| 37 |
const NLAPolicySet *policy_get_policy_set(const NLAPolicy *policy) { |
| 38 |
assert(policy); |
| 39 |
assert(policy->type == NETLINK_TYPE_NESTED); |
| 40 |
|
| 41 |
return ASSERT_PTR(policy->policy_set); |
| 42 |
} |
| 43 |
|
| 44 |
const NLAPolicySetUnion *policy_get_policy_set_union(const NLAPolicy *policy) { |
| 45 |
assert(policy); |
| 46 |
assert(IN_SET(policy->type, NETLINK_TYPE_NESTED_UNION_BY_STRING, NETLINK_TYPE_NESTED_UNION_BY_FAMILY)); |
| 47 |
|
| 48 |
return ASSERT_PTR(policy->policy_set_union); |
| 49 |
} |
| 50 |
|
| 51 |
int netlink_get_policy_set_and_header_size( |
| 52 |
sd_netlink *nl, |
| 53 |
uint16_t type, |
| 54 |
uint16_t flags, |
| 55 |
const NLAPolicySet **ret_policy_set, |
| 56 |
size_t *ret_header_size) { |
| 57 |
|
| 58 |
const NLAPolicy *policy; |
| 59 |
|
| 60 |
assert(nl); |
| 61 |
|
| 62 |
if (IN_SET(type, NLMSG_DONE, NLMSG_ERROR)) |
| 63 |
policy = policy_set_get_policy(&basic_policy_set, type); |
| 64 |
else |
| 65 |
switch (nl->protocol) { |
| 66 |
case NETLINK_ROUTE: |
| 67 |
policy = rtnl_get_policy(type); |
| 68 |
break; |
| 69 |
case NETLINK_NETFILTER: |
| 70 |
policy = nfnl_get_policy(type); |
| 71 |
break; |
| 72 |
case NETLINK_GENERIC: |
| 73 |
return genl_get_policy_set_and_header_size(nl, type, ret_policy_set, ret_header_size); |
| 74 |
case NETLINK_SOCK_DIAG: |
| 75 |
policy = sdnl_get_policy(type, flags); |
| 76 |
break; |
| 77 |
default: |
| 78 |
return -EOPNOTSUPP; |
| 79 |
} |
| 80 |
if (!policy) |
| 81 |
return -EOPNOTSUPP; |
| 82 |
|
| 83 |
if (policy_get_type(policy) != NETLINK_TYPE_NESTED) |
| 84 |
return -EOPNOTSUPP; |
| 85 |
|
| 86 |
if (ret_policy_set) |
| 87 |
*ret_policy_set = policy_get_policy_set(policy); |
| 88 |
if (ret_header_size) |
| 89 |
*ret_header_size = policy_get_size(policy); |
| 90 |
return 0; |
| 91 |
} |
| 92 |
|
| 93 |
const NLAPolicy *policy_set_get_policy(const NLAPolicySet *policy_set, uint16_t attr_type) { |
| 94 |
const NLAPolicy *policy; |
| 95 |
|
| 96 |
assert(policy_set); |
| 97 |
assert(policy_set->policies); |
| 98 |
|
| 99 |
if (attr_type >= policy_set->count) |
| 100 |
return NULL; |
| 101 |
|
| 102 |
policy = &policy_set->policies[attr_type]; |
| 103 |
|
| 104 |
if (policy->type == NETLINK_TYPE_UNSPEC) |
| 105 |
return NULL; |
| 106 |
|
| 107 |
return policy; |
| 108 |
} |
| 109 |
|
| 110 |
const NLAPolicySet *policy_set_get_policy_set(const NLAPolicySet *policy_set, uint16_t attr_type) { |
| 111 |
const NLAPolicy *policy; |
| 112 |
|
| 113 |
policy = policy_set_get_policy(policy_set, attr_type); |
| 114 |
if (!policy) |
| 115 |
return NULL; |
| 116 |
|
| 117 |
return policy_get_policy_set(policy); |
| 118 |
} |
| 119 |
|
| 120 |
const NLAPolicySetUnion *policy_set_get_policy_set_union(const NLAPolicySet *policy_set, uint16_t attr_type) { |
| 121 |
const NLAPolicy *policy; |
| 122 |
|
| 123 |
policy = policy_set_get_policy(policy_set, attr_type); |
| 124 |
if (!policy) |
| 125 |
return NULL; |
| 126 |
|
| 127 |
return policy_get_policy_set_union(policy); |
| 128 |
} |
| 129 |
|
| 130 |
uint16_t policy_set_union_get_match_attribute(const NLAPolicySetUnion *policy_set_union) { |
| 131 |
assert(policy_set_union->match_attribute != 0); |
| 132 |
|
| 133 |
return policy_set_union->match_attribute; |
| 134 |
} |
| 135 |
|
| 136 |
const NLAPolicySet *policy_set_union_get_policy_set_by_string(const NLAPolicySetUnion *policy_set_union, const char *string) { |
| 137 |
assert(policy_set_union); |
| 138 |
assert(policy_set_union->elements); |
| 139 |
assert(string); |
| 140 |
|
| 141 |
for (size_t i = 0; i < policy_set_union->count; i++) |
| 142 |
if (streq(policy_set_union->elements[i].string, string)) |
| 143 |
return &policy_set_union->elements[i].policy_set; |
| 144 |
|
| 145 |
return NULL; |
| 146 |
} |
| 147 |
|
| 148 |
const NLAPolicySet *policy_set_union_get_policy_set_by_family(const NLAPolicySetUnion *policy_set_union, int family) { |
| 149 |
assert(policy_set_union); |
| 150 |
assert(policy_set_union->elements); |
| 151 |
|
| 152 |
for (size_t i = 0; i < policy_set_union->count; i++) |
| 153 |
if (policy_set_union->elements[i].family == family) |
| 154 |
return &policy_set_union->elements[i].policy_set; |
| 155 |
|
| 156 |
return NULL; |
| 157 |
} |