Debian Codegraph @

include/netlink/attr.h

raw
1 /* SPDX-License-Identifier: LGPL-2.1-only */
2 /*
3 * Copyright (c) 2003-2013 Thomas Graf <[email protected]>
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
15 extern "C" {
16 #endif
17
18 struct nlattr;
19
20 struct 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 */
33 enum {
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 */
78 extern int nla_attr_size(int payload);
79 extern int nla_total_size(int payload);
80 extern int nla_padlen(int payload);
81
82 /* Attribute parsing */
83 extern int nla_type(const struct nlattr *);
84 extern void * nla_data(const struct nlattr *);
85 extern int nla_len(const struct nlattr *);
86 extern int nla_ok(const struct nlattr *, int);
87 extern struct nlattr * nla_next(const struct nlattr *, int *);
88 extern int nla_parse(struct nlattr **, int, struct nlattr *,
89 int, const struct nla_policy *);
90 extern int nla_validate(const struct nlattr *, int, int,
91 const struct nla_policy *);
92 extern struct nlattr * nla_find(const struct nlattr *, int, int);
93
94 /* Helper Functions */
95 extern int nla_memcpy(void *, const struct nlattr *, int);
96 extern size_t nla_strlcpy(char *, const struct nlattr *, size_t);
97 extern int nla_memcmp(const struct nlattr *, const void *, size_t);
98 extern int nla_strcmp(const struct nlattr *, const char *);
99
100 /* Unspecific attribute */
101 extern struct nlattr * nla_reserve(struct nl_msg *, int, int);
102 extern int nla_put(struct nl_msg *, int, int, const void *);
103 extern int nla_put_data(struct nl_msg *, int,
104 const struct nl_data *);
105 extern int nla_put_addr(struct nl_msg *, int, struct nl_addr *);
106
107 /* Integer attribute */
108 extern int8_t nla_get_s8(const struct nlattr *);
109 extern int nla_put_s8(struct nl_msg *, int, int8_t);
110 extern uint8_t nla_get_u8(const struct nlattr *);
111 extern int nla_put_u8(struct nl_msg *, int, uint8_t);
112 extern int16_t nla_get_s16(const struct nlattr *);
113 extern int nla_put_s16(struct nl_msg *, int, int16_t);
114 extern uint16_t nla_get_u16(const struct nlattr *);
115 extern int nla_put_u16(struct nl_msg *, int, uint16_t);
116 extern int32_t nla_get_s32(const struct nlattr *);
117 extern int nla_put_s32(struct nl_msg *, int, int32_t);
118 extern uint32_t nla_get_u32(const struct nlattr *);
119 extern int nla_put_u32(struct nl_msg *, int, uint32_t);
120 extern int64_t nla_get_s64(const struct nlattr *);
121 extern int nla_put_s64(struct nl_msg *, int, int64_t);
122 extern uint64_t nla_get_u64(const struct nlattr *);
123 extern int nla_put_u64(struct nl_msg *, int, uint64_t);
124 extern int64_t nla_get_sint(const struct nlattr *);
125 extern int nla_put_sint(struct nl_msg *, int, int64_t);
126 extern uint64_t nla_get_uint(const struct nlattr *);
127 extern int nla_put_uint(struct nl_msg *, int, uint64_t);
128
129 /* String attribute */
130 extern char * nla_get_string(const struct nlattr *);
131 extern char * nla_strdup(const struct nlattr *);
132 extern int nla_put_string(struct nl_msg *, int, const char *);
133
134 /* Flag attribute */
135 extern int nla_get_flag(const struct nlattr *);
136 extern int nla_put_flag(struct nl_msg *, int);
137
138 /* Msec attribute */
139 extern unsigned long nla_get_msecs(const struct nlattr *);
140 extern int nla_put_msecs(struct nl_msg *, int, unsigned long);
141
142 /* Attribute nesting */
143 extern int nla_put_nested(struct nl_msg *, int,
144 const struct nl_msg *);
145 extern struct nlattr * nla_nest_start(struct nl_msg *, int);
146 extern int nla_nest_end(struct nl_msg *, struct nlattr *);
147 extern int nla_nest_end_keep_empty(struct nl_msg *, struct nlattr *);
148 extern void nla_nest_cancel(struct nl_msg *, const struct nlattr *);
149 extern int nla_parse_nested(struct nlattr **, int, struct nlattr *,
150 const struct nla_policy *);
151 extern 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