Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

netlink: add nla_policy_len()

It calculates the max. length of a Netlink policy, which is usefull
for allocating Netlink buffers roughly the size of the actual
message.

Signed-off-by: Holger Eitzenberger <holger@eitzenberger.org>
Signed-off-by: Patrick McHardy <kaber@trash.net>

authored by

Holger Eitzenberger and committed by
Patrick McHardy
e487eb99 d0dba725

+28
+1
include/net/netlink.h
··· 230 230 extern int nla_parse(struct nlattr *tb[], int maxtype, 231 231 struct nlattr *head, int len, 232 232 const struct nla_policy *policy); 233 + extern int nla_policy_len(const struct nla_policy *, int); 233 234 extern struct nlattr * nla_find(struct nlattr *head, int len, int attrtype); 234 235 extern size_t nla_strlcpy(char *dst, const struct nlattr *nla, 235 236 size_t dstsize);
+27
net/netlink/attr.c
··· 133 133 } 134 134 135 135 /** 136 + * nla_policy_len - Determin the max. length of a policy 137 + * @policy: policy to use 138 + * @n: number of policies 139 + * 140 + * Determines the max. length of the policy. It is currently used 141 + * to allocated Netlink buffers roughly the size of the actual 142 + * message. 143 + * 144 + * Returns 0 on success or a negative error code. 145 + */ 146 + int 147 + nla_policy_len(const struct nla_policy *p, int n) 148 + { 149 + int i, len = 0; 150 + 151 + for (i = 0; i < n; i++) { 152 + if (p->len) 153 + len += nla_total_size(p->len); 154 + else if (nla_attr_minlen[p->type]) 155 + len += nla_total_size(nla_attr_minlen[p->type]); 156 + } 157 + 158 + return len; 159 + } 160 + 161 + /** 136 162 * nla_parse - Parse a stream of attributes into a tb buffer 137 163 * @tb: destination array with maxtype+1 elements 138 164 * @maxtype: maximum attribute type to be expected ··· 482 456 } 483 457 484 458 EXPORT_SYMBOL(nla_validate); 459 + EXPORT_SYMBOL(nla_policy_len); 485 460 EXPORT_SYMBOL(nla_parse); 486 461 EXPORT_SYMBOL(nla_find); 487 462 EXPORT_SYMBOL(nla_strlcpy);