2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2022 Alexander V. Chernikov <melifaro@FreeBSD.org>
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
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.
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
28 #ifndef _NETLINK_NETLINK_MESSAGE_PARSER_H_
29 #define _NETLINK_NETLINK_MESSAGE_PARSER_H_
33 #include <sys/bitset.h>
36 * It is not meant to be included directly
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 */
47 lb_alloc(struct linear_buffer *lb, int len)
49 len = roundup2(len, sizeof(uint64_t));
50 if (lb->offset + len > lb->size)
52 void *data = (void *)(lb->base + lb->offset);
58 lb_clear(struct linear_buffer *lb)
60 memset(lb->base, 0, lb->size);
64 #define NL_MAX_ERROR_BUF 128
65 #define SCRATCH_BUFFER_SIZE (1024 + NL_MAX_ERROR_BUF)
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 bool strict; /* Strict parsing required */
78 npt_alloc(struct nl_pstate *npt, int len)
80 return (lb_alloc(&npt->lb, len));
82 #define npt_alloc_sockaddr(_npt, _len) ((struct sockaddr *)(npt_alloc(_npt, _len)))
84 typedef int parse_field_f(void *hdr, struct nl_pstate *npt,
86 struct nlfield_parser {
91 static const struct nlfield_parser nlf_p_empty[] = {};
93 int nlf_get_ifp(void *src, struct nl_pstate *npt, void *target);
94 int nlf_get_ifpz(void *src, struct nl_pstate *npt, void *target);
95 int nlf_get_u8(void *src, struct nl_pstate *npt, void *target);
96 int nlf_get_u16(void *src, struct nl_pstate *npt, void *target);
97 int nlf_get_u32(void *src, struct nl_pstate *npt, void *target);
98 int nlf_get_u8_u32(void *src, struct nl_pstate *npt, void *target);
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 */
111 typedef bool strict_parser_f(void *hdr, struct nl_pstate *npt);
113 struct nlhdr_parser {
114 int nl_hdr_off; /* aligned netlink header size */
115 int out_hdr_off; /* target header size */
118 const struct nlfield_parser *fp; /* array of header field parsers */
119 const struct nlattr_parser *np; /* array of attribute parsers */
120 strict_parser_f *sp; /* Parser function */
123 #define NL_DECLARE_PARSER(_name, _t, _fp, _np) \
124 static const struct nlhdr_parser _name = { \
125 .nl_hdr_off = sizeof(_t), \
128 .fp_size = NL_ARRAY_LEN(_fp), \
129 .np_size = NL_ARRAY_LEN(_np), \
132 #define NL_DECLARE_STRICT_PARSER(_name, _t, _sp, _fp, _np)\
133 static const struct nlhdr_parser _name = { \
134 .nl_hdr_off = sizeof(_t), \
137 .fp_size = NL_ARRAY_LEN(_fp), \
138 .np_size = NL_ARRAY_LEN(_np), \
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), \
148 .fp_size = NL_ARRAY_LEN(_fp), \
149 .np_size = NL_ARRAY_LEN(_np), \
152 #define NL_DECLARE_ATTR_PARSER(_name, _np) \
153 static const struct nlhdr_parser _name = { \
155 .np_size = NL_ARRAY_LEN(_np), \
158 #define NL_ATTR_BMASK_SIZE 128
159 BITSET_DEFINE(nlattr_bmask, NL_ATTR_BMASK_SIZE);
161 void nl_get_attrs_bmask_raw(struct nlattr *nla_head, int len, struct nlattr_bmask *bm);
162 bool nl_has_attr(const struct nlattr_bmask *bm, unsigned int nla_type);
164 int nl_parse_attrs_raw(struct nlattr *nla_head, int len, const struct nlattr_parser *ps,
165 int pslen, struct nl_pstate *npt, void *target);
167 int nlattr_get_flag(struct nlattr *nla, struct nl_pstate *npt,
168 const void *arg, void *target);
169 int nlattr_get_ip(struct nlattr *nla, struct nl_pstate *npt,
170 const void *arg, void *target);
171 int nlattr_get_uint16(struct nlattr *nla, struct nl_pstate *npt,
172 const void *arg, void *target);
173 int nlattr_get_uint32(struct nlattr *nla, struct nl_pstate *npt,
174 const void *arg, void *target);
175 int nlattr_get_uint64(struct nlattr *nla, struct nl_pstate *npt,
176 const void *arg, void *target);
177 int nlattr_get_ifp(struct nlattr *nla, struct nl_pstate *npt,
178 const void *arg, void *target);
179 int nlattr_get_ifpz(struct nlattr *nla, struct nl_pstate *npt,
180 const void *arg, void *target);
181 int nlattr_get_ipvia(struct nlattr *nla, struct nl_pstate *npt,
182 const void *arg, void *target);
183 int nlattr_get_string(struct nlattr *nla, struct nl_pstate *npt,
184 const void *arg, void *target);
185 int nlattr_get_stringn(struct nlattr *nla, struct nl_pstate *npt,
186 const void *arg, void *target);
187 int nlattr_get_nla(struct nlattr *nla, struct nl_pstate *npt,
188 const void *arg, void *target);
189 int nlattr_get_nested(struct nlattr *nla, struct nl_pstate *npt,
190 const void *arg, void *target);
192 bool nlmsg_report_err_msg(struct nl_pstate *npt, const char *fmt, ...);
194 #define NLMSG_REPORT_ERR_MSG(_npt, _fmt, ...) { \
195 nlmsg_report_err_msg(_npt, _fmt, ## __VA_ARGS__); \
196 NLP_LOG(LOG_DEBUG, (_npt)->nlp, _fmt, ## __VA_ARGS__); \
199 bool nlmsg_report_err_offset(struct nl_pstate *npt, uint32_t off);
202 * Have it inline so compiler can optimize field accesses into
203 * the list of direct function calls without iteration.
206 nl_parse_header(void *hdr, int len, const struct nlhdr_parser *parser,
207 struct nl_pstate *npt, void *target)
211 if (__predict_false(len < parser->nl_hdr_off)) {
213 nlmsg_report_err_msg(npt, "header too short: expected %d, got %d",
214 parser->nl_hdr_off, len);
218 /* Compat with older applications: pretend there's a full header */
219 void *tmp_hdr = npt_alloc(npt, parser->nl_hdr_off);
222 memcpy(tmp_hdr, hdr, len);
224 len = parser->nl_hdr_off;
227 if (npt->strict && parser->sp != NULL && !parser->sp(hdr, npt))
230 /* Extract fields first */
231 for (int i = 0; i < parser->fp_size; i++) {
232 const struct nlfield_parser *fp = &parser->fp[i];
233 void *src = (char *)hdr + fp->off_in;
234 void *dst = (char *)target + fp->off_out;
236 error = fp->cb(src, npt, dst);
241 struct nlattr *nla_head = (struct nlattr *)((char *)hdr + parser->nl_hdr_off);
242 error = nl_parse_attrs_raw(nla_head, len - parser->nl_hdr_off, parser->np,
243 parser->np_size, npt, target);
249 nl_parse_nested(struct nlattr *nla, const struct nlhdr_parser *parser,
250 struct nl_pstate *npt, void *target)
252 struct nlattr *nla_head = (struct nlattr *)NLA_DATA(nla);
254 return (nl_parse_attrs_raw(nla_head, NLA_DATA_LEN(nla), parser->np,
255 parser->np_size, npt, target));
259 * Checks that attributes are sorted by attribute type.
262 nl_verify_parsers(const struct nlhdr_parser **parser, int count)
265 for (int i = 0; i < count; i++) {
266 const struct nlhdr_parser *p = parser[i];
268 for (int j = 0; j < p->np_size; j++) {
269 MPASS(p->np[j].type > attr_type);
270 attr_type = p->np[j].type;
275 void nl_verify_parsers(const struct nlhdr_parser **parser, int count);
276 #define NL_VERIFY_PARSERS(_p) nl_verify_parsers((_p), NL_ARRAY_LEN(_p))
279 nl_parse_nlmsg(struct nlmsghdr *hdr, const struct nlhdr_parser *parser,
280 struct nl_pstate *npt, void *target)
282 return (nl_parse_header(hdr + 1, hdr->nlmsg_len - sizeof(*hdr), parser, npt, target));
286 nl_get_attrs_bmask_nlmsg(struct nlmsghdr *hdr, const struct nlhdr_parser *parser,
287 struct nlattr_bmask *bm)
289 struct nlattr *nla_head;
291 nla_head = (struct nlattr *)((char *)(hdr + 1) + parser->nl_hdr_off);
292 int len = hdr->nlmsg_len - sizeof(*hdr) - parser->nl_hdr_off;
294 nl_get_attrs_bmask_raw(nla_head, len, bm);