xref: /freebsd/sys/netlink/netlink_message_parser.h (revision e924a2c80b9e1ace68d8ca0ffdacec65feec90a3)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
3  *
4  * Copyright (c) 2022 Alexander V. Chernikov <melifaro@FreeBSD.org>
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25  * SUCH DAMAGE.
26  */
27 
28 #ifndef _NETLINK_NETLINK_MESSAGE_PARSER_H_
29 #define _NETLINK_NETLINK_MESSAGE_PARSER_H_
30 
31 #ifdef _KERNEL
32 
33 #include <sys/bitset.h>
34 
35 /*
36  * It is not meant to be included directly
37  */
38 
39 /* Parsing state */
40 struct linear_buffer {
41 	char		*base;	/* Base allocated memory pointer */
42 	uint32_t	offset;	/* Currently used offset */
43 	uint32_t	size;	/* Total buffer size */
44 } __aligned(_Alignof(__max_align_t));
45 
46 static inline void *
lb_alloc(struct linear_buffer * lb,int len)47 lb_alloc(struct linear_buffer *lb, int len)
48 {
49 	len = roundup2(len, _Alignof(__max_align_t));
50 	if (lb->offset + len > lb->size)
51 		return (NULL);
52 	void *data = (void *)(lb->base + lb->offset);
53 	lb->offset += len;
54 	return (data);
55 }
56 
57 static inline void
lb_clear(struct linear_buffer * lb)58 lb_clear(struct linear_buffer *lb)
59 {
60 	memset(lb->base, 0, lb->size);
61 	lb->offset = 0;
62 }
63 
64 #define	NL_MAX_ERROR_BUF	128
65 #define	SCRATCH_BUFFER_SIZE	(1024 + NL_MAX_ERROR_BUF)
66 struct nl_pstate {
67 	struct linear_buffer	lb;	/* Per-message scratch buffer */
68 	struct nlpcb		*nlp;	/* Originator socket */
69 	struct nl_writer	*nw;	/* Message writer to use */
70 	struct nlmsghdr		*hdr;	/* Current parsed message header */
71 	uint32_t		err_off; /* error offset from hdr start */
72 	int			error;	/* last operation error */
73 	char			*err_msg; /* Description of last error */
74 	struct nlattr		*cookie; /* NLA to return to the userspace */
75 	bool			strict;	/* Strict parsing required */
76 };
77 
78 static inline void *
npt_alloc(struct nl_pstate * npt,int len)79 npt_alloc(struct nl_pstate *npt, int len)
80 {
81 	return (lb_alloc(&npt->lb, len));
82 }
83 #define npt_alloc_sockaddr(_npt, _len)	\
84 	((struct sockaddr *)(npt_alloc((_npt), (_len))))
85 
86 typedef int parse_field_f(void *hdr, struct nl_pstate *npt, void *target);
87 struct nlfield_parser {
88 	uint16_t	off_in;
89 	uint16_t	off_out;
90 	parse_field_f	*cb;
91 };
92 static const struct nlfield_parser nlf_p_empty[] = {};
93 
94 int nlf_get_ifp(void *src, struct nl_pstate *npt, void *target);
95 int nlf_get_ifpz(void *src, struct nl_pstate *npt, void *target);
96 int nlf_get_u8(void *src, struct nl_pstate *npt, void *target);
97 int nlf_get_u16(void *src, struct nl_pstate *npt, void *target);
98 int nlf_get_u32(void *src, struct nl_pstate *npt, void *target);
99 int nlf_get_u8_u32(void *src, struct nl_pstate *npt, void *target);
100 
101 struct nlattr_parser;
102 typedef int parse_attr_f(struct nlattr *attr, struct nl_pstate *npt,
103     const void *arg, void *target);
104 struct nlattr_parser {
105 	uint16_t	type;	/* Attribute type */
106 	uint16_t	off;	/* field offset in the target structure */
107 	parse_attr_f	*cb;	/* parser function to call */
108 	const void	*arg;
109 };
110 
111 typedef bool strict_parser_f(void *hdr, struct nl_pstate *npt);
112 typedef bool post_parser_f(void *parsed_attrs, struct nl_pstate *npt);
113 
114 struct nlhdr_parser {
115 	u_int			nl_hdr_off; /* aligned netlink header size */
116 	u_int			out_hdr_off; /* target header size */
117 	u_int			fp_size;
118 	u_int			np_size;
119 	const struct nlfield_parser	*fp; /* array of header field parsers */
120 	const struct nlattr_parser	*np; /* array of attribute parsers */
121 	strict_parser_f		*sp; /* Pre-parse strict validation function */
122 	post_parser_f		*post_parse;
123 };
124 
125 #define	NL_DECLARE_PARSER_EXT(_name, _t, _sp, _fp, _np, _pp)	\
126 static const struct nlhdr_parser _name = {			\
127 	.nl_hdr_off = sizeof(_t),				\
128 	.fp = &((_fp)[0]),					\
129 	.np = &((_np)[0]),					\
130 	.fp_size = nitems(_fp),					\
131 	.np_size = nitems(_np),					\
132 	.sp = _sp,						\
133 	.post_parse = _pp,					\
134 }
135 
136 #define	NL_DECLARE_PARSER(_name, _t, _fp, _np)			\
137 	NL_DECLARE_PARSER_EXT(_name, _t, NULL, _fp, _np, NULL)
138 
139 #define	NL_DECLARE_STRICT_PARSER(_name, _t, _sp, _fp, _np)	\
140 	NL_DECLARE_PARSER_EXT(_name, _t, _sp, _fp, _np, NULL)
141 
142 #define	NL_DECLARE_ARR_PARSER(_name, _t, _o, _fp, _np)	\
143 static const struct nlhdr_parser _name = {		\
144 	.nl_hdr_off = sizeof(_t),			\
145 	.out_hdr_off = sizeof(_o),			\
146 	.fp = &((_fp)[0]),				\
147 	.np = &((_np)[0]),				\
148 	.fp_size = nitems(_fp),				\
149 	.np_size = nitems(_np),				\
150 }
151 
152 #define	NL_DECLARE_ATTR_PARSER_EXT(_name, _np, _pp)	\
153 static const struct nlhdr_parser _name = {		\
154 	.np = &((_np)[0]),				\
155 	.np_size = nitems(_np),				\
156 	.post_parse = (_pp)				\
157 }
158 
159 #define	NL_DECLARE_ATTR_PARSER(_name, _np)		\
160 	NL_DECLARE_ATTR_PARSER_EXT(_name, _np, NULL)
161 
162 #define	NL_ATTR_BMASK_SIZE	128
163 BITSET_DEFINE(nlattr_bmask, NL_ATTR_BMASK_SIZE);
164 
165 void nl_get_attrs_bmask_raw(struct nlattr *nla_head, uint32_t len,
166     struct nlattr_bmask *bm);
167 bool nl_has_attr(const struct nlattr_bmask *bm, uint16_t nla_type);
168 
169 int nl_parse_attrs_raw(struct nlattr *nla_head, uint16_t len,
170     const struct nlattr_parser *ps, u_int pslen, struct nl_pstate *npt,
171     void *target);
172 
173 int nlattr_get_flag(struct nlattr *nla, struct nl_pstate *npt,
174     const void *arg, void *target);
175 int nlattr_get_ip(struct nlattr *nla, struct nl_pstate *npt,
176     const void *arg, void *target);
177 int nlattr_get_bool(struct nlattr *nla, struct nl_pstate *npt,
178     const void *arg, void *target);
179 int nlattr_get_uint8(struct nlattr *nla, struct nl_pstate *npt,
180     const void *arg, void *target);
181 int nlattr_get_uint16(struct nlattr *nla, struct nl_pstate *npt,
182     const void *arg, void *target);
183 int nlattr_get_uint32(struct nlattr *nla, struct nl_pstate *npt,
184     const void *arg, void *target);
185 int nlattr_get_uint64(struct nlattr *nla, struct nl_pstate *npt,
186     const void *arg, void *target);
187 int nlattr_get_in_addr(struct nlattr *nla, struct nl_pstate *npt,
188     const void *arg, void *target);
189 int nlattr_get_in6_addr(struct nlattr *nla, struct nl_pstate *npt,
190     const void *arg, void *target);
191 int nlattr_get_ifp(struct nlattr *nla, struct nl_pstate *npt,
192     const void *arg, void *target);
193 int nlattr_get_ifpz(struct nlattr *nla, struct nl_pstate *npt,
194     const void *arg, void *target);
195 int nlattr_get_ipvia(struct nlattr *nla, struct nl_pstate *npt,
196     const void *arg, void *target);
197 int nlattr_get_chara(struct nlattr *nla, struct nl_pstate *npt,
198     const void *arg, void *target);
199 int nlattr_get_string(struct nlattr *nla, struct nl_pstate *npt,
200     const void *arg, void *target);
201 int nlattr_get_stringn(struct nlattr *nla, struct nl_pstate *npt,
202     const void *arg, void *target);
203 int nlattr_get_bytes(struct nlattr *nla, struct nl_pstate *npt,
204     const void *arg, void *target);
205 int nlattr_get_nla(struct nlattr *nla, struct nl_pstate *npt,
206     const void *arg, void *target);
207 int nlattr_get_nested(struct nlattr *nla, struct nl_pstate *npt,
208     const void *arg, void *target);
209 int nlattr_get_nested_ptr(struct nlattr *nla, struct nl_pstate *npt,
210     const void *arg, void *target);
211 
212 bool nlmsg_report_err_msg(struct nl_pstate *npt, const char *fmt, ...)
213 	__printflike(2, 3);
214 
215 #define	NLMSG_REPORT_ERR_MSG(_npt, _fmt, ...) {	\
216 	nlmsg_report_err_msg(_npt, _fmt, ## __VA_ARGS__); \
217 	NLP_LOG(LOG_DEBUG, (_npt)->nlp, _fmt, ## __VA_ARGS__); \
218 }
219 
220 bool nlmsg_report_err_offset(struct nl_pstate *npt, uint32_t off);
221 
222 void nlmsg_report_cookie(struct nl_pstate *npt, struct nlattr *nla);
223 void nlmsg_report_cookie_u32(struct nl_pstate *npt, uint32_t val);
224 
225 struct nlmsghdr *nl_alloc_compat_hdr(struct nlmsghdr *hdr, uint32_t len,
226     struct nl_pstate *npt);
227 
228 /*
229  * Have it inline so compiler can optimize field accesses into
230  * the list of direct function calls without iteration.
231  */
232 static inline int
nl_parse_header(void * hdr,uint32_t len,const struct nlhdr_parser * parser,struct nl_pstate * npt,void * target)233 nl_parse_header(void *hdr, uint32_t len, const struct nlhdr_parser *parser,
234     struct nl_pstate *npt, void *target)
235 {
236 	int error;
237 
238 	MPASS(len >= parser->nl_hdr_off);
239 
240 	if (npt->strict && parser->sp != NULL && !parser->sp(hdr, npt))
241 		return (EINVAL);
242 
243 	/* Extract fields first */
244 	for (u_int i = 0; i < parser->fp_size; i++) {
245 		const struct nlfield_parser *fp = &parser->fp[i];
246 		void *src = (char *)hdr + fp->off_in;
247 		void *dst = (char *)target + fp->off_out;
248 
249 		error = fp->cb(src, npt, dst);
250 		if (error != 0)
251 			return (error);
252 	}
253 
254 	error = nl_parse_attrs_raw(
255 	    (struct nlattr *)((char *)hdr + parser->nl_hdr_off),
256 	    len - parser->nl_hdr_off, parser->np, parser->np_size, npt, target);
257 
258 	if (parser->post_parse != NULL && error == 0) {
259 		if (!parser->post_parse(target, npt))
260 			return (EINVAL);
261 	}
262 
263 	return (error);
264 }
265 
266 static inline int
nl_parse_nested(struct nlattr * nla,const struct nlhdr_parser * parser,struct nl_pstate * npt,void * target)267 nl_parse_nested(struct nlattr *nla, const struct nlhdr_parser *parser,
268     struct nl_pstate *npt, void *target)
269 {
270 	return (nl_parse_attrs_raw((struct nlattr *)NLA_DATA(nla),
271 	    NLA_DATA_LEN(nla), parser->np, parser->np_size, npt, target));
272 }
273 
274 /*
275  * Checks that attributes are sorted by attribute type.
276  */
277 static inline void
nl_verify_parsers(const struct nlhdr_parser ** parser,int count)278 nl_verify_parsers(const struct nlhdr_parser **parser, int count)
279 {
280 #ifdef INVARIANTS
281 	for (int i = 0; i < count; i++) {
282 		const struct nlhdr_parser *p = parser[i];
283 		int attr_type = 0;
284 		for (int j = 0; j < p->np_size; j++) {
285 			MPASS(p->np[j].type > attr_type);
286 			attr_type = p->np[j].type;
287 
288 			/* Recurse into nested objects. */
289 			if (p->np[j].cb == nlattr_get_nested ||
290 			    p->np[j].cb == nlattr_get_nested_ptr) {
291 				const struct nlhdr_parser *np =
292 				    (const struct nlhdr_parser *)p->np[j].arg;
293 				nl_verify_parsers(&np, 1);
294 			}
295 		}
296 	}
297 #endif
298 }
299 void nl_verify_parsers(const struct nlhdr_parser **parser, int count);
300 #define	NL_VERIFY_PARSERS(_p)	nl_verify_parsers((_p), nitems(_p))
301 
302 static inline int
nl_parse_nlmsg(struct nlmsghdr * hdr,const struct nlhdr_parser * parser,struct nl_pstate * npt,void * target)303 nl_parse_nlmsg(struct nlmsghdr *hdr, const struct nlhdr_parser *parser,
304     struct nl_pstate *npt, void *target)
305 {
306 	if (__predict_false(hdr->nlmsg_len - sizeof(struct nlmsghdr) <
307 	    parser->nl_hdr_off) &&
308 	    ((hdr = nl_alloc_compat_hdr(hdr, parser->nl_hdr_off, npt)) == NULL))
309 		return (ENOMEM);
310 	return (nl_parse_header(hdr + 1, hdr->nlmsg_len - sizeof(*hdr), parser,
311 	    npt, target));
312 }
313 
314 static inline void
nl_get_attrs_bmask_nlmsg(struct nlmsghdr * hdr,const struct nlhdr_parser * parser,struct nlattr_bmask * bm)315 nl_get_attrs_bmask_nlmsg(struct nlmsghdr *hdr,
316     const struct nlhdr_parser *parser, struct nlattr_bmask *bm)
317 {
318 	if (__predict_false(hdr->nlmsg_len - sizeof(struct nlmsghdr) <
319 	    parser->nl_hdr_off)) {
320 		/* Doesn't make sense to call nl_alloc_compat_hdr() here. */
321 		BIT_ZERO(NL_ATTR_BMASK_SIZE, bm);
322 		return;
323 	}
324 	nl_get_attrs_bmask_raw(
325 	    (struct nlattr *)((char *)(hdr + 1) + parser->nl_hdr_off),
326 	    hdr->nlmsg_len - sizeof(*hdr) - parser->nl_hdr_off, bm);
327 }
328 
329 #endif
330 #endif
331