2 * SPDX-License-Identifier: BSD-2-Clause
4 * Copyright (c) 2021 Ng Peng Nam Sean
5 * Copyright (c) 2022 Alexander V. Chernikov <melifaro@FreeBSD.org>
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 #ifndef _NETLINK_NETLINK_MESSAGE_WRITER_H_
30 #define _NETLINK_NETLINK_MESSAGE_WRITER_H_
34 #include <netinet/in.h>
37 * It is not meant to be included directly
42 typedef bool nl_writer_cb(struct nl_writer *nw, void *buf, int buflen, int cnt);
45 int alloc_len; /* allocated buffer length */
46 int offset; /* offset from the start of the buffer */
47 struct nlmsghdr *hdr; /* Pointer to the currently-filled msg */
48 char *data; /* pointer to the contiguous storage */
49 void *_storage; /* Underlying storage pointer */
50 nl_writer_cb *cb; /* Callback to flush data */
58 int num_messages; /* Number of messages in the buffer */
59 int malloc_flag; /* M_WAITOK or M_NOWAIT */
60 uint8_t writer_type; /* NS_WRITER_TYPE_* */
61 uint8_t writer_target; /* NS_WRITER_TARGET_* */
62 bool ignore_limit; /* If true, ignores RCVBUF limit */
63 bool enomem; /* True if ENOMEM occured */
64 bool suppress_ack; /* If true, don't send NLMSG_ERR */
66 #define NS_WRITER_TARGET_SOCKET 0
67 #define NS_WRITER_TARGET_GROUP 1
68 #define NS_WRITER_TARGET_CHAIN 2
70 #define NS_WRITER_TYPE_MBUF 0
71 #define NS_WRITER_TYPE_BUF 1
72 #define NS_WRITER_TYPE_LBUF 2
73 #define NS_WRITER_TYPE_MBUFC 3
74 #define NS_WRITER_TYPE_STUB 4
77 #define NLMSG_SMALL 128
78 #define NLMSG_LARGE 2048
80 /* Message and attribute writing */
84 #if defined(NETLINK) || defined(NETLINK_MODULE)
85 /* Provide optimized calls to the functions inside the same linking unit */
87 bool _nlmsg_get_unicast_writer(struct nl_writer *nw, int expected_size, struct nlpcb *nlp);
88 bool _nlmsg_get_group_writer(struct nl_writer *nw, int expected_size, int proto, int group_id);
89 bool _nlmsg_get_chain_writer(struct nl_writer *nw, int expected_size, struct mbuf **pm);
90 bool _nlmsg_flush(struct nl_writer *nw);
91 void _nlmsg_ignore_limit(struct nl_writer *nw);
93 bool _nlmsg_refill_buffer(struct nl_writer *nw, int required_size);
94 bool _nlmsg_add(struct nl_writer *nw, uint32_t portid, uint32_t seq, uint16_t type,
95 uint16_t flags, uint32_t len);
96 bool _nlmsg_end(struct nl_writer *nw);
97 void _nlmsg_abort(struct nl_writer *nw);
99 bool _nlmsg_end_dump(struct nl_writer *nw, int error, struct nlmsghdr *hdr);
103 nlmsg_get_unicast_writer(struct nl_writer *nw, int expected_size, struct nlpcb *nlp)
105 return (_nlmsg_get_unicast_writer(nw, expected_size, nlp));
109 nlmsg_get_group_writer(struct nl_writer *nw, int expected_size, int proto, int group_id)
111 return (_nlmsg_get_group_writer(nw, expected_size, proto, group_id));
115 nlmsg_get_chain_writer(struct nl_writer *nw, int expected_size, struct mbuf **pm)
117 return (_nlmsg_get_chain_writer(nw, expected_size, pm));
121 nlmsg_flush(struct nl_writer *nw)
123 return (_nlmsg_flush(nw));
127 nlmsg_ignore_limit(struct nl_writer *nw)
129 _nlmsg_ignore_limit(nw);
133 nlmsg_refill_buffer(struct nl_writer *nw, int required_size)
135 return (_nlmsg_refill_buffer(nw, required_size));
139 nlmsg_add(struct nl_writer *nw, uint32_t portid, uint32_t seq, uint16_t type,
140 uint16_t flags, uint32_t len)
142 return (_nlmsg_add(nw, portid, seq, type, flags, len));
146 nlmsg_end(struct nl_writer *nw)
148 return (_nlmsg_end(nw));
152 nlmsg_abort(struct nl_writer *nw)
154 return (_nlmsg_abort(nw));
158 nlmsg_end_dump(struct nl_writer *nw, int error, struct nlmsghdr *hdr)
160 return (_nlmsg_end_dump(nw, error, hdr));
164 /* Provide access to the functions via netlink_glue.c */
166 bool nlmsg_get_unicast_writer(struct nl_writer *nw, int expected_size, struct nlpcb *nlp);
167 bool nlmsg_get_group_writer(struct nl_writer *nw, int expected_size, int proto, int group_id);
168 bool nlmsg_get_chain_writer(struct nl_writer *nw, int expected_size, struct mbuf **pm);
169 bool nlmsg_flush(struct nl_writer *nw);
170 void nlmsg_ignore_limit(struct nl_writer *nw);
172 bool nlmsg_refill_buffer(struct nl_writer *nw, int required_size);
173 bool nlmsg_add(struct nl_writer *nw, uint32_t portid, uint32_t seq, uint16_t type,
174 uint16_t flags, uint32_t len);
175 bool nlmsg_end(struct nl_writer *nw);
176 void nlmsg_abort(struct nl_writer *nw);
178 bool nlmsg_end_dump(struct nl_writer *nw, int error, struct nlmsghdr *hdr);
180 #endif /* defined(NETLINK) || defined(NETLINK_MODULE) */
183 nlmsg_reply(struct nl_writer *nw, const struct nlmsghdr *hdr, int payload_len)
185 return (nlmsg_add(nw, hdr->nlmsg_pid, hdr->nlmsg_seq, hdr->nlmsg_type,
186 hdr->nlmsg_flags, payload_len));
189 #define nlmsg_data(_hdr) ((void *)((_hdr) + 1))
192 * KPI similar to mtodo():
193 * current (uncompleted) header is guaranteed to be contiguous,
194 * but can be reallocated, thus pointers may need to be readjusted.
197 nlattr_save_offset(const struct nl_writer *nw)
199 return (nw->offset - ((char *)nw->hdr - nw->data));
203 _nlattr_restore_offset(const struct nl_writer *nw, int off)
205 return ((void *)((char *)nw->hdr + off));
207 #define nlattr_restore_offset(_ns, _off, _t) ((_t *)_nlattr_restore_offset(_ns, _off))
210 nlattr_set_len(const struct nl_writer *nw, int off)
212 struct nlattr *nla = nlattr_restore_offset(nw, off, struct nlattr);
213 nla->nla_len = nlattr_save_offset(nw) - off;
217 nlmsg_reserve_data_raw(struct nl_writer *nw, size_t sz)
219 sz = NETLINK_ALIGN(sz);
221 if (__predict_false(nw->offset + sz > nw->alloc_len)) {
222 if (!nlmsg_refill_buffer(nw, sz))
226 void *data_ptr = &nw->data[nw->offset];
232 #define nlmsg_reserve_object(_ns, _t) ((_t *)nlmsg_reserve_data_raw(_ns, sizeof(_t)))
233 #define nlmsg_reserve_data(_ns, _sz, _t) ((_t *)nlmsg_reserve_data_raw(_ns, _sz))
236 nlattr_add_nested(struct nl_writer *nw, uint16_t nla_type)
238 int off = nlattr_save_offset(nw);
239 struct nlattr *nla = nlmsg_reserve_data(nw, sizeof(struct nlattr), struct nlattr);
240 if (__predict_false(nla == NULL))
242 nla->nla_type = nla_type;
247 _nlmsg_reserve_attr(struct nl_writer *nw, uint16_t nla_type, uint16_t sz)
249 sz += sizeof(struct nlattr);
251 struct nlattr *nla = nlmsg_reserve_data(nw, sz, struct nlattr);
252 if (__predict_false(nla == NULL))
254 nla->nla_type = nla_type;
257 return ((void *)(nla + 1));
259 #define nlmsg_reserve_attr(_ns, _at, _t) ((_t *)_nlmsg_reserve_attr(_ns, _at, NLA_ALIGN(sizeof(_t))))
262 nlattr_add(struct nl_writer *nw, int attr_type, int attr_len, const void *data)
264 int required_len = NLA_ALIGN(attr_len + sizeof(struct nlattr));
266 if (__predict_false(nw->offset + required_len > nw->alloc_len)) {
267 if (!nlmsg_refill_buffer(nw, required_len))
271 struct nlattr *nla = (struct nlattr *)(&nw->data[nw->offset]);
273 nla->nla_len = attr_len + sizeof(struct nlattr);
274 nla->nla_type = attr_type;
276 if ((attr_len % 4) != 0) {
277 /* clear padding bytes */
278 bzero((char *)nla + required_len - 4, 4);
280 memcpy((nla + 1), data, attr_len);
282 nw->offset += required_len;
287 nlattr_add_raw(struct nl_writer *nw, const struct nlattr *nla_src)
289 int attr_len = nla_src->nla_len - sizeof(struct nlattr);
291 MPASS(attr_len >= 0);
293 return (nlattr_add(nw, nla_src->nla_type, attr_len, (const void *)(nla_src + 1)));
297 nlattr_add_u8(struct nl_writer *nw, int attrtype, uint8_t value)
299 return (nlattr_add(nw, attrtype, sizeof(uint8_t), &value));
303 nlattr_add_u16(struct nl_writer *nw, int attrtype, uint16_t value)
305 return (nlattr_add(nw, attrtype, sizeof(uint16_t), &value));
309 nlattr_add_u32(struct nl_writer *nw, int attrtype, uint32_t value)
311 return (nlattr_add(nw, attrtype, sizeof(uint32_t), &value));
315 nlattr_add_u64(struct nl_writer *nw, int attrtype, uint64_t value)
317 return (nlattr_add(nw, attrtype, sizeof(uint64_t), &value));
321 nlattr_add_s8(struct nl_writer *nw, int attrtype, int8_t value)
323 return (nlattr_add(nw, attrtype, sizeof(int8_t), &value));
327 nlattr_add_s16(struct nl_writer *nw, int attrtype, int16_t value)
329 return (nlattr_add(nw, attrtype, sizeof(int16_t), &value));
333 nlattr_add_s32(struct nl_writer *nw, int attrtype, int32_t value)
335 return (nlattr_add(nw, attrtype, sizeof(int32_t), &value));
339 nlattr_add_s64(struct nl_writer *nw, int attrtype, int64_t value)
341 return (nlattr_add(nw, attrtype, sizeof(int64_t), &value));
345 nlattr_add_flag(struct nl_writer *nw, int attrtype)
347 return (nlattr_add(nw, attrtype, 0, NULL));
351 nlattr_add_string(struct nl_writer *nw, int attrtype, const char *str)
353 return (nlattr_add(nw, attrtype, strlen(str) + 1, str));
357 nlattr_add_in_addr(struct nl_writer *nw, int attrtype, const struct in_addr *in)
359 return (nlattr_add(nw, attrtype, sizeof(*in), in));
363 nlattr_add_in6_addr(struct nl_writer *nw, int attrtype, const struct in6_addr *in6)
365 return (nlattr_add(nw, attrtype, sizeof(*in6), in6));