xref: /aosp_15_r20/external/libbpf/src/nlattr.h (revision f7c14bbac8cf49633f2740db462ea43457973ec4)
1*f7c14bbaSAndroid Build Coastguard Worker /* SPDX-License-Identifier: (LGPL-2.1 OR BSD-2-Clause) */
2*f7c14bbaSAndroid Build Coastguard Worker 
3*f7c14bbaSAndroid Build Coastguard Worker /*
4*f7c14bbaSAndroid Build Coastguard Worker  * NETLINK      Netlink attributes
5*f7c14bbaSAndroid Build Coastguard Worker  *
6*f7c14bbaSAndroid Build Coastguard Worker  * Copyright (c) 2003-2013 Thomas Graf <[email protected]>
7*f7c14bbaSAndroid Build Coastguard Worker  */
8*f7c14bbaSAndroid Build Coastguard Worker 
9*f7c14bbaSAndroid Build Coastguard Worker #ifndef __LIBBPF_NLATTR_H
10*f7c14bbaSAndroid Build Coastguard Worker #define __LIBBPF_NLATTR_H
11*f7c14bbaSAndroid Build Coastguard Worker 
12*f7c14bbaSAndroid Build Coastguard Worker #include <stdint.h>
13*f7c14bbaSAndroid Build Coastguard Worker #include <string.h>
14*f7c14bbaSAndroid Build Coastguard Worker #include <errno.h>
15*f7c14bbaSAndroid Build Coastguard Worker #include <linux/netlink.h>
16*f7c14bbaSAndroid Build Coastguard Worker #include <linux/rtnetlink.h>
17*f7c14bbaSAndroid Build Coastguard Worker #include <linux/genetlink.h>
18*f7c14bbaSAndroid Build Coastguard Worker 
19*f7c14bbaSAndroid Build Coastguard Worker /* avoid multiple definition of netlink features */
20*f7c14bbaSAndroid Build Coastguard Worker #define __LINUX_NETLINK_H
21*f7c14bbaSAndroid Build Coastguard Worker 
22*f7c14bbaSAndroid Build Coastguard Worker /**
23*f7c14bbaSAndroid Build Coastguard Worker  * Standard attribute types to specify validation policy
24*f7c14bbaSAndroid Build Coastguard Worker  */
25*f7c14bbaSAndroid Build Coastguard Worker enum {
26*f7c14bbaSAndroid Build Coastguard Worker 	LIBBPF_NLA_UNSPEC,	/**< Unspecified type, binary data chunk */
27*f7c14bbaSAndroid Build Coastguard Worker 	LIBBPF_NLA_U8,		/**< 8 bit integer */
28*f7c14bbaSAndroid Build Coastguard Worker 	LIBBPF_NLA_U16,		/**< 16 bit integer */
29*f7c14bbaSAndroid Build Coastguard Worker 	LIBBPF_NLA_U32,		/**< 32 bit integer */
30*f7c14bbaSAndroid Build Coastguard Worker 	LIBBPF_NLA_U64,		/**< 64 bit integer */
31*f7c14bbaSAndroid Build Coastguard Worker 	LIBBPF_NLA_STRING,	/**< NUL terminated character string */
32*f7c14bbaSAndroid Build Coastguard Worker 	LIBBPF_NLA_FLAG,	/**< Flag */
33*f7c14bbaSAndroid Build Coastguard Worker 	LIBBPF_NLA_MSECS,	/**< Micro seconds (64bit) */
34*f7c14bbaSAndroid Build Coastguard Worker 	LIBBPF_NLA_NESTED,	/**< Nested attributes */
35*f7c14bbaSAndroid Build Coastguard Worker 	__LIBBPF_NLA_TYPE_MAX,
36*f7c14bbaSAndroid Build Coastguard Worker };
37*f7c14bbaSAndroid Build Coastguard Worker 
38*f7c14bbaSAndroid Build Coastguard Worker #define LIBBPF_NLA_TYPE_MAX (__LIBBPF_NLA_TYPE_MAX - 1)
39*f7c14bbaSAndroid Build Coastguard Worker 
40*f7c14bbaSAndroid Build Coastguard Worker /**
41*f7c14bbaSAndroid Build Coastguard Worker  * @ingroup attr
42*f7c14bbaSAndroid Build Coastguard Worker  * Attribute validation policy.
43*f7c14bbaSAndroid Build Coastguard Worker  *
44*f7c14bbaSAndroid Build Coastguard Worker  * See section @core_doc{core_attr_parse,Attribute Parsing} for more details.
45*f7c14bbaSAndroid Build Coastguard Worker  */
46*f7c14bbaSAndroid Build Coastguard Worker struct libbpf_nla_policy {
47*f7c14bbaSAndroid Build Coastguard Worker 	/** Type of attribute or LIBBPF_NLA_UNSPEC */
48*f7c14bbaSAndroid Build Coastguard Worker 	uint16_t	type;
49*f7c14bbaSAndroid Build Coastguard Worker 
50*f7c14bbaSAndroid Build Coastguard Worker 	/** Minimal length of payload required */
51*f7c14bbaSAndroid Build Coastguard Worker 	uint16_t	minlen;
52*f7c14bbaSAndroid Build Coastguard Worker 
53*f7c14bbaSAndroid Build Coastguard Worker 	/** Maximal length of payload allowed */
54*f7c14bbaSAndroid Build Coastguard Worker 	uint16_t	maxlen;
55*f7c14bbaSAndroid Build Coastguard Worker };
56*f7c14bbaSAndroid Build Coastguard Worker 
57*f7c14bbaSAndroid Build Coastguard Worker struct libbpf_nla_req {
58*f7c14bbaSAndroid Build Coastguard Worker 	struct nlmsghdr nh;
59*f7c14bbaSAndroid Build Coastguard Worker 	union {
60*f7c14bbaSAndroid Build Coastguard Worker 		struct ifinfomsg ifinfo;
61*f7c14bbaSAndroid Build Coastguard Worker 		struct tcmsg tc;
62*f7c14bbaSAndroid Build Coastguard Worker 		struct genlmsghdr gnl;
63*f7c14bbaSAndroid Build Coastguard Worker 	};
64*f7c14bbaSAndroid Build Coastguard Worker 	char buf[128];
65*f7c14bbaSAndroid Build Coastguard Worker };
66*f7c14bbaSAndroid Build Coastguard Worker 
67*f7c14bbaSAndroid Build Coastguard Worker /**
68*f7c14bbaSAndroid Build Coastguard Worker  * @ingroup attr
69*f7c14bbaSAndroid Build Coastguard Worker  * Iterate over a stream of attributes
70*f7c14bbaSAndroid Build Coastguard Worker  * @arg pos	loop counter, set to current attribute
71*f7c14bbaSAndroid Build Coastguard Worker  * @arg head	head of attribute stream
72*f7c14bbaSAndroid Build Coastguard Worker  * @arg len	length of attribute stream
73*f7c14bbaSAndroid Build Coastguard Worker  * @arg rem	initialized to len, holds bytes currently remaining in stream
74*f7c14bbaSAndroid Build Coastguard Worker  */
75*f7c14bbaSAndroid Build Coastguard Worker #define libbpf_nla_for_each_attr(pos, head, len, rem) \
76*f7c14bbaSAndroid Build Coastguard Worker 	for (pos = head, rem = len; \
77*f7c14bbaSAndroid Build Coastguard Worker 	     nla_ok(pos, rem); \
78*f7c14bbaSAndroid Build Coastguard Worker 	     pos = nla_next(pos, &(rem)))
79*f7c14bbaSAndroid Build Coastguard Worker 
80*f7c14bbaSAndroid Build Coastguard Worker /**
81*f7c14bbaSAndroid Build Coastguard Worker  * libbpf_nla_data - head of payload
82*f7c14bbaSAndroid Build Coastguard Worker  * @nla: netlink attribute
83*f7c14bbaSAndroid Build Coastguard Worker  */
libbpf_nla_data(const struct nlattr * nla)84*f7c14bbaSAndroid Build Coastguard Worker static inline void *libbpf_nla_data(const struct nlattr *nla)
85*f7c14bbaSAndroid Build Coastguard Worker {
86*f7c14bbaSAndroid Build Coastguard Worker 	return (void *)nla + NLA_HDRLEN;
87*f7c14bbaSAndroid Build Coastguard Worker }
88*f7c14bbaSAndroid Build Coastguard Worker 
libbpf_nla_getattr_u8(const struct nlattr * nla)89*f7c14bbaSAndroid Build Coastguard Worker static inline uint8_t libbpf_nla_getattr_u8(const struct nlattr *nla)
90*f7c14bbaSAndroid Build Coastguard Worker {
91*f7c14bbaSAndroid Build Coastguard Worker 	return *(uint8_t *)libbpf_nla_data(nla);
92*f7c14bbaSAndroid Build Coastguard Worker }
93*f7c14bbaSAndroid Build Coastguard Worker 
libbpf_nla_getattr_u16(const struct nlattr * nla)94*f7c14bbaSAndroid Build Coastguard Worker static inline uint16_t libbpf_nla_getattr_u16(const struct nlattr *nla)
95*f7c14bbaSAndroid Build Coastguard Worker {
96*f7c14bbaSAndroid Build Coastguard Worker 	return *(uint16_t *)libbpf_nla_data(nla);
97*f7c14bbaSAndroid Build Coastguard Worker }
98*f7c14bbaSAndroid Build Coastguard Worker 
libbpf_nla_getattr_u32(const struct nlattr * nla)99*f7c14bbaSAndroid Build Coastguard Worker static inline uint32_t libbpf_nla_getattr_u32(const struct nlattr *nla)
100*f7c14bbaSAndroid Build Coastguard Worker {
101*f7c14bbaSAndroid Build Coastguard Worker 	return *(uint32_t *)libbpf_nla_data(nla);
102*f7c14bbaSAndroid Build Coastguard Worker }
103*f7c14bbaSAndroid Build Coastguard Worker 
libbpf_nla_getattr_u64(const struct nlattr * nla)104*f7c14bbaSAndroid Build Coastguard Worker static inline uint64_t libbpf_nla_getattr_u64(const struct nlattr *nla)
105*f7c14bbaSAndroid Build Coastguard Worker {
106*f7c14bbaSAndroid Build Coastguard Worker 	return *(uint64_t *)libbpf_nla_data(nla);
107*f7c14bbaSAndroid Build Coastguard Worker }
108*f7c14bbaSAndroid Build Coastguard Worker 
libbpf_nla_getattr_str(const struct nlattr * nla)109*f7c14bbaSAndroid Build Coastguard Worker static inline const char *libbpf_nla_getattr_str(const struct nlattr *nla)
110*f7c14bbaSAndroid Build Coastguard Worker {
111*f7c14bbaSAndroid Build Coastguard Worker 	return (const char *)libbpf_nla_data(nla);
112*f7c14bbaSAndroid Build Coastguard Worker }
113*f7c14bbaSAndroid Build Coastguard Worker 
114*f7c14bbaSAndroid Build Coastguard Worker /**
115*f7c14bbaSAndroid Build Coastguard Worker  * libbpf_nla_len - length of payload
116*f7c14bbaSAndroid Build Coastguard Worker  * @nla: netlink attribute
117*f7c14bbaSAndroid Build Coastguard Worker  */
libbpf_nla_len(const struct nlattr * nla)118*f7c14bbaSAndroid Build Coastguard Worker static inline int libbpf_nla_len(const struct nlattr *nla)
119*f7c14bbaSAndroid Build Coastguard Worker {
120*f7c14bbaSAndroid Build Coastguard Worker 	return nla->nla_len - NLA_HDRLEN;
121*f7c14bbaSAndroid Build Coastguard Worker }
122*f7c14bbaSAndroid Build Coastguard Worker 
123*f7c14bbaSAndroid Build Coastguard Worker int libbpf_nla_parse(struct nlattr *tb[], int maxtype, struct nlattr *head,
124*f7c14bbaSAndroid Build Coastguard Worker 		     int len, struct libbpf_nla_policy *policy);
125*f7c14bbaSAndroid Build Coastguard Worker int libbpf_nla_parse_nested(struct nlattr *tb[], int maxtype,
126*f7c14bbaSAndroid Build Coastguard Worker 			    struct nlattr *nla,
127*f7c14bbaSAndroid Build Coastguard Worker 			    struct libbpf_nla_policy *policy);
128*f7c14bbaSAndroid Build Coastguard Worker 
129*f7c14bbaSAndroid Build Coastguard Worker int libbpf_nla_dump_errormsg(struct nlmsghdr *nlh);
130*f7c14bbaSAndroid Build Coastguard Worker 
nla_data(struct nlattr * nla)131*f7c14bbaSAndroid Build Coastguard Worker static inline struct nlattr *nla_data(struct nlattr *nla)
132*f7c14bbaSAndroid Build Coastguard Worker {
133*f7c14bbaSAndroid Build Coastguard Worker 	return (struct nlattr *)((void *)nla + NLA_HDRLEN);
134*f7c14bbaSAndroid Build Coastguard Worker }
135*f7c14bbaSAndroid Build Coastguard Worker 
req_tail(struct libbpf_nla_req * req)136*f7c14bbaSAndroid Build Coastguard Worker static inline struct nlattr *req_tail(struct libbpf_nla_req *req)
137*f7c14bbaSAndroid Build Coastguard Worker {
138*f7c14bbaSAndroid Build Coastguard Worker 	return (struct nlattr *)((void *)req + NLMSG_ALIGN(req->nh.nlmsg_len));
139*f7c14bbaSAndroid Build Coastguard Worker }
140*f7c14bbaSAndroid Build Coastguard Worker 
nlattr_add(struct libbpf_nla_req * req,int type,const void * data,int len)141*f7c14bbaSAndroid Build Coastguard Worker static inline int nlattr_add(struct libbpf_nla_req *req, int type,
142*f7c14bbaSAndroid Build Coastguard Worker 			     const void *data, int len)
143*f7c14bbaSAndroid Build Coastguard Worker {
144*f7c14bbaSAndroid Build Coastguard Worker 	struct nlattr *nla;
145*f7c14bbaSAndroid Build Coastguard Worker 
146*f7c14bbaSAndroid Build Coastguard Worker 	if (NLMSG_ALIGN(req->nh.nlmsg_len) + NLA_ALIGN(NLA_HDRLEN + len) > sizeof(*req))
147*f7c14bbaSAndroid Build Coastguard Worker 		return -EMSGSIZE;
148*f7c14bbaSAndroid Build Coastguard Worker 	if (!!data != !!len)
149*f7c14bbaSAndroid Build Coastguard Worker 		return -EINVAL;
150*f7c14bbaSAndroid Build Coastguard Worker 
151*f7c14bbaSAndroid Build Coastguard Worker 	nla = req_tail(req);
152*f7c14bbaSAndroid Build Coastguard Worker 	nla->nla_type = type;
153*f7c14bbaSAndroid Build Coastguard Worker 	nla->nla_len = NLA_HDRLEN + len;
154*f7c14bbaSAndroid Build Coastguard Worker 	if (data)
155*f7c14bbaSAndroid Build Coastguard Worker 		memcpy(nla_data(nla), data, len);
156*f7c14bbaSAndroid Build Coastguard Worker 	req->nh.nlmsg_len = NLMSG_ALIGN(req->nh.nlmsg_len) + NLA_ALIGN(nla->nla_len);
157*f7c14bbaSAndroid Build Coastguard Worker 	return 0;
158*f7c14bbaSAndroid Build Coastguard Worker }
159*f7c14bbaSAndroid Build Coastguard Worker 
nlattr_begin_nested(struct libbpf_nla_req * req,int type)160*f7c14bbaSAndroid Build Coastguard Worker static inline struct nlattr *nlattr_begin_nested(struct libbpf_nla_req *req, int type)
161*f7c14bbaSAndroid Build Coastguard Worker {
162*f7c14bbaSAndroid Build Coastguard Worker 	struct nlattr *tail;
163*f7c14bbaSAndroid Build Coastguard Worker 
164*f7c14bbaSAndroid Build Coastguard Worker 	tail = req_tail(req);
165*f7c14bbaSAndroid Build Coastguard Worker 	if (nlattr_add(req, type | NLA_F_NESTED, NULL, 0))
166*f7c14bbaSAndroid Build Coastguard Worker 		return NULL;
167*f7c14bbaSAndroid Build Coastguard Worker 	return tail;
168*f7c14bbaSAndroid Build Coastguard Worker }
169*f7c14bbaSAndroid Build Coastguard Worker 
nlattr_end_nested(struct libbpf_nla_req * req,struct nlattr * tail)170*f7c14bbaSAndroid Build Coastguard Worker static inline void nlattr_end_nested(struct libbpf_nla_req *req,
171*f7c14bbaSAndroid Build Coastguard Worker 				     struct nlattr *tail)
172*f7c14bbaSAndroid Build Coastguard Worker {
173*f7c14bbaSAndroid Build Coastguard Worker 	tail->nla_len = (void *)req_tail(req) - (void *)tail;
174*f7c14bbaSAndroid Build Coastguard Worker }
175*f7c14bbaSAndroid Build Coastguard Worker 
176*f7c14bbaSAndroid Build Coastguard Worker #endif /* __LIBBPF_NLATTR_H */
177