26#include "sys/socket.h"
28#include <netlink-private/netlink.h>
29#include <netlink-private/socket.h>
30#include <netlink-private/utils.h>
31#include <netlink/netlink.h>
32#include <netlink/utils.h>
33#include <netlink/handlers.h>
34#include <netlink/msg.h>
35#include <netlink/attr.h>
39static void __init init_default_cb(
void)
43 if ((nlcb = getenv(
"NLCB"))) {
44 if (!strcasecmp(nlcb,
"default"))
46 else if (!strcasecmp(nlcb,
"verbose"))
48 else if (!strcasecmp(nlcb,
"debug"))
51 fprintf(stderr,
"Unknown value for NLCB, valid values: "
52 "{default | verbose | debug}\n");
57static uint32_t used_ports_map[32];
58static NL_RW_LOCK(port_map_lock);
60static uint32_t generate_local_port(
void)
64 static uint16_t idx_state = 0;
65 uint32_t pid = getpid() & 0x3FFFFF;
67 nl_write_lock(&port_map_lock);
70 uint32_t t = time(NULL);
74 idx_state = t ^ (t >> 16) ^ 0x3047;
76 idx_state = idx_state + 20011;
80 for (j = 0; j < 32; j++) {
89 i = (((i-1) + 7) % 31) + 1;
91 if (used_ports_map[i] == 0xFFFFFFFF)
94 for (m = 0; m < 32; m++) {
96 if (1UL & (used_ports_map[i] >> n))
99 used_ports_map[i] |= (1UL << n);
105 nl_write_unlock(&port_map_lock);
108 pid = pid + (n << 22);
109 return pid ? pid : 1024;
113 nl_write_unlock(&port_map_lock);
117static void release_local_port(uint32_t port)
125 mask = 1UL << (nr % 32);
128 nl_write_lock(&port_map_lock);
129 BUG_ON((used_ports_map[nr] & mask) != mask);
130 used_ports_map[nr] &= ~mask;
131 nl_write_unlock(&port_map_lock);
135void _nl_socket_used_ports_release_all(
const uint32_t *used_ports)
139 for (i = 0; i < 32; i++) {
140 if (used_ports[i] != 0) {
141 nl_write_lock(&port_map_lock);
142 for (; i < 32; i++) {
143 BUG_ON((used_ports_map[i] & used_ports[i]) != used_ports[i]);
144 used_ports_map[i] &= ~(used_ports[i]);
146 nl_write_unlock(&port_map_lock);
152void _nl_socket_used_ports_set(uint32_t *used_ports, uint32_t port)
158 mask = 1UL << (nr % 32);
166 used_ports[nr] |= mask;
175static struct nl_sock *__alloc_socket(
struct nl_cb *cb)
179 sk = calloc(1,
sizeof(*sk));
184 sk->s_cb = nl_cb_get(cb);
185 sk->s_local.nl_family = AF_NETLINK;
186 sk->s_peer.nl_family = AF_NETLINK;
187 sk->s_seq_expect = sk->s_seq_next = time(NULL);
190 sk->s_flags = NL_OWN_PORT;
210 sk = __alloc_socket(cb);
231 return __alloc_socket(cb);
246 if (!(sk->s_flags & NL_OWN_PORT))
247 release_local_port(sk->s_local.nl_pid);
260static int noop_seq_check(
struct nl_msg *msg,
void *arg)
294 return sk->s_seq_next++;
311 sk->s_flags |= NL_NO_AUTO_ACK;
321 sk->s_flags &= ~NL_NO_AUTO_ACK;
327int _nl_socket_is_local_port_unspecified(
struct nl_sock *sk)
329 return (sk->s_local.nl_pid == 0);
332uint32_t _nl_socket_set_local_port_no_release(
struct nl_sock *sk,
int generate_other)
340 port = generate_local_port();
343 sk->s_local.nl_pid = port;
347 sk->s_flags |= NL_OWN_PORT;
349 sk->s_flags &= ~NL_OWN_PORT;
359uint32_t nl_socket_get_local_port(
const struct nl_sock *sk)
361 if (sk->s_local.nl_pid == 0) {
362 struct nl_sock *sk_mutable = (
struct nl_sock *) sk;
373 sk_mutable->s_local.nl_pid = generate_local_port();
374 if (sk_mutable->s_local.nl_pid == 0) {
378 sk_mutable->s_local.nl_pid = UINT32_MAX;
379 sk_mutable->s_flags |= NL_OWN_PORT;
381 sk_mutable->s_flags &= ~NL_OWN_PORT;
383 return sk->s_local.nl_pid;
400 if (!(sk->s_flags & NL_OWN_PORT))
401 release_local_port(sk->s_local.nl_pid);
402 sk->s_flags |= NL_OWN_PORT;
403 sk->s_local.nl_pid = port;
435 return -NLE_BAD_SOCK;
445 err = setsockopt(sk->s_fd, SOL_NETLINK, NETLINK_ADD_MEMBERSHIP,
446 &group,
sizeof(group));
449 NL_DBG(4,
"nl_socket_add_memberships(%p): setsockopt() failed with %d (%s)\n",
450 sk, errno, nl_strerror_l(errno));
451 return -nl_syserr2nlerr(errno);
454 group = va_arg(ap,
int);
462int nl_socket_add_membership(
struct nl_sock *sk,
int group)
485 return -NLE_BAD_SOCK;
495 err = setsockopt(sk->s_fd, SOL_NETLINK, NETLINK_DROP_MEMBERSHIP,
496 &group,
sizeof(group));
499 NL_DBG(4,
"nl_socket_drop_memberships(%p): setsockopt() failed with %d (%s)\n",
500 sk, errno, nl_strerror_l(errno));
501 return -nl_syserr2nlerr(errno);
504 group = va_arg(ap,
int);
512int nl_socket_drop_membership(
struct nl_sock *sk,
int group)
529 sk->s_local.nl_groups |= groups;
540uint32_t nl_socket_get_peer_port(
const struct nl_sock *sk)
542 return sk->s_peer.nl_pid;
545void nl_socket_set_peer_port(
struct nl_sock *sk, uint32_t port)
547 sk->s_peer.nl_pid = port;
550uint32_t nl_socket_get_peer_groups(
const struct nl_sock *sk)
552 return sk->s_peer.nl_groups;
555void nl_socket_set_peer_groups(
struct nl_sock *sk, uint32_t groups)
557 sk->s_peer.nl_groups = groups;
614 struct sockaddr_nl local = { 0 };
615 int so_type = -1, so_protocol = -1;
618 return -NLE_BAD_SOCK;
622 addrlen =
sizeof(local);
623 err = getsockname(fd, (
struct sockaddr *) &local,
626 NL_DBG(4,
"nl_socket_set_fd(%p,%d): getsockname() failed with %d (%s)\n",
627 sk, fd, errno, nl_strerror_l(errno));
628 return -nl_syserr2nlerr(errno);
630 if (addrlen !=
sizeof(local))
632 if (local.nl_family != AF_NETLINK) {
633 NL_DBG(4,
"nl_socket_set_fd(%p,%d): getsockname() returned family %d instead of %d (AF_NETLINK)\n",
634 sk, fd, local.nl_family, AF_NETLINK);
638 addrlen =
sizeof(so_type);
639 err = getsockopt(fd, SOL_SOCKET, SO_TYPE, &so_type, &addrlen);
641 NL_DBG(4,
"nl_socket_set_fd(%p,%d): getsockopt() for SO_TYPE failed with %d (%s)\n",
642 sk, fd, errno, nl_strerror_l(errno));
643 return -nl_syserr2nlerr(errno);
645 if (addrlen !=
sizeof(so_type))
647 if (so_type != SOCK_RAW) {
648 NL_DBG(4,
"nl_socket_set_fd(%p,%d): getsockopt() returned SO_TYPE %d instead of %d (SOCK_RAW)\n",
649 sk, fd, so_type, SOCK_RAW);
654 addrlen =
sizeof(so_protocol);
655 err = getsockopt(fd, SOL_SOCKET, SO_PROTOCOL, &so_protocol, &addrlen);
657 if (errno == ENOPROTOOPT)
659 NL_DBG(4,
"nl_socket_set_fd(%p,%d): getsockopt() for SO_PROTOCOL failed with %d (%s)\n",
660 sk, fd, errno, nl_strerror_l(errno));
661 return -nl_syserr2nlerr(errno);
663 if (addrlen !=
sizeof(so_protocol))
665 if (protocol >= 0 && protocol != so_protocol) {
666 NL_DBG(4,
"nl_socket_set_fd(%p,%d): getsockopt() for SO_PROTOCOL returned %d instead of %d\n",
667 sk, fd, so_protocol, protocol);
676 NL_DBG(4,
"nl_socket_set_fd(%p,%d): unknown protocol and unable to detect it via SO_PROTOCOL socket option\n",
680 so_protocol = protocol;
686 sk->s_proto = so_protocol;
700 return -NLE_BAD_SOCK;
702 if (fcntl(sk->s_fd, F_SETFL, O_NONBLOCK) < 0) {
703 NL_DBG(4,
"nl_socket_set_nonblocking(%p): fcntl() failed with %d (%s)\n",
704 sk, errno, nl_strerror_l(errno));
705 return -nl_syserr2nlerr(errno);
719 sk->s_flags |= (NL_MSG_PEEK | NL_MSG_PEEK_EXPLICIT);
730 sk->s_flags |= NL_MSG_PEEK_EXPLICIT;
731 sk->s_flags &= ~NL_MSG_PEEK;
741struct nl_cb *nl_socket_get_cb(
const struct nl_sock *sk)
743 return nl_cb_get(sk->s_cb);
746void nl_socket_set_cb(
struct nl_sock *sk,
struct nl_cb *cb)
752 sk->s_cb = nl_cb_get(cb);
769 return nl_cb_set(sk->s_cb, type, kind, func, arg);
784 return nl_cb_err(sk->s_cb, kind, func, arg);
818 return -NLE_BAD_SOCK;
820 err = setsockopt(sk->s_fd, SOL_SOCKET, SO_SNDBUF,
821 &txbuf,
sizeof(txbuf));
823 NL_DBG(4,
"nl_socket_set_buffer_size(%p): setsockopt() failed with %d (%s)\n",
824 sk, errno, nl_strerror_l(errno));
825 return -nl_syserr2nlerr(errno);
828 err = setsockopt(sk->s_fd, SOL_SOCKET, SO_RCVBUF,
829 &rxbuf,
sizeof(rxbuf));
831 NL_DBG(4,
"nl_socket_set_buffer_size(%p): setsockopt() failed with %d (%s)\n",
832 sk, errno, nl_strerror_l(errno));
833 return -nl_syserr2nlerr(errno);
866 sk->s_bufsize = bufsize;
879 return sk->s_bufsize;
894 return -NLE_BAD_SOCK;
896 err = setsockopt(sk->s_fd, SOL_SOCKET, SO_PASSCRED,
897 &state,
sizeof(state));
899 NL_DBG(4,
"nl_socket_set_passcred(%p): setsockopt() failed with %d (%s)\n",
900 sk, errno, nl_strerror_l(errno));
901 return -nl_syserr2nlerr(errno);
905 sk->s_flags |= NL_SOCK_PASSCRED;
907 sk->s_flags &= ~NL_SOCK_PASSCRED;
924 return -NLE_BAD_SOCK;
926 err = setsockopt(sk->s_fd, SOL_NETLINK, NETLINK_PKTINFO,
927 &state,
sizeof(state));
929 NL_DBG(4,
"nl_socket_recv_pktinfo(%p): setsockopt() failed with %d (%s)\n",
930 sk, errno, nl_strerror_l(errno));
931 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.