xref: /freebsd/sys/netlink/netlink_message_parser.h (revision 9729f076e4d93c5a37e78d427bfe0f1ab99bbcc6)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
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  * It is not meant to be included directly
34  */
35 
36 /* Parsing state */
37 struct linear_buffer {
38 	char		*base;	/* Base allocated memory pointer */
39 	uint32_t	offset;	/* Currently used offset */
40 	uint32_t	size;	/* Total buffer size */
41 };
42 
43 static inline void *
44 lb_alloc(struct linear_buffer *lb, int len)
45 {
46 	len = roundup2(len, sizeof(uint64_t));
47 	if (lb->offset + len > lb->size)
48 		return (NULL);
49 	void *data = (void *)(lb->base + lb->offset);
50 	lb->offset += len;
51 	return (data);
52 }
53 
54 static inline void
55 lb_clear(struct linear_buffer *lb)
56 {
57 	memset(lb->base, 0, lb->size);
58 	lb->offset = 0;
59 }
60 
61 #define	NL_MAX_ERROR_BUF	128
62 #define	SCRATCH_BUFFER_SIZE	(1024 + NL_MAX_ERROR_BUF)
63 struct nl_pstate {
64         struct linear_buffer    lb;		/* Per-message scratch buffer */
65         struct nlpcb		*nlp;		/* Originator socket */
66 	struct nl_writer	*nw;		/* Message writer to use */
67 	struct nlmsghdr		*hdr;		/* Current parsed message header */
68 	uint32_t		err_off;	/* error offset from hdr start */
69         int			error;		/* last operation error */
70 	char			*err_msg;	/* Description of last error */
71 	bool			strict;		/* Strict parsing required */
72 };
73 
74 static inline void *
75 npt_alloc(struct nl_pstate *npt, int len)
76 {
77 	return (lb_alloc(&npt->lb, len));
78 }
79 #define npt_alloc_sockaddr(_npt, _len)  ((struct sockaddr *)(npt_alloc(_npt, _len)))
80 
81 typedef int parse_field_f(void *hdr, struct nl_pstate *npt,
82     void *target);
83 struct nlfield_parser {
84 	uint16_t	off_in;
85 	uint16_t	off_out;
86 	parse_field_f	*cb;
87 };
88 static const struct nlfield_parser nlf_p_empty[] = {};
89 
90 int nlf_get_ifp(void *src, struct nl_pstate *npt, void *target);
91 int nlf_get_ifpz(void *src, struct nl_pstate *npt, void *target);
92 int nlf_get_u8(void *src, struct nl_pstate *npt, void *target);
93 int nlf_get_u16(void *src, struct nl_pstate *npt, void *target);
94 int nlf_get_u32(void *src, struct nl_pstate *npt, void *target);
95 int nlf_get_u8_u32(void *src, struct nl_pstate *npt, void *target);
96 
97 
98 struct nlattr_parser;
99 typedef int parse_attr_f(struct nlattr *attr, struct nl_pstate *npt,
100     const void *arg, void *target);
101 struct nlattr_parser {
102 	uint16_t			type;	/* Attribute type */
103 	uint16_t			off;	/* field offset in the target structure */
104 	parse_attr_f			*cb;	/* parser function to call */
105 	const void			*arg;
106 };
107 
108 typedef bool strict_parser_f(void *hdr, struct nl_pstate *npt);
109 
110 struct nlhdr_parser {
111 	int				nl_hdr_off; /* aligned netlink header size */
112 	int				out_hdr_off; /* target header size */
113 	int				fp_size;
114 	int				np_size;
115 	const struct nlfield_parser	*fp; /* array of header field parsers */
116 	const struct nlattr_parser	*np; /* array of attribute parsers */
117 	strict_parser_f			*sp; /* Parser function */
118 };
119 
120 #define	NL_DECLARE_PARSER(_name, _t, _fp, _np)		\
121 static const struct nlhdr_parser _name = {		\
122 	.nl_hdr_off = sizeof(_t),			\
123 	.fp = &((_fp)[0]),				\
124 	.np = &((_np)[0]),				\
125 	.fp_size = NL_ARRAY_LEN(_fp),			\
126 	.np_size = NL_ARRAY_LEN(_np),			\
127 }
128 
129 #define	NL_DECLARE_STRICT_PARSER(_name, _t, _sp, _fp, _np)\
130 static const struct nlhdr_parser _name = {		\
131 	.nl_hdr_off = sizeof(_t),			\
132 	.fp = &((_fp)[0]),				\
133 	.np = &((_np)[0]),				\
134 	.fp_size = NL_ARRAY_LEN(_fp),			\
135 	.np_size = NL_ARRAY_LEN(_np),			\
136 	.sp = _sp,					\
137 }
138 
139 #define	NL_DECLARE_ARR_PARSER(_name, _t, _o, _fp, _np)	\
140 static const struct nlhdr_parser _name = {		\
141 	.nl_hdr_off = sizeof(_t),			\
142 	.out_hdr_off = sizeof(_o),			\
143 	.fp = &((_fp)[0]),				\
144 	.np = &((_np)[0]),				\
145 	.fp_size = NL_ARRAY_LEN(_fp),			\
146 	.np_size = NL_ARRAY_LEN(_np),			\
147 }
148 
149 #define	NL_DECLARE_ATTR_PARSER(_name, _np)		\
150 static const struct nlhdr_parser _name = {		\
151 	.np = &((_np)[0]),				\
152 	.np_size = NL_ARRAY_LEN(_np),			\
153 }
154 
155 struct nlarr_hdr {
156 	int num_items;
157 	int max_items;
158 };
159 
160 int nl_parse_attrs_raw(struct nlattr *nla_head, int len, const struct nlattr_parser *ps,
161     int pslen, struct nl_pstate *npt, void *target);
162 int nl_parse_attrs(struct nlmsghdr *hdr, int hdrlen, struct nlattr_parser *ps,
163     int pslen, struct nl_pstate *npt, void *target);
164 
165 int nlattr_get_flag(struct nlattr *nla, struct nl_pstate *npt,
166     const void *arg, void *target);
167 int nlattr_get_ip(struct nlattr *nla, struct nl_pstate *npt,
168     const void *arg, void *target);
169 int nlattr_get_uint16(struct nlattr *nla, struct nl_pstate *npt,
170     const void *arg, void *target);
171 int nlattr_get_uint32(struct nlattr *nla, struct nl_pstate *npt,
172     const void *arg, void *target);
173 int nlattr_get_uint64(struct nlattr *nla, struct nl_pstate *npt,
174     const void *arg, void *target);
175 int nlattr_get_ifp(struct nlattr *nla, struct nl_pstate *npt,
176     const void *arg, void *target);
177 int nlattr_get_ifpz(struct nlattr *nla, struct nl_pstate *npt,
178     const void *arg, void *target);
179 int nlattr_get_ipvia(struct nlattr *nla, struct nl_pstate *npt,
180     const void *arg, void *target);
181 int nlattr_get_string(struct nlattr *nla, struct nl_pstate *npt,
182     const void *arg, void *target);
183 int nlattr_get_stringn(struct nlattr *nla, struct nl_pstate *npt,
184     const void *arg, void *target);
185 int nlattr_get_nla(struct nlattr *nla, struct nl_pstate *npt,
186     const void *arg, void *target);
187 int nlattr_get_nested(struct nlattr *nla, struct nl_pstate *npt,
188     const void *arg, void *target);
189 
190 bool nlmsg_report_err_msg(struct nl_pstate *npt, const char *fmt, ...);
191 
192 #define	NLMSG_REPORT_ERR_MSG(_npt, _fmt, ...) {	\
193 	nlmsg_report_err_msg(_npt, _fmt, ## __VA_ARGS__); \
194 	NLP_LOG(LOG_DEBUG, (_npt)->nlp, _fmt, ## __VA_ARGS__); \
195 }
196 
197 bool nlmsg_report_err_offset(struct nl_pstate *npt, uint32_t off);
198 
199 /*
200  * Have it inline so compiler can optimize field accesses into
201  * the list of direct function calls without iteration.
202  */
203 static inline int
204 nl_parse_header(void *hdr, int len, const struct nlhdr_parser *parser,
205     struct nl_pstate *npt, void *target)
206 {
207 	int error;
208 
209 	if (__predict_false(len < parser->nl_hdr_off)) {
210 		nlmsg_report_err_msg(npt, "header too short: expected %d, got %d",
211 		    parser->nl_hdr_off, len);
212 		return (EINVAL);
213 	}
214 
215 	if (npt->strict && parser->sp != NULL && !parser->sp(hdr, npt))
216 		return (EINVAL);
217 
218 	/* Extract fields first */
219 	for (int i = 0; i < parser->fp_size; i++) {
220 		const struct nlfield_parser *fp = &parser->fp[i];
221 		void *src = (char *)hdr + fp->off_in;
222 		void *dst = (char *)target + fp->off_out;
223 
224 		error = fp->cb(src, npt, dst);
225 		if (error != 0)
226 			return (error);
227 	}
228 
229 	struct nlattr *nla_head = (struct nlattr *)((char *)hdr + parser->nl_hdr_off);
230 	error = nl_parse_attrs_raw(nla_head, len - parser->nl_hdr_off, parser->np,
231 	    parser->np_size, npt, target);
232 
233 	return (error);
234 }
235 
236 static inline int
237 nl_parse_nested(struct nlattr *nla, const struct nlhdr_parser *parser,
238     struct nl_pstate *npt, void *target)
239 {
240 	struct nlattr *nla_head = (struct nlattr *)NLA_DATA(nla);
241 
242 	return (nl_parse_attrs_raw(nla_head, NLA_DATA_LEN(nla), parser->np,
243 	    parser->np_size, npt, target));
244 }
245 
246 /*
247  * Checks that attributes are sorted by attribute type.
248  */
249 static inline void
250 nl_verify_parsers(const struct nlhdr_parser **parser, int count)
251 {
252 #ifdef INVARIANTS
253 	for (int i = 0; i < count; i++) {
254 		const struct nlhdr_parser *p = parser[i];
255 		int attr_type = 0;
256 		for (int j = 0; j < p->np_size; j++) {
257 			MPASS(p->np[j].type > attr_type);
258 			attr_type = p->np[j].type;
259 		}
260 	}
261 #endif
262 }
263 void nl_verify_parsers(const struct nlhdr_parser **parser, int count);
264 #define	NL_VERIFY_PARSERS(_p)	nl_verify_parsers((_p), NL_ARRAY_LEN(_p))
265 
266 static inline int
267 nl_parse_nlmsg(struct nlmsghdr *hdr, const struct nlhdr_parser *parser,
268     struct nl_pstate *npt, void *target)
269 {
270 	return (nl_parse_header(hdr + 1, hdr->nlmsg_len - sizeof(*hdr), parser, npt, target));
271 }
272 
273 #endif
274 #endif
275