2007-09-14 23:28:01 +00:00
|
|
|
/*
|
2008-05-15 11:26:32 +00:00
|
|
|
* lib/socket.c Netlink Socket
|
2007-09-14 23:28:01 +00:00
|
|
|
*
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation version 2.1
|
|
|
|
* of the License.
|
|
|
|
*
|
2012-05-10 10:03:59 +00:00
|
|
|
* Copyright (c) 2003-2012 Thomas Graf <tgraf@suug.ch>
|
2007-09-14 23:28:01 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
2012-05-10 10:03:59 +00:00
|
|
|
* @ingroup core_types
|
2007-09-14 23:28:01 +00:00
|
|
|
* @defgroup socket Socket
|
2012-05-10 10:03:59 +00:00
|
|
|
*
|
|
|
|
* Representation of a netlink socket
|
|
|
|
*
|
|
|
|
* Related sections in the development guide:
|
|
|
|
* - @core_doc{core_sockets, Netlink Sockets}
|
|
|
|
*
|
2007-09-14 23:28:01 +00:00
|
|
|
* @{
|
2012-05-10 10:03:59 +00:00
|
|
|
*
|
|
|
|
* Header
|
|
|
|
* ------
|
|
|
|
* ~~~~{.c}
|
|
|
|
* #include <netlink/socket.h>
|
|
|
|
* ~~~~
|
2007-09-14 23:28:01 +00:00
|
|
|
*/
|
|
|
|
|
2012-10-26 19:19:26 +00:00
|
|
|
#include "defs.h"
|
|
|
|
|
2013-01-24 12:51:24 +00:00
|
|
|
#include <netlink-private/netlink.h>
|
2014-04-09 10:08:52 +00:00
|
|
|
#include <netlink-private/socket.h>
|
2007-09-14 23:28:01 +00:00
|
|
|
#include <netlink/netlink.h>
|
|
|
|
#include <netlink/utils.h>
|
|
|
|
#include <netlink/handlers.h>
|
|
|
|
#include <netlink/msg.h>
|
|
|
|
#include <netlink/attr.h>
|
|
|
|
|
2007-12-18 12:06:48 +00:00
|
|
|
static int default_cb = NL_CB_DEFAULT;
|
|
|
|
|
|
|
|
static void __init init_default_cb(void)
|
|
|
|
{
|
|
|
|
char *nlcb;
|
|
|
|
|
|
|
|
if ((nlcb = getenv("NLCB"))) {
|
|
|
|
if (!strcasecmp(nlcb, "default"))
|
|
|
|
default_cb = NL_CB_DEFAULT;
|
|
|
|
else if (!strcasecmp(nlcb, "verbose"))
|
|
|
|
default_cb = NL_CB_VERBOSE;
|
|
|
|
else if (!strcasecmp(nlcb, "debug"))
|
|
|
|
default_cb = NL_CB_DEBUG;
|
|
|
|
else {
|
|
|
|
fprintf(stderr, "Unknown value for NLCB, valid values: "
|
|
|
|
"{default | verbose | debug}\n");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-09-14 23:28:01 +00:00
|
|
|
static uint32_t used_ports_map[32];
|
2012-11-15 20:30:57 +00:00
|
|
|
static NL_RW_LOCK(port_map_lock);
|
2007-09-14 23:28:01 +00:00
|
|
|
|
|
|
|
static uint32_t generate_local_port(void)
|
|
|
|
{
|
2014-04-09 10:08:53 +00:00
|
|
|
int i, j, n, m;
|
|
|
|
static uint16_t idx_state = 0;
|
2007-09-14 23:28:01 +00:00
|
|
|
uint32_t pid = getpid() & 0x3FFFFF;
|
|
|
|
|
2012-11-15 20:30:57 +00:00
|
|
|
nl_write_lock(&port_map_lock);
|
2011-08-11 13:17:56 +00:00
|
|
|
|
2014-04-09 10:08:53 +00:00
|
|
|
if (idx_state == 0) {
|
|
|
|
uint32_t t = time(NULL);
|
|
|
|
|
|
|
|
/* from time to time (on average each 2^15 calls), the idx_state will
|
|
|
|
* be zero again. No problem, just "seed" anew with time(). */
|
|
|
|
idx_state = t ^ (t >> 16) ^ 0x3047;
|
|
|
|
} else
|
|
|
|
idx_state = idx_state + 20011; /* add prime number */
|
|
|
|
|
|
|
|
i = idx_state >> 5;
|
|
|
|
n = idx_state;
|
|
|
|
for (j = 0; j < 32; j++) {
|
|
|
|
/* walk the index somewhat randomized, with always leaving the block
|
|
|
|
* #0 as last. The reason is that libnl-1 will start at block #0,
|
|
|
|
* so just leave the first 32 ports preferably for libnl-1 owned sockets
|
|
|
|
* (this is relevant only if the applications ends up using both versions
|
|
|
|
* of the library and doesn't hurt otherwise). */
|
|
|
|
if (j == 31)
|
|
|
|
i = 0;
|
|
|
|
else
|
|
|
|
i = (((i-1) + 7) % 31) + 1;
|
|
|
|
|
2007-09-14 23:28:01 +00:00
|
|
|
if (used_ports_map[i] == 0xFFFFFFFF)
|
|
|
|
continue;
|
|
|
|
|
2014-04-09 10:08:53 +00:00
|
|
|
for (m = 0; m < 32; m++) {
|
|
|
|
n = (n + 13) % 32;
|
2007-09-14 23:28:01 +00:00
|
|
|
if (1UL & (used_ports_map[i] >> n))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
used_ports_map[i] |= (1UL << n);
|
|
|
|
n += (i * 32);
|
|
|
|
|
|
|
|
/* PID_MAX_LIMIT is currently at 2^22, leaving 10 bit
|
|
|
|
* to, i.e. 1024 unique ports per application. */
|
|
|
|
|
2012-11-15 20:30:57 +00:00
|
|
|
nl_write_unlock(&port_map_lock);
|
2011-08-11 13:17:56 +00:00
|
|
|
|
2014-04-09 10:08:50 +00:00
|
|
|
return pid + (((uint32_t)n) << 22);
|
2007-09-14 23:28:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-11-15 20:30:57 +00:00
|
|
|
nl_write_unlock(&port_map_lock);
|
2011-08-11 13:17:56 +00:00
|
|
|
|
2007-09-14 23:28:01 +00:00
|
|
|
/* Out of sockets in our own PID namespace, what to do? FIXME */
|
2013-06-28 16:53:16 +00:00
|
|
|
NL_DBG(1, "Warning: Ran out of unique local port namespace\n");
|
2014-04-09 10:08:50 +00:00
|
|
|
return UINT32_MAX;
|
2007-09-14 23:28:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void release_local_port(uint32_t port)
|
|
|
|
{
|
2007-12-17 15:26:49 +00:00
|
|
|
int nr;
|
2014-04-09 10:08:52 +00:00
|
|
|
uint32_t mask;
|
2007-09-14 23:28:01 +00:00
|
|
|
|
2014-04-09 10:08:50 +00:00
|
|
|
if (port == UINT32_MAX)
|
2007-12-17 15:26:49 +00:00
|
|
|
return;
|
2014-04-09 10:08:52 +00:00
|
|
|
|
|
|
|
BUG_ON(port == 0);
|
|
|
|
|
2007-12-17 15:26:49 +00:00
|
|
|
nr = port >> 22;
|
2014-04-09 10:08:52 +00:00
|
|
|
mask = 1UL << (nr % 32);
|
|
|
|
nr /= 32;
|
2011-08-11 13:17:56 +00:00
|
|
|
|
2012-11-15 20:30:57 +00:00
|
|
|
nl_write_lock(&port_map_lock);
|
2014-04-09 10:08:52 +00:00
|
|
|
BUG_ON((used_ports_map[nr] & mask) != mask);
|
|
|
|
used_ports_map[nr] &= ~mask;
|
2012-11-15 20:30:57 +00:00
|
|
|
nl_write_unlock(&port_map_lock);
|
2007-09-14 23:28:01 +00:00
|
|
|
}
|
|
|
|
|
2014-04-09 10:08:52 +00:00
|
|
|
/** \cond skip */
|
|
|
|
void _nl_socket_used_ports_release_all(const uint32_t *used_ports)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i = 0; i < 32; i++) {
|
|
|
|
if (used_ports[i] != 0) {
|
|
|
|
nl_write_lock(&port_map_lock);
|
|
|
|
for (; i < 32; i++) {
|
|
|
|
BUG_ON((used_ports_map[i] & used_ports[i]) != used_ports[i]);
|
|
|
|
used_ports_map[i] &= ~(used_ports[i]);
|
|
|
|
}
|
|
|
|
nl_write_unlock(&port_map_lock);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void _nl_socket_used_ports_set(uint32_t *used_ports, uint32_t port)
|
|
|
|
{
|
|
|
|
int nr;
|
|
|
|
int32_t mask;
|
|
|
|
|
|
|
|
nr = port >> 22;
|
|
|
|
mask = 1UL << (nr % 32);
|
|
|
|
nr /= 32;
|
|
|
|
|
|
|
|
/*
|
|
|
|
BUG_ON(port == UINT32_MAX || port == 0 || (getpid() & 0x3FFFFF) != (port & 0x3FFFFF));
|
|
|
|
BUG_ON(used_ports[nr] & mask);
|
|
|
|
*/
|
|
|
|
|
|
|
|
used_ports[nr] |= mask;
|
|
|
|
}
|
|
|
|
/** \endcond */
|
|
|
|
|
2007-09-14 23:28:01 +00:00
|
|
|
/**
|
|
|
|
* @name Allocation
|
|
|
|
* @{
|
|
|
|
*/
|
|
|
|
|
2008-05-15 11:26:32 +00:00
|
|
|
static struct nl_sock *__alloc_socket(struct nl_cb *cb)
|
2007-09-14 23:28:01 +00:00
|
|
|
{
|
2008-05-15 11:26:32 +00:00
|
|
|
struct nl_sock *sk;
|
2007-09-14 23:28:01 +00:00
|
|
|
|
2008-05-15 11:26:32 +00:00
|
|
|
sk = calloc(1, sizeof(*sk));
|
|
|
|
if (!sk)
|
2007-09-14 23:28:01 +00:00
|
|
|
return NULL;
|
|
|
|
|
2008-05-15 11:26:32 +00:00
|
|
|
sk->s_fd = -1;
|
2013-04-27 18:35:37 +00:00
|
|
|
sk->s_cb = nl_cb_get(cb);
|
2008-05-15 11:26:32 +00:00
|
|
|
sk->s_local.nl_family = AF_NETLINK;
|
|
|
|
sk->s_peer.nl_family = AF_NETLINK;
|
|
|
|
sk->s_seq_expect = sk->s_seq_next = time(0);
|
2014-04-09 10:08:52 +00:00
|
|
|
|
|
|
|
/* the port is 0 (unspecified), meaning NL_OWN_PORT */
|
|
|
|
sk->s_flags = NL_OWN_PORT;
|
2007-09-14 23:28:01 +00:00
|
|
|
|
2008-05-15 11:26:32 +00:00
|
|
|
return sk;
|
2007-09-14 23:28:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2008-05-15 11:26:32 +00:00
|
|
|
* Allocate new netlink socket
|
2007-09-14 23:28:01 +00:00
|
|
|
*
|
2008-05-15 11:26:32 +00:00
|
|
|
* @return Newly allocated netlink socket or NULL.
|
2007-09-14 23:28:01 +00:00
|
|
|
*/
|
2008-05-15 11:26:32 +00:00
|
|
|
struct nl_sock *nl_socket_alloc(void)
|
2007-09-14 23:28:01 +00:00
|
|
|
{
|
|
|
|
struct nl_cb *cb;
|
2013-04-27 18:35:37 +00:00
|
|
|
struct nl_sock *sk;
|
|
|
|
|
2007-12-18 12:06:48 +00:00
|
|
|
cb = nl_cb_alloc(default_cb);
|
2008-05-14 15:49:44 +00:00
|
|
|
if (!cb)
|
2007-09-14 23:28:01 +00:00
|
|
|
return NULL;
|
|
|
|
|
2013-04-27 18:35:37 +00:00
|
|
|
/* will increment cb reference count on success */
|
|
|
|
sk = __alloc_socket(cb);
|
|
|
|
|
|
|
|
nl_cb_put(cb);
|
|
|
|
|
|
|
|
return sk;
|
2007-09-14 23:28:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2008-05-15 11:26:32 +00:00
|
|
|
* Allocate new socket with custom callbacks
|
2007-09-14 23:28:01 +00:00
|
|
|
* @arg cb Callback handler
|
|
|
|
*
|
|
|
|
* The reference to the callback handler is taken into account
|
2008-05-15 11:26:32 +00:00
|
|
|
* automatically, it is released again upon calling nl_socket_free().
|
2007-09-14 23:28:01 +00:00
|
|
|
*
|
|
|
|
*@return Newly allocted socket handle or NULL.
|
|
|
|
*/
|
2008-05-15 11:26:32 +00:00
|
|
|
struct nl_sock *nl_socket_alloc_cb(struct nl_cb *cb)
|
2007-09-14 23:28:01 +00:00
|
|
|
{
|
|
|
|
if (cb == NULL)
|
|
|
|
BUG();
|
|
|
|
|
2013-04-27 18:35:37 +00:00
|
|
|
return __alloc_socket(cb);
|
2007-09-14 23:28:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2008-05-15 11:26:32 +00:00
|
|
|
* Free a netlink socket.
|
|
|
|
* @arg sk Netlink socket.
|
2007-09-14 23:28:01 +00:00
|
|
|
*/
|
2008-05-15 11:26:32 +00:00
|
|
|
void nl_socket_free(struct nl_sock *sk)
|
2007-09-14 23:28:01 +00:00
|
|
|
{
|
2008-05-15 11:26:32 +00:00
|
|
|
if (!sk)
|
2007-09-14 23:28:01 +00:00
|
|
|
return;
|
|
|
|
|
2008-05-15 11:26:32 +00:00
|
|
|
if (sk->s_fd >= 0)
|
|
|
|
close(sk->s_fd);
|
2007-12-17 15:26:49 +00:00
|
|
|
|
2008-05-15 11:26:32 +00:00
|
|
|
if (!(sk->s_flags & NL_OWN_PORT))
|
|
|
|
release_local_port(sk->s_local.nl_pid);
|
2007-09-14 23:28:01 +00:00
|
|
|
|
2008-05-15 11:26:32 +00:00
|
|
|
nl_cb_put(sk->s_cb);
|
|
|
|
free(sk);
|
2007-09-14 23:28:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/** @} */
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @name Sequence Numbers
|
|
|
|
* @{
|
|
|
|
*/
|
|
|
|
|
|
|
|
static int noop_seq_check(struct nl_msg *msg, void *arg)
|
|
|
|
{
|
|
|
|
return NL_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Disable sequence number checking.
|
2008-05-15 11:26:32 +00:00
|
|
|
* @arg sk Netlink socket.
|
2007-09-14 23:28:01 +00:00
|
|
|
*
|
2008-05-15 11:26:32 +00:00
|
|
|
* Disables checking of sequence numbers on the netlink socket This is
|
2007-09-14 23:28:01 +00:00
|
|
|
* 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.
|
|
|
|
*/
|
2008-12-10 17:00:11 +00:00
|
|
|
void nl_socket_disable_seq_check(struct nl_sock *sk)
|
2007-09-14 23:28:01 +00:00
|
|
|
{
|
2008-05-15 11:26:32 +00:00
|
|
|
nl_cb_set(sk->s_cb, NL_CB_SEQ_CHECK,
|
2007-09-14 23:28:01 +00:00
|
|
|
NL_CB_CUSTOM, noop_seq_check, NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Use next sequence number
|
2008-05-15 11:26:32 +00:00
|
|
|
* @arg sk Netlink socket.
|
2007-09-14 23:28:01 +00:00
|
|
|
*
|
|
|
|
* Uses the next available sequence number and increases the counter
|
|
|
|
* by one for subsequent calls.
|
|
|
|
*
|
|
|
|
* @return Unique serial sequence number
|
|
|
|
*/
|
2008-05-15 11:26:32 +00:00
|
|
|
unsigned int nl_socket_use_seq(struct nl_sock *sk)
|
2007-09-14 23:28:01 +00:00
|
|
|
{
|
2008-05-15 11:26:32 +00:00
|
|
|
return sk->s_seq_next++;
|
2007-09-14 23:28:01 +00:00
|
|
|
}
|
|
|
|
|
2008-10-20 11:02:38 +00:00
|
|
|
/**
|
|
|
|
* Disable automatic request for ACK
|
|
|
|
* @arg 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.
|
|
|
|
*/
|
|
|
|
void nl_socket_disable_auto_ack(struct nl_sock *sk)
|
|
|
|
{
|
|
|
|
sk->s_flags |= NL_NO_AUTO_ACK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Enable automatic request for ACK (default)
|
|
|
|
* @arg sk Netlink socket.
|
|
|
|
* @see nl_socket_disable_auto_ack
|
|
|
|
*/
|
|
|
|
void nl_socket_enable_auto_ack(struct nl_sock *sk)
|
|
|
|
{
|
|
|
|
sk->s_flags &= ~NL_NO_AUTO_ACK;
|
|
|
|
}
|
|
|
|
|
2007-09-14 23:28:01 +00:00
|
|
|
/** @} */
|
|
|
|
|
2014-04-09 10:08:52 +00:00
|
|
|
/** \cond skip */
|
|
|
|
int _nl_socket_is_local_port_unspecified(struct nl_sock *sk)
|
|
|
|
{
|
|
|
|
return (sk->s_local.nl_pid == 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t _nl_socket_generate_local_port_no_release(struct nl_sock *sk)
|
|
|
|
{
|
|
|
|
uint32_t port;
|
|
|
|
|
|
|
|
/* reset the port to generate_local_port(), but do not release
|
|
|
|
* the previously generated port. */
|
|
|
|
|
|
|
|
port = generate_local_port();
|
|
|
|
sk->s_flags &= ~NL_OWN_PORT;
|
|
|
|
sk->s_local.nl_pid = port;
|
|
|
|
return port;
|
|
|
|
}
|
|
|
|
/** \endcond */
|
|
|
|
|
2007-09-14 23:28:01 +00:00
|
|
|
/**
|
|
|
|
* @name Source Idenficiation
|
|
|
|
* @{
|
|
|
|
*/
|
|
|
|
|
2010-11-18 11:47:16 +00:00
|
|
|
uint32_t nl_socket_get_local_port(const struct nl_sock *sk)
|
2007-09-14 23:28:01 +00:00
|
|
|
{
|
2014-04-09 10:08:52 +00:00
|
|
|
if (sk->s_local.nl_pid == 0) {
|
|
|
|
/* modify the const argument sk. This is justified, because
|
|
|
|
* nobody ever saw the local_port from externally. So, we
|
|
|
|
* initilize it on first use.
|
|
|
|
*
|
|
|
|
* Note that this also means that you cannot call this function
|
|
|
|
* from multiple threads without synchronization. But nl_sock
|
|
|
|
* is not automatically threadsafe anyway, so the user is not
|
|
|
|
* allowed to do that.
|
|
|
|
*/
|
|
|
|
return _nl_socket_generate_local_port_no_release((struct nl_sock *) sk);
|
|
|
|
}
|
2008-05-15 11:26:32 +00:00
|
|
|
return sk->s_local.nl_pid;
|
2007-09-14 23:28:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set local port of socket
|
2008-05-15 11:26:32 +00:00
|
|
|
* @arg sk Netlink socket.
|
2007-09-14 23:28:01 +00:00
|
|
|
* @arg port Local port identifier
|
|
|
|
*
|
2014-04-09 10:08:52 +00:00
|
|
|
* Assigns a local port identifier to the socket.
|
|
|
|
*
|
|
|
|
* If port is 0, the port is reset to 'unspecified' as it is after newly
|
|
|
|
* calling nl_socket_alloc().
|
|
|
|
* Unspecified means, that the port will be generated automatically later
|
|
|
|
* on first use (either on nl_socket_get_local_port() or nl_connect()).
|
2007-09-14 23:28:01 +00:00
|
|
|
*/
|
2008-05-15 11:26:32 +00:00
|
|
|
void nl_socket_set_local_port(struct nl_sock *sk, uint32_t port)
|
2007-09-14 23:28:01 +00:00
|
|
|
{
|
2014-04-09 10:08:52 +00:00
|
|
|
if (!(sk->s_flags & NL_OWN_PORT))
|
|
|
|
release_local_port(sk->s_local.nl_pid);
|
|
|
|
sk->s_flags |= NL_OWN_PORT;
|
2008-05-15 11:26:32 +00:00
|
|
|
sk->s_local.nl_pid = port;
|
2007-09-14 23:28:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/** @} */
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @name Group Subscriptions
|
|
|
|
* @{
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
2008-12-01 13:38:06 +00:00
|
|
|
* Join groups
|
2008-05-15 11:26:32 +00:00
|
|
|
* @arg sk Netlink socket
|
2007-09-14 23:28:01 +00:00
|
|
|
* @arg group Group identifier
|
|
|
|
*
|
2008-12-01 13:38:06 +00:00
|
|
|
* Joins the specified groups using the modern socket option which
|
2007-09-14 23:28:01 +00:00
|
|
|
* is available since kernel version 2.6.14. It allows joining an
|
2008-12-01 13:38:06 +00:00
|
|
|
* almost arbitary number of groups without limitation. The list
|
|
|
|
* of groups has to be terminated by 0 (%NFNLGRP_NONE).
|
2007-09-14 23:28:01 +00:00
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*
|
|
|
|
* @return 0 on sucess or a negative error code.
|
|
|
|
*/
|
2008-12-01 13:38:06 +00:00
|
|
|
int nl_socket_add_memberships(struct nl_sock *sk, int group, ...)
|
2007-09-14 23:28:01 +00:00
|
|
|
{
|
|
|
|
int err;
|
2008-12-01 13:38:06 +00:00
|
|
|
va_list ap;
|
2007-09-14 23:28:01 +00:00
|
|
|
|
2008-05-15 11:26:32 +00:00
|
|
|
if (sk->s_fd == -1)
|
2008-05-14 15:49:44 +00:00
|
|
|
return -NLE_BAD_SOCK;
|
2007-12-17 15:26:49 +00:00
|
|
|
|
2008-12-01 13:38:06 +00:00
|
|
|
va_start(ap, group);
|
|
|
|
|
|
|
|
while (group != 0) {
|
2012-10-27 18:38:22 +00:00
|
|
|
if (group < 0) {
|
|
|
|
va_end(ap);
|
2008-12-01 13:38:06 +00:00
|
|
|
return -NLE_INVAL;
|
2012-10-27 18:38:22 +00:00
|
|
|
}
|
2008-12-01 13:38:06 +00:00
|
|
|
|
|
|
|
err = setsockopt(sk->s_fd, SOL_NETLINK, NETLINK_ADD_MEMBERSHIP,
|
|
|
|
&group, sizeof(group));
|
2012-10-27 18:38:22 +00:00
|
|
|
if (err < 0) {
|
|
|
|
va_end(ap);
|
2008-12-01 13:38:06 +00:00
|
|
|
return -nl_syserr2nlerr(errno);
|
2012-10-27 18:38:22 +00:00
|
|
|
}
|
2008-12-01 13:38:06 +00:00
|
|
|
|
|
|
|
group = va_arg(ap, int);
|
|
|
|
}
|
|
|
|
|
|
|
|
va_end(ap);
|
2007-09-14 23:28:01 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2008-12-10 17:00:11 +00:00
|
|
|
int nl_socket_add_membership(struct nl_sock *sk, int group)
|
|
|
|
{
|
|
|
|
return nl_socket_add_memberships(sk, group, 0);
|
|
|
|
}
|
|
|
|
|
2007-09-14 23:28:01 +00:00
|
|
|
/**
|
2008-12-01 13:38:06 +00:00
|
|
|
* Leave groups
|
2008-05-15 11:26:32 +00:00
|
|
|
* @arg sk Netlink socket
|
2007-09-14 23:28:01 +00:00
|
|
|
* @arg group Group identifier
|
|
|
|
*
|
2008-12-01 13:38:06 +00:00
|
|
|
* 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).
|
2007-09-14 23:28:01 +00:00
|
|
|
*
|
|
|
|
* @see nl_socket_add_membership
|
|
|
|
* @return 0 on success or a negative error code.
|
|
|
|
*/
|
2008-12-01 13:38:06 +00:00
|
|
|
int nl_socket_drop_memberships(struct nl_sock *sk, int group, ...)
|
2007-09-14 23:28:01 +00:00
|
|
|
{
|
|
|
|
int err;
|
2008-12-01 13:38:06 +00:00
|
|
|
va_list ap;
|
2007-09-14 23:28:01 +00:00
|
|
|
|
2008-05-15 11:26:32 +00:00
|
|
|
if (sk->s_fd == -1)
|
2008-05-14 15:49:44 +00:00
|
|
|
return -NLE_BAD_SOCK;
|
2007-12-17 15:26:49 +00:00
|
|
|
|
2008-12-01 13:38:06 +00:00
|
|
|
va_start(ap, group);
|
|
|
|
|
|
|
|
while (group != 0) {
|
2012-10-27 18:38:22 +00:00
|
|
|
if (group < 0) {
|
|
|
|
va_end(ap);
|
2008-12-01 13:38:06 +00:00
|
|
|
return -NLE_INVAL;
|
2012-10-27 18:38:22 +00:00
|
|
|
}
|
2008-12-01 13:38:06 +00:00
|
|
|
|
|
|
|
err = setsockopt(sk->s_fd, SOL_NETLINK, NETLINK_DROP_MEMBERSHIP,
|
|
|
|
&group, sizeof(group));
|
2012-10-27 18:38:22 +00:00
|
|
|
if (err < 0) {
|
|
|
|
va_end(ap);
|
2008-12-01 13:38:06 +00:00
|
|
|
return -nl_syserr2nlerr(errno);
|
2012-10-27 18:38:22 +00:00
|
|
|
}
|
2008-12-01 13:38:06 +00:00
|
|
|
|
|
|
|
group = va_arg(ap, int);
|
|
|
|
}
|
|
|
|
|
|
|
|
va_end(ap);
|
2007-09-14 23:28:01 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2008-12-10 17:00:11 +00:00
|
|
|
int nl_socket_drop_membership(struct nl_sock *sk, int group)
|
|
|
|
{
|
|
|
|
return nl_socket_drop_memberships(sk, group, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-09-14 23:28:01 +00:00
|
|
|
/**
|
|
|
|
* Join multicast groups (deprecated)
|
2008-05-15 11:26:32 +00:00
|
|
|
* @arg sk Netlink socket.
|
2007-09-14 23:28:01 +00:00
|
|
|
* @arg 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.
|
|
|
|
*/
|
2008-05-15 11:26:32 +00:00
|
|
|
void nl_join_groups(struct nl_sock *sk, int groups)
|
2007-09-14 23:28:01 +00:00
|
|
|
{
|
2008-05-15 11:26:32 +00:00
|
|
|
sk->s_local.nl_groups |= groups;
|
2007-09-14 23:28:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/** @} */
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @name Peer Identfication
|
|
|
|
* @{
|
|
|
|
*/
|
|
|
|
|
2010-11-18 11:47:16 +00:00
|
|
|
uint32_t nl_socket_get_peer_port(const struct nl_sock *sk)
|
2007-09-14 23:28:01 +00:00
|
|
|
{
|
2008-05-15 11:26:32 +00:00
|
|
|
return sk->s_peer.nl_pid;
|
2007-09-14 23:28:01 +00:00
|
|
|
}
|
|
|
|
|
2008-05-15 11:26:32 +00:00
|
|
|
void nl_socket_set_peer_port(struct nl_sock *sk, uint32_t port)
|
2007-09-14 23:28:01 +00:00
|
|
|
{
|
2008-05-15 11:26:32 +00:00
|
|
|
sk->s_peer.nl_pid = port;
|
2007-09-14 23:28:01 +00:00
|
|
|
}
|
|
|
|
|
2010-11-18 11:47:16 +00:00
|
|
|
uint32_t nl_socket_get_peer_groups(const struct nl_sock *sk)
|
2010-10-13 14:53:34 +00:00
|
|
|
{
|
|
|
|
return sk->s_peer.nl_groups;
|
|
|
|
}
|
|
|
|
|
|
|
|
void nl_socket_set_peer_groups(struct nl_sock *sk, uint32_t groups)
|
|
|
|
{
|
|
|
|
sk->s_peer.nl_groups = groups;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2007-09-14 23:28:01 +00:00
|
|
|
/** @} */
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @name File Descriptor
|
|
|
|
* @{
|
|
|
|
*/
|
|
|
|
|
2012-11-06 16:36:53 +00:00
|
|
|
/**
|
|
|
|
* Return the file descriptor of the backing socket
|
|
|
|
* @arg sk Netlink socket
|
|
|
|
*
|
|
|
|
* Only valid after calling nl_connect() to create and bind the respective
|
|
|
|
* socket.
|
|
|
|
*
|
|
|
|
* @return File descriptor or -1 if not available.
|
|
|
|
*/
|
2010-11-18 11:47:16 +00:00
|
|
|
int nl_socket_get_fd(const struct nl_sock *sk)
|
2007-09-14 23:28:01 +00:00
|
|
|
{
|
2008-05-15 11:26:32 +00:00
|
|
|
return sk->s_fd;
|
2007-09-14 23:28:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2008-05-15 11:26:32 +00:00
|
|
|
* Set file descriptor of socket to non-blocking state
|
|
|
|
* @arg sk Netlink socket.
|
2007-09-14 23:28:01 +00:00
|
|
|
*
|
|
|
|
* @return 0 on success or a negative error code.
|
|
|
|
*/
|
2010-11-18 11:47:16 +00:00
|
|
|
int nl_socket_set_nonblocking(const struct nl_sock *sk)
|
2007-09-14 23:28:01 +00:00
|
|
|
{
|
2008-05-15 11:26:32 +00:00
|
|
|
if (sk->s_fd == -1)
|
2008-05-14 15:49:44 +00:00
|
|
|
return -NLE_BAD_SOCK;
|
2007-12-17 15:26:49 +00:00
|
|
|
|
2008-05-15 11:26:32 +00:00
|
|
|
if (fcntl(sk->s_fd, F_SETFL, O_NONBLOCK) < 0)
|
2008-05-14 15:49:44 +00:00
|
|
|
return -nl_syserr2nlerr(errno);
|
2007-09-14 23:28:01 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Enable use of MSG_PEEK when reading from socket
|
2008-05-15 11:26:32 +00:00
|
|
|
* @arg sk Netlink socket.
|
2007-09-14 23:28:01 +00:00
|
|
|
*/
|
2008-05-15 11:26:32 +00:00
|
|
|
void nl_socket_enable_msg_peek(struct nl_sock *sk)
|
2007-09-14 23:28:01 +00:00
|
|
|
{
|
2008-05-15 11:26:32 +00:00
|
|
|
sk->s_flags |= NL_MSG_PEEK;
|
2007-09-14 23:28:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Disable use of MSG_PEEK when reading from socket
|
2008-05-15 11:26:32 +00:00
|
|
|
* @arg sk Netlink socket.
|
2007-09-14 23:28:01 +00:00
|
|
|
*/
|
2008-05-15 11:26:32 +00:00
|
|
|
void nl_socket_disable_msg_peek(struct nl_sock *sk)
|
2007-09-14 23:28:01 +00:00
|
|
|
{
|
2008-05-15 11:26:32 +00:00
|
|
|
sk->s_flags &= ~NL_MSG_PEEK;
|
2007-09-14 23:28:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/** @} */
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @name Callback Handler
|
|
|
|
* @{
|
|
|
|
*/
|
|
|
|
|
2010-11-18 11:47:16 +00:00
|
|
|
struct nl_cb *nl_socket_get_cb(const struct nl_sock *sk)
|
2007-09-14 23:28:01 +00:00
|
|
|
{
|
2008-05-15 11:26:32 +00:00
|
|
|
return nl_cb_get(sk->s_cb);
|
2007-09-14 23:28:01 +00:00
|
|
|
}
|
|
|
|
|
2008-05-15 11:26:32 +00:00
|
|
|
void nl_socket_set_cb(struct nl_sock *sk, struct nl_cb *cb)
|
2007-09-14 23:28:01 +00:00
|
|
|
{
|
2013-04-27 18:35:37 +00:00
|
|
|
if (cb == NULL)
|
|
|
|
BUG();
|
|
|
|
|
2008-05-15 11:26:32 +00:00
|
|
|
nl_cb_put(sk->s_cb);
|
|
|
|
sk->s_cb = nl_cb_get(cb);
|
2007-09-14 23:28:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2011-08-30 16:33:25 +00:00
|
|
|
* Modify the callback handler associated with the socket
|
2008-05-15 11:26:32 +00:00
|
|
|
* @arg sk Netlink socket.
|
2007-09-14 23:28:01 +00:00
|
|
|
* @arg type which type callback to set
|
|
|
|
* @arg kind kind of callback
|
|
|
|
* @arg func callback function
|
2011-08-30 16:33:25 +00:00
|
|
|
* @arg arg argument to be passed to callback function
|
2007-09-14 23:28:01 +00:00
|
|
|
*
|
|
|
|
* @see nl_cb_set
|
|
|
|
*/
|
2008-05-15 11:26:32 +00:00
|
|
|
int nl_socket_modify_cb(struct nl_sock *sk, enum nl_cb_type type,
|
2007-09-14 23:28:01 +00:00
|
|
|
enum nl_cb_kind kind, nl_recvmsg_msg_cb_t func,
|
|
|
|
void *arg)
|
|
|
|
{
|
2008-05-15 11:26:32 +00:00
|
|
|
return nl_cb_set(sk->s_cb, type, kind, func, arg);
|
2007-09-14 23:28:01 +00:00
|
|
|
}
|
|
|
|
|
2011-08-30 16:31:47 +00:00
|
|
|
/**
|
|
|
|
* Modify the error callback handler associated with the socket
|
|
|
|
* @arg sk Netlink socket.
|
|
|
|
* @arg kind kind of callback
|
|
|
|
* @arg func callback function
|
|
|
|
* @arg arg argument to be passed to callback function
|
|
|
|
*
|
|
|
|
* @see nl_cb_err
|
|
|
|
*/
|
|
|
|
int nl_socket_modify_err_cb(struct nl_sock *sk, enum nl_cb_kind kind,
|
|
|
|
nl_recvmsg_err_cb_t func, void *arg)
|
|
|
|
{
|
|
|
|
return nl_cb_err(sk->s_cb, kind, func, arg);
|
|
|
|
}
|
|
|
|
|
2007-09-14 23:28:01 +00:00
|
|
|
/** @} */
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @name Utilities
|
|
|
|
* @{
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
2008-05-15 11:26:32 +00:00
|
|
|
* Set socket buffer size of netlink socket.
|
|
|
|
* @arg sk Netlink socket.
|
2007-09-14 23:28:01 +00:00
|
|
|
* @arg rxbuf New receive socket buffer size in bytes.
|
|
|
|
* @arg txbuf New transmit socket buffer size in bytes.
|
|
|
|
*
|
2008-05-15 11:26:32 +00:00
|
|
|
* Sets the socket buffer size of a netlink socket to the specified
|
2007-09-14 23:28:01 +00:00
|
|
|
* values \c rxbuf and \c txbuf. Providing a value of \c 0 assumes a
|
|
|
|
* good default value.
|
|
|
|
*
|
|
|
|
* @note It is not required to call this function prior to nl_connect().
|
|
|
|
* @return 0 on sucess or a negative error code.
|
|
|
|
*/
|
2008-12-10 17:00:11 +00:00
|
|
|
int nl_socket_set_buffer_size(struct nl_sock *sk, int rxbuf, int txbuf)
|
2007-09-14 23:28:01 +00:00
|
|
|
{
|
|
|
|
int err;
|
|
|
|
|
|
|
|
if (rxbuf <= 0)
|
|
|
|
rxbuf = 32768;
|
|
|
|
|
|
|
|
if (txbuf <= 0)
|
|
|
|
txbuf = 32768;
|
2007-12-17 15:26:49 +00:00
|
|
|
|
2008-05-15 11:26:32 +00:00
|
|
|
if (sk->s_fd == -1)
|
2008-05-14 15:49:44 +00:00
|
|
|
return -NLE_BAD_SOCK;
|
2007-09-14 23:28:01 +00:00
|
|
|
|
2008-05-15 11:26:32 +00:00
|
|
|
err = setsockopt(sk->s_fd, SOL_SOCKET, SO_SNDBUF,
|
2007-09-14 23:28:01 +00:00
|
|
|
&txbuf, sizeof(txbuf));
|
|
|
|
if (err < 0)
|
2008-05-14 15:49:44 +00:00
|
|
|
return -nl_syserr2nlerr(errno);
|
2007-09-14 23:28:01 +00:00
|
|
|
|
2008-05-15 11:26:32 +00:00
|
|
|
err = setsockopt(sk->s_fd, SOL_SOCKET, SO_RCVBUF,
|
2007-09-14 23:28:01 +00:00
|
|
|
&rxbuf, sizeof(rxbuf));
|
|
|
|
if (err < 0)
|
2008-05-14 15:49:44 +00:00
|
|
|
return -nl_syserr2nlerr(errno);
|
2007-09-14 23:28:01 +00:00
|
|
|
|
2008-05-15 11:26:32 +00:00
|
|
|
sk->s_flags |= NL_SOCK_BUFSIZE_SET;
|
2007-09-14 23:28:01 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-10-09 14:15:08 +00:00
|
|
|
/**
|
|
|
|
* Set default message buffer size of netlink socket.
|
|
|
|
* @arg sk Netlink socket.
|
|
|
|
* @arg bufsize Default message buffer size in bytes.
|
|
|
|
*
|
|
|
|
* Sets the default message buffer size to the specified length in bytes.
|
|
|
|
* The default message buffer size limits the maximum message size the
|
|
|
|
* socket will be able to receive. It is generally recommneded to specify
|
|
|
|
* a buffer size no less than the size of a memory page.
|
|
|
|
*
|
|
|
|
* @return 0 on success or a negative error code.
|
|
|
|
*/
|
|
|
|
int nl_socket_set_msg_buf_size(struct nl_sock *sk, size_t bufsize)
|
|
|
|
{
|
|
|
|
sk->s_bufsize = bufsize;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get default message buffer size of netlink socket.
|
|
|
|
* @arg sk Netlink socket.
|
|
|
|
*
|
|
|
|
* @return Size of default message buffer.
|
|
|
|
*/
|
|
|
|
size_t nl_socket_get_msg_buf_size(struct nl_sock *sk)
|
|
|
|
{
|
|
|
|
return sk->s_bufsize;
|
|
|
|
}
|
|
|
|
|
2007-09-14 23:28:01 +00:00
|
|
|
/**
|
2008-05-15 11:26:32 +00:00
|
|
|
* Enable/disable credential passing on netlink socket.
|
|
|
|
* @arg sk Netlink socket.
|
2007-09-14 23:28:01 +00:00
|
|
|
* @arg state New state (0 - disabled, 1 - enabled)
|
|
|
|
*
|
|
|
|
* @return 0 on success or a negative error code
|
|
|
|
*/
|
2008-12-10 17:00:11 +00:00
|
|
|
int nl_socket_set_passcred(struct nl_sock *sk, int state)
|
2007-09-14 23:28:01 +00:00
|
|
|
{
|
|
|
|
int err;
|
|
|
|
|
2008-05-15 11:26:32 +00:00
|
|
|
if (sk->s_fd == -1)
|
2008-05-14 15:49:44 +00:00
|
|
|
return -NLE_BAD_SOCK;
|
2007-12-17 15:26:49 +00:00
|
|
|
|
2008-05-15 11:26:32 +00:00
|
|
|
err = setsockopt(sk->s_fd, SOL_SOCKET, SO_PASSCRED,
|
2007-09-14 23:28:01 +00:00
|
|
|
&state, sizeof(state));
|
|
|
|
if (err < 0)
|
2008-05-14 15:49:44 +00:00
|
|
|
return -nl_syserr2nlerr(errno);
|
2007-09-14 23:28:01 +00:00
|
|
|
|
|
|
|
if (state)
|
2008-05-15 11:26:32 +00:00
|
|
|
sk->s_flags |= NL_SOCK_PASSCRED;
|
2007-09-14 23:28:01 +00:00
|
|
|
else
|
2008-05-15 11:26:32 +00:00
|
|
|
sk->s_flags &= ~NL_SOCK_PASSCRED;
|
2007-09-14 23:28:01 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Enable/disable receival of additional packet information
|
2008-05-15 11:26:32 +00:00
|
|
|
* @arg sk Netlink socket.
|
2007-09-14 23:28:01 +00:00
|
|
|
* @arg state New state (0 - disabled, 1 - enabled)
|
|
|
|
*
|
|
|
|
* @return 0 on success or a negative error code
|
|
|
|
*/
|
2008-05-15 11:26:32 +00:00
|
|
|
int nl_socket_recv_pktinfo(struct nl_sock *sk, int state)
|
2007-09-14 23:28:01 +00:00
|
|
|
{
|
|
|
|
int err;
|
|
|
|
|
2008-05-15 11:26:32 +00:00
|
|
|
if (sk->s_fd == -1)
|
2008-05-14 15:49:44 +00:00
|
|
|
return -NLE_BAD_SOCK;
|
2007-12-17 15:26:49 +00:00
|
|
|
|
2008-05-15 11:26:32 +00:00
|
|
|
err = setsockopt(sk->s_fd, SOL_NETLINK, NETLINK_PKTINFO,
|
2007-09-14 23:28:01 +00:00
|
|
|
&state, sizeof(state));
|
|
|
|
if (err < 0)
|
2008-05-14 15:49:44 +00:00
|
|
|
return -nl_syserr2nlerr(errno);
|
2007-09-14 23:28:01 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** @} */
|
|
|
|
|
|
|
|
/** @} */
|