18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: (LGPL-2.1 OR BSD-2-Clause)
28c2ecf20Sopenharmony_ci
38c2ecf20Sopenharmony_ci/*
48c2ecf20Sopenharmony_ci * NETLINK      Netlink attributes
58c2ecf20Sopenharmony_ci *
68c2ecf20Sopenharmony_ci * Copyright (c) 2003-2013 Thomas Graf <tgraf@suug.ch>
78c2ecf20Sopenharmony_ci */
88c2ecf20Sopenharmony_ci
98c2ecf20Sopenharmony_ci#include <errno.h>
108c2ecf20Sopenharmony_ci#include <string.h>
118c2ecf20Sopenharmony_ci#include <stdio.h>
128c2ecf20Sopenharmony_ci#include <linux/rtnetlink.h>
138c2ecf20Sopenharmony_ci#include "nlattr.h"
148c2ecf20Sopenharmony_ci#include "libbpf_internal.h"
158c2ecf20Sopenharmony_ci
168c2ecf20Sopenharmony_cistatic uint16_t nla_attr_minlen[LIBBPF_NLA_TYPE_MAX+1] = {
178c2ecf20Sopenharmony_ci	[LIBBPF_NLA_U8]		= sizeof(uint8_t),
188c2ecf20Sopenharmony_ci	[LIBBPF_NLA_U16]	= sizeof(uint16_t),
198c2ecf20Sopenharmony_ci	[LIBBPF_NLA_U32]	= sizeof(uint32_t),
208c2ecf20Sopenharmony_ci	[LIBBPF_NLA_U64]	= sizeof(uint64_t),
218c2ecf20Sopenharmony_ci	[LIBBPF_NLA_STRING]	= 1,
228c2ecf20Sopenharmony_ci	[LIBBPF_NLA_FLAG]	= 0,
238c2ecf20Sopenharmony_ci};
248c2ecf20Sopenharmony_ci
258c2ecf20Sopenharmony_cistatic struct nlattr *nla_next(const struct nlattr *nla, int *remaining)
268c2ecf20Sopenharmony_ci{
278c2ecf20Sopenharmony_ci	int totlen = NLA_ALIGN(nla->nla_len);
288c2ecf20Sopenharmony_ci
298c2ecf20Sopenharmony_ci	*remaining -= totlen;
308c2ecf20Sopenharmony_ci	return (struct nlattr *) ((char *) nla + totlen);
318c2ecf20Sopenharmony_ci}
328c2ecf20Sopenharmony_ci
338c2ecf20Sopenharmony_cistatic int nla_ok(const struct nlattr *nla, int remaining)
348c2ecf20Sopenharmony_ci{
358c2ecf20Sopenharmony_ci	return remaining >= sizeof(*nla) &&
368c2ecf20Sopenharmony_ci	       nla->nla_len >= sizeof(*nla) &&
378c2ecf20Sopenharmony_ci	       nla->nla_len <= remaining;
388c2ecf20Sopenharmony_ci}
398c2ecf20Sopenharmony_ci
408c2ecf20Sopenharmony_cistatic int nla_type(const struct nlattr *nla)
418c2ecf20Sopenharmony_ci{
428c2ecf20Sopenharmony_ci	return nla->nla_type & NLA_TYPE_MASK;
438c2ecf20Sopenharmony_ci}
448c2ecf20Sopenharmony_ci
458c2ecf20Sopenharmony_cistatic int validate_nla(struct nlattr *nla, int maxtype,
468c2ecf20Sopenharmony_ci			struct libbpf_nla_policy *policy)
478c2ecf20Sopenharmony_ci{
488c2ecf20Sopenharmony_ci	struct libbpf_nla_policy *pt;
498c2ecf20Sopenharmony_ci	unsigned int minlen = 0;
508c2ecf20Sopenharmony_ci	int type = nla_type(nla);
518c2ecf20Sopenharmony_ci
528c2ecf20Sopenharmony_ci	if (type < 0 || type > maxtype)
538c2ecf20Sopenharmony_ci		return 0;
548c2ecf20Sopenharmony_ci
558c2ecf20Sopenharmony_ci	pt = &policy[type];
568c2ecf20Sopenharmony_ci
578c2ecf20Sopenharmony_ci	if (pt->type > LIBBPF_NLA_TYPE_MAX)
588c2ecf20Sopenharmony_ci		return 0;
598c2ecf20Sopenharmony_ci
608c2ecf20Sopenharmony_ci	if (pt->minlen)
618c2ecf20Sopenharmony_ci		minlen = pt->minlen;
628c2ecf20Sopenharmony_ci	else if (pt->type != LIBBPF_NLA_UNSPEC)
638c2ecf20Sopenharmony_ci		minlen = nla_attr_minlen[pt->type];
648c2ecf20Sopenharmony_ci
658c2ecf20Sopenharmony_ci	if (libbpf_nla_len(nla) < minlen)
668c2ecf20Sopenharmony_ci		return -1;
678c2ecf20Sopenharmony_ci
688c2ecf20Sopenharmony_ci	if (pt->maxlen && libbpf_nla_len(nla) > pt->maxlen)
698c2ecf20Sopenharmony_ci		return -1;
708c2ecf20Sopenharmony_ci
718c2ecf20Sopenharmony_ci	if (pt->type == LIBBPF_NLA_STRING) {
728c2ecf20Sopenharmony_ci		char *data = libbpf_nla_data(nla);
738c2ecf20Sopenharmony_ci
748c2ecf20Sopenharmony_ci		if (data[libbpf_nla_len(nla) - 1] != '\0')
758c2ecf20Sopenharmony_ci			return -1;
768c2ecf20Sopenharmony_ci	}
778c2ecf20Sopenharmony_ci
788c2ecf20Sopenharmony_ci	return 0;
798c2ecf20Sopenharmony_ci}
808c2ecf20Sopenharmony_ci
818c2ecf20Sopenharmony_cistatic inline int nlmsg_len(const struct nlmsghdr *nlh)
828c2ecf20Sopenharmony_ci{
838c2ecf20Sopenharmony_ci	return nlh->nlmsg_len - NLMSG_HDRLEN;
848c2ecf20Sopenharmony_ci}
858c2ecf20Sopenharmony_ci
868c2ecf20Sopenharmony_ci/**
878c2ecf20Sopenharmony_ci * Create attribute index based on a stream of attributes.
888c2ecf20Sopenharmony_ci * @arg tb		Index array to be filled (maxtype+1 elements).
898c2ecf20Sopenharmony_ci * @arg maxtype		Maximum attribute type expected and accepted.
908c2ecf20Sopenharmony_ci * @arg head		Head of attribute stream.
918c2ecf20Sopenharmony_ci * @arg len		Length of attribute stream.
928c2ecf20Sopenharmony_ci * @arg policy		Attribute validation policy.
938c2ecf20Sopenharmony_ci *
948c2ecf20Sopenharmony_ci * Iterates over the stream of attributes and stores a pointer to each
958c2ecf20Sopenharmony_ci * attribute in the index array using the attribute type as index to
968c2ecf20Sopenharmony_ci * the array. Attribute with a type greater than the maximum type
978c2ecf20Sopenharmony_ci * specified will be silently ignored in order to maintain backwards
988c2ecf20Sopenharmony_ci * compatibility. If \a policy is not NULL, the attribute will be
998c2ecf20Sopenharmony_ci * validated using the specified policy.
1008c2ecf20Sopenharmony_ci *
1018c2ecf20Sopenharmony_ci * @see nla_validate
1028c2ecf20Sopenharmony_ci * @return 0 on success or a negative error code.
1038c2ecf20Sopenharmony_ci */
1048c2ecf20Sopenharmony_ciint libbpf_nla_parse(struct nlattr *tb[], int maxtype, struct nlattr *head,
1058c2ecf20Sopenharmony_ci		     int len, struct libbpf_nla_policy *policy)
1068c2ecf20Sopenharmony_ci{
1078c2ecf20Sopenharmony_ci	struct nlattr *nla;
1088c2ecf20Sopenharmony_ci	int rem, err;
1098c2ecf20Sopenharmony_ci
1108c2ecf20Sopenharmony_ci	memset(tb, 0, sizeof(struct nlattr *) * (maxtype + 1));
1118c2ecf20Sopenharmony_ci
1128c2ecf20Sopenharmony_ci	libbpf_nla_for_each_attr(nla, head, len, rem) {
1138c2ecf20Sopenharmony_ci		int type = nla_type(nla);
1148c2ecf20Sopenharmony_ci
1158c2ecf20Sopenharmony_ci		if (type > maxtype)
1168c2ecf20Sopenharmony_ci			continue;
1178c2ecf20Sopenharmony_ci
1188c2ecf20Sopenharmony_ci		if (policy) {
1198c2ecf20Sopenharmony_ci			err = validate_nla(nla, maxtype, policy);
1208c2ecf20Sopenharmony_ci			if (err < 0)
1218c2ecf20Sopenharmony_ci				goto errout;
1228c2ecf20Sopenharmony_ci		}
1238c2ecf20Sopenharmony_ci
1248c2ecf20Sopenharmony_ci		if (tb[type])
1258c2ecf20Sopenharmony_ci			pr_warn("Attribute of type %#x found multiple times in message, "
1268c2ecf20Sopenharmony_ci				"previous attribute is being ignored.\n", type);
1278c2ecf20Sopenharmony_ci
1288c2ecf20Sopenharmony_ci		tb[type] = nla;
1298c2ecf20Sopenharmony_ci	}
1308c2ecf20Sopenharmony_ci
1318c2ecf20Sopenharmony_ci	err = 0;
1328c2ecf20Sopenharmony_cierrout:
1338c2ecf20Sopenharmony_ci	return err;
1348c2ecf20Sopenharmony_ci}
1358c2ecf20Sopenharmony_ci
1368c2ecf20Sopenharmony_ci/**
1378c2ecf20Sopenharmony_ci * Create attribute index based on nested attribute
1388c2ecf20Sopenharmony_ci * @arg tb              Index array to be filled (maxtype+1 elements).
1398c2ecf20Sopenharmony_ci * @arg maxtype         Maximum attribute type expected and accepted.
1408c2ecf20Sopenharmony_ci * @arg nla             Nested Attribute.
1418c2ecf20Sopenharmony_ci * @arg policy          Attribute validation policy.
1428c2ecf20Sopenharmony_ci *
1438c2ecf20Sopenharmony_ci * Feeds the stream of attributes nested into the specified attribute
1448c2ecf20Sopenharmony_ci * to libbpf_nla_parse().
1458c2ecf20Sopenharmony_ci *
1468c2ecf20Sopenharmony_ci * @see libbpf_nla_parse
1478c2ecf20Sopenharmony_ci * @return 0 on success or a negative error code.
1488c2ecf20Sopenharmony_ci */
1498c2ecf20Sopenharmony_ciint libbpf_nla_parse_nested(struct nlattr *tb[], int maxtype,
1508c2ecf20Sopenharmony_ci			    struct nlattr *nla,
1518c2ecf20Sopenharmony_ci			    struct libbpf_nla_policy *policy)
1528c2ecf20Sopenharmony_ci{
1538c2ecf20Sopenharmony_ci	return libbpf_nla_parse(tb, maxtype, libbpf_nla_data(nla),
1548c2ecf20Sopenharmony_ci				libbpf_nla_len(nla), policy);
1558c2ecf20Sopenharmony_ci}
1568c2ecf20Sopenharmony_ci
1578c2ecf20Sopenharmony_ci/* dump netlink extended ack error message */
1588c2ecf20Sopenharmony_ciint libbpf_nla_dump_errormsg(struct nlmsghdr *nlh)
1598c2ecf20Sopenharmony_ci{
1608c2ecf20Sopenharmony_ci	struct libbpf_nla_policy extack_policy[NLMSGERR_ATTR_MAX + 1] = {
1618c2ecf20Sopenharmony_ci		[NLMSGERR_ATTR_MSG]	= { .type = LIBBPF_NLA_STRING },
1628c2ecf20Sopenharmony_ci		[NLMSGERR_ATTR_OFFS]	= { .type = LIBBPF_NLA_U32 },
1638c2ecf20Sopenharmony_ci	};
1648c2ecf20Sopenharmony_ci	struct nlattr *tb[NLMSGERR_ATTR_MAX + 1], *attr;
1658c2ecf20Sopenharmony_ci	struct nlmsgerr *err;
1668c2ecf20Sopenharmony_ci	char *errmsg = NULL;
1678c2ecf20Sopenharmony_ci	int hlen, alen;
1688c2ecf20Sopenharmony_ci
1698c2ecf20Sopenharmony_ci	/* no TLVs, nothing to do here */
1708c2ecf20Sopenharmony_ci	if (!(nlh->nlmsg_flags & NLM_F_ACK_TLVS))
1718c2ecf20Sopenharmony_ci		return 0;
1728c2ecf20Sopenharmony_ci
1738c2ecf20Sopenharmony_ci	err = (struct nlmsgerr *)NLMSG_DATA(nlh);
1748c2ecf20Sopenharmony_ci	hlen = sizeof(*err);
1758c2ecf20Sopenharmony_ci
1768c2ecf20Sopenharmony_ci	/* if NLM_F_CAPPED is set then the inner err msg was capped */
1778c2ecf20Sopenharmony_ci	if (!(nlh->nlmsg_flags & NLM_F_CAPPED))
1788c2ecf20Sopenharmony_ci		hlen += nlmsg_len(&err->msg);
1798c2ecf20Sopenharmony_ci
1808c2ecf20Sopenharmony_ci	attr = (struct nlattr *) ((void *) err + hlen);
1818c2ecf20Sopenharmony_ci	alen = (void *)nlh + nlh->nlmsg_len - (void *)attr;
1828c2ecf20Sopenharmony_ci
1838c2ecf20Sopenharmony_ci	if (libbpf_nla_parse(tb, NLMSGERR_ATTR_MAX, attr, alen,
1848c2ecf20Sopenharmony_ci			     extack_policy) != 0) {
1858c2ecf20Sopenharmony_ci		pr_warn("Failed to parse extended error attributes\n");
1868c2ecf20Sopenharmony_ci		return 0;
1878c2ecf20Sopenharmony_ci	}
1888c2ecf20Sopenharmony_ci
1898c2ecf20Sopenharmony_ci	if (tb[NLMSGERR_ATTR_MSG])
1908c2ecf20Sopenharmony_ci		errmsg = (char *) libbpf_nla_data(tb[NLMSGERR_ATTR_MSG]);
1918c2ecf20Sopenharmony_ci
1928c2ecf20Sopenharmony_ci	pr_warn("Kernel error message: %s\n", errmsg);
1938c2ecf20Sopenharmony_ci
1948c2ecf20Sopenharmony_ci	return 0;
1958c2ecf20Sopenharmony_ci}
196