2 * lib/nl.c Core Netlink Interface
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation version 2.1
9 * Copyright (c) 2003-2006 Thomas Graf <tgraf@suug.ch>
13 * @defgroup nl Core Netlink API
16 * @par 1) Creating the netlink handle
18 * struct nl_handle *handle;
20 * // Allocate and initialize a new netlink handle
21 * handle = nl_handle_new();
23 * // Are multiple handles being allocated? You have to provide a unique
24 * // netlink process id and overwrite the default local process id.
25 * nl_handle_set_pid(handle, MY_UNIQUE_PID);
27 * // Is this socket used for event processing? You need to disable sequence
28 * // number checking in order to be able to receive messages not explicitely
30 * nl_disable_sequence_check(handle);
32 * // Use nl_handle_get_fd() to fetch the file description, for example to
33 * // put a socket into non-blocking i/o mode.
34 * fcntl(nl_handle_get_fd(handle), F_SETFL, O_NONBLOCK);
37 * @par 2) Joining Groups
39 * // You may join/subscribe to as many groups as you want, don't forget
40 * // to eventually disable sequence number checking. Note: Joining must
41 * // be done before connecting/binding the socket.
42 * nl_join_groups(handle, GROUP_ID1 | GROUP_ID2);
45 * @par 3) Connecting the socket
47 * // Bind and connect the socket to a protocol, NETLINK_ROUTE in this example.
48 * nl_connect(handle, NETLINK_ROUTE);
51 * @par 4) Sending data
53 * // The most rudimentary method is to use nl_sendto() simply pushing
54 * // a piece of data to the other netlink peer. This method is not
56 * const char buf[] = { 0x01, 0x02, 0x03, 0x04 };
57 * nl_sendto(handle, buf, sizeof(buf));
59 * // A more comfortable interface is nl_send() taking a pointer to
60 * // a netlink message.
61 * struct nl_msg *msg = my_msg_builder();
62 * nl_send(handle, nlmsg_hdr(msg));
64 * // nl_sendmsg() provides additional control over the sendmsg() message
65 * // header in order to allow more specific addressing of multiple peers etc.
66 * struct msghdr hdr = { ... };
67 * nl_sendmsg(handle, nlmsg_hdr(msg), &hdr);
69 * // You're probably too lazy to fill out the netlink pid, sequence number
70 * // and message flags all the time. nl_send_auto_complete() automatically
71 * // extends your message header as needed with an appropriate sequence
72 * // number, the netlink pid stored in the netlink handle and the message
73 * // flags NLM_F_REQUEST and NLM_F_ACK
74 * nl_send_auto_complete(handle, nlmsg_hdr(msg));
76 * // Simple protocols don't require the complex message construction interface
77 * // and may favour nl_send_simple() to easly send a bunch of payload
78 * // encapsulated in a netlink message header.
79 * nl_send_simple(handle, MY_MSG_TYPE, 0, buf, sizeof(buf));
82 * @par 5) Receiving data
84 * // nl_recv() receives a single message allocating a buffer for the message
85 * // content and gives back the pointer to you.
86 * struct sockaddr_nl peer;
88 * nl_recv(handle, &peer, &msg);
90 * // nl_recvmsgs() receives a bunch of messages until the callback system
91 * // orders it to state, usually after receving a compolete multi part
93 * nl_recvmsgs(handle, my_callback_configuration);
95 * // nl_recvmsgs_def() acts just like nl_recvmsg() but uses the callback
96 * // configuration stored in the handle.
97 * nl_recvmsgs_def(handle);
99 * // In case you want to wait for the ACK to be recieved that you requested
100 * // with your latest message, you can call nl_wait_for_ack()
101 * nl_wait_for_ack(handle);
104 * @par 6) Cleaning up
106 * // Close the socket first to release kernel memory
109 * // Finally destroy the netlink handle
110 * nl_handle_destroy(handle);
116 #include <netlink-local.h>
117 #include <netlink/netlink.h>
118 #include <netlink/utils.h>
119 #include <netlink/handlers.h>
120 #include <netlink/msg.h>
121 #include <netlink/attr.h>
124 * @name Handle Management
129 * Allocate and initialize new non-default netlink handle.
130 * @arg kind Kind of callback handler to use per default.
132 * Allocates and initializes a new netlink handle, the netlink process id
133 * is set to the local process id which may conflict if multiple handles
134 * are created, therefore you may have to overwrite it using
135 * nl_handle_set_pid(). The initial sequence number is initialized to the
138 * @return Newly allocated netlink handle or NULL.
140 struct nl_handle *nl_handle_alloc_nondefault(enum nl_cb_kind kind)
142 struct nl_handle *handle;
144 handle = calloc(1, sizeof(*handle));
148 handle->h_cb = nl_cb_new(kind);
152 handle->h_local.nl_family = AF_NETLINK;
153 handle->h_peer.nl_family = AF_NETLINK;
154 handle->h_local.nl_pid = getpid();
155 handle->h_seq_expect = handle->h_seq_next = time(0);
159 nl_handle_destroy(handle);
165 * Allocate and initialize new netlink handle.
167 * Allocates and initializes a new netlink handle, the netlink process id
168 * is set to the local process id which may conflict if multiple handles
169 * are created, therefore you may have to overwrite it using
170 * nl_handle_set_pid(). The initial sequence number is initialized to the
171 * current UNIX time. The default callback (NL_CB_DEFAULT) handlers are
174 * @return Newly allocated netlink handle or NULL.
176 struct nl_handle *nl_handle_alloc(void)
178 return nl_handle_alloc_nondefault(NL_CB_DEFAULT);
182 * Destroy netlink handle.
183 * @arg handle Netlink handle.
185 void nl_handle_destroy(struct nl_handle *handle)
190 nl_cb_destroy(handle->h_cb);
202 * Set socket buffer size of netlink handle.
203 * @arg handle Netlink handle.
204 * @arg rxbuf New receive socket buffer size in bytes.
205 * @arg txbuf New transmit socket buffer size in bytes.
207 * Sets the socket buffer size of a netlink handle to the specified
208 * values \c rxbuf and \c txbuf. Providing a value of \c 0 assumes a
209 * good default value.
211 * @note It is not required to call this function prior to nl_connect().
212 * @return 0 on sucess or a negative error code.
214 int nl_set_buffer_size(struct nl_handle *handle, int rxbuf, int txbuf)
224 err = setsockopt(handle->h_fd, SOL_SOCKET, SO_SNDBUF,
225 &txbuf, sizeof(txbuf));
227 return nl_error(errno, "setsockopt(SO_SNDBUF) failed");
229 err = setsockopt(handle->h_fd, SOL_SOCKET, SO_RCVBUF,
230 &rxbuf, sizeof(rxbuf));
232 return nl_error(errno, "setsockopt(SO_RCVBUF) failed");
234 handle->h_flags |= NL_SOCK_BUFSIZE_SET;
240 * Enable/disable credential passing on netlink handle.
241 * @arg handle Netlink handle
242 * @arg state New state (0 - disabled, 1 - enabled)
244 int nl_set_passcred(struct nl_handle *handle, int state)
248 err = setsockopt(handle->h_fd, SOL_SOCKET, SO_PASSCRED,
249 &state, sizeof(state));
251 return nl_error(errno, "setsockopt(SO_PASSCRED) failed");
254 handle->h_flags |= NL_SOCK_PASSCRED;
256 handle->h_flags &= ~NL_SOCK_PASSCRED;
262 * Join multicast groups.
263 * @arg handle Netlink handle.
264 * @arg groups Bitmask of groups to join.
266 * @note Joining of groups must be done prior to connecting/binding
267 * the socket (nl_connect()).
269 void nl_join_groups(struct nl_handle *handle, int groups)
271 handle->h_local.nl_groups |= groups;
275 #define SOL_NETLINK 270
278 int nl_join_group(struct nl_handle *handle, int group)
282 err = setsockopt(handle->h_fd, SOL_NETLINK, NETLINK_ADD_MEMBERSHIP,
283 &group, sizeof(group));
285 return nl_error(errno, "setsockopt(NETLINK_ADD_MEMBERSHIP) "
291 static int noop_seq_check(struct nl_msg *msg, void *arg)
297 * Disable sequence number checking.
298 * @arg handle Netlink handle.
300 * Disables checking of sequence numbers on the netlink handle. This is
301 * required to allow messages to be processed which were not requested by
302 * a preceding request message, e.g. netlink events.
304 void nl_disable_sequence_check(struct nl_handle *handle)
306 nl_cb_set(nl_handle_get_cb(handle), NL_CB_SEQ_CHECK,
307 NL_CB_CUSTOM, noop_seq_check, NULL);
313 * @name Acccess Functions
318 * Get netlink process identifier of netlink handle.
319 * @arg handle Netlink handle.
320 * @return Netlink process identifier.
322 pid_t nl_handle_get_pid(struct nl_handle *handle)
324 return handle->h_local.nl_pid;
328 * Set netlink process identifier of netlink handle.
329 * @arg handle Netlink handle.
330 * @arg pid New netlink process identifier.
332 void nl_handle_set_pid(struct nl_handle *handle, pid_t pid)
334 handle->h_local.nl_pid = pid;
338 * Get netlink process identifier of peer from netlink handle.
339 * @arg handle Netlink handle.
340 * @return Netlink process identifier.
342 pid_t nl_handle_get_peer_pid(struct nl_handle *handle)
344 return handle->h_peer.nl_pid;
348 * Set netlink process identifier of peer in netlink handle.
349 * @arg handle Netlink handle.
350 * @arg pid New netlink process identifier.
352 void nl_handle_set_peer_pid(struct nl_handle *handle, pid_t pid)
354 handle->h_peer.nl_pid = pid;
358 * Get file descriptor of netlink handle.
359 * @arg handle Netlink handle.
360 * @return File descriptor of netlink socket or -1 if not connected.
362 int nl_handle_get_fd(struct nl_handle *handle)
368 * Get local netlink address of netlink handle.
369 * @arg handle Netlink handle.
370 * @return Local netlink address.
372 struct sockaddr_nl *nl_handle_get_local_addr(struct nl_handle *handle)
374 return &handle->h_local;
378 * Get peer netlink address of netlink handle.
379 * @arg handle Netlink handle.
380 * @note The peer address is undefined while the socket is unconnected.
381 * @return Netlink address of the peer.
383 struct sockaddr_nl *nl_handle_get_peer_addr(struct nl_handle *handle)
385 return &handle->h_peer;
389 * Get callback configuration of netlink handle.
390 * @arg handle Netlink handle.
391 * @return Currently active callback configuration or NULL if not available.
393 struct nl_cb *nl_handle_get_cb(struct nl_handle *handle)
401 * @name Connection Management
406 * Create and connect netlink socket.
407 * @arg handle Netlink handle.
408 * @arg protocol Netlink protocol to use.
410 * Creates a netlink socket using the specified protocol, binds the socket
411 * and issues a connection attempt.
413 * @return 0 on success or a negative error code.
415 int nl_connect(struct nl_handle *handle, int protocol)
420 handle->h_fd = socket(AF_NETLINK, SOCK_RAW, protocol);
421 if (handle->h_fd < 0)
422 return nl_error(1, "socket(AF_NETLINK, ...) failed");
424 if (!(handle->h_flags & NL_SOCK_BUFSIZE_SET)) {
425 err = nl_set_buffer_size(handle, 0, 0);
430 err = bind(handle->h_fd, (struct sockaddr*) &handle->h_local,
431 sizeof(handle->h_local));
433 return nl_error(1, "bind() failed");
435 addrlen = sizeof(handle->h_local);
436 err = getsockname(handle->h_fd, (struct sockaddr *) &handle->h_local,
439 return nl_error(1, "getsockname failed");
441 if (addrlen != sizeof(handle->h_local))
442 return nl_error(EADDRNOTAVAIL, "Invalid address length");
444 if (handle->h_local.nl_family != AF_NETLINK)
445 return nl_error(EPFNOSUPPORT, "Address format not supported");
447 handle->h_proto = protocol;
453 * Close/Disconnect netlink socket.
454 * @arg handle Netlink handle
456 void nl_close(struct nl_handle *handle)
458 if (handle->h_fd >= 0) {
474 * Send raw data over netlink socket.
475 * @arg handle Netlink handle.
476 * @arg buf Data buffer.
477 * @arg size Size of data buffer.
478 * @return Number of characters written on success or a negative error code.
480 int nl_sendto(struct nl_handle *handle, void *buf, size_t size)
484 ret = sendto(handle->h_fd, buf, size, 0, (struct sockaddr *)
485 &handle->h_peer, sizeof(handle->h_peer));
487 return nl_errno(errno);
493 * Send netlink message with control over sendmsg() message header.
494 * @arg handle Netlink handle.
495 * @arg msg Netlink message to be sent.
496 * @arg hdr Sendmsg() message header.
497 * @return Number of characters sent on sucess or a negative error code.
499 int nl_sendmsg(struct nl_handle *handle, struct nl_msg *msg, struct msghdr *hdr)
505 .iov_base = (void *) nlmsg_hdr(msg),
506 .iov_len = nlmsg_hdr(msg)->nlmsg_len,
512 nlmsg_set_src(msg, &handle->h_local);
514 cb = nl_handle_get_cb(handle);
515 if (cb->cb_set[NL_CB_MSG_OUT])
516 if (nl_cb_call(cb, NL_CB_MSG_OUT, msg) != NL_PROCEED)
519 ret = sendmsg(handle->h_fd, hdr, 0);
521 return nl_errno(errno);
528 * Send netlink message.
529 * @arg handle Netlink handle
530 * @arg msg Netlink message to be sent.
532 * @return Number of characters sent on success or a negative error code.
534 int nl_send(struct nl_handle *handle, struct nl_msg *msg)
536 struct sockaddr_nl *dst;
539 struct msghdr hdr = {
540 .msg_name = (void *) &handle->h_peer,
541 .msg_namelen = sizeof(struct sockaddr_nl),
544 /* Overwrite destination if specified in the message itself, defaults
545 * to the peer address of the handle.
547 dst = nlmsg_get_dst(msg);
548 if (dst->nl_family == AF_NETLINK)
551 /* Add credentials if present. */
552 creds = nlmsg_get_creds(msg);
554 char buf[CMSG_SPACE(sizeof(struct ucred))];
555 struct cmsghdr *cmsg;
557 hdr.msg_control = buf;
558 hdr.msg_controllen = sizeof(buf);
560 cmsg = CMSG_FIRSTHDR(&hdr);
561 cmsg->cmsg_level = SOL_SOCKET;
562 cmsg->cmsg_type = SCM_CREDENTIALS;
563 cmsg->cmsg_len = CMSG_LEN(sizeof(struct ucred));
564 memcpy(CMSG_DATA(cmsg), creds, sizeof(struct ucred));
567 return nl_sendmsg(handle, msg, &hdr);
571 * Send netlink message and check & extend header values as needed.
572 * @arg handle Netlink handle.
573 * @arg msg Netlink message to be sent.
575 * Checks the netlink message \c nlh for completness and extends it
576 * as required before sending it out. Checked fields include pid,
577 * sequence nr, and flags.
580 * @return Number of characters sent or a negative error code.
582 int nl_send_auto_complete(struct nl_handle *handle, struct nl_msg *msg)
584 struct nlmsghdr *nlh;
586 nlh = nlmsg_hdr(msg);
587 if (nlh->nlmsg_pid == 0)
588 nlh->nlmsg_pid = handle->h_local.nl_pid;
590 if (nlh->nlmsg_seq == 0)
591 nlh->nlmsg_seq = handle->h_seq_next++;
593 nlh->nlmsg_flags |= (NLM_F_REQUEST | NLM_F_ACK);
595 if (handle->h_cb->cb_send_ow)
596 return handle->h_cb->cb_send_ow(handle, msg);
598 return nl_send(handle, msg);
602 * Send simple netlink message using nl_send_auto_complete()
603 * @arg handle Netlink handle.
604 * @arg type Netlink message type.
605 * @arg flags Netlink message flags.
606 * @arg buf Data buffer.
607 * @arg size Size of data buffer.
609 * Builds a netlink message with the specified type and flags and
610 * appends the specified data as payload to the message.
612 * @see nl_send_auto_complete()
613 * @return Number of characters sent on success or a negative error code.
615 int nl_send_simple(struct nl_handle *handle, int type, int flags, void *buf,
620 struct nlmsghdr nlh = {
621 .nlmsg_len = nlmsg_msg_size(0),
623 .nlmsg_flags = flags,
626 msg = nlmsg_build(&nlh);
628 return nl_errno(ENOMEM);
631 nlmsg_append(msg, buf, size, 1);
633 err = nl_send_auto_complete(handle, msg);
647 * Receive netlink message from netlink socket.
648 * @arg handle Netlink handle.
649 * @arg nla Destination pointer for peer's netlink address.
650 * @arg buf Destination pointer for message content.
651 * @arg creds Destination pointer for credentials.
653 * Receives a netlink message, allocates a buffer in \c *buf and
654 * stores the message content. The peer's netlink address is stored
655 * in \c *nla. The caller is responsible for freeing the buffer allocated
656 * in \c *buf if a positive value is returned. Interruped system calls
657 * are handled by repeating the read. The input buffer size is determined
658 * by peeking before the actual read is done.
660 * A non-blocking sockets causes the function to return immediately if
661 * no data is available.
663 * @return Number of octets read, 0 on EOF or a negative error code.
665 int nl_recv(struct nl_handle *handle, struct sockaddr_nl *nla,
666 unsigned char **buf, struct ucred **creds)
669 int flags = MSG_PEEK;
675 struct msghdr msg = {
676 .msg_name = (void *) nla,
677 .msg_namelen = sizeof(sizeof(struct sockaddr_nl)),
684 struct cmsghdr *cmsg;
686 iov.iov_base = *buf = calloc(1, iov.iov_len);
688 if (handle->h_flags & NL_SOCK_PASSCRED) {
689 msg.msg_controllen = CMSG_SPACE(sizeof(struct ucred));
690 msg.msg_control = calloc(1, msg.msg_controllen);
694 if ((n = recvmsg(handle->h_fd, &msg, flags)) <= 0) {
700 else if (errno == EAGAIN)
703 free(msg.msg_control);
705 return nl_error(errno, "recvmsg failed");
710 if (iov.iov_len < n) {
711 /* Provided buffer is not long enough, enlarge it
714 iov.iov_base = *buf = realloc(*buf, iov.iov_len);
716 } else if (msg.msg_flags & MSG_CTRUNC) {
717 msg.msg_controllen *= 2;
718 msg.msg_control = realloc(msg.msg_control, msg.msg_controllen);
720 } else if (flags != 0) {
721 /* Buffer is big enough, do the actual reading */
726 if (msg.msg_namelen != sizeof(struct sockaddr_nl)) {
727 free(msg.msg_control);
729 return nl_error(EADDRNOTAVAIL, "socket address size mismatch");
732 for (cmsg = CMSG_FIRSTHDR(&msg); cmsg; cmsg = CMSG_NXTHDR(&msg, cmsg)) {
733 if (cmsg->cmsg_level == SOL_SOCKET &&
734 cmsg->cmsg_type == SCM_CREDENTIALS) {
735 *creds = calloc(1, sizeof(struct ucred));
736 memcpy(*creds, CMSG_DATA(cmsg), sizeof(struct ucred));
741 free(msg.msg_control);
745 free(msg.msg_control);
752 * Receive a set of messages from a netlink socket.
753 * @arg handle netlink handle
754 * @arg cb set of callbacks to control the behaviour.
756 * Repeatedly calls nl_recv() and parses the messages as netlink
757 * messages. Stops reading if one of the callbacks returns
758 * NL_EXIT or nl_recv returns either 0 or a negative error code.
760 * A non-blocking sockets causes the function to return immediately if
761 * no data is available.
763 * @return 0 on success or a negative error code from nl_recv().
765 int nl_recvmsgs(struct nl_handle *handle, struct nl_cb *cb)
768 unsigned char *buf = NULL;
769 struct nlmsghdr *hdr;
770 struct sockaddr_nl nla = {0};
771 struct nl_msg *msg = NULL;
772 struct ucred *creds = NULL;
776 n = cb->cb_recv_ow(handle, &nla, &buf, &creds);
778 n = nl_recv(handle, &nla, &buf, &creds);
783 NL_DBG(3, "recvmsgs(%p): Read %d bytes\n", handle, n);
785 hdr = (struct nlmsghdr *) buf;
786 while (nlmsg_ok(hdr, n)) {
787 NL_DBG(3, "recgmsgs(%p): Processing valid message...\n",
791 msg = nlmsg_convert(hdr);
793 err = nl_errno(ENOMEM);
797 nlmsg_set_proto(msg, handle->h_proto);
798 nlmsg_set_src(msg, &nla);
800 nlmsg_set_creds(msg, creds);
802 /* Raw callback is the first, it gives the most control
803 * to the user and he can do his very own parsing. */
804 if (cb->cb_set[NL_CB_MSG_IN]) {
805 err = nl_cb_call(cb, NL_CB_MSG_IN, msg);
808 else if (err == NL_EXIT || err < 0)
812 /* Sequence number checking. The check may be done by
813 * the user, otherwise a very simple check is applied
814 * enforcing strict ordering */
815 if (cb->cb_set[NL_CB_SEQ_CHECK]) {
816 err = nl_cb_call(cb, NL_CB_SEQ_CHECK, msg);
819 else if (err == NL_EXIT || err < 0)
821 } else if (hdr->nlmsg_seq != handle->h_seq_expect) {
822 if (cb->cb_set[NL_CB_INVALID]) {
823 err = nl_cb_call(cb, NL_CB_INVALID, msg);
826 else if (err == NL_EXIT || err < 0)
832 if (hdr->nlmsg_type == NLMSG_DONE ||
833 hdr->nlmsg_type == NLMSG_ERROR ||
834 hdr->nlmsg_type == NLMSG_NOOP ||
835 hdr->nlmsg_type == NLMSG_OVERRUN) {
836 /* We can't check for !NLM_F_MULTI since some netlink
837 * users in the kernel are broken. */
838 handle->h_seq_expect++;
839 NL_DBG(3, "recvmsgs(%p): Increased expected " \
840 "sequence number to %d\n",
841 handle, handle->h_seq_expect);
844 /* Other side wishes to see an ack for this message */
845 if (hdr->nlmsg_flags & NLM_F_ACK) {
846 if (cb->cb_set[NL_CB_SEND_ACK]) {
847 err = nl_cb_call(cb, NL_CB_SEND_ACK, msg);
850 else if (err == NL_EXIT || err < 0)
853 /* FIXME: implement */
857 /* messages terminates a multpart message, this is
858 * usually the end of a message and therefore we slip
859 * out of the loop by default. the user may overrule
860 * this action by skipping this packet. */
861 if (hdr->nlmsg_type == NLMSG_DONE) {
862 if (cb->cb_set[NL_CB_FINISH]) {
863 err = nl_cb_call(cb, NL_CB_FINISH, msg);
866 else if (err == NL_EXIT || err < 0)
873 /* Message to be ignored, the default action is to
874 * skip this message if no callback is specified. The
875 * user may overrule this action by returning
877 else if (hdr->nlmsg_type == NLMSG_NOOP) {
878 if (cb->cb_set[NL_CB_SKIPPED]) {
879 err = nl_cb_call(cb, NL_CB_SKIPPED, msg);
882 else if (err == NL_EXIT || err < 0)
888 /* Data got lost, report back to user. The default action is to
889 * quit parsing. The user may overrule this action by retuning
890 * NL_SKIP or NL_PROCEED (dangerous) */
891 else if (hdr->nlmsg_type == NLMSG_OVERRUN) {
892 if (cb->cb_set[NL_CB_OVERRUN]) {
893 err = nl_cb_call(cb, NL_CB_OVERRUN, msg);
896 else if (err == NL_EXIT || err < 0)
902 /* Message carries a nlmsgerr */
903 else if (hdr->nlmsg_type == NLMSG_ERROR) {
904 struct nlmsgerr *e = nlmsg_data(hdr);
906 if (hdr->nlmsg_len < nlmsg_msg_size(sizeof(*e))) {
907 /* Truncated error message, the default action
908 * is to stop parsing. The user may overrule
909 * this action by returning NL_SKIP or
910 * NL_PROCEED (dangerous) */
911 if (cb->cb_set[NL_CB_INVALID]) {
912 err = nl_cb_call(cb, NL_CB_INVALID,
916 else if (err == NL_EXIT || err < 0)
920 } else if (e->error) {
921 /* Error message reported back from kernel. */
923 err = cb->cb_err(&nla, e,
927 else if (err == NL_EXIT || err < 0) {
934 nl_error(-e->error, "Netlink Error");
938 } else if (cb->cb_set[NL_CB_ACK]) {
940 err = nl_cb_call(cb, NL_CB_ACK, msg);
943 else if (err == NL_EXIT || err < 0)
947 /* Valid message (not checking for MULTIPART bit to
948 * get along with broken kernels. NL_SKIP has no
950 if (cb->cb_set[NL_CB_VALID]) {
951 err = nl_cb_call(cb, NL_CB_VALID, msg);
954 else if (err == NL_EXIT || err < 0)
959 hdr = nlmsg_next(hdr, &n);
969 /* Multipart message not yet complete, continue reading */
970 goto continue_reading;
981 * Receive a set of message from a netlink socket using handlers in nl_handle.
982 * @arg handle netlink handle
984 * Calls nl_recvmsgs() with the handlers configured in the netlink handle.
986 int nl_recvmsgs_def(struct nl_handle *handle)
988 if (handle->h_cb->cb_recvmsgs_ow)
989 return handle->h_cb->cb_recvmsgs_ow(handle, handle->h_cb);
991 return nl_recvmsgs(handle, handle->h_cb);
994 static int ack_wait_handler(struct nl_msg *msg, void *arg)
1001 * @arg handle netlink handle
1002 * @pre The netlink socket must be in blocking state.
1004 * Waits until an ACK is received for the latest not yet acknowledged
1007 int nl_wait_for_ack(struct nl_handle *handle)
1010 struct nl_cb *cb = nl_cb_clone(nl_handle_get_cb(handle));
1012 nl_cb_set(cb, NL_CB_ACK, NL_CB_CUSTOM, ack_wait_handler, NULL);
1014 err = nl_recvmsgs(handle, cb);
1023 * @name Netlink Family Translations
1027 static struct trans_tbl nlfamilies[] = {
1028 __ADD(NETLINK_ROUTE,route)
1029 __ADD(NETLINK_W1,w1)
1030 __ADD(NETLINK_USERSOCK,usersock)
1031 __ADD(NETLINK_FIREWALL,firewall)
1032 __ADD(NETLINK_INET_DIAG,inetdiag)
1033 __ADD(NETLINK_NFLOG,nflog)
1034 __ADD(NETLINK_XFRM,xfrm)
1035 __ADD(NETLINK_SELINUX,selinux)
1036 __ADD(NETLINK_ISCSI,iscsi)
1037 __ADD(NETLINK_AUDIT,audit)
1038 __ADD(NETLINK_FIB_LOOKUP,fib_lookup)
1039 __ADD(NETLINK_CONNECTOR,connector)
1040 __ADD(NETLINK_NETFILTER,netfilter)
1041 __ADD(NETLINK_IP6_FW,ip6_fw)
1042 __ADD(NETLINK_DNRTMSG,dnrtmsg)
1043 __ADD(NETLINK_KOBJECT_UEVENT,kobject_uevent)
1044 __ADD(NETLINK_GENERIC,generic)
1048 * Convert netlink family to character string.
1049 * @arg family Netlink family.
1050 * @arg buf Destination buffer.
1051 * @arg size Size of destination buffer.
1053 * Converts a netlink family to a character string and stores it in
1054 * the specified destination buffer.
1056 * @return The destination buffer or the family encoded in hexidecimal
1057 * form if no match was found.
1059 char * nl_nlfamily2str(int family, char *buf, size_t size)
1061 return __type2str(family, buf, size, nlfamilies,
1062 ARRAY_SIZE(nlfamilies));
1066 * Convert character string to netlink family.
1067 * @arg name Name of netlink family.
1069 * Converts the provided character string specifying a netlink
1070 * family to the corresponding numeric value.
1072 * @return Numeric netlink family or a negative value if no match was found.
1074 int nl_str2nlfamily(const char *name)
1076 return __str2type(name, nlfamilies, ARRAY_SIZE(nlfamilies));