24#include "nl-default.h"
27#include <sys/socket.h>
29#include <netlink/netlink.h>
30#include <netlink/utils.h>
31#include <netlink/handlers.h>
32#include <netlink/msg.h>
33#include <netlink/attr.h>
36#include "nl-priv-dynamic-core/nl-core.h"
37#include "nl-aux-core/nl-core.h"
40#define SOL_NETLINK 270
45static void _nl_init init_default_cb(
void)
49 if ((nlcb = getenv(
"NLCB"))) {
50 if (!strcasecmp(nlcb,
"default"))
52 else if (!strcasecmp(nlcb,
"verbose"))
54 else if (!strcasecmp(nlcb,
"debug"))
57 fprintf(stderr,
"Unknown value for NLCB, valid values: "
58 "{default | verbose | debug}\n");
63static uint32_t _badrandom_from_time(
void)
71 result = (uint32_t)v64;
76 result ^= (~(v64 >> 32));
81static uint32_t used_ports_map[32];
82static NL_RW_LOCK(port_map_lock);
84static uint32_t generate_local_port(
void)
88 static uint16_t idx_state = 0;
89 uint32_t pid = getpid() & 0x3FFFFF;
91 nl_write_lock(&port_map_lock);
94 uint32_t t = _badrandom_from_time();
98 idx_state = t ^ (t >> 16) ^ 0x3047;
100 idx_state = idx_state + 20011;
104 for (j = 0; j < 32; j++) {
113 i = (((i-1) + 7) % 31) + 1;
115 if (used_ports_map[i] == 0xFFFFFFFF)
118 for (m = 0; m < 32; m++) {
120 if (1UL & (used_ports_map[i] >> n))
123 used_ports_map[i] |= (1UL << n);
129 nl_write_unlock(&port_map_lock);
132 pid = pid + (n << 22);
133 return pid ? pid : 1024;
137 nl_write_unlock(&port_map_lock);
141static void release_local_port(uint32_t port)
149 mask = 1UL << (nr % 32);
152 nl_write_lock(&port_map_lock);
153 BUG_ON((used_ports_map[nr] & mask) != mask);
154 used_ports_map[nr] &= ~mask;
155 nl_write_unlock(&port_map_lock);
159void _nl_socket_used_ports_release_all(
const uint32_t *used_ports)
163 for (i = 0; i < 32; i++) {
164 if (used_ports[i] != 0) {
165 nl_write_lock(&port_map_lock);
166 for (; i < 32; i++) {
167 BUG_ON((used_ports_map[i] & used_ports[i]) != used_ports[i]);
168 used_ports_map[i] &= ~(used_ports[i]);
170 nl_write_unlock(&port_map_lock);
176void _nl_socket_used_ports_set(uint32_t *used_ports, uint32_t port)
182 mask = 1UL << (nr % 32);
190 used_ports[nr] |= mask;
199static struct nl_sock *__alloc_socket(
struct nl_cb *cb)
203 sk = calloc(1,
sizeof(*sk));
208 sk->s_cb = nl_cb_get(cb);
209 sk->s_local.nl_family = AF_NETLINK;
210 sk->s_peer.nl_family = AF_NETLINK;
211 sk->s_seq_next = _badrandom_from_time();
212 sk->s_seq_expect = sk->s_seq_next;
215 sk->s_flags = NL_OWN_PORT;
235 sk = __alloc_socket(cb);
256 return __alloc_socket(cb);
271 if (!(sk->s_flags & NL_OWN_PORT))
272 release_local_port(sk->s_local.nl_pid);
285static int noop_seq_check(
struct nl_msg *msg,
void *arg)
319 return sk->s_seq_next++;
336 sk->s_flags |= NL_NO_AUTO_ACK;
346 sk->s_flags &= ~NL_NO_AUTO_ACK;
352int _nl_socket_is_local_port_unspecified(
struct nl_sock *sk)
354 return (sk->s_local.nl_pid == 0);
357uint32_t _nl_socket_set_local_port_no_release(
struct nl_sock *sk,
int generate_other)
365 port = generate_local_port();
368 sk->s_local.nl_pid = port;
372 sk->s_flags |= NL_OWN_PORT;
374 sk->s_flags &= ~NL_OWN_PORT;
384uint32_t nl_socket_get_local_port(
const struct nl_sock *sk)
386 if (sk->s_local.nl_pid == 0) {
387 struct nl_sock *sk_mutable = (
struct nl_sock *) sk;
398 sk_mutable->s_local.nl_pid = generate_local_port();
399 if (sk_mutable->s_local.nl_pid == 0) {
403 sk_mutable->s_local.nl_pid = UINT32_MAX;
404 sk_mutable->s_flags |= NL_OWN_PORT;
406 sk_mutable->s_flags &= ~NL_OWN_PORT;
408 return sk->s_local.nl_pid;
425 if (!(sk->s_flags & NL_OWN_PORT))
426 release_local_port(sk->s_local.nl_pid);
427 sk->s_flags |= NL_OWN_PORT;
428 sk->s_local.nl_pid = port;
460 return -NLE_BAD_SOCK;
470 err = setsockopt(sk->s_fd, SOL_NETLINK, NETLINK_ADD_MEMBERSHIP,
471 &group,
sizeof(group));
474 NL_DBG(4,
"nl_socket_add_memberships(%p): setsockopt() failed with %d (%s)\n",
475 sk, errno, nl_strerror_l(errno));
476 return -nl_syserr2nlerr(errno);
479 group = va_arg(ap,
int);
487int nl_socket_add_membership(
struct nl_sock *sk,
int group)
510 return -NLE_BAD_SOCK;
520 err = setsockopt(sk->s_fd, SOL_NETLINK, NETLINK_DROP_MEMBERSHIP,
521 &group,
sizeof(group));
524 NL_DBG(4,
"nl_socket_drop_memberships(%p): setsockopt() failed with %d (%s)\n",
525 sk, errno, nl_strerror_l(errno));
526 return -nl_syserr2nlerr(errno);
529 group = va_arg(ap,
int);
537int nl_socket_drop_membership(
struct nl_sock *sk,
int group)
554 sk->s_local.nl_groups |= groups;
565uint32_t nl_socket_get_peer_port(
const struct nl_sock *sk)
567 return sk->s_peer.nl_pid;
570void nl_socket_set_peer_port(
struct nl_sock *sk, uint32_t port)
572 sk->s_peer.nl_pid = port;
575uint32_t nl_socket_get_peer_groups(
const struct nl_sock *sk)
577 return sk->s_peer.nl_groups;
580void nl_socket_set_peer_groups(
struct nl_sock *sk, uint32_t groups)
582 sk->s_peer.nl_groups = groups;
639 struct sockaddr_nl local = { 0 };
640 int so_type = -1, so_protocol = -1;
643 return -NLE_BAD_SOCK;
647 addrlen =
sizeof(local);
648 err = getsockname(fd, (
struct sockaddr *) &local,
651 NL_DBG(4,
"nl_socket_set_fd(%p,%d): getsockname() failed with %d (%s)\n",
652 sk, fd, errno, nl_strerror_l(errno));
653 return -nl_syserr2nlerr(errno);
655 if (addrlen !=
sizeof(local))
657 if (local.nl_family != AF_NETLINK) {
658 NL_DBG(4,
"nl_socket_set_fd(%p,%d): getsockname() returned family %d instead of %d (AF_NETLINK)\n",
659 sk, fd, local.nl_family, AF_NETLINK);
663 addrlen =
sizeof(so_type);
664 err = getsockopt(fd, SOL_SOCKET, SO_TYPE, &so_type, &addrlen);
666 NL_DBG(4,
"nl_socket_set_fd(%p,%d): getsockopt() for SO_TYPE failed with %d (%s)\n",
667 sk, fd, errno, nl_strerror_l(errno));
668 return -nl_syserr2nlerr(errno);
670 if (addrlen !=
sizeof(so_type))
672 if (so_type != SOCK_RAW) {
673 NL_DBG(4,
"nl_socket_set_fd(%p,%d): getsockopt() returned SO_TYPE %d instead of %d (SOCK_RAW)\n",
674 sk, fd, so_type, SOCK_RAW);
679 addrlen =
sizeof(so_protocol);
680 err = getsockopt(fd, SOL_SOCKET, SO_PROTOCOL, &so_protocol, &addrlen);
682 if (errno == ENOPROTOOPT)
684 NL_DBG(4,
"nl_socket_set_fd(%p,%d): getsockopt() for SO_PROTOCOL failed with %d (%s)\n",
685 sk, fd, errno, nl_strerror_l(errno));
686 return -nl_syserr2nlerr(errno);
688 if (addrlen !=
sizeof(so_protocol))
690 if (protocol >= 0 && protocol != so_protocol) {
691 NL_DBG(4,
"nl_socket_set_fd(%p,%d): getsockopt() for SO_PROTOCOL returned %d instead of %d\n",
692 sk, fd, so_protocol, protocol);
701 NL_DBG(4,
"nl_socket_set_fd(%p,%d): unknown protocol and unable to detect it via SO_PROTOCOL socket option\n",
705 so_protocol = protocol;
711 sk->s_proto = so_protocol;
725 return -NLE_BAD_SOCK;
727 if (fcntl(sk->s_fd, F_SETFL, O_NONBLOCK) < 0) {
728 NL_DBG(4,
"nl_socket_set_nonblocking(%p): fcntl() failed with %d (%s)\n",
729 sk, errno, nl_strerror_l(errno));
730 return -nl_syserr2nlerr(errno);
744 sk->s_flags |= (NL_MSG_PEEK | NL_MSG_PEEK_EXPLICIT);
755 sk->s_flags |= NL_MSG_PEEK_EXPLICIT;
756 sk->s_flags &= ~NL_MSG_PEEK;
766struct nl_cb *nl_socket_get_cb(
const struct nl_sock *sk)
768 return nl_cb_get(sk->s_cb);
771void nl_socket_set_cb(
struct nl_sock *sk,
struct nl_cb *cb)
777 sk->s_cb = nl_cb_get(cb);
794 return nl_cb_set(sk->s_cb, type, kind, func, arg);
809 return nl_cb_err(sk->s_cb, kind, func, arg);
843 return -NLE_BAD_SOCK;
845 err = setsockopt(sk->s_fd, SOL_SOCKET, SO_SNDBUF,
846 &txbuf,
sizeof(txbuf));
848 NL_DBG(4,
"nl_socket_set_buffer_size(%p): setsockopt() failed with %d (%s)\n",
849 sk, errno, nl_strerror_l(errno));
850 return -nl_syserr2nlerr(errno);
853 err = setsockopt(sk->s_fd, SOL_SOCKET, SO_RCVBUF,
854 &rxbuf,
sizeof(rxbuf));
856 NL_DBG(4,
"nl_socket_set_buffer_size(%p): setsockopt() failed with %d (%s)\n",
857 sk, errno, nl_strerror_l(errno));
858 return -nl_syserr2nlerr(errno);
891 sk->s_bufsize = bufsize;
904 return sk->s_bufsize;
919 return -NLE_BAD_SOCK;
921 err = setsockopt(sk->s_fd, SOL_SOCKET, SO_PASSCRED,
922 &state,
sizeof(state));
924 NL_DBG(4,
"nl_socket_set_passcred(%p): setsockopt() failed with %d (%s)\n",
925 sk, errno, nl_strerror_l(errno));
926 return -nl_syserr2nlerr(errno);
930 sk->s_flags |= NL_SOCK_PASSCRED;
932 sk->s_flags &= ~NL_SOCK_PASSCRED;
949 return -NLE_BAD_SOCK;
951 err = setsockopt(sk->s_fd, SOL_NETLINK, NETLINK_PKTINFO,
952 &state,
sizeof(state));
954 NL_DBG(4,
"nl_socket_recv_pktinfo(%p): setsockopt() failed with %d (%s)\n",
955 sk, errno, nl_strerror_l(errno));
956 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.