Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

3 Commits   0 Branches   0 Tags
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   1) // SPDX-License-Identifier: (LGPL-2.1 OR BSD-2-Clause)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   2) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   3) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   4)  * NETLINK      Netlink attributes
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   5)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   6)  * Copyright (c) 2003-2013 Thomas Graf <tgraf@suug.ch>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   7)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   8) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   9) #include <errno.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  10) #include <string.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  11) #include <stdio.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  12) #include <linux/rtnetlink.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  13) #include "nlattr.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  14) #include "libbpf_internal.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  15) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  16) static uint16_t nla_attr_minlen[LIBBPF_NLA_TYPE_MAX+1] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  17) 	[LIBBPF_NLA_U8]		= sizeof(uint8_t),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  18) 	[LIBBPF_NLA_U16]	= sizeof(uint16_t),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  19) 	[LIBBPF_NLA_U32]	= sizeof(uint32_t),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  20) 	[LIBBPF_NLA_U64]	= sizeof(uint64_t),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  21) 	[LIBBPF_NLA_STRING]	= 1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  22) 	[LIBBPF_NLA_FLAG]	= 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  23) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  24) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  25) static struct nlattr *nla_next(const struct nlattr *nla, int *remaining)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  26) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  27) 	int totlen = NLA_ALIGN(nla->nla_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  28) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  29) 	*remaining -= totlen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  30) 	return (struct nlattr *) ((char *) nla + totlen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  31) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  32) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  33) static int nla_ok(const struct nlattr *nla, int remaining)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  34) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  35) 	return remaining >= sizeof(*nla) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  36) 	       nla->nla_len >= sizeof(*nla) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  37) 	       nla->nla_len <= remaining;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  38) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  39) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  40) static int nla_type(const struct nlattr *nla)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  41) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  42) 	return nla->nla_type & NLA_TYPE_MASK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  43) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  44) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  45) static int validate_nla(struct nlattr *nla, int maxtype,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  46) 			struct libbpf_nla_policy *policy)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  47) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  48) 	struct libbpf_nla_policy *pt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  49) 	unsigned int minlen = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  50) 	int type = nla_type(nla);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  51) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  52) 	if (type < 0 || type > maxtype)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  53) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  54) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  55) 	pt = &policy[type];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  56) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  57) 	if (pt->type > LIBBPF_NLA_TYPE_MAX)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  58) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  59) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  60) 	if (pt->minlen)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  61) 		minlen = pt->minlen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  62) 	else if (pt->type != LIBBPF_NLA_UNSPEC)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  63) 		minlen = nla_attr_minlen[pt->type];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  64) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  65) 	if (libbpf_nla_len(nla) < minlen)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  66) 		return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  67) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  68) 	if (pt->maxlen && libbpf_nla_len(nla) > pt->maxlen)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  69) 		return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  70) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  71) 	if (pt->type == LIBBPF_NLA_STRING) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  72) 		char *data = libbpf_nla_data(nla);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  73) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  74) 		if (data[libbpf_nla_len(nla) - 1] != '\0')
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  75) 			return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  76) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  77) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  78) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  79) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  80) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  81) static inline int nlmsg_len(const struct nlmsghdr *nlh)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  82) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  83) 	return nlh->nlmsg_len - NLMSG_HDRLEN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  84) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  85) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  86) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  87)  * Create attribute index based on a stream of attributes.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  88)  * @arg tb		Index array to be filled (maxtype+1 elements).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  89)  * @arg maxtype		Maximum attribute type expected and accepted.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  90)  * @arg head		Head of attribute stream.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  91)  * @arg len		Length of attribute stream.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  92)  * @arg policy		Attribute validation policy.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  93)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  94)  * Iterates over the stream of attributes and stores a pointer to each
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  95)  * attribute in the index array using the attribute type as index to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  96)  * the array. Attribute with a type greater than the maximum type
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  97)  * specified will be silently ignored in order to maintain backwards
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  98)  * compatibility. If \a policy is not NULL, the attribute will be
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  99)  * validated using the specified policy.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101)  * @see nla_validate
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102)  * @return 0 on success or a negative error code.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) int libbpf_nla_parse(struct nlattr *tb[], int maxtype, struct nlattr *head,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) 		     int len, struct libbpf_nla_policy *policy)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) 	struct nlattr *nla;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) 	int rem, err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) 	memset(tb, 0, sizeof(struct nlattr *) * (maxtype + 1));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) 	libbpf_nla_for_each_attr(nla, head, len, rem) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) 		int type = nla_type(nla);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) 		if (type > maxtype)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) 		if (policy) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) 			err = validate_nla(nla, maxtype, policy);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) 			if (err < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) 				goto errout;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) 		if (tb[type])
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) 			pr_warn("Attribute of type %#x found multiple times in message, "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) 				"previous attribute is being ignored.\n", type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) 		tb[type] = nla;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) 	err = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) errout:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) 	return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137)  * Create attribute index based on nested attribute
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138)  * @arg tb              Index array to be filled (maxtype+1 elements).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139)  * @arg maxtype         Maximum attribute type expected and accepted.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140)  * @arg nla             Nested Attribute.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141)  * @arg policy          Attribute validation policy.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143)  * Feeds the stream of attributes nested into the specified attribute
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144)  * to libbpf_nla_parse().
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146)  * @see libbpf_nla_parse
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147)  * @return 0 on success or a negative error code.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) int libbpf_nla_parse_nested(struct nlattr *tb[], int maxtype,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) 			    struct nlattr *nla,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) 			    struct libbpf_nla_policy *policy)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) 	return libbpf_nla_parse(tb, maxtype, libbpf_nla_data(nla),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) 				libbpf_nla_len(nla), policy);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) /* dump netlink extended ack error message */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) int libbpf_nla_dump_errormsg(struct nlmsghdr *nlh)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) 	struct libbpf_nla_policy extack_policy[NLMSGERR_ATTR_MAX + 1] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) 		[NLMSGERR_ATTR_MSG]	= { .type = LIBBPF_NLA_STRING },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) 		[NLMSGERR_ATTR_OFFS]	= { .type = LIBBPF_NLA_U32 },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) 	};
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) 	struct nlattr *tb[NLMSGERR_ATTR_MAX + 1], *attr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) 	struct nlmsgerr *err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) 	char *errmsg = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) 	int hlen, alen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) 	/* no TLVs, nothing to do here */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) 	if (!(nlh->nlmsg_flags & NLM_F_ACK_TLVS))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) 	err = (struct nlmsgerr *)NLMSG_DATA(nlh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) 	hlen = sizeof(*err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) 	/* if NLM_F_CAPPED is set then the inner err msg was capped */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) 	if (!(nlh->nlmsg_flags & NLM_F_CAPPED))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) 		hlen += nlmsg_len(&err->msg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) 	attr = (struct nlattr *) ((void *) err + hlen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) 	alen = nlh->nlmsg_len - hlen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) 	if (libbpf_nla_parse(tb, NLMSGERR_ATTR_MAX, attr, alen,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) 			     extack_policy) != 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) 		pr_warn("Failed to parse extended error attributes\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) 	if (tb[NLMSGERR_ATTR_MSG])
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) 		errmsg = (char *) libbpf_nla_data(tb[NLMSGERR_ATTR_MSG]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) 	pr_warn("Kernel error message: %s\n", errmsg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) }