libnl: /data/src/libnl3/lib/nl.c Source File

libnl 3.10.0

nl.c
1/* SPDX-License-Identifier: LGPL-2.1-only */
2/*
3 * Copyright (c) 2003-2012 Thomas Graf <tgraf@suug.ch>
4 */
5
6/**
7 * @defgroup core Core Library (libnl)
8 *
9 * Socket handling, connection management, sending and receiving of data,
10 * message construction and parsing, object caching system, ...
11 *
12 * This is the API reference of the core library. It is not meant as a guide
13 * but as a reference. Please refer to the core library guide for detailed
14 * documentation on the library architecture and examples:
15 *
16 * * @ref_asciidoc{core,_,Netlink Core Library Development Guide}
17 *
18 *
19 * @{
20 */
21
22#include "nl-default.h"
23
24#include <linux/socket.h>
25
26#include <netlink/netlink.h>
27#include <netlink/utils.h>
28#include <netlink/handlers.h>
29#include <netlink/msg.h>
30#include <netlink/attr.h>
31
32#include "nl-core.h"
33#include "nl-priv-dynamic-core/nl-core.h"
34#include "nl-aux-core/nl-core.h"
35#include "nl-priv-dynamic-core/cache-api.h"
36
37/**
38 * @defgroup core_types Data Types
39 *
40 * Core library data types
41 * @{
42 * @}
43 *
44 * @defgroup send_recv Send & Receive Data
45 *
46 * Connection management, sending & receiving of data
47 *
48 * Related sections in the development guide:
49 * - @core_doc{core_send_recv, Sending & Receiving}
50 * - @core_doc{core_sockets, Sockets}
51 *
52 * @{
53 *
54 * Header
55 * ------
56 * ~~~~{.c}
57 * #include <netlink/netlink.h>
58 * ~~~~
59 */
60
61/**
62 * @name Connection Management
63 * @{
64 */
65
66/**
67 * Create file descriptor and bind socket.
68 * @arg sk Netlink socket (required)
69 * @arg protocol Netlink protocol to use (required)
70 *
71 * Creates a new Netlink socket using `socket()` and binds the socket to the
72 * protocol and local port specified in the `sk` socket object. Fails if
73 * the socket is already connected.
74 *
75 * @note If available, the `close-on-exec` (`SOCK_CLOEXEC`) feature is enabled
76 * automatically on the new file descriptor. This causes the socket to
77 * be closed automatically if any of the `exec` family functions succeed.
78 * This is essential for multi threaded programs.
79 *
80 * @note The local port (`nl_socket_get_local_port()`) is unspecified after
81 * creating a new socket. It only gets determined when accessing the
82 * port the first time or during `nl_connect()`. When nl_connect()
83 * fails during `bind()` due to `ADDRINUSE`, it will retry with
84 * different ports if the port is unspecified. Unless you want to enforce
85 * the use of a specific local port, don't access the local port (or
86 * reset it to `unspecified` by calling `nl_socket_set_local_port(sk, 0)`).
87 * This capability is indicated by
88 * `%NL_CAPABILITY_NL_CONNECT_RETRY_GENERATE_PORT_ON_ADDRINUSE`.
89 *
90 * @note nl_connect() creates and sets the file descriptor. You can setup the file
91 * descriptor yourself by creating and binding it, and then calling
92 * nl_socket_set_fd(). The result will be the same.
93 *
94 * @see nl_socket_alloc()
95 * @see nl_close()
96 * @see nl_socket_set_fd()
97 *
98 * @return 0 on success or a negative error code.
99 *
100 * @retval -NLE_BAD_SOCK Socket is already connected
101 */
102 int nl_connect(struct nl_sock *sk, int protocol)
103{
104 int err, flags = 0;
105 int errsv;
106 socklen_t addrlen;
107 struct sockaddr_nl local = { 0 };
108 int try_bind = 1;
109
110#ifdef SOCK_CLOEXEC
111 flags |= SOCK_CLOEXEC;
112#endif
113
114 if (sk->s_fd != -1)
115 return -NLE_BAD_SOCK;
116
117 sk->s_fd = socket(AF_NETLINK, SOCK_RAW | flags, protocol);
118 if (sk->s_fd < 0) {
119 errsv = errno;
120 NL_DBG(4, "nl_connect(%p): socket() failed with %d (%s)\n", sk, errsv,
121 nl_strerror_l(errsv));
122 err = -nl_syserr2nlerr(errsv);
123 goto errout;
124 }
125
126 err = nl_socket_set_buffer_size(sk, 0, 0);
127 if (err < 0)
128 goto errout;
129
130 if (_nl_socket_is_local_port_unspecified (sk)) {
131 uint32_t port;
132 uint32_t used_ports[32] = { 0 };
133 int ntries = 0;
134
135 while (1) {
136 if (ntries++ > 5) {
137 /* try only a few times. We hit this only if many ports are already in
138 * use but allocated *outside* libnl/generate_local_port(). */
139 _nl_socket_set_local_port_no_release (sk, 0);
140 break;
141 }
142
143 port = _nl_socket_set_local_port_no_release(sk, 1);
144 if (port == 0)
145 break;
146
147 err = bind(sk->s_fd, (struct sockaddr*) &sk->s_local,
148 sizeof(sk->s_local));
149 if (err == 0) {
150 try_bind = 0;
151 break;
152 }
153
154 errsv = errno;
155 if (errsv == EADDRINUSE) {
156 NL_DBG(4, "nl_connect(%p): local port %u already in use. Retry.\n", sk, (unsigned) port);
157 _nl_socket_used_ports_set(used_ports, port);
158 } else {
159 NL_DBG(4, "nl_connect(%p): bind() for port %u failed with %d (%s)\n",
160 sk, (unsigned) port, errsv, nl_strerror_l(errsv));
161 _nl_socket_used_ports_release_all(used_ports);
162 err = -nl_syserr2nlerr(errsv);
163 goto errout;
164 }
165 }
166 _nl_socket_used_ports_release_all(used_ports);
167 }
168 if (try_bind) {
169 err = bind(sk->s_fd, (struct sockaddr*) &sk->s_local,
170 sizeof(sk->s_local));
171 if (err != 0) {
172 errsv = errno;
173 NL_DBG(4, "nl_connect(%p): bind() failed with %d (%s)\n",
174 sk, errsv, nl_strerror_l(errsv));
175 err = -nl_syserr2nlerr(errsv);
176 goto errout;
177 }
178 }
179
180 addrlen = sizeof(local);
181 err = getsockname(sk->s_fd, (struct sockaddr *) &local,
182 &addrlen);
183 if (err < 0) {
184 NL_DBG(4, "nl_connect(%p): getsockname() failed with %d (%s)\n",
185 sk, errno, nl_strerror_l(errno));
186 err = -nl_syserr2nlerr(errno);
187 goto errout;
188 }
189
190 if (addrlen != sizeof(local)) {
191 err = -NLE_NOADDR;
192 goto errout;
193 }
194
195 if (local.nl_family != AF_NETLINK) {
196 err = -NLE_AF_NOSUPPORT;
197 goto errout;
198 }
199
200 if (sk->s_local.nl_pid != local.nl_pid) {
201 /* The port id is different. That can happen if the port id was zero
202 * and kernel assigned a local port. */
203 nl_socket_set_local_port (sk, local.nl_pid);
204 }
205 sk->s_local = local;
206 sk->s_proto = protocol;
207
208 return 0;
209errout:
210 if (sk->s_fd != -1) {
211 close(sk->s_fd);
212 sk->s_fd = -1;
213 }
214
215 return err;
216}
217
218/**
219 * Close Netlink socket
220 * @arg sk Netlink socket (required)
221 *
222 * Closes the Netlink socket using `close()`.
223 *
224 * @note The socket is closed automatically if a `struct nl_sock` object is
225 * freed using `nl_socket_free()`.
226 *
227 * @see nl_connect()
228 */
229 void nl_close(struct nl_sock *sk)
230{
231 if (sk->s_fd >= 0) {
232 close(sk->s_fd);
233 sk->s_fd = -1;
234 }
235
236 sk->s_proto = 0;
237}
238
239/** @} */
240
241/**
242 * @name Send
243 * @{
244 */
245
246/**
247 * Transmit raw data over Netlink socket.
248 * @arg sk Netlink socket (required)
249 * @arg buf Buffer carrying data to send (required)
250 * @arg size Size of buffer (required)
251 *
252 * Transmits "raw" data over the specified Netlink socket. Unlike the other
253 * transmit functions it does not modify the data in any way. It directly
254 * passes the buffer \c buf of \c size to sendto().
255 *
256 * The message is addressed to the peer as specified in the socket by either
257 * the nl_socket_set_peer_port() or nl_socket_set_peer_groups() function.
258 *
259 * @note Because there is no indication on the message boundaries of the data
260 * being sent, the \c NL_CB_MSG_OUT callback handler will not be invoked
261 * for data that is being sent using this function.
262 *
263 * @see nl_socket_set_peer_port()
264 * @see nl_socket_set_peer_groups()
265 * @see nl_sendmsg()
266 *
267 * @return Number of bytes sent or a negative error code.
268 */
269 int nl_sendto(struct nl_sock *sk, void *buf, size_t size)
270{
271 int ret;
272
273 if (!buf)
274 return -NLE_INVAL;
275
276 if (sk->s_fd < 0)
277 return -NLE_BAD_SOCK;
278
279 ret = sendto(sk->s_fd, buf, size, 0, (struct sockaddr *)
280 &sk->s_peer, sizeof(sk->s_peer));
281 if (ret < 0) {
282 NL_DBG(4, "nl_sendto(%p): sendto() failed with %d (%s)\n",
283 sk, errno, nl_strerror_l(errno));
284 return -nl_syserr2nlerr(errno);
285 }
286
287 return ret;
288}
289
290/**
291 * Transmit Netlink message using sendmsg()
292 * @arg sk Netlink socket (required)
293 * @arg msg Netlink message to be sent (required)
294 * @arg hdr sendmsg() message header (required)
295 *
296 * Transmits the message specified in \c hdr over the Netlink socket using the
297 * sendmsg() system call.
298 *
299 * @attention
300 * The `msg` argument will *not* be used to derive the message payload that
301 * is being sent out. The `msg` argument is *only* passed on to the
302 * `NL_CB_MSG_OUT` callback. The caller is responsible to initialize the
303 * `hdr` struct properly and have it point to the message payload and
304 * socket address.
305 *
306 * @note
307 * This function uses `nlmsg_set_src()` to modify the `msg` argument prior to
308 * invoking the `NL_CB_MSG_OUT` callback to provide the local port number.
309 *
310 * @callback This function triggers the `NL_CB_MSG_OUT` callback.
311 *
312 * @attention
313 * Think twice before using this function. It provides a low level access to
314 * the Netlink socket. Among other limitations, it does not add credentials
315 * even if enabled or respect the destination address specified in the `msg`
316 * object.
317 *
318 * @see nl_socket_set_local_port()
319 * @see nl_send_auto()
320 * @see nl_send_iovec()
321 *
322 * @return Number of bytes sent on success or a negative error code.
323 *
324 * @lowlevel
325 */
326 int nl_sendmsg(struct nl_sock *sk, struct nl_msg *msg, struct msghdr *hdr)
327{
328 struct nl_cb *cb;
329 int ret;
330
331 if (sk->s_fd < 0)
332 return -NLE_BAD_SOCK;
333
334 nlmsg_set_src(msg, &sk->s_local);
335
336 cb = sk->s_cb;
337 if (cb->cb_set[NL_CB_MSG_OUT])
338 if ((ret = nl_cb_call(cb, NL_CB_MSG_OUT, msg)) != NL_OK)
339 return ret;
340
341 ret = sendmsg(sk->s_fd, hdr, 0);
342 if (ret < 0) {
343 NL_DBG(4, "nl_sendmsg(%p): sendmsg() failed with %d (%s)\n",
344 sk, errno, nl_strerror_l(errno));
345 return -nl_syserr2nlerr(errno);
346 }
347
348 NL_DBG(4, "sent %d bytes\n", ret);
349 return ret;
350}
351
352
353/**
354 * Transmit Netlink message (taking IO vector)
355 * @arg sk Netlink socket (required)
356 * @arg msg Netlink message to be sent (required)
357 * @arg iov IO vector to be sent (required)
358 * @arg iovlen Number of struct iovec to be sent (required)
359 *
360 * This function is identical to nl_send() except that instead of taking a
361 * `struct nl_msg` object it takes an IO vector. Please see the description
362 * of `nl_send()`.
363 *
364 * @callback This function triggers the `NL_CB_MSG_OUT` callback.
365 *
366 * @see nl_send()
367 *
368 * @return Number of bytes sent on success or a negative error code.
369 *
370 * @lowlevel
371 */
372 int nl_send_iovec(struct nl_sock *sk, struct nl_msg *msg, struct iovec *iov, unsigned iovlen)
373{
374 struct sockaddr_nl *dst;
375 struct ucred *creds;
376 struct msghdr hdr = {
377 .msg_name = (void *) &sk->s_peer,
378 .msg_namelen = sizeof(struct sockaddr_nl),
379 .msg_iov = iov,
380 .msg_iovlen = iovlen,
381 };
382 char buf[CMSG_SPACE(sizeof(struct ucred))];
383
384 /* Overwrite destination if specified in the message itself, defaults
385 * to the peer address of the socket.
386 */
387 dst = nlmsg_get_dst(msg);
388 if (dst->nl_family == AF_NETLINK)
389 hdr.msg_name = dst;
390
391 /* Add credentials if present. */
392 creds = nlmsg_get_creds(msg);
393 if (creds != NULL) {
394 struct cmsghdr *cmsg;
395
396 hdr.msg_control = buf;
397 hdr.msg_controllen = sizeof(buf);
398
399 cmsg = CMSG_FIRSTHDR(&hdr);
400 cmsg->cmsg_level = SOL_SOCKET;
401 cmsg->cmsg_type = SCM_CREDENTIALS;
402 cmsg->cmsg_len = CMSG_LEN(sizeof(struct ucred));
403 memcpy(CMSG_DATA(cmsg), creds, sizeof(struct ucred));
404 }
405
406 return nl_sendmsg(sk, msg, &hdr);
407}
408
409/**
410 * Transmit Netlink message
411 * @arg sk Netlink socket (required)
412 * @arg msg Netlink message (required)
413 *
414 * Transmits the Netlink message `msg` over the Netlink socket using the
415 * `sendmsg()` system call. This function is based on `nl_send_iovec()` but
416 * takes care of initializing a `struct iovec` based on the `msg` object.
417 *
418 * The message is addressed to the peer as specified in the socket by either
419 * the nl_socket_set_peer_port() or nl_socket_set_peer_groups() function.
420 * The peer address can be overwritten by specifying an address in the `msg`
421 * object using nlmsg_set_dst().
422 *
423 * If present in the `msg`, credentials set by the nlmsg_set_creds() function
424 * are added to the control buffer of the message.
425 *
426 * @par Overwriting Capability:
427 * Calls to this function can be overwritten by providing an alternative using
428 * the nl_cb_overwrite_send() function.
429 *
430 * @callback This function triggers the `NL_CB_MSG_OUT` callback.
431 *
432 * @attention
433 * Unlike `nl_send_auto()`, this function does *not* finalize the message in
434 * terms of automatically adding needed flags or filling out port numbers.
435 *
436 * @see nl_send_auto()
437 * @see nl_send_iovec()
438 * @see nl_socket_set_peer_port()
439 * @see nl_socket_set_peer_groups()
440 * @see nlmsg_set_dst()
441 * @see nlmsg_set_creds()
442 * @see nl_cb_overwrite_send()
443 *
444 * @return Number of bytes sent on success or a negative error code.
445*/
446 int nl_send(struct nl_sock *sk, struct nl_msg *msg)
447{
448 struct nl_cb *cb = sk->s_cb;
449
450 if (cb->cb_send_ow)
451 return cb->cb_send_ow(sk, msg);
452 else {
453 struct iovec iov = {
454 .iov_base = (void *) nlmsg_hdr(msg),
455 .iov_len = nlmsg_hdr(msg)->nlmsg_len,
456 };
457
458 return nl_send_iovec(sk, msg, &iov, 1);
459 }
460}
461
462/**
463 * Finalize Netlink message
464 * @arg sk Netlink socket (required)
465 * @arg msg Netlink message (required)
466 *
467 * This function finalizes a Netlink message by completing the message with
468 * desirable flags and values depending on the socket configuration.
469 *
470 * - If not yet filled out, the source address of the message (`nlmsg_pid`)
471 * will be set to the local port number of the socket.
472 * - If not yet specified, the next available sequence number is assigned
473 * to the message (`nlmsg_seq`).
474 * - If not yet specified, the protocol field of the message will be set to
475 * the protocol field of the socket.
476 * - The `NLM_F_REQUEST` Netlink message flag will be set.
477 * - The `NLM_F_ACK` flag will be set if Auto-ACK mode is enabled on the
478 * socket.
479 */
480 void nl_complete_msg(struct nl_sock *sk, struct nl_msg *msg)
481{
482 struct nlmsghdr *nlh;
483
484 nlh = nlmsg_hdr(msg);
485 if (nlh->nlmsg_pid == NL_AUTO_PORT)
486 nlh->nlmsg_pid = nl_socket_get_local_port(sk);
487
488 if (nlh->nlmsg_seq == NL_AUTO_SEQ)
489 nlh->nlmsg_seq = nl_socket_use_seq(sk);
490
491 if (msg->nm_protocol == -1)
492 msg->nm_protocol = sk->s_proto;
493
494 nlh->nlmsg_flags |= NLM_F_REQUEST;
495
496 if (!(sk->s_flags & NL_NO_AUTO_ACK))
497 nlh->nlmsg_flags |= NLM_F_ACK;
498}
499
500/**
501 * Finalize and transmit Netlink message
502 * @arg sk Netlink socket (required)
503 * @arg msg Netlink message (required)
504 *
505 * Finalizes the message by passing it to `nl_complete_msg()` and transmits it
506 * by passing it to `nl_send()`.
507 *
508 * @callback This function triggers the `NL_CB_MSG_OUT` callback.
509 *
510 * @see nl_complete_msg()
511 * @see nl_send()
512 *
513 * @return Number of bytes sent or a negative error code.
514 */
515 int nl_send_auto(struct nl_sock *sk, struct nl_msg *msg)
516{
517 nl_complete_msg(sk, msg);
518
519 return nl_send(sk, msg);
520}
521
522/**
523 * Finalize and transmit Netlink message and wait for ACK or error message
524 * @arg sk Netlink socket (required)
525 * @arg msg Netlink message (required)
526 *
527 * Passes the `msg` to `nl_send_auto()` to finalize and transmit it. Frees the
528 * message and waits (sleeps) for the ACK or error message to be received.
529 *
530 * @attention
531 * Disabling Auto-ACK (nl_socket_disable_auto_ack()) will cause this function
532 * to return immediately after transmitting the message. However, the peer may
533 * still be returning an error message in response to the request. It is the
534 * responsibility of the caller to handle such messages.
535 *
536 * @callback This function triggers the `NL_CB_MSG_OUT` callback.
537 *
538 * @attention
539 * This function frees the `msg` object after transmitting it by calling
540 * `nlmsg_free()`.
541 *
542 * @see nl_send_auto().
543 * @see nl_wait_for_ack()
544 *
545 * @return 0 on success or a negative error code.
546 */
547 int nl_send_sync(struct nl_sock *sk, struct nl_msg *msg)
548{
549 int err;
550
551 err = nl_send_auto(sk, msg);
552 nlmsg_free(msg);
553 if (err < 0)
554 return err;
555
556 return wait_for_ack(sk);
557}
558
559/**
560 * Construct and transmit a Netlink message
561 * @arg sk Netlink socket (required)
562 * @arg type Netlink message type (required)
563 * @arg flags Netlink message flags (optional)
564 * @arg buf Data buffer (optional)
565 * @arg size Size of data buffer (optional)
566 *
567 * Allocates a new Netlink message based on `type` and `flags`. If `buf`
568 * points to payload of length `size` that payload will be appended to the
569 * message.
570 *
571 * Sends out the message using `nl_send_auto()` and frees the message
572 * afterwards.
573 *
574 * @see nl_send_auto()
575 *
576 * @return Number of characters sent on success or a negative error code.
577 * @retval -NLE_NOMEM Unable to allocate Netlink message
578 */
579 int nl_send_simple(struct nl_sock *sk, int type, int flags, void *buf,
580 size_t size)
581{
582 int err;
583 struct nl_msg *msg;
584
585 msg = nlmsg_alloc_simple(type, flags);
586 if (!msg)
587 return -NLE_NOMEM;
588
589 if (buf && size) {
590 err = nlmsg_append(msg, buf, size, NLMSG_ALIGNTO);
591 if (err < 0)
592 goto errout;
593 }
594
595 err = nl_send_auto(sk, msg);
596errout:
597 nlmsg_free(msg);
598
599 return err;
600}
601
602/** @} */
603
604/**
605 * @name Receive
606 * @{
607 */
608
609/**
610 * Receive data from netlink socket
611 * @arg sk Netlink socket (required)
612 * @arg nla Netlink socket structure to hold address of peer (required)
613 * @arg buf Destination pointer for message content (required)
614 * @arg creds Destination pointer for credentials (optional)
615 *
616 * Receives data from a connected netlink socket using recvmsg() and returns
617 * the number of bytes read. The read data is stored in a newly allocated
618 * buffer that is assigned to \c *buf. The peer's netlink address will be
619 * stored in \c *nla.
620 *
621 * This function blocks until data is available to be read unless the socket
622 * has been put into non-blocking mode using nl_socket_set_nonblocking() in
623 * which case this function will return immediately with a return value of
624 * -NLA_AGAIN (versions before 3.2.22 returned instead 0, in which case you
625 * should check first clear errno and then check for errno EAGAIN).
626 *
627 * The buffer size used when reading from the netlink socket and thus limiting
628 * the maximum size of a netlink message that can be read defaults to the size
629 * of a memory page (getpagesize()). The buffer size can be modified on a per
630 * socket level using the function nl_socket_set_msg_buf_size().
631 *
632 * If message peeking is enabled using nl_socket_enable_msg_peek() the size of
633 * the message to be read will be determined using the MSG_PEEK flag prior to
634 * performing the actual read. This leads to an additional recvmsg() call for
635 * every read operation which has performance implications and is not
636 * recommended for high throughput protocols.
637 *
638 * An eventual interruption of the recvmsg() system call is automatically
639 * handled by retrying the operation.
640 *
641 * If receiving of credentials has been enabled using the function
642 * nl_socket_set_passcred(), this function will allocate a new struct ucred
643 * filled with the received credentials and assign it to \c *creds. The caller
644 * is responsible for freeing the buffer.
645 *
646 * @note The caller is responsible to free the returned data buffer and if
647 * enabled, the credentials buffer.
648 *
649 * @see nl_socket_set_nonblocking()
650 * @see nl_socket_set_msg_buf_size()
651 * @see nl_socket_enable_msg_peek()
652 * @see nl_socket_set_passcred()
653 *
654 * @return Number of bytes read, 0 on EOF, 0 on no data event (non-blocking
655 * mode), or a negative error code.
656 */
657 int nl_recv(struct nl_sock *sk, struct sockaddr_nl *nla,
658 unsigned char **buf, struct ucred **creds)
659{
660 ssize_t n;
661 int flags = 0;
662 static int page_size = 0; /* GLOBAL! */
663 struct iovec iov;
664 struct msghdr msg = {
665 .msg_name = (void *) nla,
666 .msg_namelen = sizeof(struct sockaddr_nl),
667 .msg_iov = &iov,
668 .msg_iovlen = 1,
669 };
670 struct ucred* tmpcreds = NULL;
671 int retval = 0;
672
673 if (!buf || !nla)
674 return -NLE_INVAL;
675
676 if ( (sk->s_flags & NL_MSG_PEEK)
677 || (!(sk->s_flags & NL_MSG_PEEK_EXPLICIT) && sk->s_bufsize == 0))
678 flags |= MSG_PEEK | MSG_TRUNC;
679
680 if (page_size == 0)
681 page_size = getpagesize() * 4;
682
683 iov.iov_len = sk->s_bufsize ? sk->s_bufsize : ((size_t)page_size);
684 iov.iov_base = malloc(iov.iov_len);
685
686 if (!iov.iov_base) {
687 retval = -NLE_NOMEM;
688 goto abort;
689 }
690
691 if (creds && (sk->s_flags & NL_SOCK_PASSCRED)) {
692 msg.msg_controllen = CMSG_SPACE(sizeof(struct ucred));
693 msg.msg_control = malloc(msg.msg_controllen);
694 if (!msg.msg_control) {
695 retval = -NLE_NOMEM;
696 goto abort;
697 }
698 }
699retry:
700
701 n = recvmsg(sk->s_fd, &msg, flags);
702 if (!n) {
703 retval = 0;
704 goto abort;
705 }
706 if (n < 0) {
707 if (errno == EINTR) {
708 NL_DBG(3, "recvmsg() returned EINTR, retrying\n");
709 goto retry;
710 }
711
712 NL_DBG(4, "recvmsg(%p): nl_recv() failed with %d (%s)\n",
713 sk, errno, nl_strerror_l(errno));
714 retval = -nl_syserr2nlerr(errno);
715 goto abort;
716 }
717
718 if (msg.msg_flags & MSG_CTRUNC) {
719 void *tmp;
720
721 if (msg.msg_controllen == 0) {
722 retval = -NLE_MSG_TRUNC;
723 NL_DBG(4, "recvmsg(%p): Received unexpected control data", sk);
724 goto abort;
725 }
726
727 msg.msg_controllen *= 2;
728 tmp = realloc(msg.msg_control, msg.msg_controllen);
729 if (!tmp) {
730 retval = -NLE_NOMEM;
731 goto abort;
732 }
733 msg.msg_control = tmp;
734 goto retry;
735 }
736
737 if (iov.iov_len < ((size_t)n) || (msg.msg_flags & MSG_TRUNC)) {
738 void *tmp;
739
740 /* respond with error to an incomplete message */
741 if (flags == 0) {
742 retval = -NLE_MSG_TRUNC;
743 goto abort;
744 }
745
746 /* Provided buffer is not long enough, enlarge it
747 * to size of n (which should be total length of the message)
748 * and try again. */
749 iov.iov_len = n;
750 tmp = realloc(iov.iov_base, iov.iov_len);
751 if (!tmp) {
752 retval = -NLE_NOMEM;
753 goto abort;
754 }
755 iov.iov_base = tmp;
756 flags = 0;
757 goto retry;
758 }
759
760 if (flags != 0) {
761 /* Buffer is big enough, do the actual reading */
762 flags = 0;
763 goto retry;
764 }
765
766 if (msg.msg_namelen != sizeof(struct sockaddr_nl)) {
767 retval = -NLE_NOADDR;
768 goto abort;
769 }
770
771 if (creds && (sk->s_flags & NL_SOCK_PASSCRED)) {
772 struct cmsghdr *cmsg;
773
774 for (cmsg = CMSG_FIRSTHDR(&msg); cmsg; cmsg = CMSG_NXTHDR(&msg, cmsg)) {
775 if (cmsg->cmsg_level != SOL_SOCKET)
776 continue;
777 if (cmsg->cmsg_type != SCM_CREDENTIALS)
778 continue;
779 tmpcreds = malloc(sizeof(*tmpcreds));
780 if (!tmpcreds) {
781 retval = -NLE_NOMEM;
782 goto abort;
783 }
784 memcpy(tmpcreds, CMSG_DATA(cmsg), sizeof(*tmpcreds));
785 break;
786 }
787 }
788
789 retval = n;
790abort:
791 free(msg.msg_control);
792
793 if (retval <= 0) {
794 free(iov.iov_base);
795 iov.iov_base = NULL;
796 free(tmpcreds);
797 tmpcreds = NULL;
798 } else
799 *buf = iov.iov_base;
800
801 if (creds)
802 *creds = tmpcreds;
803
804 return retval;
805}
806
807/** @cond SKIP */
808#define NL_CB_CALL(cb, type, msg) \
809do { \
810 err = nl_cb_call(cb, type, msg); \
811 switch (err) { \
812 case NL_OK: \
813 err = 0; \
814 break; \
815 case NL_SKIP: \
816 goto skip; \
817 case NL_STOP: \
818 goto stop; \
819 default: \
820 goto out; \
821 } \
822} while (0)
823/** @endcond */
824
825static int recvmsgs(struct nl_sock *sk, struct nl_cb *cb)
826{
827 int n, err = 0, multipart = 0, interrupted = 0, nrecv = 0;
828 unsigned char *buf = NULL;
829 struct nlmsghdr *hdr;
830
831 /*
832 nla is passed on to not only to nl_recv() but may also be passed
833 to a function pointer provided by the caller which may or may not
834 initialize the variable. Thomas Graf.
835 */
836 struct sockaddr_nl nla = {0};
837 struct nl_msg *msg = NULL;
838 struct ucred *creds = NULL;
839
840continue_reading:
841 NL_DBG(3, "Attempting to read from %p\n", sk);
842 if (cb->cb_recv_ow)
843 n = cb->cb_recv_ow(sk, &nla, &buf, &creds);
844 else
845 n = nl_recv(sk, &nla, &buf, &creds);
846
847 if (n <= 0)
848 return n;
849
850 NL_DBG(3, "recvmsgs(%p): Read %d bytes\n", sk, n);
851
852 hdr = (struct nlmsghdr *) buf;
853 while (nlmsg_ok(hdr, n)) {
854 NL_DBG(3, "recvmsgs(%p): Processing valid message...\n", sk);
855
856 nlmsg_free(msg);
857 msg = nlmsg_convert(hdr);
858 if (!msg) {
859 err = -NLE_NOMEM;
860 goto out;
861 }
862
863 nlmsg_set_proto(msg, sk->s_proto);
864 nlmsg_set_src(msg, &nla);
865 if (creds)
866 nlmsg_set_creds(msg, creds);
867
868 nrecv++;
869
870 /* Raw callback is the first, it gives the most control
871 * to the user and he can do his very own parsing. */
872 if (cb->cb_set[NL_CB_MSG_IN])
873 NL_CB_CALL(cb, NL_CB_MSG_IN, msg);
874
875 /* Sequence number checking. The check may be done by
876 * the user, otherwise a very simple check is applied
877 * enforcing strict ordering */
878 if (cb->cb_set[NL_CB_SEQ_CHECK]) {
879 NL_CB_CALL(cb, NL_CB_SEQ_CHECK, msg);
880
881 /* Only do sequence checking if auto-ack mode is enabled */
882 } else if (!(sk->s_flags & NL_NO_AUTO_ACK)) {
883 if (hdr->nlmsg_seq != sk->s_seq_expect) {
884 if (cb->cb_set[NL_CB_INVALID])
885 NL_CB_CALL(cb, NL_CB_INVALID, msg);
886 else {
887 err = -NLE_SEQ_MISMATCH;
888 goto out;
889 }
890 }
891 }
892
893 if (hdr->nlmsg_type == NLMSG_DONE ||
894 hdr->nlmsg_type == NLMSG_ERROR ||
895 hdr->nlmsg_type == NLMSG_NOOP ||
896 hdr->nlmsg_type == NLMSG_OVERRUN) {
897 /* We can't check for !NLM_F_MULTI since some netlink
898 * users in the kernel are broken. */
899 sk->s_seq_expect++;
900 NL_DBG(3, "recvmsgs(%p): Increased expected " \
901 "sequence number to %d\n",
902 sk, sk->s_seq_expect);
903 }
904
905 if (hdr->nlmsg_flags & NLM_F_MULTI)
906 multipart = 1;
907
908 if (hdr->nlmsg_flags & NLM_F_DUMP_INTR) {
909 if (cb->cb_set[NL_CB_DUMP_INTR])
910 NL_CB_CALL(cb, NL_CB_DUMP_INTR, msg);
911 else {
912 /*
913 * We have to continue reading to clear
914 * all messages until a NLMSG_DONE is
915 * received and report the inconsistency.
916 */
917 interrupted = 1;
918 }
919 }
920
921 /* Other side wishes to see an ack for this message */
922 if (hdr->nlmsg_flags & NLM_F_ACK) {
923 if (cb->cb_set[NL_CB_SEND_ACK])
924 NL_CB_CALL(cb, NL_CB_SEND_ACK, msg);
925 else {
926 /* FIXME: implement */
927 }
928 }
929
930 /* messages terminates a multipart message, this is
931 * usually the end of a message and therefore we slip
932 * out of the loop by default. the user may overrule
933 * this action by skipping this packet. */
934 if (hdr->nlmsg_type == NLMSG_DONE) {
935 multipart = 0;
936 if (cb->cb_set[NL_CB_FINISH])
937 NL_CB_CALL(cb, NL_CB_FINISH, msg);
938 }
939
940 /* Message to be ignored, the default action is to
941 * skip this message if no callback is specified. The
942 * user may overrule this action by returning
943 * NL_PROCEED. */
944 else if (hdr->nlmsg_type == NLMSG_NOOP) {
945 if (cb->cb_set[NL_CB_SKIPPED])
946 NL_CB_CALL(cb, NL_CB_SKIPPED, msg);
947 else
948 goto skip;
949 }
950
951 /* Data got lost, report back to user. The default action is to
952 * quit parsing. The user may overrule this action by retuning
953 * NL_SKIP or NL_PROCEED (dangerous) */
954 else if (hdr->nlmsg_type == NLMSG_OVERRUN) {
955 if (cb->cb_set[NL_CB_OVERRUN])
956 NL_CB_CALL(cb, NL_CB_OVERRUN, msg);
957 else {
958 err = -NLE_MSG_OVERFLOW;
959 goto out;
960 }
961 }
962
963 /* Message carries a nlmsgerr */
964 else if (hdr->nlmsg_type == NLMSG_ERROR) {
965 struct nlmsgerr *e = nlmsg_data(hdr);
966
967 if (hdr->nlmsg_len <
968 ((unsigned)nlmsg_size(sizeof(*e)))) {
969 /* Truncated error message, the default action
970 * is to stop parsing. The user may overrule
971 * this action by returning NL_SKIP or
972 * NL_PROCEED (dangerous) */
973 if (cb->cb_set[NL_CB_INVALID])
974 NL_CB_CALL(cb, NL_CB_INVALID, msg);
975 else {
976 err = -NLE_MSG_TRUNC;
977 goto out;
978 }
979 } else if (e->error) {
980 NL_DBG(4, "recvmsgs(%p): RTNETLINK responded with %d (%s)\n",
981 sk, -e->error, nl_strerror_l(-e->error));
982
983 /* Error message reported back from kernel. */
984 if (cb->cb_err) {
985 err = cb->cb_err(&nla, e,
986 cb->cb_err_arg);
987 if (err < 0)
988 goto out;
989 else if (err == NL_SKIP)
990 goto skip;
991 else if (err == NL_STOP) {
992 err = -nl_syserr2nlerr(e->error);
993 goto out;
994 }
995 } else {
996 err = -nl_syserr2nlerr(e->error);
997 goto out;
998 }
999 } else if (cb->cb_set[NL_CB_ACK])
1000 NL_CB_CALL(cb, NL_CB_ACK, msg);
1001 } else {
1002 /* Valid message (not checking for MULTIPART bit to
1003 * get along with broken kernels. NL_SKIP has no
1004 * effect on this. */
1005 if (cb->cb_set[NL_CB_VALID])
1006 NL_CB_CALL(cb, NL_CB_VALID, msg);
1007 }
1008skip:
1009 err = 0;
1010 hdr = nlmsg_next(hdr, &n);
1011 }
1012
1013 nlmsg_free(msg);
1014 free(buf);
1015 free(creds);
1016 buf = NULL;
1017 msg = NULL;
1018 creds = NULL;
1019
1020 if (multipart) {
1021 /* Multipart message not yet complete, continue reading */
1022 goto continue_reading;
1023 }
1024stop:
1025 err = 0;
1026out:
1027 nlmsg_free(msg);
1028 free(buf);
1029 free(creds);
1030
1031 if (interrupted)
1032 err = -NLE_DUMP_INTR;
1033
1034 if (!err)
1035 err = nrecv;
1036
1037 return err;
1038}
1039
1040/**
1041 * Receive a set of messages from a netlink socket and report parsed messages
1042 * @arg sk Netlink socket.
1043 * @arg cb set of callbacks to control behaviour.
1044 *
1045 * This function is identical to nl_recvmsgs() to the point that it will
1046 * return the number of parsed messages instead of 0 on success.
1047 *
1048 * @see nl_recvmsgs()
1049 *
1050 * @return Number of received messages or a negative error code from nl_recv().
1051 */
1052 int nl_recvmsgs_report(struct nl_sock *sk, struct nl_cb *cb)
1053{
1054 if (cb->cb_recvmsgs_ow)
1055 return cb->cb_recvmsgs_ow(sk, cb);
1056 else
1057 return recvmsgs(sk, cb);
1058}
1059
1060/**
1061 * Receive a set of messages from a netlink socket.
1062 * @arg sk Netlink socket.
1063 * @arg cb set of callbacks to control behaviour.
1064 *
1065 * Repeatedly calls nl_recv() or the respective replacement if provided
1066 * by the application (see nl_cb_overwrite_recv()) and parses the
1067 * received data as netlink messages. Stops reading if one of the
1068 * callbacks returns NL_STOP or nl_recv returns either 0 or a negative error code.
1069 *
1070 * A non-blocking sockets causes the function to return immediately if
1071 * no data is available.
1072 *
1073 * @see nl_recvmsgs_report()
1074 *
1075 * @return 0 on success or a negative error code from nl_recv().
1076 */
1077 int nl_recvmsgs(struct nl_sock *sk, struct nl_cb *cb)
1078{
1079 int err;
1080
1081 if ((err = nl_recvmsgs_report(sk, cb)) > 0)
1082 err = 0;
1083
1084 return err;
1085}
1086
1087/**
1088 * Receive a set of message from a netlink socket using handlers in nl_sock.
1089 * @arg sk Netlink socket.
1090 *
1091 * Calls nl_recvmsgs() with the handlers configured in the netlink socket.
1092 */
1093 int nl_recvmsgs_default(struct nl_sock *sk)
1094{
1095 return nl_recvmsgs(sk, sk->s_cb);
1096
1097}
1098
1099static int ack_wait_handler(struct nl_msg *msg, void *arg)
1100{
1101 return NL_STOP;
1102}
1103
1104/**
1105 * Wait for ACK.
1106 * @arg sk Netlink socket.
1107 * @pre The netlink socket must be in blocking state.
1108 *
1109 * Waits until an ACK is received for the latest not yet acknowledged
1110 * netlink message.
1111 */
1112 int nl_wait_for_ack(struct nl_sock *sk)
1113{
1114 int err;
1115 struct nl_cb *cb;
1116
1117 cb = nl_cb_clone(sk->s_cb);
1118 if (cb == NULL)
1119 return -NLE_NOMEM;
1120
1121 nl_cb_set(cb, NL_CB_ACK, NL_CB_CUSTOM, ack_wait_handler, NULL);
1122 err = nl_recvmsgs(sk, cb);
1123 nl_cb_put(cb);
1124
1125 return err;
1126}
1127
1128/** @cond SKIP */
1129struct pickup_param
1130{
1131 int (*parser)(struct nl_cache_ops *, struct sockaddr_nl *,
1132 struct nlmsghdr *, struct nl_parser_param *);
1133 struct nl_object *result;
1134 int *syserror;
1135};
1136
1137static int __store_answer(struct nl_object *obj, struct nl_parser_param *p)
1138{
1139 struct pickup_param *pp = p->pp_arg;
1140 /*
1141 * the parser will put() the object at the end, expecting the cache
1142 * to take the reference.
1143 */
1144 nl_object_get(obj);
1145 pp->result = obj;
1146
1147 return 0;
1148}
1149
1150static int __pickup_answer(struct nl_msg *msg, void *arg)
1151{
1152 struct pickup_param *pp = arg;
1153 struct nl_parser_param parse_arg = {
1154 .pp_cb = __store_answer,
1155 .pp_arg = pp,
1156 };
1157
1158 return pp->parser(NULL, &msg->nm_src, msg->nm_nlh, &parse_arg);
1159}
1160
1161static int __pickup_answer_syserr(struct sockaddr_nl *nla, struct nlmsgerr *nlerr, void *arg)
1162{
1163 *(((struct pickup_param *) arg)->syserror) = nlerr->error;
1164
1165 return -nl_syserr2nlerr(nlerr->error);
1166}
1167
1168/** @endcond */
1169
1170/**
1171 * Pickup netlink answer, parse is and return object
1172 * @arg sk Netlink socket
1173 * @arg parser Parser function to parse answer
1174 * @arg result Result pointer to return parsed object
1175 *
1176 * @return 0 on success or a negative error code.
1177 */
1178 int nl_pickup(struct nl_sock *sk,
1179 int (*parser)(struct nl_cache_ops *, struct sockaddr_nl *,
1180 struct nlmsghdr *, struct nl_parser_param *),
1181 struct nl_object **result)
1182{
1183 return nl_pickup_keep_syserr(sk, parser, result, NULL);
1184}
1185
1186/**
1187 * Pickup netlink answer, parse is and return object with preserving system error
1188 * @arg sk Netlink socket
1189 * @arg parser Parser function to parse answer
1190 * @arg result Result pointer to return parsed object
1191 * @arg syserr Result pointer for the system error in case of failure
1192 *
1193 * @return 0 on success or a negative error code.
1194 */
1195 int nl_pickup_keep_syserr(struct nl_sock *sk,
1196 int (*parser)(struct nl_cache_ops *, struct sockaddr_nl *,
1197 struct nlmsghdr *, struct nl_parser_param *),
1198 struct nl_object **result,
1199 int *syserror)
1200{
1201 struct nl_cb *cb;
1202 int err;
1203 struct pickup_param pp = {
1204 .parser = parser,
1205 };
1206
1207 cb = nl_cb_clone(sk->s_cb);
1208 if (cb == NULL)
1209 return -NLE_NOMEM;
1210
1211 nl_cb_set(cb, NL_CB_VALID, NL_CB_CUSTOM, __pickup_answer, &pp);
1212 if (syserror) {
1213 *syserror = 0;
1214 pp.syserror = syserror;
1215 nl_cb_err(cb, NL_CB_CUSTOM, __pickup_answer_syserr, &pp);
1216 }
1217
1218 err = nl_recvmsgs(sk, cb);
1219 if (err < 0)
1220 goto errout;
1221
1222 *result = pp.result;
1223errout:
1224 nl_cb_put(cb);
1225
1226 return err;
1227}
1228
1229/** @} */
1230
1231/**
1232 * @name Deprecated
1233 * @{
1234 */
1235
1236/**
1237 * @deprecated Please use nl_complete_msg()
1238 */
1239 void nl_auto_complete(struct nl_sock *sk, struct nl_msg *msg)
1240{
1241 nl_complete_msg(sk, msg);
1242}
1243
1244/**
1245 * @deprecated Please use nl_send_auto()
1246 */
1247 int nl_send_auto_complete(struct nl_sock *sk, struct nl_msg *msg)
1248{
1249 return nl_send_auto(sk, msg);
1250}
1251
1252
1253/** @} */
1254
1255/** @} */
1256
1257/** @} */
nl_cb_set
int nl_cb_set(struct nl_cb *cb, enum nl_cb_type type, enum nl_cb_kind kind, nl_recvmsg_msg_cb_t func, void *arg)
Set up a callback.
Definition handlers.c:290
nl_cb_clone
struct nl_cb * nl_cb_clone(struct nl_cb *orig)
Clone an existing callback handle.
Definition handlers.c:227
nl_cb_err
int nl_cb_err(struct nl_cb *cb, enum nl_cb_kind kind, nl_recvmsg_err_cb_t func, void *arg)
Set up an error callback.
Definition handlers.c:340
NL_STOP
@ NL_STOP
Stop parsing altogether and discard remaining messages.
Definition handlers.h:62
NL_OK
@ NL_OK
Proceed with whatever would come next.
Definition handlers.h:58
NL_SKIP
@ NL_SKIP
Skip this message.
Definition handlers.h:60
NL_CB_SKIPPED
@ NL_CB_SKIPPED
Message wants to be skipped.
Definition handlers.h:95
NL_CB_FINISH
@ NL_CB_FINISH
Last message in a series of multi part messages received.
Definition handlers.h:91
NL_CB_MSG_OUT
@ NL_CB_MSG_OUT
Called for every message sent out except for nl_sendto()
Definition handlers.h:101
NL_CB_DUMP_INTR
@ NL_CB_DUMP_INTR
Flag NLM_F_DUMP_INTR is set in message.
Definition handlers.h:109
NL_CB_MSG_IN
@ NL_CB_MSG_IN
Called for every message received.
Definition handlers.h:99
NL_CB_OVERRUN
@ NL_CB_OVERRUN
Report received that data was lost.
Definition handlers.h:93
NL_CB_VALID
@ NL_CB_VALID
Message is valid.
Definition handlers.h:89
NL_CB_ACK
@ NL_CB_ACK
Message is an acknowledgement.
Definition handlers.h:97
NL_CB_SEQ_CHECK
@ NL_CB_SEQ_CHECK
Called instead of internal sequence number checking.
Definition handlers.h:105
NL_CB_SEND_ACK
@ NL_CB_SEND_ACK
Sending of an acknowledge message has been requested.
Definition handlers.h:107
NL_CB_INVALID
@ NL_CB_INVALID
Message is malformed and invalid.
Definition handlers.h:103
NL_CB_CUSTOM
@ NL_CB_CUSTOM
Customized handler specified by the user.
Definition handlers.h:77
nlmsg_ok
int nlmsg_ok(const struct nlmsghdr *nlh, int remaining)
check if the netlink message fits into the remaining bytes
Definition msg.c:185
NL_AUTO_PORT
#define NL_AUTO_PORT
Will cause the netlink port to be set to the port assigned to the netlink icoket ust before sending t...
Definition msg.h:29
nlmsg_hdr
struct nlmsghdr * nlmsg_hdr(struct nl_msg *n)
Return actual netlink message.
Definition msg.c:550
nlmsg_alloc_simple
struct nl_msg * nlmsg_alloc_simple(int nlmsgtype, int flags)
Allocate a new netlink message.
Definition msg.c:352
nlmsg_data
void * nlmsg_data(const struct nlmsghdr *nlh)
Return pointer to message payload.
Definition msg.c:108
nlmsg_next
struct nlmsghdr * nlmsg_next(struct nlmsghdr *nlh, int *remaining)
next netlink message in message stream
Definition msg.c:200
nlmsg_convert
struct nl_msg * nlmsg_convert(struct nlmsghdr *hdr)
Convert a netlink message received from a netlink socket to a nl_msg.
Definition msg.c:393
nlmsg_free
void nlmsg_free(struct nl_msg *msg)
Release a reference from an netlink message.
Definition msg.c:572
NL_AUTO_SEQ
#define NL_AUTO_SEQ
May be used to refer to a sequence number which should be automatically set just before sending the m...
Definition msg.h:40
nlmsg_append
int nlmsg_append(struct nl_msg *n, void *data, size_t len, int pad)
Append data to tail of a netlink message.
Definition msg.c:456
nlmsg_size
int nlmsg_size(int payload)
Calculates size of netlink message based on payload length.
Definition msg.c:57
nl_object_get
void nl_object_get(struct nl_object *obj)
Acquire a reference on a object.
Definition object.c:210
nl_send_iovec
int nl_send_iovec(struct nl_sock *sk, struct nl_msg *msg, struct iovec *iov, unsigned iovlen)
Transmit Netlink message (taking IO vector)
Definition nl.c:372
nl_send
int nl_send(struct nl_sock *sk, struct nl_msg *msg)
Transmit Netlink message.
Definition nl.c:446
nl_recv
int nl_recv(struct nl_sock *sk, struct sockaddr_nl *nla, unsigned char **buf, struct ucred **creds)
Receive data from netlink socket.
Definition nl.c:657
nl_recvmsgs_default
int nl_recvmsgs_default(struct nl_sock *sk)
Receive a set of message from a netlink socket using handlers in nl_sock.
Definition nl.c:1093
nl_send_auto
int nl_send_auto(struct nl_sock *sk, struct nl_msg *msg)
Finalize and transmit Netlink message.
Definition nl.c:515
nl_complete_msg
void nl_complete_msg(struct nl_sock *sk, struct nl_msg *msg)
Finalize Netlink message.
Definition nl.c:480
nl_send_sync
int nl_send_sync(struct nl_sock *sk, struct nl_msg *msg)
Finalize and transmit Netlink message and wait for ACK or error message.
Definition nl.c:547
nl_auto_complete
void nl_auto_complete(struct nl_sock *sk, struct nl_msg *msg)
Definition nl.c:1239
nl_connect
int nl_connect(struct nl_sock *sk, int protocol)
Create file descriptor and bind socket.
Definition nl.c:102
nl_recvmsgs_report
int nl_recvmsgs_report(struct nl_sock *sk, struct nl_cb *cb)
Receive a set of messages from a netlink socket and report parsed messages.
Definition nl.c:1052
nl_pickup_keep_syserr
int nl_pickup_keep_syserr(struct nl_sock *sk, int(*parser)(struct nl_cache_ops *, struct sockaddr_nl *, struct nlmsghdr *, struct nl_parser_param *), struct nl_object **result, int *syserror)
Pickup netlink answer, parse is and return object with preserving system error.
Definition nl.c:1195
nl_send_auto_complete
int nl_send_auto_complete(struct nl_sock *sk, struct nl_msg *msg)
Definition nl.c:1247
nl_recvmsgs
int nl_recvmsgs(struct nl_sock *sk, struct nl_cb *cb)
Receive a set of messages from a netlink socket.
Definition nl.c:1077
nl_pickup
int nl_pickup(struct nl_sock *sk, int(*parser)(struct nl_cache_ops *, struct sockaddr_nl *, struct nlmsghdr *, struct nl_parser_param *), struct nl_object **result)
Pickup netlink answer, parse is and return object.
Definition nl.c:1178
nl_close
void nl_close(struct nl_sock *sk)
Close Netlink socket.
Definition nl.c:229
nl_sendmsg
int nl_sendmsg(struct nl_sock *sk, struct nl_msg *msg, struct msghdr *hdr)
Transmit Netlink message using sendmsg()
Definition nl.c:326
nl_sendto
int nl_sendto(struct nl_sock *sk, void *buf, size_t size)
Transmit raw data over Netlink socket.
Definition nl.c:269
nl_wait_for_ack
int nl_wait_for_ack(struct nl_sock *sk)
Wait for ACK.
Definition nl.c:1112
nl_send_simple
int nl_send_simple(struct nl_sock *sk, int type, int flags, void *buf, size_t size)
Construct and transmit a Netlink message.
Definition nl.c:579
nl_socket_set_local_port
void nl_socket_set_local_port(struct nl_sock *sk, uint32_t port)
Set local port of socket.
Definition socket.c:428
nl_socket_use_seq
unsigned int nl_socket_use_seq(struct nl_sock *sk)
Use next sequence number.
Definition socket.c:318
nl_socket_set_buffer_size
int nl_socket_set_buffer_size(struct nl_sock *sk, int rxbuf, int txbuf)
Set socket buffer size of netlink socket.
Definition socket.c:837
nl_cb
Definition nl-core.h:10
nl_cb::cb_recvmsgs_ow
int(* cb_recvmsgs_ow)(struct nl_sock *, struct nl_cb *)
May be used to replace nl_recvmsgs with your own implementation in all internal calls to nl_recvmsgs.
Definition nl-core.h:19
nl_cb::cb_recv_ow
int(* cb_recv_ow)(struct nl_sock *, struct sockaddr_nl *, unsigned char **, struct ucred **)
Overwrite internal calls to nl_recv, must return the number of octets read and allocate a buffer for ...
Definition nl-core.h:24
nl_cb::cb_send_ow
int(* cb_send_ow)(struct nl_sock *, struct nl_msg *)
Overwrites internal calls to nl_send, must send the netlink message.
Definition nl-core.h:31