xref: /linux/tools/net/ynl/lib/ynl-priv.h (revision f058b2dd70b1a5503dff899010aeb53b436091e5)
1 /* SPDX-License-Identifier: GPL-2.0 OR BSD-3-Clause */
2 #ifndef __YNL_C_PRIV_H
3 #define __YNL_C_PRIV_H 1
4 
5 #include <stdbool.h>
6 #include <stddef.h>
7 #include <linux/types.h>
8 
9 struct ynl_parse_arg;
10 
11 /*
12  * YNL internals / low level stuff
13  */
14 
15 enum ynl_policy_type {
16 	YNL_PT_REJECT = 1,
17 	YNL_PT_IGNORE,
18 	YNL_PT_NEST,
19 	YNL_PT_FLAG,
20 	YNL_PT_BINARY,
21 	YNL_PT_U8,
22 	YNL_PT_U16,
23 	YNL_PT_U32,
24 	YNL_PT_U64,
25 	YNL_PT_UINT,
26 	YNL_PT_NUL_STR,
27 	YNL_PT_BITFIELD32,
28 };
29 
30 enum ynl_parse_result {
31 	YNL_PARSE_CB_ERROR = -1,
32 	YNL_PARSE_CB_STOP = 0,
33 	YNL_PARSE_CB_OK = 1,
34 };
35 
36 #define YNL_SOCKET_BUFFER_SIZE		(1 << 17)
37 
38 #define YNL_ARRAY_SIZE(array)	(sizeof(array) ?			\
39 				 sizeof(array) / sizeof(array[0]) : 0)
40 
41 typedef int (*ynl_parse_cb_t)(const struct nlmsghdr *nlh,
42 			      struct ynl_parse_arg *yarg);
43 
44 struct ynl_policy_attr {
45 	enum ynl_policy_type type;
46 	unsigned int len;
47 	const char *name;
48 	struct ynl_policy_nest *nest;
49 };
50 
51 struct ynl_policy_nest {
52 	unsigned int max_attr;
53 	struct ynl_policy_attr *table;
54 };
55 
56 struct ynl_parse_arg {
57 	struct ynl_sock *ys;
58 	struct ynl_policy_nest *rsp_policy;
59 	void *data;
60 };
61 
62 struct ynl_dump_list_type {
63 	struct ynl_dump_list_type *next;
64 	unsigned char data[] __attribute__((aligned(8)));
65 };
66 extern struct ynl_dump_list_type *YNL_LIST_END;
67 
68 static inline bool ynl_dump_obj_is_last(void *obj)
69 {
70 	unsigned long uptr = (unsigned long)obj;
71 
72 	uptr -= offsetof(struct ynl_dump_list_type, data);
73 	return uptr == (unsigned long)YNL_LIST_END;
74 }
75 
76 static inline void *ynl_dump_obj_next(void *obj)
77 {
78 	unsigned long uptr = (unsigned long)obj;
79 	struct ynl_dump_list_type *list;
80 
81 	uptr -= offsetof(struct ynl_dump_list_type, data);
82 	list = (void *)uptr;
83 	uptr = (unsigned long)list->next;
84 	uptr += offsetof(struct ynl_dump_list_type, data);
85 
86 	return (void *)uptr;
87 }
88 
89 struct ynl_ntf_base_type {
90 	__u16 family;
91 	__u8 cmd;
92 	struct ynl_ntf_base_type *next;
93 	void (*free)(struct ynl_ntf_base_type *ntf);
94 	unsigned char data[] __attribute__((aligned(8)));
95 };
96 
97 struct nlmsghdr *
98 ynl_gemsg_start_req(struct ynl_sock *ys, __u32 id, __u8 cmd, __u8 version);
99 struct nlmsghdr *
100 ynl_gemsg_start_dump(struct ynl_sock *ys, __u32 id, __u8 cmd, __u8 version);
101 
102 int ynl_attr_validate(struct ynl_parse_arg *yarg, const struct nlattr *attr);
103 
104 /* YNL specific helpers used by the auto-generated code */
105 
106 struct ynl_req_state {
107 	struct ynl_parse_arg yarg;
108 	ynl_parse_cb_t cb;
109 	__u32 rsp_cmd;
110 };
111 
112 struct ynl_dump_state {
113 	struct ynl_parse_arg yarg;
114 	void *first;
115 	struct ynl_dump_list_type *last;
116 	size_t alloc_sz;
117 	ynl_parse_cb_t cb;
118 	__u32 rsp_cmd;
119 };
120 
121 struct ynl_ntf_info {
122 	struct ynl_policy_nest *policy;
123 	ynl_parse_cb_t cb;
124 	size_t alloc_sz;
125 	void (*free)(struct ynl_ntf_base_type *ntf);
126 };
127 
128 int ynl_exec(struct ynl_sock *ys, struct nlmsghdr *req_nlh,
129 	     struct ynl_req_state *yrs);
130 int ynl_exec_dump(struct ynl_sock *ys, struct nlmsghdr *req_nlh,
131 		  struct ynl_dump_state *yds);
132 
133 void ynl_error_unknown_notification(struct ynl_sock *ys, __u8 cmd);
134 int ynl_error_parse(struct ynl_parse_arg *yarg, const char *msg);
135 
136 /* Netlink message handling helpers */
137 
138 static inline struct nlmsghdr *ynl_nlmsg_put_header(void *buf)
139 {
140 	struct nlmsghdr *nlh = buf;
141 
142 	memset(nlh, 0, sizeof(*nlh));
143 	nlh->nlmsg_len = NLMSG_HDRLEN;
144 
145 	return nlh;
146 }
147 
148 static inline unsigned int ynl_nlmsg_data_len(const struct nlmsghdr *nlh)
149 {
150 	return nlh->nlmsg_len - NLMSG_HDRLEN;
151 }
152 
153 static inline void *ynl_nlmsg_data(const struct nlmsghdr *nlh)
154 {
155 	return (unsigned char *)nlh + NLMSG_HDRLEN;
156 }
157 
158 static inline void *
159 ynl_nlmsg_data_offset(const struct nlmsghdr *nlh, unsigned int offset)
160 {
161 	return (unsigned char *)nlh + NLMSG_HDRLEN + offset;
162 }
163 
164 static inline void *ynl_nlmsg_end_addr(const struct nlmsghdr *nlh)
165 {
166 	return (char *)nlh + nlh->nlmsg_len;
167 }
168 
169 static inline void *
170 ynl_nlmsg_put_extra_header(struct nlmsghdr *nlh, unsigned int size)
171 {
172 	void *tail = ynl_nlmsg_end_addr(nlh);
173 
174 	nlh->nlmsg_len += NLMSG_ALIGN(size);
175 	return tail;
176 }
177 
178 /* Netlink attribute helpers */
179 
180 static inline unsigned int ynl_attr_type(const struct nlattr *attr)
181 {
182 	return attr->nla_type & NLA_TYPE_MASK;
183 }
184 
185 static inline unsigned int ynl_attr_data_len(const struct nlattr *attr)
186 {
187 	return attr->nla_len - NLA_HDRLEN;
188 }
189 
190 static inline void *ynl_attr_data(const struct nlattr *attr)
191 {
192 	return (unsigned char *)attr + NLA_HDRLEN;
193 }
194 
195 static inline void *ynl_attr_data_end(const struct nlattr *attr)
196 {
197 	return ynl_attr_data(attr) + ynl_attr_data_len(attr);
198 }
199 
200 #define ynl_attr_for_each(attr, nlh, fixed_hdr_sz)			\
201 	for ((attr) = ynl_attr_first(nlh, (nlh)->nlmsg_len,		\
202 				     NLMSG_HDRLEN + fixed_hdr_sz); attr; \
203 	     (attr) = ynl_attr_next(ynl_nlmsg_end_addr(nlh), attr))
204 
205 #define ynl_attr_for_each_nested(attr, outer)				\
206 	for ((attr) = ynl_attr_first(outer, outer->nla_len,		\
207 				     sizeof(struct nlattr)); attr;	\
208 	     (attr) = ynl_attr_next(ynl_attr_data_end(outer), attr))
209 
210 #define ynl_attr_for_each_payload(start, len, attr)			\
211 	for ((attr) = ynl_attr_first(start, len, 0); attr;		\
212 	     (attr) = ynl_attr_next(start + len, attr))
213 
214 static inline struct nlattr *
215 ynl_attr_if_good(const void *end, struct nlattr *attr)
216 {
217 	if (attr + 1 > (const struct nlattr *)end)
218 		return NULL;
219 	if (ynl_attr_data_end(attr) > end)
220 		return NULL;
221 	return attr;
222 }
223 
224 static inline struct nlattr *
225 ynl_attr_next(const void *end, const struct nlattr *prev)
226 {
227 	struct nlattr *attr;
228 
229 	attr = (void *)((char *)prev + NLA_ALIGN(prev->nla_len));
230 	return ynl_attr_if_good(end, attr);
231 }
232 
233 static inline struct nlattr *
234 ynl_attr_first(const void *start, size_t len, size_t skip)
235 {
236 	struct nlattr *attr;
237 
238 	attr = (void *)((char *)start + NLMSG_ALIGN(skip));
239 	return ynl_attr_if_good(start + len, attr);
240 }
241 
242 static inline struct nlattr *
243 ynl_attr_nest_start(struct nlmsghdr *nlh, unsigned int attr_type)
244 {
245 	struct nlattr *attr;
246 
247 	attr = ynl_nlmsg_end_addr(nlh);
248 	attr->nla_type = attr_type | NLA_F_NESTED;
249 	nlh->nlmsg_len += NLA_HDRLEN;
250 
251 	return attr;
252 }
253 
254 static inline void
255 ynl_attr_nest_end(struct nlmsghdr *nlh, struct nlattr *attr)
256 {
257 	attr->nla_len = (char *)ynl_nlmsg_end_addr(nlh) - (char *)attr;
258 }
259 
260 static inline void
261 ynl_attr_put(struct nlmsghdr *nlh, unsigned int attr_type,
262 	     const void *value, size_t size)
263 {
264 	struct nlattr *attr;
265 
266 	attr = ynl_nlmsg_end_addr(nlh);
267 	attr->nla_type = attr_type;
268 	attr->nla_len = NLA_HDRLEN + size;
269 
270 	memcpy(ynl_attr_data(attr), value, size);
271 
272 	nlh->nlmsg_len += NLMSG_ALIGN(attr->nla_len);
273 }
274 
275 static inline void
276 ynl_attr_put_str(struct nlmsghdr *nlh, unsigned int attr_type, const char *str)
277 {
278 	struct nlattr *attr;
279 	const char *end;
280 
281 	attr = ynl_nlmsg_end_addr(nlh);
282 	attr->nla_type = attr_type;
283 
284 	end = stpcpy(ynl_attr_data(attr), str);
285 	attr->nla_len =
286 		NLA_HDRLEN + NLA_ALIGN(end - (char *)ynl_attr_data(attr));
287 
288 	nlh->nlmsg_len += NLMSG_ALIGN(attr->nla_len);
289 }
290 
291 static inline const char *ynl_attr_get_str(const struct nlattr *attr)
292 {
293 	return (const char *)ynl_attr_data(attr);
294 }
295 
296 static inline __s8 ynl_attr_get_s8(const struct nlattr *attr)
297 {
298 	return *(__s8 *)ynl_attr_data(attr);
299 }
300 
301 static inline __s16 ynl_attr_get_s16(const struct nlattr *attr)
302 {
303 	return *(__s16 *)ynl_attr_data(attr);
304 }
305 
306 static inline __s32 ynl_attr_get_s32(const struct nlattr *attr)
307 {
308 	return *(__s32 *)ynl_attr_data(attr);
309 }
310 
311 static inline __s64 ynl_attr_get_s64(const struct nlattr *attr)
312 {
313 	__s64 tmp;
314 
315 	memcpy(&tmp, (unsigned char *)(attr + 1), sizeof(tmp));
316 	return tmp;
317 }
318 
319 static inline __u8 ynl_attr_get_u8(const struct nlattr *attr)
320 {
321 	return *(__u8 *)ynl_attr_data(attr);
322 }
323 
324 static inline __u16 ynl_attr_get_u16(const struct nlattr *attr)
325 {
326 	return *(__u16 *)ynl_attr_data(attr);
327 }
328 
329 static inline __u32 ynl_attr_get_u32(const struct nlattr *attr)
330 {
331 	return *(__u32 *)ynl_attr_data(attr);
332 }
333 
334 static inline __u64 ynl_attr_get_u64(const struct nlattr *attr)
335 {
336 	__u64 tmp;
337 
338 	memcpy(&tmp, (unsigned char *)(attr + 1), sizeof(tmp));
339 	return tmp;
340 }
341 
342 static inline void
343 ynl_attr_put_s8(struct nlmsghdr *nlh, unsigned int attr_type, __s8 value)
344 {
345 	ynl_attr_put(nlh, attr_type, &value, sizeof(value));
346 }
347 
348 static inline void
349 ynl_attr_put_s16(struct nlmsghdr *nlh, unsigned int attr_type, __s16 value)
350 {
351 	ynl_attr_put(nlh, attr_type, &value, sizeof(value));
352 }
353 
354 static inline void
355 ynl_attr_put_s32(struct nlmsghdr *nlh, unsigned int attr_type, __s32 value)
356 {
357 	ynl_attr_put(nlh, attr_type, &value, sizeof(value));
358 }
359 
360 static inline void
361 ynl_attr_put_s64(struct nlmsghdr *nlh, unsigned int attr_type, __s64 value)
362 {
363 	ynl_attr_put(nlh, attr_type, &value, sizeof(value));
364 }
365 
366 static inline void
367 ynl_attr_put_u8(struct nlmsghdr *nlh, unsigned int attr_type, __u8 value)
368 {
369 	ynl_attr_put(nlh, attr_type, &value, sizeof(value));
370 }
371 
372 static inline void
373 ynl_attr_put_u16(struct nlmsghdr *nlh, unsigned int attr_type, __u16 value)
374 {
375 	ynl_attr_put(nlh, attr_type, &value, sizeof(value));
376 }
377 
378 static inline void
379 ynl_attr_put_u32(struct nlmsghdr *nlh, unsigned int attr_type, __u32 value)
380 {
381 	ynl_attr_put(nlh, attr_type, &value, sizeof(value));
382 }
383 
384 static inline void
385 ynl_attr_put_u64(struct nlmsghdr *nlh, unsigned int attr_type, __u64 value)
386 {
387 	ynl_attr_put(nlh, attr_type, &value, sizeof(value));
388 }
389 
390 static inline __u64 ynl_attr_get_uint(const struct nlattr *attr)
391 {
392 	switch (ynl_attr_data_len(attr)) {
393 	case 4:
394 		return ynl_attr_get_u32(attr);
395 	case 8:
396 		return ynl_attr_get_u64(attr);
397 	default:
398 		return 0;
399 	}
400 }
401 
402 static inline __s64 ynl_attr_get_sint(const struct nlattr *attr)
403 {
404 	switch (ynl_attr_data_len(attr)) {
405 	case 4:
406 		return ynl_attr_get_s32(attr);
407 	case 8:
408 		return ynl_attr_get_s64(attr);
409 	default:
410 		return 0;
411 	}
412 }
413 
414 static inline void
415 ynl_attr_put_uint(struct nlmsghdr *nlh, __u16 type, __u64 data)
416 {
417 	if ((__u32)data == (__u64)data)
418 		ynl_attr_put_u32(nlh, type, data);
419 	else
420 		ynl_attr_put_u64(nlh, type, data);
421 }
422 
423 static inline void
424 ynl_attr_put_sint(struct nlmsghdr *nlh, __u16 type, __s64 data)
425 {
426 	if ((__s32)data == (__s64)data)
427 		ynl_attr_put_s32(nlh, type, data);
428 	else
429 		ynl_attr_put_s64(nlh, type, data);
430 }
431 #endif
432