libnl  3.2.3
Core

Modules

 Abstract Address
 Caching
 Abstract Data
 Callbacks/Customization
 Messages
 

Netlink Message Construction/Parsing Interface.


 Socket
 Utilities

Connection Management

int nl_connect (struct nl_sock *sk, int protocol)
 Create and connect netlink socket.
void nl_close (struct nl_sock *sk)
 Close/Disconnect netlink socket.

Send

int nl_sendto (struct nl_sock *sk, void *buf, size_t size)
 Send raw data over netlink socket.
int nl_sendmsg (struct nl_sock *sk, struct nl_msg *msg, struct msghdr *hdr)
 Send netlink message with control over sendmsg() message header.
int nl_send_iovec (struct nl_sock *sk, struct nl_msg *msg, struct iovec *iov, unsigned iovlen)
 Send netlink message.
int nl_send (struct nl_sock *sk, struct nl_msg *msg)
 Send netlink message.
void nl_complete_msg (struct nl_sock *sk, struct nl_msg *msg)
void nl_auto_complete (struct nl_sock *sk, struct nl_msg *msg)
int nl_send_auto (struct nl_sock *sk, struct nl_msg *msg)
 Automatically complete and send a netlink message.
int nl_send_auto_complete (struct nl_sock *sk, struct nl_msg *msg)
int nl_send_sync (struct nl_sock *sk, struct nl_msg *msg)
 Send netlink message and wait for response (sync request-response)
int nl_send_simple (struct nl_sock *sk, int type, int flags, void *buf, size_t size)
 Send simple netlink message using nl_send_auto_complete()

Receive

int nl_recv (struct nl_sock *sk, struct sockaddr_nl *nla, unsigned char **buf, struct ucred **creds)
 Receive data from netlink socket.
int nl_recvmsgs (struct nl_sock *sk, struct nl_cb *cb)
 Receive a set of messages from a netlink socket.
int nl_recvmsgs_default (struct nl_sock *sk)
 Receive a set of message from a netlink socket using handlers in nl_sock.
int nl_wait_for_ack (struct nl_sock *sk)
 Wait for ACK.
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.
#define NL_CB_CALL(cb, type, msg)

Detailed Description

1) Connecting the socket
 // Bind and connect the socket to a protocol, NETLINK_ROUTE in this example.
 nl_connect(sk, NETLINK_ROUTE);
2) Sending data
 // The most rudimentary method is to use nl_sendto() simply pushing
 // a piece of data to the other netlink peer. This method is not
 // recommended.
 const char buf[] = { 0x01, 0x02, 0x03, 0x04 };
 nl_sendto(sk, buf, sizeof(buf));

 // A more comfortable interface is nl_send() taking a pointer to
 // a netlink message.
 struct nl_msg *msg = my_msg_builder();
 nl_send(sk, nlmsg_hdr(msg));

 // nl_sendmsg() provides additional control over the sendmsg() message
 // header in order to allow more specific addressing of multiple peers etc.
 struct msghdr hdr = { ... };
 nl_sendmsg(sk, nlmsg_hdr(msg), &hdr);

 // You're probably too lazy to fill out the netlink pid, sequence number
 // and message flags all the time. nl_send_auto_complete() automatically
 // extends your message header as needed with an appropriate sequence
 // number, the netlink pid stored in the netlink socket and the message
 // flags NLM_F_REQUEST and NLM_F_ACK (if not disabled in the socket)
 nl_send_auto_complete(sk, nlmsg_hdr(msg));

 // Simple protocols don't require the complex message construction interface
 // and may favour nl_send_simple() to easly send a bunch of payload
 // encapsulated in a netlink message header.
 nl_send_simple(sk, MY_MSG_TYPE, 0, buf, sizeof(buf));
3) Receiving data
 // nl_recv() receives a single message allocating a buffer for the message
 // content and gives back the pointer to you.
 struct sockaddr_nl peer;
 unsigned char *msg;
 nl_recv(sk, &peer, &msg);

 // nl_recvmsgs() receives a bunch of messages until the callback system
 // orders it to state, usually after receving a compolete multi part
 // message series.
 nl_recvmsgs(sk, my_callback_configuration);

 // nl_recvmsgs_default() acts just like nl_recvmsg() but uses the callback
 // configuration stored in the socket.
 nl_recvmsgs_default(sk);

 // In case you want to wait for the ACK to be recieved that you requested
 // with your latest message, you can call nl_wait_for_ack()
 nl_wait_for_ack(sk);
4) Closing
 // Close the socket first to release kernel memory
 nl_close(sk);

Define Documentation

#define NL_CB_CALL (   cb,
  type,
  msg 
)
Value:
do { \
        err = nl_cb_call(cb, type, msg); \
        switch (err) { \
        case NL_OK: \
                err = 0; \
                break; \
        case NL_SKIP: \
                goto skip; \
        case NL_STOP: \
                goto stop; \
        default: \
                goto out; \
        } \
} while (0)

Definition at line 544 of file nl.c.


Function Documentation

int nl_connect ( struct nl_sock *  sk,
int  protocol 
)

Create and connect netlink socket.

Parameters:
skNetlink socket.
protocolNetlink protocol to use.

Creates a netlink socket using the specified protocol, binds the socket and issues a connection attempt.

Note:
SOCK_CLOEXEC is set on the socket if available.
Returns:
0 on success or a negative error code.

Definition at line 108 of file nl.c.

References nl_socket_set_buffer_size().

Referenced by nfnl_connect(), and nl_cache_mngr_alloc().

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

void nl_close ( struct nl_sock *  sk)

Close/Disconnect netlink socket.

Parameters:
skNetlink socket.

Definition at line 168 of file nl.c.

Referenced by nl_cache_mngr_free().

+ Here is the caller graph for this function:

int nl_sendto ( struct nl_sock *  sk,
void *  buf,
size_t  size 
)

Send raw data over netlink socket.

Parameters:
skNetlink socket.
bufData buffer.
sizeSize of data buffer.
Returns:
Number of characters written on success or a negative error code.

Definition at line 192 of file nl.c.

int nl_sendmsg ( struct nl_sock *  sk,
struct nl_msg *  msg,
struct msghdr *  hdr 
)

Send netlink message with control over sendmsg() message header.

Parameters:
skNetlink socket.
msgNetlink message to be sent.
hdrSendmsg() message header.
Returns:
Number of characters sent on sucess or a negative error code.

Definition at line 211 of file nl.c.

References NL_CB_MSG_OUT, and NL_OK.

Referenced by nl_send_iovec().

+ Here is the caller graph for this function:

int nl_send_iovec ( struct nl_sock *  sk,
struct nl_msg *  msg,
struct iovec *  iov,
unsigned  iovlen 
)

Send netlink message.

Parameters:
skNetlink socket.
msgNetlink message to be sent.
ioviovec to be sent.
iovlennumber of struct iovec to be sent.
See also:
nl_sendmsg()
Returns:
Number of characters sent on success or a negative error code.

Definition at line 241 of file nl.c.

References nl_sendmsg().

Referenced by nl_send().

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

int nl_send ( struct nl_sock *  sk,
struct nl_msg *  msg 
)

Send netlink message.

Parameters:
skNetlink socket.
msgNetlink message to be sent.
See also:
nl_sendmsg()
Returns:
Number of characters sent on success or a negative error code.

Definition at line 287 of file nl.c.

References nl_send_iovec(), and nlmsg_hdr().

Referenced by nl_send_auto().

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

int nl_send_auto ( struct nl_sock *  sk,
struct nl_msg *  msg 
)

Automatically complete and send a netlink message.

Parameters:
skNetlink socket.
msgNetlink message to be sent.

This function takes a netlink message and passes it on to nl_auto_complete() for completion.

Checks the netlink message nlh for completness and extends it as required before sending it out. Checked fields include pid, sequence nr, and flags.

See also:
nl_send()
Returns:
Number of characters sent or a negative error code.

Definition at line 337 of file nl.c.

References nl_send().

Referenced by nl_send_sync(), and rtnl_link_get_kernel().

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

int nl_send_sync ( struct nl_sock *  sk,
struct nl_msg *  msg 
)

Send netlink message and wait for response (sync request-response)

Parameters:
skNetlink socket
msgNetlink message to be sent

This function takes a netlink message and sends it using nl_send_auto(). It will then wait for the response (ACK or error message) to be received. Threfore this function will block until the operation has been completed.

Note:
Disabling auto-ack (nl_socket_disable_auto_ack()) will cause this function to return immediately after sending. In this case, it is the responsibility of the caller to handle any eventual error messages returned.
See also:
nl_send_auto().
Returns:
0 on success or a negative error code.

Definition at line 373 of file nl.c.

References nl_send_auto(), and nlmsg_free().

Referenced by rtnl_class_add(), rtnl_class_delete(), rtnl_cls_add(), rtnl_cls_change(), rtnl_cls_delete(), rtnl_link_add(), rtnl_link_delete(), rtnl_qdisc_add(), rtnl_qdisc_delete(), and rtnl_qdisc_update().

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

int nl_send_simple ( struct nl_sock *  sk,
int  type,
int  flags,
void *  buf,
size_t  size 
)

Send simple netlink message using nl_send_auto_complete()

Parameters:
skNetlink socket.
typeNetlink message type.
flagsNetlink message flags.
bufData buffer.
sizeSize of data buffer.

Builds a netlink message with the specified type and flags and appends the specified data as payload to the message.

See also:
nl_send_auto_complete()
Returns:
Number of characters sent on success or a negative error code.

Definition at line 399 of file nl.c.

References nlmsg_alloc_simple(), nlmsg_append(), and nlmsg_free().

Referenced by genl_send_simple(), nfnl_send_simple(), and nl_rtgen_request().

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

int nl_recv ( struct nl_sock *  sk,
struct sockaddr_nl *  nla,
unsigned char **  buf,
struct ucred **  creds 
)

Receive data from netlink socket.

Parameters:
skNetlink socket.
nlaDestination pointer for peer's netlink address.
bufDestination pointer for message content.
credsDestination pointer for credentials.

Receives a netlink message, allocates a buffer in *buf and stores the message content. The peer's netlink address is stored in *nla. The caller is responsible for freeing the buffer allocated in *buf if a positive value is returned. Interrupted system calls are handled by repeating the read. The input buffer size is determined by peeking before the actual read is done.

A non-blocking sockets causes the function to return immediately with a return value of 0 if no data is available.

Returns:
Number of octets read, 0 on EOF or a negative error code.

Definition at line 449 of file nl.c.

int nl_recvmsgs ( struct nl_sock *  sk,
struct nl_cb *  cb 
)

Receive a set of messages from a netlink socket.

Parameters:
skNetlink socket.
cbset of callbacks to control behaviour.

Repeatedly calls nl_recv() or the respective replacement if provided by the application (see nl_cb_overwrite_recv()) and parses the received data as netlink messages. Stops reading if one of the callbacks returns NL_STOP or nl_recv returns either 0 or a negative error code.

A non-blocking sockets causes the function to return immediately if no data is available.

Returns:
0 on success or a negative error code from nl_recv().

Definition at line 775 of file nl.c.

Referenced by nl_pickup(), nl_recvmsgs_default(), and nl_wait_for_ack().

+ Here is the caller graph for this function:

int nl_recvmsgs_default ( struct nl_sock *  sk)

Receive a set of message from a netlink socket using handlers in nl_sock.

Parameters:
skNetlink socket.

Calls nl_recvmsgs() with the handlers configured in the netlink socket.

Definition at line 789 of file nl.c.

References nl_recvmsgs().

Referenced by nl_cache_mngr_data_ready().

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

int nl_wait_for_ack ( struct nl_sock *  sk)

Wait for ACK.

Parameters:
skNetlink socket.
Precondition:
The netlink socket must be in blocking state.

Waits until an ACK is received for the latest not yet acknowledged netlink message.

Definition at line 808 of file nl.c.

References NL_CB_ACK, nl_cb_clone(), NL_CB_CUSTOM, nl_cb_set(), and nl_recvmsgs().

Referenced by rtnl_link_get_kernel().

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

int nl_pickup ( struct nl_sock *  sk,
int(*)(struct nl_cache_ops *, struct sockaddr_nl *, struct nlmsghdr *, struct nl_parser_param *)  parser,
struct nl_object **  result 
)

Pickup netlink answer, parse is and return object.

Parameters:
skNetlink socket
parserParser function to parse answer
resultResult pointer to return parsed object
Returns:
0 on success or a negative error code.

Definition at line 866 of file nl.c.

References nl_cb_clone(), NL_CB_CUSTOM, nl_cb_set(), NL_CB_VALID, and nl_recvmsgs().

Referenced by rtnl_link_get_kernel().

+ Here is the call graph for this function:

+ Here is the caller graph for this function: