netfilter
firewalling, NAT, and packet mangling for linux
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Modules Pages
Core
Collaboration diagram for Core:

Modules

 Abstract Address
 
 Caching
 
 Abstract Data
 
 Callbacks/Customization
 
 Messages
 Netlink Message Construction/Parsing Interface.
 
 Netlink socket helpers
 
 Utilities
 

Connection Management

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

Send

int nl_sendto (struct nl_sock *sk, void *buf, size_t size)
 Send raw data over netlink socket. More...
 
int nl_sendmsg (struct nl_sock *sk, struct nl_msg *msg, struct msghdr *hdr)
 Send netlink message with control over sendmsg() message header. More...
 
int nl_send_iovec (struct nl_sock *sk, struct nl_msg *msg, const struct iovec *iov, unsigned iovlen)
 Send netlink message. More...
 
int nl_send (struct nl_sock *sk, struct nl_msg *msg)
 Send netlink message. More...
 
void nl_auto_complete (struct nl_sock *sk, struct nl_msg *msg)
 
int nl_send_auto_complete (struct nl_sock *sk, struct nl_msg *msg)
 Send netlink message and check & extend header values as needed. More...
 
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() More...
 

Receive

int nl_recv (struct nl_sock *sk, struct sockaddr_nl *nla, unsigned char **buf, struct ucred **creds)
 Receive data from netlink socket. More...
 
int nl_recvmsgs (struct nl_sock *sk, struct nl_cb *cb)
 Receive a set of messages from a netlink socket. More...
 
int nl_recvmsgs_default (struct nl_sock *sk)
 Receive a set of message from a netlink socket using handlers in nl_sock. More...
 
int nl_wait_for_ack (struct nl_sock *sk)
 Wait for ACK. More...
 
#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.
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)
// 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.
// 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()
4) Closing
// Close the socket first to release kernel memory

Macro Definition 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)
struct expr
Stop parsing altogether and discard remaining messages.
Definition: handlers.h:65
Skip this message.
Definition: handlers.h:63
Proceed with wathever would come next.
Definition: handlers.h:61

Function Documentation

void nl_auto_complete ( struct nl_sock sk,
struct nl_msg msg 
)
void nl_close ( struct nl_sock sk)

Close/Disconnect netlink socket.

  • sk Netlink socket.

References nl_sock::s_fd, and nl_sock::s_proto.

Referenced by main(), and nl_cache_mngr_free().

Here is the caller graph for this function:

int nl_connect ( struct nl_sock sk,
int  protocol 
)

Create and connect netlink socket.

  • sk Netlink socket.
  • protocol Netlink protocol to use.

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

Returns
0 on success or a negative error code.

References sockaddr_nl::nl_family, NL_SOCK_BUFSIZE_SET, nl_socket_set_buffer_size(), nl_syserr2nlerr(), NLE_AF_NOSUPPORT, NLE_NOADDR, nl_sock::s_fd, nl_sock::s_flags, nl_sock::s_local, and nl_sock::s_proto.

Referenced by genl_connect(), nfnl_connect(), nl_cache_mngr_alloc(), and nl_cli_connect().

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.

  • sk Netlink socket.
  • nla Destination pointer for peer's netlink address.
  • buf Destination pointer for message content.
  • creds Destination 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. Interruped 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.

References flags, MSG_TRUNC, NL_DBG, NL_MSG_PEEK, NL_SOCK_PASSCRED, nl_syserr2nlerr(), NLE_NOADDR, NULL, nl_sock::s_fd, and nl_sock::s_flags.

Here is the call graph for this function:

int nl_recvmsgs ( struct nl_sock sk,
struct nl_cb cb 
)

Receive a set of messages from a netlink socket.

  • sk Netlink socket.
  • cb set 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().

References nl_cb::cb_recvmsgs_ow.

Referenced by __cache_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.

  • sk Netlink socket.

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

References nl_recvmsgs(), and nl_sock::s_cb.

Referenced by main(), and nl_cache_mngr_data_ready().

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.

  • sk Netlink socket.
  • msg Netlink message to be sent.
    See also
    nl_sendmsg()
    Returns
    Number of characters sent on success or a negative error code.

References nl_send_iovec(), nlmsg_hdr(), and nlmsghdr::nlmsg_len.

Referenced by nl_send_auto_complete().

Here is the call graph for this function:

Here is the caller graph for this function:

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

Send netlink message.

  • sk Netlink socket.
  • msg Netlink message to be sent.
  • iov iovec to be sent.
  • iovlen number of struct iovec to be sent.
    See also
    nl_sendmsg()
    Returns
    Number of characters sent on success or a negative error code.

References buf, sockaddr_nl::nl_family, nl_sendmsg(), nlmsg_get_creds(), nlmsg_get_dst(), NULL, and nl_sock::s_peer.

Referenced by nfnl_queue_msg_send_verdict_payload(), and nl_send().

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()

  • sk Netlink socket.
  • type Netlink message type.
  • flags Netlink message flags.
  • buf Data buffer.
  • size Size 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.

References nl_send_auto_complete(), NLE_NOMEM, NLMSG_ALIGNTO, 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_sendmsg ( struct nl_sock sk,
struct nl_msg msg,
struct msghdr *  hdr 
)

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

  • sk Netlink socket.
  • msg Netlink message to be sent.
  • hdr Sendmsg() message header.
    Returns
    Number of characters sent on sucess or a negative error code.

References nl_cb::cb_set, NL_CB_MSG_OUT, NL_DBG, NL_OK, nl_syserr2nlerr(), nlmsg_set_src(), nl_sock::s_cb, nl_sock::s_fd, and nl_sock::s_local.

Referenced by nl_send_iovec().

Here is the call graph for this function:

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.

  • sk Netlink socket.
  • buf Data buffer.
  • size Size of data buffer.
    Returns
    Number of characters written on success or a negative error code.

References nl_syserr2nlerr(), nl_sock::s_fd, and nl_sock::s_peer.

Here is the call graph for this function:

int nl_wait_for_ack ( struct nl_sock sk)

Wait for ACK.

  • sk Netlink socket.
    Precondition
    The netlink socket must be in blocking state.
    Waits until an ACK is received for the latest not yet acknowledged netlink message.

References NL_CB_ACK, nl_cb_clone(), NL_CB_CUSTOM, nl_cb_put(), nl_cb_set(), nl_recvmsgs(), NLE_NOMEM, NULL, and nl_sock::s_cb.

Referenced by rtnl_cls_add(), rtnl_cls_change(), and rtnl_cls_delete().

Here is the call graph for this function:

Here is the caller graph for this function: