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-2008 Thomas Graf <tgraf@suug.ch>
16 * @par 1) Connecting the socket
18 * // Bind and connect the socket to a protocol, NETLINK_ROUTE in this example.
19 * nl_connect(sk, NETLINK_ROUTE);
22 * @par 2) Sending data
24 * // The most rudimentary method is to use nl_sendto() simply pushing
25 * // a piece of data to the other netlink peer. This method is not
27 * const char buf[] = { 0x01, 0x02, 0x03, 0x04 };
28 * nl_sendto(sk, buf, sizeof(buf));
30 * // A more comfortable interface is nl_send() taking a pointer to
31 * // a netlink message.
32 * struct nl_msg *msg = my_msg_builder();
33 * nl_send(sk, nlmsg_hdr(msg));
35 * // nl_sendmsg() provides additional control over the sendmsg() message
36 * // header in order to allow more specific addressing of multiple peers etc.
37 * struct msghdr hdr = { ... };
38 * nl_sendmsg(sk, nlmsg_hdr(msg), &hdr);
40 * // You're probably too lazy to fill out the netlink pid, sequence number
41 * // and message flags all the time. nl_send_auto_complete() automatically
42 * // extends your message header as needed with an appropriate sequence
43 * // number, the netlink pid stored in the netlink socket and the message
44 * // flags NLM_F_REQUEST and NLM_F_ACK (if not disabled in the socket)
45 * nl_send_auto_complete(sk, nlmsg_hdr(msg));
47 * // Simple protocols don't require the complex message construction interface
48 * // and may favour nl_send_simple() to easly send a bunch of payload
49 * // encapsulated in a netlink message header.
50 * nl_send_simple(sk, MY_MSG_TYPE, 0, buf, sizeof(buf));
53 * @par 3) Receiving data
55 * // nl_recv() receives a single message allocating a buffer for the message
56 * // content and gives back the pointer to you.
57 * struct sockaddr_nl peer;
59 * nl_recv(sk, &peer, &msg);
61 * // nl_recvmsgs() receives a bunch of messages until the callback system
62 * // orders it to state, usually after receving a compolete multi part
64 * nl_recvmsgs(sk, my_callback_configuration);
66 * // nl_recvmsgs_default() acts just like nl_recvmsg() but uses the callback
67 * // configuration stored in the socket.
68 * nl_recvmsgs_default(sk);
70 * // In case you want to wait for the ACK to be recieved that you requested
71 * // with your latest message, you can call nl_wait_for_ack()
72 * nl_wait_for_ack(sk);
77 * // Close the socket first to release kernel memory
84 #include <netlink-local.h>
85 #include <netlink/netlink.h>
86 #include <netlink/utils.h>
87 #include <netlink/handlers.h>
88 #include <netlink/msg.h>
89 #include <netlink/attr.h>
92 * @name Connection Management
97 * Create and connect netlink socket.
98 * @arg sk Netlink socket.
99 * @arg protocol Netlink protocol to use.
101 * Creates a netlink socket using the specified protocol, binds the socket
102 * and issues a connection attempt.
104 * @return 0 on success or a negative error code.
106 int nl_connect(struct nl_sock
*sk
, int protocol
)
113 flags
= SOCK_CLOEXEC
;
116 sk
->s_fd
= socket(AF_NETLINK
, SOCK_RAW
| flags
, protocol
);
118 err
= -nl_syserr2nlerr(errno
);
122 if (!(sk
->s_flags
& NL_SOCK_BUFSIZE_SET
)) {
123 err
= nl_socket_set_buffer_size(sk
, 0, 0);
128 err
= bind(sk
->s_fd
, (struct sockaddr
*) &sk
->s_local
,
129 sizeof(sk
->s_local
));
131 err
= -nl_syserr2nlerr(errno
);
135 addrlen
= sizeof(sk
->s_local
);
136 err
= getsockname(sk
->s_fd
, (struct sockaddr
*) &sk
->s_local
,
139 err
= -nl_syserr2nlerr(errno
);
143 if (addrlen
!= sizeof(sk
->s_local
)) {
148 if (sk
->s_local
.nl_family
!= AF_NETLINK
) {
149 err
= -NLE_AF_NOSUPPORT
;
153 sk
->s_proto
= protocol
;
164 * Close/Disconnect netlink socket.
165 * @arg sk Netlink socket.
167 void nl_close(struct nl_sock
*sk
)
185 * Send raw data over netlink socket.
186 * @arg sk Netlink socket.
187 * @arg buf Data buffer.
188 * @arg size Size of data buffer.
189 * @return Number of characters written on success or a negative error code.
191 int nl_sendto(struct nl_sock
*sk
, void *buf
, size_t size
)
195 if (sk
->s_debug_tx_cb
)
196 sk
->s_debug_tx_cb(sk
->s_debug_tx_priv
, buf
, size
);
198 ret
= sendto(sk
->s_fd
, buf
, size
, 0, (struct sockaddr
*)
199 &sk
->s_peer
, sizeof(sk
->s_peer
));
201 return -nl_syserr2nlerr(errno
);
207 * Send netlink message with control over sendmsg() message header.
208 * @arg sk Netlink socket.
209 * @arg msg Netlink message to be sent.
210 * @arg hdr Sendmsg() message header.
211 * @return Number of characters sent on sucess or a negative error code.
213 int nl_sendmsg(struct nl_sock
*sk
, struct nl_msg
*msg
, struct msghdr
*hdr
)
219 .iov_base
= (void *) nlmsg_hdr(msg
),
220 .iov_len
= nlmsg_hdr(msg
)->nlmsg_len
,
226 nlmsg_set_src(msg
, &sk
->s_local
);
229 if (cb
->cb_set
[NL_CB_MSG_OUT
])
230 if (nl_cb_call(cb
, NL_CB_MSG_OUT
, msg
) != NL_OK
)
233 if (sk
->s_debug_tx_cb
)
234 sk
->s_debug_tx_cb(sk
->s_debug_tx_priv
, iov
.iov_base
, iov
.iov_len
);
236 ret
= sendmsg(sk
->s_fd
, hdr
, 0);
238 return -nl_syserr2nlerr(errno
);
245 * Send netlink message.
246 * @arg sk Netlink socket.
247 * @arg msg Netlink message to be sent.
249 * @return Number of characters sent on success or a negative error code.
251 int nl_send(struct nl_sock
*sk
, struct nl_msg
*msg
)
253 struct sockaddr_nl
*dst
;
256 struct msghdr hdr
= {
257 .msg_name
= (void *) &sk
->s_peer
,
258 .msg_namelen
= sizeof(struct sockaddr_nl
),
261 /* Overwrite destination if specified in the message itself, defaults
262 * to the peer address of the socket.
264 dst
= nlmsg_get_dst(msg
);
265 if (dst
->nl_family
== AF_NETLINK
)
268 /* Add credentials if present. */
269 creds
= nlmsg_get_creds(msg
);
271 char buf
[CMSG_SPACE(sizeof(struct ucred
))];
272 struct cmsghdr
*cmsg
;
274 hdr
.msg_control
= buf
;
275 hdr
.msg_controllen
= sizeof(buf
);
277 cmsg
= CMSG_FIRSTHDR(&hdr
);
278 cmsg
->cmsg_level
= SOL_SOCKET
;
279 cmsg
->cmsg_type
= SCM_CREDENTIALS
;
280 cmsg
->cmsg_len
= CMSG_LEN(sizeof(struct ucred
));
281 memcpy(CMSG_DATA(cmsg
), creds
, sizeof(struct ucred
));
284 return nl_sendmsg(sk
, msg
, &hdr
);
288 * Send netlink message and check & extend header values as needed.
289 * @arg sk Netlink socket.
290 * @arg msg Netlink message to be sent.
292 * Checks the netlink message \c nlh for completness and extends it
293 * as required before sending it out. Checked fields include pid,
294 * sequence nr, and flags.
297 * @return Number of characters sent or a negative error code.
299 int nl_send_auto_complete(struct nl_sock
*sk
, struct nl_msg
*msg
)
301 struct nlmsghdr
*nlh
;
302 struct nl_cb
*cb
= sk
->s_cb
;
304 nlh
= nlmsg_hdr(msg
);
305 if (nlh
->nlmsg_pid
== 0)
306 nlh
->nlmsg_pid
= sk
->s_local
.nl_pid
;
308 if (nlh
->nlmsg_seq
== 0)
309 nlh
->nlmsg_seq
= sk
->s_seq_next
++;
311 if (msg
->nm_protocol
== -1)
312 msg
->nm_protocol
= sk
->s_proto
;
314 nlh
->nlmsg_flags
|= NLM_F_REQUEST
;
316 if (!(sk
->s_flags
& NL_NO_AUTO_ACK
))
317 nlh
->nlmsg_flags
|= NLM_F_ACK
;
320 return cb
->cb_send_ow(sk
, msg
);
322 return nl_send(sk
, msg
);
326 * Send simple netlink message using nl_send_auto_complete()
327 * @arg sk Netlink socket.
328 * @arg type Netlink message type.
329 * @arg flags Netlink message flags.
330 * @arg buf Data buffer.
331 * @arg size Size of data buffer.
333 * Builds a netlink message with the specified type and flags and
334 * appends the specified data as payload to the message.
336 * @see nl_send_auto_complete()
337 * @return Number of characters sent on success or a negative error code.
339 int nl_send_simple(struct nl_sock
*sk
, int type
, int flags
, void *buf
,
345 msg
= nlmsg_alloc_simple(type
, flags
);
350 err
= nlmsg_append(msg
, buf
, size
, NLMSG_ALIGNTO
);
356 err
= nl_send_auto_complete(sk
, msg
);
371 * Receive data from netlink socket
372 * @arg sk Netlink socket.
373 * @arg nla Destination pointer for peer's netlink address.
374 * @arg buf Destination pointer for message content.
375 * @arg creds Destination pointer for credentials.
377 * Receives a netlink message, allocates a buffer in \c *buf and
378 * stores the message content. The peer's netlink address is stored
379 * in \c *nla. The caller is responsible for freeing the buffer allocated
380 * in \c *buf if a positive value is returned. Interrupted system calls
381 * are handled by repeating the read. The input buffer size is determined
382 * by peeking before the actual read is done.
384 * A non-blocking sockets causes the function to return immediately with
385 * a return value of 0 if no data is available.
387 * @return Number of octets read, 0 on EOF or a negative error code.
389 int nl_recv(struct nl_sock
*sk
, struct sockaddr_nl
*nla
,
390 unsigned char **buf
, struct ucred
**creds
)
394 static int page_size
= 0;
396 struct msghdr msg
= {
397 .msg_name
= (void *) nla
,
398 .msg_namelen
= sizeof(struct sockaddr_nl
),
405 struct cmsghdr
*cmsg
;
407 if (sk
->s_flags
& NL_MSG_PEEK
)
411 page_size
= getpagesize() * 4;
413 iov
.iov_len
= page_size
;
414 iov
.iov_base
= *buf
= calloc(1, iov
.iov_len
);
416 return -nl_syserr2nlerr(errno
);
418 if (sk
->s_flags
& NL_SOCK_PASSCRED
) {
419 msg
.msg_controllen
= CMSG_SPACE(sizeof(struct ucred
));
420 msg
.msg_control
= calloc(1, msg
.msg_controllen
);
424 n
= recvmsg(sk
->s_fd
, &msg
, flags
);
428 if (errno
== EINTR
) {
429 NL_DBG(3, "recvmsg() returned EINTR, retrying\n");
431 } else if (errno
== EAGAIN
) {
432 NL_DBG(3, "recvmsg() returned EAGAIN, aborting\n");
435 free(msg
.msg_control
);
438 return -nl_syserr2nlerr(errno
);
442 if (iov
.iov_len
< (size_t) n
||
443 msg
.msg_flags
& MSG_TRUNC
) {
444 /* Provided buffer is not long enough, enlarge it
447 iov
.iov_base
= *buf
= realloc(*buf
, iov
.iov_len
);
449 } else if (msg
.msg_flags
& MSG_CTRUNC
) {
450 msg
.msg_controllen
*= 2;
451 msg
.msg_control
= realloc(msg
.msg_control
, msg
.msg_controllen
);
453 } else if (flags
!= 0) {
454 /* Buffer is big enough, do the actual reading */
459 if (msg
.msg_namelen
!= sizeof(struct sockaddr_nl
)) {
460 free(msg
.msg_control
);
466 for (cmsg
= CMSG_FIRSTHDR(&msg
); cmsg
; cmsg
= CMSG_NXTHDR(&msg
, cmsg
)) {
467 if (cmsg
->cmsg_level
== SOL_SOCKET
&&
468 cmsg
->cmsg_type
== SCM_CREDENTIALS
) {
469 *creds
= calloc(1, sizeof(struct ucred
));
470 memcpy(*creds
, CMSG_DATA(cmsg
), sizeof(struct ucred
));
475 free(msg
.msg_control
);
479 free(msg
.msg_control
);
485 #define NL_CB_CALL(cb, type, msg) \
487 err = nl_cb_call(cb, type, msg); \
501 static int recvmsgs(struct nl_sock
*sk
, struct nl_cb
*cb
)
503 int n
, err
= 0, multipart
= 0;
504 unsigned char *buf
= NULL
;
505 struct nlmsghdr
*hdr
;
506 struct sockaddr_nl nla
= {0};
507 struct nl_msg
*msg
= NULL
;
508 struct ucred
*creds
= NULL
;
511 NL_DBG(3, "Attempting to read from %p\n", sk
);
513 n
= cb
->cb_recv_ow(sk
, &nla
, &buf
, &creds
);
515 n
= nl_recv(sk
, &nla
, &buf
, &creds
);
520 /* make clang analyzer happy */
521 assert(n
> 0 && buf
);
523 NL_DBG(3, "recvmsgs(%p): Read %d bytes\n", sk
, n
);
525 hdr
= (struct nlmsghdr
*) buf
;
526 while (nlmsg_ok(hdr
, n
)) {
527 NL_DBG(3, "recgmsgs(%p): Processing valid message...\n", sk
);
530 msg
= nlmsg_convert(hdr
);
536 if (sk
->s_debug_rx_cb
)
537 sk
->s_debug_rx_cb(sk
->s_debug_rx_priv
, hdr
, hdr
->nlmsg_len
);
539 nlmsg_set_proto(msg
, sk
->s_proto
);
540 nlmsg_set_src(msg
, &nla
);
542 nlmsg_set_creds(msg
, creds
);
544 /* Raw callback is the first, it gives the most control
545 * to the user and he can do his very own parsing. */
546 if (cb
->cb_set
[NL_CB_MSG_IN
])
547 NL_CB_CALL(cb
, NL_CB_MSG_IN
, msg
);
549 /* Sequence number checking. The check may be done by
550 * the user, otherwise a very simple check is applied
551 * enforcing strict ordering */
552 if (cb
->cb_set
[NL_CB_SEQ_CHECK
])
553 NL_CB_CALL(cb
, NL_CB_SEQ_CHECK
, msg
);
554 else if (hdr
->nlmsg_seq
!= sk
->s_seq_expect
) {
555 if (cb
->cb_set
[NL_CB_INVALID
])
556 NL_CB_CALL(cb
, NL_CB_INVALID
, msg
);
558 err
= -NLE_SEQ_MISMATCH
;
563 if (hdr
->nlmsg_type
== NLMSG_DONE
||
564 hdr
->nlmsg_type
== NLMSG_ERROR
||
565 hdr
->nlmsg_type
== NLMSG_NOOP
||
566 hdr
->nlmsg_type
== NLMSG_OVERRUN
) {
567 /* We can't check for !NLM_F_MULTI since some netlink
568 * users in the kernel are broken. */
570 NL_DBG(3, "recvmsgs(%p): Increased expected " \
571 "sequence number to %d\n",
572 sk
, sk
->s_seq_expect
);
575 if (hdr
->nlmsg_flags
& NLM_F_MULTI
)
578 /* Other side wishes to see an ack for this message */
579 if (hdr
->nlmsg_flags
& NLM_F_ACK
) {
580 if (cb
->cb_set
[NL_CB_SEND_ACK
])
581 NL_CB_CALL(cb
, NL_CB_SEND_ACK
, msg
);
583 /* FIXME: implement */
587 /* messages terminates a multpart message, this is
588 * usually the end of a message and therefore we slip
589 * out of the loop by default. the user may overrule
590 * this action by skipping this packet. */
591 if (hdr
->nlmsg_type
== NLMSG_DONE
) {
593 if (cb
->cb_set
[NL_CB_FINISH
])
594 NL_CB_CALL(cb
, NL_CB_FINISH
, msg
);
597 /* Message to be ignored, the default action is to
598 * skip this message if no callback is specified. The
599 * user may overrule this action by returning
601 else if (hdr
->nlmsg_type
== NLMSG_NOOP
) {
602 if (cb
->cb_set
[NL_CB_SKIPPED
])
603 NL_CB_CALL(cb
, NL_CB_SKIPPED
, msg
);
608 /* Data got lost, report back to user. The default action is to
609 * quit parsing. The user may overrule this action by retuning
610 * NL_SKIP or NL_PROCEED (dangerous) */
611 else if (hdr
->nlmsg_type
== NLMSG_OVERRUN
) {
612 if (cb
->cb_set
[NL_CB_OVERRUN
])
613 NL_CB_CALL(cb
, NL_CB_OVERRUN
, msg
);
615 err
= -NLE_MSG_OVERFLOW
;
620 /* Message carries a nlmsgerr */
621 else if (hdr
->nlmsg_type
== NLMSG_ERROR
) {
622 struct nlmsgerr
*e
= nlmsg_data(hdr
);
624 if (hdr
->nlmsg_len
< (unsigned) nlmsg_msg_size(sizeof(*e
))) {
625 /* Truncated error message, the default action
626 * is to stop parsing. The user may overrule
627 * this action by returning NL_SKIP or
628 * NL_PROCEED (dangerous) */
629 if (cb
->cb_set
[NL_CB_INVALID
])
630 NL_CB_CALL(cb
, NL_CB_INVALID
, msg
);
632 err
= -NLE_MSG_TRUNC
;
635 } else if (e
->error
) {
636 /* Error message reported back from kernel. */
638 err
= cb
->cb_err(&nla
, e
,
642 else if (err
== NL_SKIP
)
644 else if (err
== NL_STOP
) {
645 err
= -nl_syserr2nlerr(e
->error
);
649 err
= -nl_syserr2nlerr(e
->error
);
652 } else if (cb
->cb_set
[NL_CB_ACK
])
653 NL_CB_CALL(cb
, NL_CB_ACK
, msg
);
655 /* Valid message (not checking for MULTIPART bit to
656 * get along with broken kernels. NL_SKIP has no
658 if (cb
->cb_set
[NL_CB_VALID
])
659 NL_CB_CALL(cb
, NL_CB_VALID
, msg
);
662 hdr
= nlmsg_next(hdr
, &n
);
673 /* Multipart message not yet complete, continue reading */
674 goto continue_reading
;
687 * Receive a set of messages from a netlink socket.
688 * @arg sk Netlink socket.
689 * @arg cb set of callbacks to control behaviour.
691 * Repeatedly calls nl_recv() or the respective replacement if provided
692 * by the application (see nl_cb_overwrite_recv()) and parses the
693 * received data as netlink messages. Stops reading if one of the
694 * callbacks returns NL_STOP or nl_recv returns either 0 or a negative error code.
696 * A non-blocking sockets causes the function to return immediately if
697 * no data is available.
699 * @return 0 on success or a negative error code from nl_recv().
701 int nl_recvmsgs(struct nl_sock
*sk
, struct nl_cb
*cb
)
703 if (cb
->cb_recvmsgs_ow
)
704 return cb
->cb_recvmsgs_ow(sk
, cb
);
706 return recvmsgs(sk
, cb
);
710 static int ack_wait_handler(struct nl_msg
*msg
, void *arg
)
717 * @arg sk Netlink socket.
718 * @pre The netlink socket must be in blocking state.
720 * Waits until an ACK is received for the latest not yet acknowledged
723 int nl_wait_for_ack(struct nl_sock
*sk
)
728 cb
= nl_cb_clone(sk
->s_cb
);
732 nl_cb_set(cb
, NL_CB_ACK
, NL_CB_CUSTOM
, ack_wait_handler
, NULL
);
733 err
= nl_recvmsgs(sk
, cb
);