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 #include <sys/cdefs.h>
29 #include <sys/param.h>
30 #include <sys/malloc.h>
31 #include <sys/lock.h>
32 #include <sys/rmlock.h>
33 #include <sys/mbuf.h>
34 #include <sys/ck.h>
35 #include <sys/socket.h>
36 #include <sys/socketvar.h>
37 #include <sys/syslog.h>
38
39 #include <netlink/netlink.h>
40 #include <netlink/netlink_ctl.h>
41 #include <netlink/netlink_linux.h>
42 #include <netlink/netlink_var.h>
43
44 #define DEBUG_MOD_NAME nl_writer
45 #define DEBUG_MAX_LEVEL LOG_DEBUG3
46 #include <netlink/netlink_debug.h>
47 _DECLARE_DEBUG(LOG_DEBUG);
48
49 /*
50 * The goal of this file is to provide convenient message writing KPI on top of
51 * different storage methods (mbufs, uio, temporary memory chunks).
52 *
53 * The main KPI guarantee is the the (last) message always resides in the contiguous
54 * memory buffer, so one is able to update the header after writing the entire message.
55 *
56 * This guarantee comes with a side effect of potentially reallocating underlying
57 * buffer, so one needs to update the desired pointers after something is added
58 * to the header.
59 *
60 * Messaging layer contains hooks performing transparent Linux translation for the messages.
61 *
62 * There are 3 types of supported targets:
63 * * socket (adds mbufs to the socket buffer, used for message replies)
64 * * group (sends mbuf/chain to the specified groups, used for the notifications)
65 * * chain (returns mbuf chain, used in Linux message translation code)
66 *
67 * There are 3 types of storage:
68 * * NS_WRITER_TYPE_MBUF (mbuf-based, most efficient, used when a single message
69 * fits in MCLBYTES)
70 * * NS_WRITER_TYPE_BUF (fallback, malloc-based, used when a single message needs
71 * to be larger than one supported by NS_WRITER_TYPE_MBUF)
72 * * NS_WRITER_TYPE_LBUF (malloc-based, similar to NS_WRITER_TYPE_BUF, used for
73 * Linux sockets, calls translation hook prior to sending messages to the socket).
74 *
75 * Internally, KPI switches between different types of storage when memory requirements
76 * change. It happens transparently to the caller.
77 */
78
79
80 typedef bool nlwriter_op_init(struct nl_writer *nw, int size, bool waitok);
81 typedef bool nlwriter_op_write(struct nl_writer *nw, void *buf, int buflen, int cnt);
82
83 struct nlwriter_ops {
84 nlwriter_op_init *init;
85 nlwriter_op_write *write_socket;
86 nlwriter_op_write *write_group;
87 nlwriter_op_write *write_chain;
88 };
89
90 /*
91 * NS_WRITER_TYPE_BUF
92 * Writes message to a temporary memory buffer,
93 * flushing to the socket/group when buffer size limit is reached
94 */
95 static bool
nlmsg_get_ns_buf(struct nl_writer * nw,int size,bool waitok)96 nlmsg_get_ns_buf(struct nl_writer *nw, int size, bool waitok)
97 {
98 int mflag = waitok ? M_WAITOK : M_NOWAIT;
99 nw->_storage = malloc(size, M_NETLINK, mflag | M_ZERO);
100 if (__predict_false(nw->_storage == NULL))
101 return (false);
102 nw->alloc_len = size;
103 nw->offset = 0;
104 nw->hdr = NULL;
105 nw->data = nw->_storage;
106 nw->writer_type = NS_WRITER_TYPE_BUF;
107 nw->malloc_flag = mflag;
108 nw->num_messages = 0;
109 nw->enomem = false;
110 return (true);
111 }
112
113 static bool
nlmsg_write_socket_buf(struct nl_writer * nw,void * buf,int datalen,int cnt)114 nlmsg_write_socket_buf(struct nl_writer *nw, void *buf, int datalen, int cnt)
115 {
116 NL_LOG(LOG_DEBUG2, "IN: ptr: %p len: %d arg: %p", buf, datalen, nw);
117 if (__predict_false(datalen == 0)) {
118 free(buf, M_NETLINK);
119 return (true);
120 }
121
122 struct mbuf *m = m_getm2(NULL, datalen, nw->malloc_flag, MT_DATA, M_PKTHDR);
123 if (__predict_false(m == NULL)) {
124 /* XXX: should we set sorcverr? */
125 free(buf, M_NETLINK);
126 return (false);
127 }
128 m_append(m, datalen, buf);
129 free(buf, M_NETLINK);
130
131 int io_flags = (nw->ignore_limit) ? NL_IOF_IGNORE_LIMIT : 0;
132 return (nl_send_one(m, (struct nlpcb *)(nw->arg_ptr), cnt, io_flags));
133 }
134
135 static bool
nlmsg_write_group_buf(struct nl_writer * nw,void * buf,int datalen,int cnt)136 nlmsg_write_group_buf(struct nl_writer *nw, void *buf, int datalen, int cnt)
137 {
138 NL_LOG(LOG_DEBUG2, "IN: ptr: %p len: %d arg: %p", buf, datalen, nw->arg_ptr);
139 if (__predict_false(datalen == 0)) {
140 free(buf, M_NETLINK);
141 return (true);
142 }
143
144 struct mbuf *m = m_getm2(NULL, datalen, nw->malloc_flag, MT_DATA, M_PKTHDR);
145 if (__predict_false(m == NULL)) {
146 free(buf, M_NETLINK);
147 return (false);
148 }
149 bool success = m_append(m, datalen, buf) != 0;
150 free(buf, M_NETLINK);
151
152 if (!success)
153 return (false);
154
155 nl_send_group(m, cnt, nw->arg_uint >> 16, nw->arg_uint & 0xFFFF);
156 return (true);
157 }
158
159 static bool
nlmsg_write_chain_buf(struct nl_writer * nw,void * buf,int datalen,int cnt)160 nlmsg_write_chain_buf(struct nl_writer *nw, void *buf, int datalen, int cnt)
161 {
162 struct mbuf **m0 = (struct mbuf **)(nw->arg_ptr);
163 NL_LOG(LOG_DEBUG2, "IN: ptr: %p len: %d arg: %p", buf, datalen, nw->arg_ptr);
164
165 if (__predict_false(datalen == 0)) {
166 free(buf, M_NETLINK);
167 return (true);
168 }
169
170 if (*m0 == NULL) {
171 struct mbuf *m;
172
173 m = m_getm2(NULL, datalen, nw->malloc_flag, MT_DATA, M_PKTHDR);
174 if (__predict_false(m == NULL)) {
175 free(buf, M_NETLINK);
176 return (false);
177 }
178 *m0 = m;
179 }
180 if (__predict_false(m_append(*m0, datalen, buf) == 0)) {
181 free(buf, M_NETLINK);
182 return (false);
183 }
184 return (true);
185 }
186
187
188 /*
189 * NS_WRITER_TYPE_MBUF
190 * Writes message to the allocated mbuf,
191 * flushing to socket/group when mbuf size limit is reached.
192 * This is the most efficient mechanism as it avoids double-copying.
193 *
194 * Allocates a single mbuf suitable to store up to @size bytes of data.
195 * If size < MHLEN (around 160 bytes), allocates mbuf with pkghdr
196 * If size <= MCLBYTES (2k), allocate a single mbuf cluster
197 * Otherwise, return NULL.
198 */
199 static bool
nlmsg_get_ns_mbuf(struct nl_writer * nw,int size,bool waitok)200 nlmsg_get_ns_mbuf(struct nl_writer *nw, int size, bool waitok)
201 {
202 struct mbuf *m;
203
204 int mflag = waitok ? M_WAITOK : M_NOWAIT;
205 m = m_get2(size, mflag, MT_DATA, M_PKTHDR);
206 if (__predict_false(m == NULL))
207 return (false);
208 nw->alloc_len = M_TRAILINGSPACE(m);
209 nw->offset = 0;
210 nw->hdr = NULL;
211 nw->_storage = (void *)m;
212 nw->data = mtod(m, void *);
213 nw->writer_type = NS_WRITER_TYPE_MBUF;
214 nw->malloc_flag = mflag;
215 nw->num_messages = 0;
216 nw->enomem = false;
217 memset(nw->data, 0, size);
218 NL_LOG(LOG_DEBUG2, "alloc mbuf %p req_len %d alloc_len %d data_ptr %p",
219 m, size, nw->alloc_len, nw->data);
220 return (true);
221 }
222
223 static bool
nlmsg_write_socket_mbuf(struct nl_writer * nw,void * buf,int datalen,int cnt)224 nlmsg_write_socket_mbuf(struct nl_writer *nw, void *buf, int datalen, int cnt)
225 {
226 struct mbuf *m = (struct mbuf *)buf;
227 NL_LOG(LOG_DEBUG2, "IN: ptr: %p len: %d arg: %p", buf, datalen, nw->arg_ptr);
228
229 if (__predict_false(datalen == 0)) {
230 m_freem(m);
231 return (true);
232 }
233
234 m->m_pkthdr.len = datalen;
235 m->m_len = datalen;
236 int io_flags = (nw->ignore_limit) ? NL_IOF_IGNORE_LIMIT : 0;
237 return (nl_send_one(m, (struct nlpcb *)(nw->arg_ptr), cnt, io_flags));
238 }
239
240 static bool
nlmsg_write_group_mbuf(struct nl_writer * nw,void * buf,int datalen,int cnt)241 nlmsg_write_group_mbuf(struct nl_writer *nw, void *buf, int datalen, int cnt)
242 {
243 struct mbuf *m = (struct mbuf *)buf;
244 NL_LOG(LOG_DEBUG2, "IN: ptr: %p len: %d arg: %p", buf, datalen, nw->arg_ptr);
245
246 if (__predict_false(datalen == 0)) {
247 m_freem(m);
248 return (true);
249 }
250
251 m->m_pkthdr.len = datalen;
252 m->m_len = datalen;
253 nl_send_group(m, cnt, nw->arg_uint >> 16, nw->arg_uint & 0xFFFF);
254 return (true);
255 }
256
257 static bool
nlmsg_write_chain_mbuf(struct nl_writer * nw,void * buf,int datalen,int cnt)258 nlmsg_write_chain_mbuf(struct nl_writer *nw, void *buf, int datalen, int cnt)
259 {
260 struct mbuf *m_new = (struct mbuf *)buf;
261 struct mbuf **m0 = (struct mbuf **)(nw->arg_ptr);
262
263 NL_LOG(LOG_DEBUG2, "IN: ptr: %p len: %d arg: %p", buf, datalen, nw->arg_ptr);
264
265 if (__predict_false(datalen == 0)) {
266 m_freem(m_new);
267 return (true);
268 }
269
270 m_new->m_pkthdr.len = datalen;
271 m_new->m_len = datalen;
272
273 if (*m0 == NULL) {
274 *m0 = m_new;
275 } else {
276 struct mbuf *m_last;
277 for (m_last = *m0; m_last->m_next != NULL; m_last = m_last->m_next)
278 ;
279 m_last->m_next = m_new;
280 (*m0)->m_pkthdr.len += datalen;
281 }
282
283 return (true);
284 }
285
286 /*
287 * NS_WRITER_TYPE_LBUF
288 * Writes message to the allocated memory buffer,
289 * flushing to socket/group when mbuf size limit is reached.
290 * Calls linux handler to rewrite messages before sending to the socket.
291 */
292 static bool
nlmsg_get_ns_lbuf(struct nl_writer * nw,int size,bool waitok)293 nlmsg_get_ns_lbuf(struct nl_writer *nw, int size, bool waitok)
294 {
295 int mflag = waitok ? M_WAITOK : M_NOWAIT;
296 size = roundup2(size, sizeof(void *));
297 int add_size = sizeof(struct linear_buffer) + SCRATCH_BUFFER_SIZE;
298 char *buf = malloc(add_size + size * 2, M_NETLINK, mflag | M_ZERO);
299 if (__predict_false(buf == NULL))
300 return (false);
301
302 /* Fill buffer header first */
303 struct linear_buffer *lb = (struct linear_buffer *)buf;
304 lb->base = &buf[sizeof(struct linear_buffer) + size];
305 lb->size = size + SCRATCH_BUFFER_SIZE;
306
307 nw->alloc_len = size;
308 nw->offset = 0;
309 nw->hdr = NULL;
310 nw->_storage = buf;
311 nw->data = (char *)(lb + 1);
312 nw->malloc_flag = mflag;
313 nw->writer_type = NS_WRITER_TYPE_LBUF;
314 nw->num_messages = 0;
315 nw->enomem = false;
316 return (true);
317 }
318
319 static bool
nlmsg_write_socket_lbuf(struct nl_writer * nw,void * buf,int datalen,int cnt)320 nlmsg_write_socket_lbuf(struct nl_writer *nw, void *buf, int datalen, int cnt)
321 {
322 struct linear_buffer *lb = (struct linear_buffer *)buf;
323 char *data = (char *)(lb + 1);
324 struct nlpcb *nlp = (struct nlpcb *)(nw->arg_ptr);
325
326 if (__predict_false(datalen == 0)) {
327 free(buf, M_NETLINK);
328 return (true);
329 }
330
331 struct mbuf *m = NULL;
332 if (linux_netlink_p != NULL)
333 m = linux_netlink_p->msgs_to_linux(nlp->nl_proto, data, datalen, nlp);
334 free(buf, M_NETLINK);
335
336 if (__predict_false(m == NULL)) {
337 /* XXX: should we set sorcverr? */
338 return (false);
339 }
340
341 int io_flags = (nw->ignore_limit) ? NL_IOF_IGNORE_LIMIT : 0;
342 return (nl_send_one(m, nlp, cnt, io_flags));
343 }
344
345 /* Shouldn't be called (maybe except Linux code originating message) */
346 static bool
nlmsg_write_group_lbuf(struct nl_writer * nw,void * buf,int datalen,int cnt)347 nlmsg_write_group_lbuf(struct nl_writer *nw, void *buf, int datalen, int cnt)
348 {
349 struct linear_buffer *lb = (struct linear_buffer *)buf;
350 char *data = (char *)(lb + 1);
351
352 if (__predict_false(datalen == 0)) {
353 free(buf, M_NETLINK);
354 return (true);
355 }
356
357 struct mbuf *m = m_getm2(NULL, datalen, nw->malloc_flag, MT_DATA, M_PKTHDR);
358 if (__predict_false(m == NULL)) {
359 free(buf, M_NETLINK);
360 return (false);
361 }
362 m_append(m, datalen, data);
363 free(buf, M_NETLINK);
364
365 nl_send_group(m, cnt, nw->arg_uint >> 16, nw->arg_uint & 0xFFFF);
366 return (true);
367 }
368
369 static const struct nlwriter_ops nlmsg_writers[] = {
370 /* NS_WRITER_TYPE_MBUF */
371 {
372 .init = nlmsg_get_ns_mbuf,
373 .write_socket = nlmsg_write_socket_mbuf,
374 .write_group = nlmsg_write_group_mbuf,
375 .write_chain = nlmsg_write_chain_mbuf,
376 },
377 /* NS_WRITER_TYPE_BUF */
378 {
379 .init = nlmsg_get_ns_buf,
380 .write_socket = nlmsg_write_socket_buf,
381 .write_group = nlmsg_write_group_buf,
382 .write_chain = nlmsg_write_chain_buf,
383 },
384 /* NS_WRITER_TYPE_LBUF */
385 {
386 .init = nlmsg_get_ns_lbuf,
387 .write_socket = nlmsg_write_socket_lbuf,
388 .write_group = nlmsg_write_group_lbuf,
389 },
390 };
391
392 static void
nlmsg_set_callback(struct nl_writer * nw)393 nlmsg_set_callback(struct nl_writer *nw)
394 {
395 const struct nlwriter_ops *pops = &nlmsg_writers[nw->writer_type];
396
397 switch (nw->writer_target) {
398 case NS_WRITER_TARGET_SOCKET:
399 nw->cb = pops->write_socket;
400 break;
401 case NS_WRITER_TARGET_GROUP:
402 nw->cb = pops->write_group;
403 break;
404 case NS_WRITER_TARGET_CHAIN:
405 nw->cb = pops->write_chain;
406 break;
407 default:
408 panic("not implemented");
409 }
410 }
411
412 static bool
nlmsg_get_buf_type(struct nl_writer * nw,int size,int type,bool waitok)413 nlmsg_get_buf_type(struct nl_writer *nw, int size, int type, bool waitok)
414 {
415 MPASS(type + 1 <= sizeof(nlmsg_writers) / sizeof(nlmsg_writers[0]));
416 NL_LOG(LOG_DEBUG3, "Setting up nw %p size %d type %d", nw, size, type);
417 return (nlmsg_writers[type].init(nw, size, waitok));
418 }
419
420 static bool
nlmsg_get_buf(struct nl_writer * nw,int size,bool waitok,bool is_linux)421 nlmsg_get_buf(struct nl_writer *nw, int size, bool waitok, bool is_linux)
422 {
423 int type;
424
425 if (!is_linux) {
426 if (__predict_true(size <= MCLBYTES))
427 type = NS_WRITER_TYPE_MBUF;
428 else
429 type = NS_WRITER_TYPE_BUF;
430 } else
431 type = NS_WRITER_TYPE_LBUF;
432 return (nlmsg_get_buf_type(nw, size, type, waitok));
433 }
434
435 bool
nlmsg_get_unicast_writer(struct nl_writer * nw,int size,struct nlpcb * nlp)436 nlmsg_get_unicast_writer(struct nl_writer *nw, int size, struct nlpcb *nlp)
437 {
438 if (!nlmsg_get_buf(nw, size, false, nlp->nl_linux))
439 return (false);
440 nw->arg_ptr = (void *)nlp;
441 nw->writer_target = NS_WRITER_TARGET_SOCKET;
442 nlmsg_set_callback(nw);
443 return (true);
444 }
445
446 bool
nlmsg_get_group_writer(struct nl_writer * nw,int size,int protocol,int group_id)447 nlmsg_get_group_writer(struct nl_writer *nw, int size, int protocol, int group_id)
448 {
449 if (!nlmsg_get_buf(nw, size, false, false))
450 return (false);
451 nw->arg_uint = (uint64_t)protocol << 16 | (uint64_t)group_id;
452 nw->writer_target = NS_WRITER_TARGET_GROUP;
453 nlmsg_set_callback(nw);
454 return (true);
455 }
456
457 bool
nlmsg_get_chain_writer(struct nl_writer * nw,int size,struct mbuf ** pm)458 nlmsg_get_chain_writer(struct nl_writer *nw, int size, struct mbuf **pm)
459 {
460 if (!nlmsg_get_buf(nw, size, false, false))
461 return (false);
462 *pm = NULL;
463 nw->arg_ptr = (void *)pm;
464 nw->writer_target = NS_WRITER_TARGET_CHAIN;
465 nlmsg_set_callback(nw);
466 NL_LOG(LOG_DEBUG3, "setup cb %p (need %p)", nw->cb, &nlmsg_write_chain_mbuf);
467 return (true);
468 }
469
470 void
nlmsg_ignore_limit(struct nl_writer * nw)471 nlmsg_ignore_limit(struct nl_writer *nw)
472 {
473 nw->ignore_limit = true;
474 }
475
476 bool
nlmsg_flush(struct nl_writer * nw)477 nlmsg_flush(struct nl_writer *nw)
478 {
479
480 if (__predict_false(nw->hdr != NULL)) {
481 /* Last message has not been completed, skip it. */
482 int completed_len = (char *)nw->hdr - nw->data;
483 /* Send completed messages */
484 nw->offset -= nw->offset - completed_len;
485 nw->hdr = NULL;
486 }
487
488 NL_LOG(LOG_DEBUG2, "OUT");
489 bool result = nw->cb(nw, nw->_storage, nw->offset, nw->num_messages);
490 nw->_storage = NULL;
491
492 if (!result) {
493 NL_LOG(LOG_DEBUG, "nw %p offset %d: flush with %p() failed", nw, nw->offset, nw->cb);
494 }
495
496 return (result);
497 }
498
499 /*
500 * Flushes previous data and allocates new underlying storage
501 * sufficient for holding at least @required_len bytes.
502 * Return true on success.
503 */
504 bool
nlmsg_refill_buffer(struct nl_writer * nw,int required_len)505 nlmsg_refill_buffer(struct nl_writer *nw, int required_len)
506 {
507 struct nl_writer ns_new = {};
508 int completed_len, new_len;
509
510 if (nw->enomem)
511 return (false);
512
513 NL_LOG(LOG_DEBUG3, "no space at offset %d/%d (want %d), trying to reclaim",
514 nw->offset, nw->alloc_len, required_len);
515
516 /* Calculated new buffer size and allocate it s*/
517 completed_len = (nw->hdr != NULL) ? (char *)nw->hdr - nw->data : nw->offset;
518 if (completed_len > 0 && required_len < MCLBYTES) {
519 /* We already ran out of space, use the largest effective size */
520 new_len = max(nw->alloc_len, MCLBYTES);
521 } else {
522 if (nw->alloc_len < MCLBYTES)
523 new_len = MCLBYTES;
524 else
525 new_len = nw->alloc_len * 2;
526 while (new_len < required_len)
527 new_len *= 2;
528 }
529 bool waitok = (nw->malloc_flag == M_WAITOK);
530 bool is_linux = (nw->writer_type == NS_WRITER_TYPE_LBUF);
531 if (!nlmsg_get_buf(&ns_new, new_len, waitok, is_linux)) {
532 nw->enomem = true;
533 NL_LOG(LOG_DEBUG, "getting new buf failed, setting ENOMEM");
534 return (false);
535 }
536 if (nw->ignore_limit)
537 nlmsg_ignore_limit(&ns_new);
538
539 /* Update callback data */
540 ns_new.writer_target = nw->writer_target;
541 nlmsg_set_callback(&ns_new);
542 ns_new.arg_uint = nw->arg_uint;
543
544 /* Copy last (unfinished) header to the new storage */
545 int last_len = nw->offset - completed_len;
546 if (last_len > 0) {
547 memcpy(ns_new.data, nw->hdr, last_len);
548 ns_new.hdr = (struct nlmsghdr *)ns_new.data;
549 ns_new.offset = last_len;
550 }
551
552 NL_LOG(LOG_DEBUG2, "completed: %d bytes, copied: %d bytes", completed_len, last_len);
553
554 /* Flush completed headers & switch to the new nw */
555 nlmsg_flush(nw);
556 memcpy(nw, &ns_new, sizeof(struct nl_writer));
557 NL_LOG(LOG_DEBUG2, "switched buffer: used %d/%d bytes", nw->offset, nw->alloc_len);
558
559 return (true);
560 }
561
562 bool
nlmsg_add(struct nl_writer * nw,uint32_t portid,uint32_t seq,uint16_t type,uint16_t flags,uint32_t len)563 nlmsg_add(struct nl_writer *nw, uint32_t portid, uint32_t seq, uint16_t type,
564 uint16_t flags, uint32_t len)
565 {
566 struct nlmsghdr *hdr;
567
568 MPASS(nw->hdr == NULL);
569
570 int required_len = NETLINK_ALIGN(len + sizeof(struct nlmsghdr));
571 if (__predict_false(nw->offset + required_len > nw->alloc_len)) {
572 if (!nlmsg_refill_buffer(nw, required_len))
573 return (false);
574 }
575
576 hdr = (struct nlmsghdr *)(&nw->data[nw->offset]);
577
578 hdr->nlmsg_len = len;
579 hdr->nlmsg_type = type;
580 hdr->nlmsg_flags = flags;
581 hdr->nlmsg_seq = seq;
582 hdr->nlmsg_pid = portid;
583
584 nw->hdr = hdr;
585 nw->offset += sizeof(struct nlmsghdr);
586
587 return (true);
588 }
589
590 bool
nlmsg_end(struct nl_writer * nw)591 nlmsg_end(struct nl_writer *nw)
592 {
593 MPASS(nw->hdr != NULL);
594
595 if (nw->enomem) {
596 NL_LOG(LOG_DEBUG, "ENOMEM when dumping message");
597 nlmsg_abort(nw);
598 return (false);
599 }
600
601 nw->hdr->nlmsg_len = (uint32_t)(nw->data + nw->offset - (char *)nw->hdr);
602 NL_LOG(LOG_DEBUG2, "wrote msg len: %u type: %d: flags: 0x%X seq: %u pid: %u",
603 nw->hdr->nlmsg_len, nw->hdr->nlmsg_type, nw->hdr->nlmsg_flags,
604 nw->hdr->nlmsg_seq, nw->hdr->nlmsg_pid);
605 nw->hdr = NULL;
606 nw->num_messages++;
607 return (true);
608 }
609
610 void
nlmsg_abort(struct nl_writer * nw)611 nlmsg_abort(struct nl_writer *nw)
612 {
613 if (nw->hdr != NULL) {
614 nw->offset = (uint32_t)((char *)nw->hdr - nw->data);
615 nw->hdr = NULL;
616 }
617 }
618
619 void
nlmsg_ack(struct nlpcb * nlp,int error,struct nlmsghdr * hdr,struct nl_pstate * npt)620 nlmsg_ack(struct nlpcb *nlp, int error, struct nlmsghdr *hdr,
621 struct nl_pstate *npt)
622 {
623 struct nlmsgerr *errmsg;
624 int payload_len;
625 uint32_t flags = nlp->nl_flags;
626 struct nl_writer *nw = npt->nw;
627 bool cap_ack;
628
629 payload_len = sizeof(struct nlmsgerr);
630
631 /*
632 * The only case when we send the full message in the
633 * reply is when there is an error and NETLINK_CAP_ACK
634 * is not set.
635 */
636 cap_ack = (error == 0) || (flags & NLF_CAP_ACK);
637 if (!cap_ack)
638 payload_len += hdr->nlmsg_len - sizeof(struct nlmsghdr);
639 payload_len = NETLINK_ALIGN(payload_len);
640
641 uint16_t nl_flags = cap_ack ? NLM_F_CAPPED : 0;
642 if ((npt->err_msg || npt->err_off) && nlp->nl_flags & NLF_EXT_ACK)
643 nl_flags |= NLM_F_ACK_TLVS;
644
645 NL_LOG(LOG_DEBUG3, "acknowledging message type %d seq %d",
646 hdr->nlmsg_type, hdr->nlmsg_seq);
647
648 if (!nlmsg_add(nw, nlp->nl_port, hdr->nlmsg_seq, NLMSG_ERROR, nl_flags, payload_len))
649 goto enomem;
650
651 errmsg = nlmsg_reserve_data(nw, payload_len, struct nlmsgerr);
652 errmsg->error = error;
653 /* In case of error copy the whole message, else just the header */
654 memcpy(&errmsg->msg, hdr, cap_ack ? sizeof(*hdr) : hdr->nlmsg_len);
655
656 if (npt->err_msg != NULL && nlp->nl_flags & NLF_EXT_ACK)
657 nlattr_add_string(nw, NLMSGERR_ATTR_MSG, npt->err_msg);
658 if (npt->err_off != 0 && nlp->nl_flags & NLF_EXT_ACK)
659 nlattr_add_u32(nw, NLMSGERR_ATTR_OFFS, npt->err_off);
660 if (npt->cookie != NULL)
661 nlattr_add_raw(nw, npt->cookie);
662
663 if (nlmsg_end(nw))
664 return;
665 enomem:
666 NLP_LOG(LOG_DEBUG, nlp, "error allocating ack data for message %d seq %u",
667 hdr->nlmsg_type, hdr->nlmsg_seq);
668 nlmsg_abort(nw);
669 }
670
671 bool
nlmsg_end_dump(struct nl_writer * nw,int error,struct nlmsghdr * hdr)672 nlmsg_end_dump(struct nl_writer *nw, int error, struct nlmsghdr *hdr)
673 {
674 if (!nlmsg_add(nw, hdr->nlmsg_pid, hdr->nlmsg_seq, NLMSG_DONE, 0, sizeof(int))) {
675 NL_LOG(LOG_DEBUG, "Error finalizing table dump");
676 return (false);
677 }
678 /* Save operation result */
679 int *perror = nlmsg_reserve_object(nw, int);
680 NL_LOG(LOG_DEBUG2, "record error=%d at off %d (%p)", error,
681 nw->offset, perror);
682 *perror = error;
683 nlmsg_end(nw);
684 nw->suppress_ack = true;
685
686 return (true);
687 }
688