Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
freebsd
GitHub Repository: freebsd/freebsd-src
Path: blob/main/sys/netlink/netlink_message_writer.h
104901 views
1
/*-
2
* SPDX-License-Identifier: BSD-2-Clause
3
*
4
* Copyright (c) 2021 Ng Peng Nam Sean
5
* Copyright (c) 2022 Alexander V. Chernikov <[email protected]>
6
*
7
* Redistribution and use in source and binary forms, with or without
8
* modification, are permitted provided that the following conditions
9
* are met:
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.
15
*
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
26
* SUCH DAMAGE.
27
*/
28
29
#ifndef _NETLINK_NETLINK_MESSAGE_WRITER_H_
30
#define _NETLINK_NETLINK_MESSAGE_WRITER_H_
31
32
#ifdef _KERNEL
33
34
#include <netinet/in.h>
35
36
/*
37
* It is not meant to be included directly
38
*/
39
40
struct nl_buf;
41
struct nl_writer;
42
struct ifnet;
43
typedef bool nl_writer_cb(struct nl_writer *nw);
44
45
struct nl_writer {
46
struct nl_buf *buf; /* Underlying storage pointer */
47
struct nlmsghdr *hdr; /* Pointer to the currently-filled msg */
48
nl_writer_cb *cb; /* Callback to flush data */
49
union {
50
struct nlpcb *nlp;
51
struct {
52
uint16_t proto;
53
uint16_t id;
54
int priv;
55
} group;
56
};
57
const struct ifnet *ifp; /* Used by Linux translation only */
58
u_int num_messages; /* Number of messages in the buffer */
59
int malloc_flag; /* M_WAITOK or M_NOWAIT */
60
bool ignore_limit; /* If true, ignores RCVBUF limit */
61
bool enomem; /* True if ENOMEM occured */
62
bool suppress_ack; /* If true, don't send NLMSG_ERR */
63
};
64
65
#define NLMSG_SMALL 128
66
#define NLMSG_LARGE 2048
67
68
/* Message and attribute writing */
69
#if defined(NETLINK) || defined(NETLINK_MODULE)
70
/* Provide optimized calls to the functions inside the same linking unit */
71
72
bool _nl_writer_unicast(struct nl_writer *, size_t, struct nlpcb *nlp, bool);
73
bool _nl_writer_group(struct nl_writer *, size_t, uint16_t, uint16_t, int,
74
bool);
75
bool _nlmsg_flush(struct nl_writer *nw);
76
void _nlmsg_ignore_limit(struct nl_writer *nw);
77
78
bool _nlmsg_refill_buffer(struct nl_writer *nw, size_t required_len);
79
bool _nlmsg_add(struct nl_writer *nw, uint32_t portid, uint32_t seq,
80
uint16_t type, uint16_t flags, uint32_t len);
81
bool _nlmsg_end(struct nl_writer *nw);
82
void _nlmsg_abort(struct nl_writer *nw);
83
84
bool _nlmsg_end_dump(struct nl_writer *nw, int error, struct nlmsghdr *hdr);
85
86
87
static inline bool
88
nl_writer_unicast(struct nl_writer *nw, size_t size, struct nlpcb *nlp,
89
bool waitok)
90
{
91
return (_nl_writer_unicast(nw, size, nlp, waitok));
92
}
93
94
static inline bool
95
nl_writer_group(struct nl_writer *nw, size_t size, uint16_t proto,
96
uint16_t group_id, int priv, bool waitok)
97
{
98
return (_nl_writer_group(nw, size, proto, group_id, priv, waitok));
99
}
100
101
static inline bool
102
nlmsg_flush(struct nl_writer *nw)
103
{
104
return (_nlmsg_flush(nw));
105
}
106
107
static inline void
108
nlmsg_ignore_limit(struct nl_writer *nw)
109
{
110
_nlmsg_ignore_limit(nw);
111
}
112
113
static inline bool
114
nlmsg_refill_buffer(struct nl_writer *nw, size_t required_size)
115
{
116
return (_nlmsg_refill_buffer(nw, required_size));
117
}
118
119
static inline bool
120
nlmsg_add(struct nl_writer *nw, uint32_t portid, uint32_t seq, uint16_t type,
121
uint16_t flags, uint32_t len)
122
{
123
return (_nlmsg_add(nw, portid, seq, type, flags, len));
124
}
125
126
static inline bool
127
nlmsg_end(struct nl_writer *nw)
128
{
129
return (_nlmsg_end(nw));
130
}
131
132
static inline void
133
nlmsg_abort(struct nl_writer *nw)
134
{
135
return (_nlmsg_abort(nw));
136
}
137
138
static inline bool
139
nlmsg_end_dump(struct nl_writer *nw, int error, struct nlmsghdr *hdr)
140
{
141
return (_nlmsg_end_dump(nw, error, hdr));
142
}
143
144
#else
145
/* Provide access to the functions via netlink_glue.c */
146
147
bool nl_writer_unicast(struct nl_writer *, size_t, struct nlpcb *, bool waitok);
148
bool nl_writer_group(struct nl_writer *, size_t, uint16_t, uint16_t, int,
149
bool waitok);
150
bool nlmsg_flush(struct nl_writer *nw);
151
void nlmsg_ignore_limit(struct nl_writer *nw);
152
153
bool nlmsg_refill_buffer(struct nl_writer *nw, size_t required_size);
154
bool nlmsg_add(struct nl_writer *nw, uint32_t portid, uint32_t seq,
155
uint16_t type, uint16_t flags, uint32_t len);
156
bool nlmsg_end(struct nl_writer *nw);
157
void nlmsg_abort(struct nl_writer *nw);
158
159
bool nlmsg_end_dump(struct nl_writer *nw, int error, struct nlmsghdr *hdr);
160
161
#endif /* defined(NETLINK) || defined(NETLINK_MODULE) */
162
163
static inline bool
164
nlmsg_reply(struct nl_writer *nw, const struct nlmsghdr *hdr, int payload_len)
165
{
166
return (nlmsg_add(nw, hdr->nlmsg_pid, hdr->nlmsg_seq, hdr->nlmsg_type,
167
hdr->nlmsg_flags, payload_len));
168
}
169
170
/*
171
* KPI similar to mtodo():
172
* current (uncompleted) header is guaranteed to be contiguous,
173
* but can be reallocated, thus pointers may need to be readjusted.
174
*/
175
u_int nlattr_save_offset(const struct nl_writer *nw);
176
177
static inline void *
178
_nlattr_restore_offset(const struct nl_writer *nw, int off)
179
{
180
return ((void *)((char *)nw->hdr + off));
181
}
182
#define nlattr_restore_offset(_ns, _off, _t) ((_t *)_nlattr_restore_offset(_ns, _off))
183
184
static inline void
185
nlattr_set_len(const struct nl_writer *nw, int off)
186
{
187
struct nlattr *nla = nlattr_restore_offset(nw, off, struct nlattr);
188
nla->nla_len = nlattr_save_offset(nw) - off;
189
}
190
191
void *nlmsg_reserve_data_raw(struct nl_writer *nw, size_t sz);
192
#define nlmsg_reserve_object(_ns, _t) ((_t *)nlmsg_reserve_data_raw(_ns, sizeof(_t)))
193
#define nlmsg_reserve_data(_ns, _sz, _t) ((_t *)nlmsg_reserve_data_raw(_ns, _sz))
194
195
static inline int
196
nlattr_add_nested(struct nl_writer *nw, uint16_t nla_type)
197
{
198
int off = nlattr_save_offset(nw);
199
struct nlattr *nla = nlmsg_reserve_data(nw, sizeof(struct nlattr), struct nlattr);
200
if (__predict_false(nla == NULL))
201
return (0);
202
nla->nla_type = nla_type;
203
return (off);
204
}
205
206
static inline void *
207
_nlmsg_reserve_attr(struct nl_writer *nw, uint16_t nla_type, uint16_t sz)
208
{
209
sz += sizeof(struct nlattr);
210
211
struct nlattr *nla = nlmsg_reserve_data(nw, sz, struct nlattr);
212
if (__predict_false(nla == NULL))
213
return (NULL);
214
nla->nla_type = nla_type;
215
nla->nla_len = sz;
216
217
return ((void *)(nla + 1));
218
}
219
#define nlmsg_reserve_attr(_ns, _at, _t) ((_t *)_nlmsg_reserve_attr(_ns, _at, NLA_ALIGN(sizeof(_t))))
220
221
bool nlattr_add(struct nl_writer *nw, uint16_t attr_type, uint16_t attr_len,
222
const void *data);
223
224
static inline bool
225
nlattr_add_raw(struct nl_writer *nw, const struct nlattr *nla_src)
226
{
227
MPASS(nla_src->nla_len >= sizeof(struct nlattr));
228
229
return (nlattr_add(nw, nla_src->nla_type,
230
nla_src->nla_len - sizeof(struct nlattr),
231
(const void *)(nla_src + 1)));
232
}
233
234
static inline bool
235
nlattr_add_bool(struct nl_writer *nw, uint16_t attrtype, bool value)
236
{
237
return (nlattr_add(nw, attrtype, sizeof(bool), &value));
238
}
239
240
static inline bool
241
nlattr_add_u8(struct nl_writer *nw, uint16_t attrtype, uint8_t value)
242
{
243
return (nlattr_add(nw, attrtype, sizeof(uint8_t), &value));
244
}
245
246
static inline bool
247
nlattr_add_u16(struct nl_writer *nw, uint16_t attrtype, uint16_t value)
248
{
249
return (nlattr_add(nw, attrtype, sizeof(uint16_t), &value));
250
}
251
252
static inline bool
253
nlattr_add_u32(struct nl_writer *nw, uint16_t attrtype, uint32_t value)
254
{
255
return (nlattr_add(nw, attrtype, sizeof(uint32_t), &value));
256
}
257
258
static inline bool
259
nlattr_add_u64(struct nl_writer *nw, uint16_t attrtype, uint64_t value)
260
{
261
return (nlattr_add(nw, attrtype, sizeof(uint64_t), &value));
262
}
263
264
static inline bool
265
nlattr_add_s8(struct nl_writer *nw, uint16_t attrtype, int8_t value)
266
{
267
return (nlattr_add(nw, attrtype, sizeof(int8_t), &value));
268
}
269
270
static inline bool
271
nlattr_add_s16(struct nl_writer *nw, uint16_t attrtype, int16_t value)
272
{
273
return (nlattr_add(nw, attrtype, sizeof(int16_t), &value));
274
}
275
276
static inline bool
277
nlattr_add_s32(struct nl_writer *nw, uint16_t attrtype, int32_t value)
278
{
279
return (nlattr_add(nw, attrtype, sizeof(int32_t), &value));
280
}
281
282
static inline bool
283
nlattr_add_s64(struct nl_writer *nw, uint16_t attrtype, int64_t value)
284
{
285
return (nlattr_add(nw, attrtype, sizeof(int64_t), &value));
286
}
287
288
static inline bool
289
nlattr_add_time_t(struct nl_writer *nw, uint16_t attrtype, time_t value)
290
{
291
return (nlattr_add(nw, attrtype, sizeof(time_t), &value));
292
}
293
294
static inline bool
295
nlattr_add_flag(struct nl_writer *nw, uint16_t attrtype)
296
{
297
return (nlattr_add(nw, attrtype, 0, NULL));
298
}
299
300
static inline bool
301
nlattr_add_string(struct nl_writer *nw, uint16_t attrtype, const char *str)
302
{
303
return (nlattr_add(nw, attrtype, strlen(str) + 1, str));
304
}
305
306
static inline bool
307
nlattr_add_in_addr(struct nl_writer *nw, uint16_t attrtype,
308
const struct in_addr *in)
309
{
310
return (nlattr_add(nw, attrtype, sizeof(*in), in));
311
}
312
313
static inline bool
314
nlattr_add_in6_addr(struct nl_writer *nw, uint16_t attrtype,
315
const struct in6_addr *in6)
316
{
317
return (nlattr_add(nw, attrtype, sizeof(*in6), in6));
318
}
319
#endif
320
#endif
321
322