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

Functions

int mnl_socket_get_fd (const struct mnl_socket *nl)
 mnl_socket_get_fd - obtain file descriptor from netlink socket More...
 
 EXPORT_SYMBOL (mnl_socket_get_fd)
 
unsigned int mnl_socket_get_portid (const struct mnl_socket *nl)
 mnl_socket_get_portid - obtain Netlink PortID from netlink socket More...
 
 EXPORT_SYMBOL (mnl_socket_get_portid)
 
struct mnl_socketmnl_socket_open (int bus)
 mnl_socket_open - open a netlink socket More...
 
 EXPORT_SYMBOL (mnl_socket_open)
 
struct mnl_socketmnl_socket_fdopen (int fd)
 mnl_socket_fdopen - associates a mnl_socket object with pre-existing socket. More...
 
 EXPORT_SYMBOL (mnl_socket_fdopen)
 
int mnl_socket_bind (struct mnl_socket *nl, unsigned int groups, pid_t pid)
 mnl_socket_bind - bind netlink socket More...
 
 EXPORT_SYMBOL (mnl_socket_bind)
 
ssize_t mnl_socket_sendto (const struct mnl_socket *nl, const void *buf, size_t len)
 mnl_socket_sendto - send a netlink message of a certain size More...
 
 EXPORT_SYMBOL (mnl_socket_sendto)
 
ssize_t mnl_socket_recvfrom (const struct mnl_socket *nl, void *buf, size_t bufsiz)
 mnl_socket_recvfrom - receive a netlink message More...
 
 EXPORT_SYMBOL (mnl_socket_recvfrom)
 
int mnl_socket_close (struct mnl_socket *nl)
 mnl_socket_close - close a given netlink socket More...
 
 EXPORT_SYMBOL (mnl_socket_close)
 
int mnl_socket_setsockopt (const struct mnl_socket *nl, int type, void *buf, socklen_t len)
 mnl_socket_setsockopt - set Netlink socket option More...
 
 EXPORT_SYMBOL (mnl_socket_setsockopt)
 
int mnl_socket_getsockopt (const struct mnl_socket *nl, int type, void *buf, socklen_t *len)
 mnl_socket_getsockopt - get a Netlink socket option More...
 
 EXPORT_SYMBOL (mnl_socket_getsockopt)
 

Allocation

struct nl_socknl_socket_alloc (void)
 Allocate new netlink socket. More...
 
struct nl_socknl_socket_alloc_cb (struct nl_cb *cb)
 Allocate new socket with custom callbacks. More...
 
void nl_socket_free (struct nl_sock *sk)
 Free a netlink socket. More...
 

Sequence Numbers

void nl_socket_disable_seq_check (struct nl_sock *sk)
 Disable sequence number checking. More...
 
unsigned int nl_socket_use_seq (struct nl_sock *sk)
 Use next sequence number. More...
 
void nl_socket_disable_auto_ack (struct nl_sock *sk)
 Disable automatic request for ACK. More...
 
void nl_socket_enable_auto_ack (struct nl_sock *sk)
 Enable automatic request for ACK (default) More...
 

Source Idenficiation

uint32_t nl_socket_get_local_port (struct nl_sock *sk)
 
void nl_socket_set_local_port (struct nl_sock *sk, uint32_t port)
 Set local port of socket. More...
 

Group Subscriptions

int nl_socket_add_memberships (struct nl_sock *sk, int group,...)
 Join groups. More...
 
int nl_socket_add_membership (struct nl_sock *sk, int group)
 
int nl_socket_drop_memberships (struct nl_sock *sk, int group,...)
 Leave groups. More...
 
int nl_socket_drop_membership (struct nl_sock *sk, int group)
 
void nl_join_groups (struct nl_sock *sk, int groups)
 Join multicast groups (deprecated) More...
 

Peer Identfication

uint32_t nl_socket_get_peer_port (struct nl_sock *sk)
 
void nl_socket_set_peer_port (struct nl_sock *sk, uint32_t port)
 

File Descriptor

int nl_socket_get_fd (struct nl_sock *sk)
 
int nl_socket_set_nonblocking (struct nl_sock *sk)
 Set file descriptor of socket to non-blocking state. More...
 
void nl_socket_enable_msg_peek (struct nl_sock *sk)
 Enable use of MSG_PEEK when reading from socket. More...
 
void nl_socket_disable_msg_peek (struct nl_sock *sk)
 Disable use of MSG_PEEK when reading from socket. More...
 

Callback Handler

struct nl_cbnl_socket_get_cb (struct nl_sock *sk)
 
void nl_socket_set_cb (struct nl_sock *sk, struct nl_cb *cb)
 
int nl_socket_modify_cb (struct nl_sock *sk, enum nl_cb_type type, enum nl_cb_kind kind, nl_recvmsg_msg_cb_t func, void *arg)
 Modify the callback handler associated to the socket. More...
 

Utilities

int nl_socket_set_buffer_size (struct nl_sock *sk, int rxbuf, int txbuf)
 Set socket buffer size of netlink socket. More...
 
int nl_socket_set_passcred (struct nl_sock *sk, int state)
 Enable/disable credential passing on netlink socket. More...
 
int nl_socket_recv_pktinfo (struct nl_sock *sk, int state)
 Enable/disable receival of additional packet information. More...
 

Detailed Description

Function Documentation

EXPORT_SYMBOL ( mnl_socket_get_fd  )
EXPORT_SYMBOL ( mnl_socket_get_portid  )
EXPORT_SYMBOL ( mnl_socket_open  )
EXPORT_SYMBOL ( mnl_socket_fdopen  )
EXPORT_SYMBOL ( mnl_socket_bind  )
EXPORT_SYMBOL ( mnl_socket_sendto  )
EXPORT_SYMBOL ( mnl_socket_recvfrom  )
EXPORT_SYMBOL ( mnl_socket_close  )
EXPORT_SYMBOL ( mnl_socket_setsockopt  )
EXPORT_SYMBOL ( mnl_socket_getsockopt  )
int mnl_socket_bind ( struct mnl_socket nl,
unsigned int  groups,
pid_t  pid 
)

mnl_socket_bind - bind netlink socket

Parameters
nlnetlink socket obtained via mnl_socket_open()
groupsthe group of message you're interested in
pidthe port ID you want to use (use zero for automatic selection)

On error, this function returns -1 and errno is appropriately set. On success, 0 is returned. You can use MNL_SOCKET_AUTOPID which is 0 for automatic port ID selection.

References mnl_socket::addr, mnl_socket::fd, sockaddr_nl::nl_family, sockaddr_nl::nl_groups, and sockaddr_nl::nl_pid.

Referenced by cthelper_init(), mnl_event_nssocket(), netlink_monitor(), nfct_mnl_open(), nft_batch_is_supported(), nft_compatible_revision(), nft_init(), and xtables_events_main().

Here is the caller graph for this function:

int mnl_socket_close ( struct mnl_socket nl)

mnl_socket_close - close a given netlink socket

Parameters
nlnetlink socket obtained via mnl_socket_open()

On error, this function returns -1 and errno is appropriately set. On success, it returns 0.

References mnl_socket::fd.

Referenced by cthelper_kill(), main(), mnl_event_nssocket(), nft_batch_is_supported(), nft_compatible_revision(), nft_fini(), nfts_socket_close(), and xtables_events_main().

Here is the caller graph for this function:

struct mnl_socket* mnl_socket_fdopen ( int  fd)

mnl_socket_fdopen - associates a mnl_socket object with pre-existing socket.

Parameters
fdpre-existing socket descriptor.

On error, it returns NULL and errno is appropriately set. Otherwise, it returns a valid pointer to the mnl_socket structure. It also sets the portID if the socket fd is already bound and it is AF_NETLINK.

Note that mnl_socket_get_portid() returns 0 if this function is used with non-netlink socket.

References mnl_socket::addr, mnl_socket::fd, sockaddr_nl::nl_family, and NULL.

Referenced by mnl_nssocket_open().

Here is the caller graph for this function:

int mnl_socket_get_fd ( const struct mnl_socket nl)

mnl_socket_get_fd - obtain file descriptor from netlink socket

Parameters
nlnetlink socket obtained via mnl_socket_open()

This function returns the file descriptor of a given netlink socket.

References mnl_socket::fd.

Referenced by cthelper_init(), handle_qacb(), and mnl_batch_talk().

Here is the caller graph for this function:

unsigned int mnl_socket_get_portid ( const struct mnl_socket nl)

mnl_socket_get_portid - obtain Netlink PortID from netlink socket

Parameters
nlnetlink socket obtained via mnl_socket_open()

This function returns the Netlink PortID of a given netlink socket. It's a common mistake to assume that this PortID equals the process ID which is not always true. This is the case if you open more than one socket that is binded to the same Netlink subsystem from the same process.

References mnl_socket::addr, and sockaddr_nl::nl_pid.

Referenced by cthelper_init(), mnl_batch_supported(), mnl_batch_talk(), nft_batch_is_supported(), nft_compatible_revision(), and nft_init().

Here is the caller graph for this function:

int mnl_socket_getsockopt ( const struct mnl_socket nl,
int  type,
void *  buf,
socklen_t len 
)

mnl_socket_getsockopt - get a Netlink socket option

Parameters
nlnetlink socket obtained via mnl_socket_open()
typetype of Netlink socket options
bufpointer to the buffer to store the value of this option
lensize of the information written in the buffer

On error, this function returns -1 and errno is appropriately set.

References mnl_socket::fd, and SOL_NETLINK.

struct mnl_socket* mnl_socket_open ( int  bus)

mnl_socket_open - open a netlink socket

Parameters
busthe netlink socket bus ID (see NETLINK_* constants)

On error, it returns NULL and errno is appropriately set. Otherwise, it returns a valid pointer to the mnl_socket structure.

References mnl_socket::fd, and NULL.

Referenced by cthelper_init(), nfct_mnl_open(), nft_batch_is_supported(), nft_compatible_revision(), nft_init(), and xtables_events_main().

Here is the caller graph for this function:

ssize_t mnl_socket_recvfrom ( const struct mnl_socket nl,
void *  buf,
size_t  bufsiz 
)

mnl_socket_recvfrom - receive a netlink message

Parameters
nlnetlink socket obtained via mnl_socket_open()
bufbuffer that you want to use to store the netlink message
bufsizsize of the buffer passed to store the netlink message

On error, it returns -1 and errno is appropriately set. If errno is set to ENOSPC, it means that the buffer that you have passed to store the netlink message is too small, so you have received a truncated message. To avoid this, you have to allocate a buffer of MNL_SOCKET_BUFFER_SIZE (which is 8KB, see linux/netlink.h for more information). Using this buffer size ensures that your buffer is big enough to store the netlink message without truncating it.

References buf, mnl_socket::fd, MSG_TRUNC, and NULL.

Referenced by handle_qacb(), mnl_batch_supported(), mnl_batch_talk(), mnl_talk(), nfct_mnl_talk(), nft_batch_is_supported(), nft_compatible_revision(), and xtables_events_main().

Here is the caller graph for this function:

ssize_t mnl_socket_sendto ( const struct mnl_socket nl,
const void *  buf,
size_t  len 
)

mnl_socket_sendto - send a netlink message of a certain size

Parameters
nlnetlink socket obtained via mnl_socket_open()
bufbuffer containing the netlink message to be sent
lennumber of bytes in the buffer that you want to send

On error, it returns -1 and errno is appropriately set. Otherwise, it returns the number of bytes sent.

References mnl_socket::fd, and sockaddr_nl::nl_family.

Referenced by mnl_batch_supported(), mnl_talk(), nfct_mnl_talk(), nft_batch_is_supported(), and nft_compatible_revision().

Here is the caller graph for this function:

int mnl_socket_setsockopt ( const struct mnl_socket nl,
int  type,
void *  buf,
socklen_t  len 
)

mnl_socket_setsockopt - set Netlink socket option

Parameters
nlnetlink socket obtained via mnl_socket_open()
typetype of Netlink socket options
bufthe buffer that contains the data about this option
lenthe size of the buffer passed

This function allows you to set some Netlink socket option. As of writing this (see linux/netlink.h), the existing options are:

  • #define NETLINK_ADD_MEMBERSHIP 1
  • #define NETLINK_DROP_MEMBERSHIP 2
  • #define NETLINK_PKTINFO 3
  • #define NETLINK_BROADCAST_ERROR 4
  • #define NETLINK_NO_ENOBUFS 5

In the early days, Netlink only supported 32 groups expressed in a 32-bits mask. However, since 2.6.14, Netlink may have up to 2^32 multicast groups but you have to use setsockopt() with NETLINK_ADD_MEMBERSHIP to join a given multicast group. This function internally calls setsockopt() to join a given netlink multicast group. You can still use mnl_bind() and the 32-bit mask to join a set of Netlink multicast groups.

On error, this function returns -1 and errno is appropriately set.

References mnl_socket::fd, and SOL_NETLINK.

void nl_join_groups ( struct nl_sock sk,
int  groups 
)

Join multicast groups (deprecated)

  • sk Netlink socket.
  • groups Bitmask of groups to join.

This function defines the old way of joining multicast group which has to be done prior to calling nl_connect(). It works on any kernel version but is very limited as only 32 groups can be joined.

References sockaddr_nl::nl_groups, and nl_sock::s_local.

int nl_socket_add_membership ( struct nl_sock sk,
int  group 
)

References nl_socket_add_memberships().

Referenced by main(), and nl_cache_mngr_add().

Here is the call graph for this function:

Here is the caller graph for this function:

int nl_socket_add_memberships ( struct nl_sock sk,
int  group,
  ... 
)

Join groups.

  • sk Netlink socket
  • group Group identifier

Joins the specified groups using the modern socket option which is available since kernel version 2.6.14. It allows joining an almost arbitary number of groups without limitation. The list of groups has to be terminated by 0 (NFNLGRP_NONE).

Make sure to use the correct group definitions as the older bitmask definitions for nl_join_groups() are likely to still be present for backward compatibility reasons.

Returns
0 on sucess or a negative error code.

References NETLINK_ADD_MEMBERSHIP, nl_syserr2nlerr(), NLE_BAD_SOCK, NLE_INVAL, nl_sock::s_fd, and SOL_NETLINK.

Referenced by nl_socket_add_membership().

Here is the call graph for this function:

Here is the caller graph for this function:

struct nl_sock* nl_socket_alloc ( void  )

Allocate new netlink socket.

Returns
Newly allocated netlink socket or NULL.

References nl_cb_alloc(), and NULL.

Referenced by nfnl_queue_socket_alloc(), and nl_cli_alloc_socket().

Here is the call graph for this function:

Here is the caller graph for this function:

struct nl_sock* nl_socket_alloc_cb ( struct nl_cb cb)

Allocate new socket with custom callbacks.

  • cb Callback handler

The reference to the callback handler is taken into account automatically, it is released again upon calling nl_socket_free().

Returns
Newly allocted socket handle or NULL.

References BUG, nl_cb_get(), and NULL.

Here is the call graph for this function:

void nl_socket_disable_auto_ack ( struct nl_sock sk)

Disable automatic request for ACK.

  • sk Netlink socket.

The default behaviour of a socket is to request an ACK for each message sent to allow for the caller to synchronize to the completion of the netlink operation. This function disables this behaviour and will result in requests being sent which will not have the NLM_F_ACK flag set automatically. However, it is still possible for the caller to set the NLM_F_ACK flag explicitely.

References NL_NO_AUTO_ACK, and nl_sock::s_flags.

Referenced by nfnl_queue_socket_alloc().

Here is the caller graph for this function:

void nl_socket_disable_msg_peek ( struct nl_sock sk)

Disable use of MSG_PEEK when reading from socket.

  • sk Netlink socket.

References NL_MSG_PEEK, and nl_sock::s_flags.

void nl_socket_disable_seq_check ( struct nl_sock sk)

Disable sequence number checking.

  • sk Netlink socket.

Disables checking of sequence numbers on the netlink socket This is required to allow messages to be processed which were not requested by a preceding request message, e.g. netlink events.

Note
This function modifies the NL_CB_SEQ_CHECK configuration in the callback handle associated with the socket.

References NL_CB_CUSTOM, NL_CB_SEQ_CHECK, nl_cb_set(), NULL, and nl_sock::s_cb.

Referenced by main(), and nl_cache_mngr_alloc().

Here is the call graph for this function:

Here is the caller graph for this function:

int nl_socket_drop_membership ( struct nl_sock sk,
int  group 
)

References nl_socket_drop_memberships().

Referenced by nl_cache_mngr_add().

Here is the call graph for this function:

Here is the caller graph for this function:

int nl_socket_drop_memberships ( struct nl_sock sk,
int  group,
  ... 
)

Leave groups.

  • sk Netlink socket
  • group Group identifier

Leaves the specified groups using the modern socket option which is available since kernel version 2.6.14. The list of groups has to terminated by 0 (NFNLGRP_NONE).

See also
nl_socket_add_membership
Returns
0 on success or a negative error code.

References NETLINK_DROP_MEMBERSHIP, nl_syserr2nlerr(), NLE_BAD_SOCK, NLE_INVAL, nl_sock::s_fd, and SOL_NETLINK.

Referenced by nl_socket_drop_membership().

Here is the call graph for this function:

Here is the caller graph for this function:

void nl_socket_enable_auto_ack ( struct nl_sock sk)

Enable automatic request for ACK (default)

References NL_NO_AUTO_ACK, and nl_sock::s_flags.

void nl_socket_enable_msg_peek ( struct nl_sock sk)

Enable use of MSG_PEEK when reading from socket.

  • sk Netlink socket.

References NL_MSG_PEEK, and nl_sock::s_flags.

void nl_socket_free ( struct nl_sock sk)

Free a netlink socket.

  • sk Netlink socket.

References nl_cb_put(), NL_OWN_PORT, sockaddr_nl::nl_pid, nl_sock::s_cb, nl_sock::s_fd, nl_sock::s_flags, and nl_sock::s_local.

Here is the call graph for this function:

struct nl_cb* nl_socket_get_cb ( struct nl_sock sk)

References nl_cb_get(), and nl_sock::s_cb.

Here is the call graph for this function:

int nl_socket_get_fd ( struct nl_sock sk)

References nl_sock::s_fd.

Referenced by main(), nl_cache_mngr_get_fd(), and nl_cache_mngr_poll().

Here is the caller graph for this function:

uint32_t nl_socket_get_local_port ( struct nl_sock sk)
uint32_t nl_socket_get_peer_port ( struct nl_sock sk)
int nl_socket_modify_cb ( struct nl_sock sk,
enum nl_cb_type  type,
enum nl_cb_kind  kind,
nl_recvmsg_msg_cb_t  func,
void *  arg 
)

Modify the callback handler associated to the socket.

  • sk Netlink socket.
  • type which type callback to set
  • kind kind of callback
  • func callback function
  • arg argument to be passwd to callback function
See also
nl_cb_set

References nl_cb_set(), and nl_sock::s_cb.

Referenced by main(), and nl_cache_mngr_alloc().

Here is the call graph for this function:

Here is the caller graph for this function:

int nl_socket_recv_pktinfo ( struct nl_sock sk,
int  state 
)

Enable/disable receival of additional packet information.

  • sk Netlink socket.
  • state New state (0 - disabled, 1 - enabled)
Returns
0 on success or a negative error code

References NETLINK_PKTINFO, nl_syserr2nlerr(), NLE_BAD_SOCK, nl_sock::s_fd, and SOL_NETLINK.

Here is the call graph for this function:

int nl_socket_set_buffer_size ( struct nl_sock sk,
int  rxbuf,
int  txbuf 
)

Set socket buffer size of netlink socket.

  • sk Netlink socket.
  • rxbuf New receive socket buffer size in bytes.
  • txbuf New transmit socket buffer size in bytes.

Sets the socket buffer size of a netlink socket to the specified values rxbuf and txbuf. Providing a value of 0 assumes a good default value.

Note
It is not required to call this function prior to nl_connect().
Returns
0 on sucess or a negative error code.

References NL_SOCK_BUFSIZE_SET, nl_syserr2nlerr(), NLE_BAD_SOCK, nl_sock::s_fd, and nl_sock::s_flags.

Referenced by main(), and nl_connect().

Here is the call graph for this function:

Here is the caller graph for this function:

void nl_socket_set_cb ( struct nl_sock sk,
struct nl_cb cb 
)

References nl_cb_get(), nl_cb_put(), and nl_sock::s_cb.

Here is the call graph for this function:

void nl_socket_set_local_port ( struct nl_sock sk,
uint32_t  port 
)

Set local port of socket.

  • sk Netlink socket.
  • port Local port identifier

Assigns a local port identifier to the socket. If port is 0 a unique port identifier will be generated automatically.

References NL_OWN_PORT, sockaddr_nl::nl_pid, nl_sock::s_flags, and nl_sock::s_local.

int nl_socket_set_nonblocking ( struct nl_sock sk)

Set file descriptor of socket to non-blocking state.

  • sk Netlink socket.
Returns
0 on success or a negative error code.

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

Referenced by nl_cache_mngr_alloc().

Here is the call graph for this function:

Here is the caller graph for this function:

int nl_socket_set_passcred ( struct nl_sock sk,
int  state 
)

Enable/disable credential passing on netlink socket.

  • sk Netlink socket.
  • state New state (0 - disabled, 1 - enabled)
Returns
0 on success or a negative error code

References NL_SOCK_PASSCRED, nl_syserr2nlerr(), NLE_BAD_SOCK, nl_sock::s_fd, and nl_sock::s_flags.

Here is the call graph for this function:

void nl_socket_set_peer_port ( struct nl_sock sk,
uint32_t  port 
)
unsigned int nl_socket_use_seq ( struct nl_sock sk)

Use next sequence number.

  • sk Netlink socket.

Uses the next available sequence number and increases the counter by one for subsequent calls.

Returns
Unique serial sequence number

References nl_sock::s_seq_next.