libnl: /data/src/libnl/include/netlink/attr.h Source File

libnl 3.12.0

attr.h
1/* SPDX-License-Identifier: LGPL-2.1-only */
2/*
3 * Copyright (c) 2003-2013 Thomas Graf <tgraf@suug.ch>
4 */
5
6#ifndef NETLINK_ATTR_H_
7#define NETLINK_ATTR_H_
8
9#include <netlink/netlink.h>
10#include <netlink/object.h>
11#include <netlink/addr.h>
12#include <netlink/data.h>
13
14#ifdef __cplusplus
15extern "C" {
16#endif
17
18struct nlattr;
19
20struct nl_msg;
21
22/**
23 * @name Basic Attribute Data Types
24 * @{
25 */
26
27/**
28 * @ingroup attr
29 * Basic attribute data types
30 *
31 * See section @core_doc{core_attr_parse,Attribute Parsing} for more details.
32 */
33enum {
34 NLA_UNSPEC, /**< Unspecified type, binary data chunk */
35 NLA_U8, /**< 8 bit integer */
36 NLA_U16, /**< 16 bit integer */
37 NLA_U32, /**< 32 bit integer */
38 NLA_U64, /**< 64 bit integer */
39 NLA_STRING, /**< NUL terminated character string */
40 NLA_FLAG, /**< Flag */
41 NLA_MSECS, /**< Micro seconds (64bit) */
42 NLA_NESTED, /**< Nested attributes */
43 NLA_NESTED_COMPAT,
44 NLA_NUL_STRING,
45 NLA_BINARY,
46 NLA_S8,
47 NLA_S16,
48 NLA_S32,
49 NLA_S64,
50 /* Skip unimplemented attr types */
51 NLA_SINT = 20,
52 NLA_UINT,
53 __NLA_TYPE_MAX,
54};
55
56#define NLA_TYPE_MAX (__NLA_TYPE_MAX - 1)
57
58/** @} */
59
60/**
61 * @ingroup attr
62 * Attribute validation policy.
63 *
64 * See section @core_doc{core_attr_parse,Attribute Parsing} for more details.
65 */
66 struct nla_policy {
67 /** Type of attribute or NLA_UNSPEC */
68 uint16_t type;
69
70 /** Minimal length of payload required */
71 uint16_t minlen;
72
73 /** Maximal length of payload allowed */
74 uint16_t maxlen;
75};
76
77/* Size calculations */
78extern int nla_attr_size(int payload);
79extern int nla_total_size(int payload);
80extern int nla_padlen(int payload);
81
82/* Attribute parsing */
83extern int nla_type(const struct nlattr *);
84extern void * nla_data(const struct nlattr *);
85extern int nla_len(const struct nlattr *);
86extern int nla_ok(const struct nlattr *, int);
87extern struct nlattr * nla_next(const struct nlattr *, int *);
88extern int nla_parse(struct nlattr **, int, struct nlattr *,
89 int, const struct nla_policy *);
90extern int nla_validate(const struct nlattr *, int, int,
91 const struct nla_policy *);
92extern struct nlattr * nla_find(const struct nlattr *, int, int);
93
94/* Helper Functions */
95extern int nla_memcpy(void *, const struct nlattr *, int);
96extern size_t nla_strlcpy(char *, const struct nlattr *, size_t);
97extern int nla_memcmp(const struct nlattr *, const void *, size_t);
98extern int nla_strcmp(const struct nlattr *, const char *);
99
100/* Unspecific attribute */
101extern struct nlattr * nla_reserve(struct nl_msg *, int, int);
102extern int nla_put(struct nl_msg *, int, int, const void *);
103extern int nla_put_data(struct nl_msg *, int,
104 const struct nl_data *);
105extern int nla_put_addr(struct nl_msg *, int, struct nl_addr *);
106
107/* Integer attribute */
108extern int8_t nla_get_s8(const struct nlattr *);
109extern int nla_put_s8(struct nl_msg *, int, int8_t);
110extern uint8_t nla_get_u8(const struct nlattr *);
111extern int nla_put_u8(struct nl_msg *, int, uint8_t);
112extern int16_t nla_get_s16(const struct nlattr *);
113extern int nla_put_s16(struct nl_msg *, int, int16_t);
114extern uint16_t nla_get_u16(const struct nlattr *);
115extern int nla_put_u16(struct nl_msg *, int, uint16_t);
116extern int32_t nla_get_s32(const struct nlattr *);
117extern int nla_put_s32(struct nl_msg *, int, int32_t);
118extern uint32_t nla_get_u32(const struct nlattr *);
119extern int nla_put_u32(struct nl_msg *, int, uint32_t);
120extern int64_t nla_get_s64(const struct nlattr *);
121extern int nla_put_s64(struct nl_msg *, int, int64_t);
122extern uint64_t nla_get_u64(const struct nlattr *);
123extern int nla_put_u64(struct nl_msg *, int, uint64_t);
124extern int64_t nla_get_sint(const struct nlattr *);
125extern int nla_put_sint(struct nl_msg *, int, int64_t);
126extern uint64_t nla_get_uint(const struct nlattr *);
127extern int nla_put_uint(struct nl_msg *, int, uint64_t);
128
129/* String attribute */
130extern char * nla_get_string(const struct nlattr *);
131extern char * nla_strdup(const struct nlattr *);
132extern int nla_put_string(struct nl_msg *, int, const char *);
133
134/* Flag attribute */
135extern int nla_get_flag(const struct nlattr *);
136extern int nla_put_flag(struct nl_msg *, int);
137
138/* Msec attribute */
139extern unsigned long nla_get_msecs(const struct nlattr *);
140extern int nla_put_msecs(struct nl_msg *, int, unsigned long);
141
142/* Attribute nesting */
143extern int nla_put_nested(struct nl_msg *, int,
144 const struct nl_msg *);
145extern struct nlattr * nla_nest_start(struct nl_msg *, int);
146extern int nla_nest_end(struct nl_msg *, struct nlattr *);
147extern int nla_nest_end_keep_empty(struct nl_msg *, struct nlattr *);
148extern void nla_nest_cancel(struct nl_msg *, const struct nlattr *);
149extern int nla_parse_nested(struct nlattr **, int, struct nlattr *,
150 const struct nla_policy *);
151extern int nla_is_nested(const struct nlattr *);
152
153/**
154 * @name Attribute Construction (Exception Based)
155 * @{
156 */
157
158/**
159 * @ingroup attr
160 * Add unspecific attribute to netlink message.
161 * @arg msg Netlink message.
162 * @arg attrtype Attribute type.
163 * @arg attrlen Length of attribute payload.
164 * @arg data Head of attribute payload.
165 */
166#define NLA_PUT(msg, attrtype, attrlen, data) \
167 do { \
168 if (nla_put(msg, attrtype, attrlen, data) < 0) \
169 goto nla_put_failure; \
170 } while(0)
171
172/**
173 * @ingroup attr
174 * Add atomic type attribute to netlink message.
175 * @arg msg Netlink message.
176 * @arg type Atomic type.
177 * @arg attrtype Attribute type.
178 * @arg value Head of attribute payload.
179 */
180#define NLA_PUT_TYPE(msg, type, attrtype, value) \
181 do { \
182 type __tmp = value; \
183 NLA_PUT(msg, attrtype, sizeof(type), &__tmp); \
184 } while(0)
185
186/**
187 * Add 8 bit signed integer attribute to netlink message.
188 * @arg msg Netlink message.
189 * @arg attrtype Attribute type.
190 * @arg value Numeric value.
191 */
192#define NLA_PUT_S8(msg, attrtype, value) \
193 NLA_PUT_TYPE(msg, int8_t, attrtype, value)
194
195/**
196 * Add 8 bit integer attribute to netlink message.
197 * @arg msg Netlink message.
198 * @arg attrtype Attribute type.
199 * @arg value Numeric value.
200 */
201#define NLA_PUT_U8(msg, attrtype, value) \
202 NLA_PUT_TYPE(msg, uint8_t, attrtype, value)
203
204/**
205 * Add 16 bit signed integer attribute to netlink message.
206 * @arg msg Netlink message.
207 * @arg attrtype Attribute type.
208 * @arg value Numeric value.
209 */
210#define NLA_PUT_S16(msg, attrtype, value) \
211 NLA_PUT_TYPE(msg, int16_t, attrtype, value)
212
213/**
214 * Add 16 bit integer attribute to netlink message.
215 * @arg msg Netlink message.
216 * @arg attrtype Attribute type.
217 * @arg value Numeric value.
218 */
219#define NLA_PUT_U16(msg, attrtype, value) \
220 NLA_PUT_TYPE(msg, uint16_t, attrtype, value)
221
222/**
223 * Add 32 bit signed integer attribute to netlink message.
224 * @arg msg Netlink message.
225 * @arg attrtype Attribute type.
226 * @arg value Numeric value.
227 */
228#define NLA_PUT_S32(msg, attrtype, value) \
229 NLA_PUT_TYPE(msg, int32_t, attrtype, value)
230
231/**
232 * Add 32 bit integer attribute to netlink message.
233 * @arg msg Netlink message.
234 * @arg attrtype Attribute type.
235 * @arg value Numeric value.
236 */
237#define NLA_PUT_U32(msg, attrtype, value) \
238 NLA_PUT_TYPE(msg, uint32_t, attrtype, value)
239
240/**
241 * Add 64 bit signed integer attribute to netlink message.
242 * @arg msg Netlink message.
243 * @arg attrtype Attribute type.
244 * @arg value Numeric value.
245 */
246#define NLA_PUT_S64(msg, attrtype, value) \
247 NLA_PUT_TYPE(msg, int64_t, attrtype, value)
248
249/**
250 * Add 64 bit integer attribute to netlink message.
251 * @arg msg Netlink message.
252 * @arg attrtype Attribute type.
253 * @arg value Numeric value.
254 */
255#define NLA_PUT_U64(msg, attrtype, value) \
256 NLA_PUT_TYPE(msg, uint64_t, attrtype, value)
257
258/**
259 * Add string attribute to netlink message.
260 * @arg msg Netlink message.
261 * @arg attrtype Attribute type.
262 * @arg value NUL terminated character string.
263 */
264#define NLA_PUT_STRING(msg, attrtype, value) \
265 NLA_PUT(msg, attrtype, (int) strlen(value) + 1, value)
266
267/**
268 * Add flag attribute to netlink message.
269 * @arg msg Netlink message.
270 * @arg attrtype Attribute type.
271 */
272#define NLA_PUT_FLAG(msg, attrtype) \
273 NLA_PUT(msg, attrtype, 0, NULL)
274
275/**
276 * Add msecs attribute to netlink message.
277 * @arg msg Netlink message.
278 * @arg attrtype Attribute type.
279 * @arg msecs Numeric value in micro seconds.
280 */
281#define NLA_PUT_MSECS(msg, attrtype, msecs) \
282 NLA_PUT_U64(msg, attrtype, msecs)
283
284/**
285 * Add address attribute to netlink message.
286 * @arg msg Netlink message.
287 * @arg attrtype Attribute type.
288 * @arg addr Abstract address object.
289 */
290#define NLA_PUT_ADDR(msg, attrtype, addr) \
291 NLA_PUT(msg, attrtype, nl_addr_get_len(addr), \
292 nl_addr_get_binary_addr(addr))
293
294/**
295 * Add abstract data attribute to netlink message.
296 * @arg msg Netlink message.
297 * @arg attrtype Attribute type.
298 * @arg data Abstract data object.
299 */
300#define NLA_PUT_DATA(msg, attrtype, data) \
301 NLA_PUT(msg, attrtype, nl_data_get_size(data), \
302 nl_data_get(data))
303
304/** @} */
305
306/**
307 * @name Iterators
308 * @{
309 */
310
311/**
312 * @ingroup attr
313 * Iterate over a stream of attributes
314 * @arg pos loop counter, set to current attribute
315 * @arg head head of attribute stream
316 * @arg len length of attribute stream
317 * @arg rem initialized to len, holds bytes currently remaining in stream
318 */
319#define nla_for_each_attr(pos, head, len, rem) \
320 for (pos = head, rem = len; \
321 nla_ok(pos, rem); \
322 pos = nla_next(pos, &(rem)))
323
324/**
325 * @ingroup attr
326 * Iterate over a stream of nested attributes
327 * @arg pos loop counter, set to current attribute
328 * @arg nla attribute containing the nested attributes
329 * @arg rem initialized to len, holds bytes currently remaining in stream
330 */
331#define nla_for_each_nested(pos, nla, rem) \
332 for (pos = (struct nlattr *) nla_data(nla), rem = nla_len(nla); \
333 nla_ok(pos, rem); \
334 pos = nla_next(pos, &(rem)))
335
336/** @} */
337
338#ifdef __cplusplus
339}
340#endif
341
342#endif
nla_validate
int nla_validate(const struct nlattr *, int, int, const struct nla_policy *)
Validate a stream of attributes.
Definition attr.c:296
nla_put_s16
int nla_put_s16(struct nl_msg *, int, int16_t)
Add 16 bit signed integer attribute to netlink message.
Definition attr.c:628
nla_get_u32
uint32_t nla_get_u32(const struct nlattr *)
Return payload of 32 bit integer attribute.
Definition attr.c:714
nla_put_u16
int nla_put_u16(struct nl_msg *, int, uint16_t)
Add 16 bit integer attribute to netlink message.
Definition attr.c:653
nla_get_u16
uint16_t nla_get_u16(const struct nlattr *)
Return payload of 16 bit integer attribute.
Definition attr.c:664
nla_strcmp
int nla_strcmp(const struct nlattr *, const char *)
Compare string attribute payload with string.
Definition attr.c:428
nla_put_data
int nla_put_data(struct nl_msg *, int, const struct nl_data *)
Add abstract data as unspecific attribute to netlink message.
Definition attr.c:539
nla_put_nested
int nla_put_nested(struct nl_msg *, int, const struct nl_msg *)
Add nested attributes to netlink message.
Definition attr.c:956
nla_next
struct nlattr * nla_next(const struct nlattr *, int *)
Return next attribute in a stream of attributes.
Definition attr.c:172
nla_parse
int nla_parse(struct nlattr *tb[], int maxtype, struct nlattr *head, int len, const struct nla_policy *policy)
Create attribute index based on a stream of attributes.
Definition attr.c:245
nla_put_string
int nla_put_string(struct nl_msg *, int, const char *)
Add string attribute to netlink message.
Definition attr.c:858
nla_get_u64
uint64_t nla_get_u64(const struct nlattr *)
Return payload of u64 attribute.
Definition attr.c:769
nla_get_flag
int nla_get_flag(const struct nlattr *)
Return true if flag attribute is set.
Definition attr.c:904
nla_type
int nla_type(const struct nlattr *)
Return type of the attribute.
Definition attr.c:108
nla_put_u64
int nla_put_u64(struct nl_msg *, int, uint64_t)
Add 64 bit integer attribute to netlink message.
Definition attr.c:758
nla_get_uint
uint64_t nla_get_uint(const struct nlattr *)
Return payload of variable-length unsigned integer attribute.
Definition attr.c:836
nla_ok
int nla_ok(const struct nlattr *, int)
Check if the attribute header and payload can be accessed safely.
Definition attr.c:147
nla_data
void * nla_data(const struct nlattr *)
Return pointer to the payload section.
Definition attr.c:119
nla_is_nested
int nla_is_nested(const struct nlattr *)
Return true if attribute has NLA_F_NESTED flag set.
Definition attr.c:1113
nla_put_u32
int nla_put_u32(struct nl_msg *, int, uint32_t)
Add 32 bit integer attribute to netlink message.
Definition attr.c:703
nla_put_addr
int nla_put_addr(struct nl_msg *, int, struct nl_addr *)
Add abstract address as unspecific attribute to netlink message.
Definition attr.c:554
nla_memcmp
int nla_memcmp(const struct nlattr *, const void *, size_t)
Compare attribute payload with memory area.
Definition attr.c:410
nla_put_uint
int nla_put_uint(struct nl_msg *, int, uint64_t)
Add variable-length unsigned integer attribute to netlink message.
Definition attr.c:820
nla_put_s64
int nla_put_s64(struct nl_msg *, int, int64_t)
Add 64 bit signed integer attribute to netlink message.
Definition attr.c:728
nla_put_msecs
int nla_put_msecs(struct nl_msg *, int, unsigned long)
Add a msecs netlink attribute to a netlink message.
Definition attr.c:921
nla_get_u8
uint8_t nla_get_u8(const struct nlattr *)
Return value of 8 bit integer attribute.
Definition attr.c:614
nla_attr_size
int nla_attr_size(int payload)
Return size of attribute whithout padding.
Definition attr.c:54
nla_get_s16
int16_t nla_get_s16(const struct nlattr *)
Return payload of 16 bit signed integer attribute.
Definition attr.c:639
nla_get_s64
int64_t nla_get_s64(const struct nlattr *)
Return payload of s64 attribute.
Definition attr.c:739
nla_put_flag
int nla_put_flag(struct nl_msg *, int)
Add flag netlink attribute to netlink message.
Definition attr.c:893
nla_put_s32
int nla_put_s32(struct nl_msg *, int, int32_t)
Add 32 bit signed integer attribute to netlink message.
Definition attr.c:678
nla_memcpy
int nla_memcpy(void *, const struct nlattr *, int)
Copy attribute payload to another memory area.
Definition attr.c:355
nla_nest_start
struct nlattr * nla_nest_start(struct nl_msg *, int)
Start a new level of nested attributes.
Definition attr.c:974
nla_get_msecs
unsigned long nla_get_msecs(const struct nlattr *)
Return payload of msecs attribute.
Definition attr.c:932
nla_strlcpy
size_t nla_strlcpy(char *, const struct nlattr *, size_t)
Copy string attribute payload to a buffer.
Definition attr.c:383
nla_parse_nested
int nla_parse_nested(struct nlattr *tb[], int maxtype, struct nlattr *nla, const struct nla_policy *policy)
Create attribute index based on nested attribute.
Definition attr.c:1101
nla_get_sint
int64_t nla_get_sint(const struct nlattr *)
Return payload of variable-length signed integer attribute.
Definition attr.c:804
nla_get_string
char * nla_get_string(const struct nlattr *)
Return payload of string attribute.
Definition attr.c:869
nla_get_s32
int32_t nla_get_s32(const struct nlattr *)
Return payload of 32 bit signed integer attribute.
Definition attr.c:689
nla_nest_cancel
void nla_nest_cancel(struct nl_msg *, const struct nlattr *)
Cancel the addition of a nested attribute.
Definition attr.c:1066
nla_len
int nla_len(const struct nlattr *)
Return length of the payload .
Definition attr.c:130
nla_put_u8
int nla_put_u8(struct nl_msg *, int, uint8_t)
Add 8 bit integer attribute to netlink message.
Definition attr.c:603
nla_nest_end
int nla_nest_end(struct nl_msg *, struct nlattr *)
Finalize nesting of attributes.
Definition attr.c:1037
nla_nest_end_keep_empty
int nla_nest_end_keep_empty(struct nl_msg *, struct nlattr *)
Finalize nesting of attributes without stripping off empty attributes.
Definition attr.c:1052
nla_get_s8
int8_t nla_get_s8(const struct nlattr *)
Return value of 8 bit signed integer attribute.
Definition attr.c:589
nla_find
struct nlattr * nla_find(const struct nlattr *, int, int)
Find a single attribute in a stream of attributes.
Definition attr.c:325
nla_padlen
int nla_padlen(int payload)
Return length of padding at the tail of the attribute.
Definition attr.c:90
nla_put
int nla_put(struct nl_msg *, int, int, const void *)
Add a unspecific attribute to netlink message.
Definition attr.c:505
nla_put_s8
int nla_put_s8(struct nl_msg *, int, int8_t)
Add 8 bit signed integer attribute to netlink message.
Definition attr.c:578
nla_reserve
struct nlattr * nla_reserve(struct nl_msg *, int, int)
Reserve space for a attribute.
Definition attr.c:461
nla_total_size
int nla_total_size(int payload)
Return size of attribute including padding.
Definition attr.c:72
nla_put_sint
int nla_put_sint(struct nl_msg *, int, int64_t)
Add variable-length signed integer attribute to netlink message.
Definition attr.c:788
NLA_U64
@ NLA_U64
64 bit integer
Definition attr.h:38
NLA_STRING
@ NLA_STRING
NUL terminated character string.
Definition attr.h:39
NLA_UNSPEC
@ NLA_UNSPEC
Unspecified type, binary data chunk.
Definition attr.h:34
NLA_MSECS
@ NLA_MSECS
Micro seconds (64bit)
Definition attr.h:41
NLA_U8
@ NLA_U8
8 bit integer
Definition attr.h:35
NLA_FLAG
@ NLA_FLAG
Flag.
Definition attr.h:40
NLA_U16
@ NLA_U16
16 bit integer
Definition attr.h:36
NLA_NESTED
@ NLA_NESTED
Nested attributes.
Definition attr.h:42
NLA_U32
@ NLA_U32
32 bit integer
Definition attr.h:37
nla_policy
Attribute validation policy.
Definition attr.h:66
nla_policy::maxlen
uint16_t maxlen
Maximal length of payload allowed.
Definition attr.h:74
nla_policy::minlen
uint16_t minlen
Minimal length of payload required.
Definition attr.h:71
nla_policy::type
uint16_t type
Type of attribute or NLA_UNSPEC.
Definition attr.h:68