24#include "nl-default.h"
28#include <sys/socket.h>
30#include <netlink/netlink.h>
31#include <netlink/utils.h>
32#include <netlink/handlers.h>
33#include <netlink/msg.h>
34#include <netlink/attr.h>
37#include "nl-priv-dynamic-core/nl-core.h"
38#include "nl-aux-core/nl-core.h"
41#define SOL_NETLINK 270
46static void _nl_init init_default_cb(
void)
50 if ((nlcb = getenv(
"NLCB"))) {
51 if (!strcasecmp(nlcb,
"default"))
53 else if (!strcasecmp(nlcb,
"verbose"))
55 else if (!strcasecmp(nlcb,
"debug"))
58 fprintf(stderr,
"Unknown value for NLCB, valid values: "
59 "{default | verbose | debug}\n");
64static uint32_t _badrandom_from_time(
void)
72 result = (uint32_t)v64;
77 result ^= (~(v64 >> 32));
82static uint32_t used_ports_map[32];
83static NL_RW_LOCK(port_map_lock);
85static uint32_t generate_local_port(
void)
89 static uint16_t idx_state = 0;
90 uint32_t pid = getpid() & 0x3FFFFF;
92 nl_write_lock(&port_map_lock);
95 uint32_t t = _badrandom_from_time();
99 idx_state = t ^ (t >> 16) ^ 0x3047;
101 idx_state = idx_state + 20011;
105 for (j = 0; j < 32; j++) {
114 i = (((i-1) + 7) % 31) + 1;
116 if (used_ports_map[i] == 0xFFFFFFFF)
119 for (m = 0; m < 32; m++) {
121 if (1UL & (used_ports_map[i] >> n))
124 used_ports_map[i] |= (1UL << n);
130 nl_write_unlock(&port_map_lock);
133 pid = pid + (n << 22);
134 return pid ? pid : 1024;
138 nl_write_unlock(&port_map_lock);
142static void release_local_port(uint32_t port)
150 mask = 1UL << (nr % 32);
153 nl_write_lock(&port_map_lock);
154 BUG_ON((used_ports_map[nr] & mask) != mask);
155 used_ports_map[nr] &= ~mask;
156 nl_write_unlock(&port_map_lock);
160void _nl_socket_used_ports_release_all(
const uint32_t *used_ports)
164 for (i = 0; i < 32; i++) {
165 if (used_ports[i] != 0) {
166 nl_write_lock(&port_map_lock);
167 for (; i < 32; i++) {
168 BUG_ON((used_ports_map[i] & used_ports[i]) != used_ports[i]);
169 used_ports_map[i] &= ~(used_ports[i]);
171 nl_write_unlock(&port_map_lock);
177void _nl_socket_used_ports_set(uint32_t *used_ports, uint32_t port)
183 mask = 1UL << (nr % 32);
191 used_ports[nr] |= mask;
200static struct nl_sock *__alloc_socket(
struct nl_cb *cb)
204 sk = calloc(1,
sizeof(*sk));
209 sk->s_cb = nl_cb_get(cb);
210 sk->s_local.nl_family = AF_NETLINK;
211 sk->s_peer.nl_family = AF_NETLINK;
212 sk->s_seq_next = _badrandom_from_time();
213 sk->s_seq_expect = sk->s_seq_next;
216 sk->s_flags = NL_OWN_PORT;
236 sk = __alloc_socket(cb);
257 return __alloc_socket(cb);
272 if (!(sk->s_flags & NL_OWN_PORT))
273 release_local_port(sk->s_local.nl_pid);
286static int noop_seq_check(
struct nl_msg *msg,
void *arg)
320 if (sk->s_seq_next == UINT_MAX) {
324 return sk->s_seq_next++;
341 sk->s_flags |= NL_NO_AUTO_ACK;
351 sk->s_flags &= ~NL_NO_AUTO_ACK;
357int _nl_socket_is_local_port_unspecified(
struct nl_sock *sk)
359 return (sk->s_local.nl_pid == 0);
362uint32_t _nl_socket_set_local_port_no_release(
struct nl_sock *sk,
int generate_other)
370 port = generate_local_port();
373 sk->s_local.nl_pid = port;
377 sk->s_flags |= NL_OWN_PORT;
379 sk->s_flags &= ~NL_OWN_PORT;
389uint32_t nl_socket_get_local_port(
const struct nl_sock *sk)
391 if (sk->s_local.nl_pid == 0) {
392 struct nl_sock *sk_mutable = (
struct nl_sock *) sk;
403 sk_mutable->s_local.nl_pid = generate_local_port();
404 if (sk_mutable->s_local.nl_pid == 0) {
408 sk_mutable->s_local.nl_pid = UINT32_MAX;
409 sk_mutable->s_flags |= NL_OWN_PORT;
411 sk_mutable->s_flags &= ~NL_OWN_PORT;
413 return sk->s_local.nl_pid;
430 if (!(sk->s_flags & NL_OWN_PORT))
431 release_local_port(sk->s_local.nl_pid);
432 sk->s_flags |= NL_OWN_PORT;
433 sk->s_local.nl_pid = port;
465 return -NLE_BAD_SOCK;
475 err = setsockopt(sk->s_fd, SOL_NETLINK, NETLINK_ADD_MEMBERSHIP,
476 &group,
sizeof(group));
479 NL_DBG(4,
"nl_socket_add_memberships(%p): setsockopt() failed with %d (%s)\n",
480 sk, errno, nl_strerror_l(errno));
481 return -nl_syserr2nlerr(errno);
484 group = va_arg(ap,
int);
492int nl_socket_add_membership(
struct nl_sock *sk,
int group)
515 return -NLE_BAD_SOCK;
525 err = setsockopt(sk->s_fd, SOL_NETLINK, NETLINK_DROP_MEMBERSHIP,
526 &group,
sizeof(group));
529 NL_DBG(4,
"nl_socket_drop_memberships(%p): setsockopt() failed with %d (%s)\n",
530 sk, errno, nl_strerror_l(errno));
531 return -nl_syserr2nlerr(errno);
534 group = va_arg(ap,
int);
542int nl_socket_drop_membership(
struct nl_sock *sk,
int group)
559 sk->s_local.nl_groups |= groups;
570uint32_t nl_socket_get_peer_port(
const struct nl_sock *sk)
572 return sk->s_peer.nl_pid;
575void nl_socket_set_peer_port(
struct nl_sock *sk, uint32_t port)
577 sk->s_peer.nl_pid = port;
580uint32_t nl_socket_get_peer_groups(
const struct nl_sock *sk)
582 return sk->s_peer.nl_groups;
585void nl_socket_set_peer_groups(
struct nl_sock *sk, uint32_t groups)
587 sk->s_peer.nl_groups = groups;
644 struct sockaddr_nl local = { 0 };
645 int so_type = -1, so_protocol = -1;
648 return -NLE_BAD_SOCK;
652 addrlen =
sizeof(local);
653 err = getsockname(fd, (
struct sockaddr *) &local,
656 NL_DBG(4,
"nl_socket_set_fd(%p,%d): getsockname() failed with %d (%s)\n",
657 sk, fd, errno, nl_strerror_l(errno));
658 return -nl_syserr2nlerr(errno);
660 if (addrlen !=
sizeof(local))
662 if (local.nl_family != AF_NETLINK) {
663 NL_DBG(4,
"nl_socket_set_fd(%p,%d): getsockname() returned family %d instead of %d (AF_NETLINK)\n",
664 sk, fd, local.nl_family, AF_NETLINK);
668 addrlen =
sizeof(so_type);
669 err = getsockopt(fd, SOL_SOCKET, SO_TYPE, &so_type, &addrlen);
671 NL_DBG(4,
"nl_socket_set_fd(%p,%d): getsockopt() for SO_TYPE failed with %d (%s)\n",
672 sk, fd, errno, nl_strerror_l(errno));
673 return -nl_syserr2nlerr(errno);
675 if (addrlen !=
sizeof(so_type))
677 if (so_type != SOCK_RAW) {
678 NL_DBG(4,
"nl_socket_set_fd(%p,%d): getsockopt() returned SO_TYPE %d instead of %d (SOCK_RAW)\n",
679 sk, fd, so_type, SOCK_RAW);
684 addrlen =
sizeof(so_protocol);
685 err = getsockopt(fd, SOL_SOCKET, SO_PROTOCOL, &so_protocol, &addrlen);
687 if (errno == ENOPROTOOPT)
689 NL_DBG(4,
"nl_socket_set_fd(%p,%d): getsockopt() for SO_PROTOCOL failed with %d (%s)\n",
690 sk, fd, errno, nl_strerror_l(errno));
691 return -nl_syserr2nlerr(errno);
693 if (addrlen !=
sizeof(so_protocol))
695 if (protocol >= 0 && protocol != so_protocol) {
696 NL_DBG(4,
"nl_socket_set_fd(%p,%d): getsockopt() for SO_PROTOCOL returned %d instead of %d\n",
697 sk, fd, so_protocol, protocol);
706 NL_DBG(4,
"nl_socket_set_fd(%p,%d): unknown protocol and unable to detect it via SO_PROTOCOL socket option\n",
710 so_protocol = protocol;
716 sk->s_proto = so_protocol;
730 return -NLE_BAD_SOCK;
732 if (fcntl(sk->s_fd, F_SETFL, O_NONBLOCK) < 0) {
733 NL_DBG(4,
"nl_socket_set_nonblocking(%p): fcntl() failed with %d (%s)\n",
734 sk, errno, nl_strerror_l(errno));
735 return -nl_syserr2nlerr(errno);
749 sk->s_flags |= (NL_MSG_PEEK | NL_MSG_PEEK_EXPLICIT);
760 sk->s_flags |= NL_MSG_PEEK_EXPLICIT;
761 sk->s_flags &= ~NL_MSG_PEEK;
771struct nl_cb *nl_socket_get_cb(
const struct nl_sock *sk)
773 return nl_cb_get(sk->s_cb);
776void nl_socket_set_cb(
struct nl_sock *sk,
struct nl_cb *cb)
782 sk->s_cb = nl_cb_get(cb);
799 return nl_cb_set(sk->s_cb, type, kind, func, arg);
814 return nl_cb_err(sk->s_cb, kind, func, arg);
848 return -NLE_BAD_SOCK;
850 err = setsockopt(sk->s_fd, SOL_SOCKET, SO_SNDBUF,
851 &txbuf,
sizeof(txbuf));
853 NL_DBG(4,
"nl_socket_set_buffer_size(%p): setsockopt() failed with %d (%s)\n",
854 sk, errno, nl_strerror_l(errno));
855 return -nl_syserr2nlerr(errno);
858 err = setsockopt(sk->s_fd, SOL_SOCKET, SO_RCVBUF,
859 &rxbuf,
sizeof(rxbuf));
861 NL_DBG(4,
"nl_socket_set_buffer_size(%p): setsockopt() failed with %d (%s)\n",
862 sk, errno, nl_strerror_l(errno));
863 return -nl_syserr2nlerr(errno);
896 sk->s_bufsize = bufsize;
909 return sk->s_bufsize;
924 return -NLE_BAD_SOCK;
926 err = setsockopt(sk->s_fd, SOL_SOCKET, SO_PASSCRED,
927 &state,
sizeof(state));
929 NL_DBG(4,
"nl_socket_set_passcred(%p): setsockopt() failed with %d (%s)\n",
930 sk, errno, nl_strerror_l(errno));
931 return -nl_syserr2nlerr(errno);
935 sk->s_flags |= NL_SOCK_PASSCRED;
937 sk->s_flags &= ~NL_SOCK_PASSCRED;
954 return -NLE_BAD_SOCK;
956 err = setsockopt(sk->s_fd, SOL_NETLINK, NETLINK_PKTINFO,
957 &state,
sizeof(state));
959 NL_DBG(4,
"nl_socket_recv_pktinfo(%p): setsockopt() failed with %d (%s)\n",
960 sk, errno, nl_strerror_l(errno));
961 return -nl_syserr2nlerr(errno);
int(* nl_recvmsg_err_cb_t)(struct sockaddr_nl *nla, struct nlmsgerr *nlerr, void *arg)
nl_recvmsgs() callback for error message processing customization
nl_cb_type
Callback types.
int(* nl_recvmsg_msg_cb_t)(struct nl_msg *msg, void *arg)
nl_recvmsgs() callback for message processing customization
nl_cb_kind
Callback kinds.
int nl_cb_set(struct nl_cb *cb, enum nl_cb_type type, enum nl_cb_kind kind, nl_recvmsg_msg_cb_t func, void *arg)
Set up a callback.
struct nl_cb * nl_cb_alloc(enum nl_cb_kind kind)
Allocate a new callback handle.
int nl_cb_err(struct nl_cb *cb, enum nl_cb_kind kind, nl_recvmsg_err_cb_t func, void *arg)
Set up an error callback.
@ NL_OK
Proceed with whatever would come next.
@ NL_CB_SEQ_CHECK
Called instead of internal sequence number checking.
@ NL_CB_DEFAULT
Default handlers (quiet)
@ NL_CB_CUSTOM
Customized handler specified by the user.
@ NL_CB_VERBOSE
Verbose default handlers (error messages printed)
@ NL_CB_DEBUG
Debug handlers for debugging.
int nl_socket_get_fd(const struct nl_sock *sk)
Return the file descriptor of the backing socket.
void nl_socket_set_local_port(struct nl_sock *sk, uint32_t port)
Set local port of socket.
void nl_socket_disable_auto_ack(struct nl_sock *sk)
Disable automatic request for ACK.
size_t nl_socket_get_msg_buf_size(struct nl_sock *sk)
Get default message buffer size of netlink socket.
int nl_socket_recv_pktinfo(struct nl_sock *sk, int state)
Enable/disable receival of additional packet information.
int nl_socket_modify_err_cb(struct nl_sock *sk, enum nl_cb_kind kind, nl_recvmsg_err_cb_t func, void *arg)
Modify the error callback handler associated with the socket.
void nl_join_groups(struct nl_sock *sk, int groups)
Join multicast groups (deprecated)
int nl_socket_set_passcred(struct nl_sock *sk, int state)
Enable/disable credential passing on netlink socket.
int nl_socket_add_memberships(struct nl_sock *sk, int group,...)
Join groups.
void nl_socket_disable_msg_peek(struct nl_sock *sk)
Disable use of MSG_PEEK when reading from socket.
struct nl_sock * nl_socket_alloc(void)
Allocate new netlink socket.
int nl_socket_set_msg_buf_size(struct nl_sock *sk, size_t bufsize)
Set default message buffer size of netlink socket.
struct nl_sock * nl_socket_alloc_cb(struct nl_cb *cb)
Allocate new socket with custom callbacks.
int nl_socket_set_nonblocking(const struct nl_sock *sk)
Set file descriptor of socket to non-blocking state.
void nl_socket_enable_auto_ack(struct nl_sock *sk)
Enable automatic request for ACK (default)
void nl_socket_enable_msg_peek(struct nl_sock *sk)
Enable use of MSG_PEEK when reading from socket.
int nl_socket_set_fd(struct nl_sock *sk, int protocol, int fd)
Set the socket file descriptor externally which initializes the socket similar to nl_connect().
unsigned int nl_socket_use_seq(struct nl_sock *sk)
Use next sequence number.
void nl_socket_disable_seq_check(struct nl_sock *sk)
Disable sequence number checking.
int nl_socket_set_buffer_size(struct nl_sock *sk, int rxbuf, int txbuf)
Set socket buffer size of netlink socket.
void nl_socket_free(struct nl_sock *sk)
Free a netlink socket.
int nl_socket_drop_memberships(struct nl_sock *sk, int group,...)
Leave groups.
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 with the socket.