32 #include "sys/socket.h"
34 #include <netlink-private/netlink.h>
35 #include <netlink-private/socket.h>
36 #include <netlink/netlink.h>
37 #include <netlink/utils.h>
38 #include <netlink/handlers.h>
39 #include <netlink/msg.h>
40 #include <netlink/attr.h>
44 static void __init init_default_cb(
void)
48 if ((nlcb = getenv(
"NLCB"))) {
49 if (!strcasecmp(nlcb,
"default"))
51 else if (!strcasecmp(nlcb,
"verbose"))
53 else if (!strcasecmp(nlcb,
"debug"))
56 fprintf(stderr,
"Unknown value for NLCB, valid values: "
57 "{default | verbose | debug}\n");
62 static uint32_t used_ports_map[32];
63 static NL_RW_LOCK(port_map_lock);
65 static uint32_t generate_local_port(
void)
68 static uint16_t idx_state = 0;
69 uint32_t pid = getpid() & 0x3FFFFF;
71 nl_write_lock(&port_map_lock);
74 uint32_t t = time(NULL);
78 idx_state = t ^ (t >> 16) ^ 0x3047;
80 idx_state = idx_state + 20011;
84 for (j = 0; j < 32; j++) {
93 i = (((i-1) + 7) % 31) + 1;
95 if (used_ports_map[i] == 0xFFFFFFFF)
98 for (m = 0; m < 32; m++) {
100 if (1UL & (used_ports_map[i] >> n))
103 used_ports_map[i] |= (1UL << n);
109 nl_write_unlock(&port_map_lock);
112 pid = pid + (((uint32_t)n) << 22);
113 return pid ? pid : 1024;
117 nl_write_unlock(&port_map_lock);
121 static void release_local_port(uint32_t port)
129 mask = 1UL << (nr % 32);
132 nl_write_lock(&port_map_lock);
133 BUG_ON((used_ports_map[nr] & mask) != mask);
134 used_ports_map[nr] &= ~mask;
135 nl_write_unlock(&port_map_lock);
139 void _nl_socket_used_ports_release_all(
const uint32_t *used_ports)
143 for (i = 0; i < 32; i++) {
144 if (used_ports[i] != 0) {
145 nl_write_lock(&port_map_lock);
146 for (; i < 32; i++) {
147 BUG_ON((used_ports_map[i] & used_ports[i]) != used_ports[i]);
148 used_ports_map[i] &= ~(used_ports[i]);
150 nl_write_unlock(&port_map_lock);
156 void _nl_socket_used_ports_set(uint32_t *used_ports, uint32_t port)
162 mask = 1UL << (nr % 32);
170 used_ports[nr] |= mask;
179 static struct nl_sock *__alloc_socket(
struct nl_cb *cb)
183 sk = calloc(1,
sizeof(*sk));
188 sk->s_cb = nl_cb_get(cb);
189 sk->s_local.nl_family = AF_NETLINK;
190 sk->s_peer.nl_family = AF_NETLINK;
191 sk->s_seq_expect = sk->s_seq_next = time(NULL);
194 sk->s_flags = NL_OWN_PORT;
214 sk = __alloc_socket(cb);
235 return __alloc_socket(cb);
250 if (!(sk->s_flags & NL_OWN_PORT))
251 release_local_port(sk->s_local.nl_pid);
264 static int noop_seq_check(
struct nl_msg *msg,
void *arg)
298 return sk->s_seq_next++;
315 sk->s_flags |= NL_NO_AUTO_ACK;
325 sk->s_flags &= ~NL_NO_AUTO_ACK;
331 int _nl_socket_is_local_port_unspecified(
struct nl_sock *sk)
333 return (sk->s_local.nl_pid == 0);
336 uint32_t _nl_socket_set_local_port_no_release(
struct nl_sock *sk,
int generate_other)
344 port = generate_local_port();
347 sk->s_local.nl_pid = port;
351 sk->s_flags |= NL_OWN_PORT;
353 sk->s_flags &= ~NL_OWN_PORT;
363 uint32_t nl_socket_get_local_port(
const struct nl_sock *sk)
365 if (sk->s_local.nl_pid == 0) {
366 struct nl_sock *sk_mutable = (
struct nl_sock *) sk;
377 sk_mutable->s_local.nl_pid = generate_local_port();
378 if (sk_mutable->s_local.nl_pid == 0) {
382 sk_mutable->s_local.nl_pid = UINT32_MAX;
383 sk_mutable->s_flags |= NL_OWN_PORT;
385 sk_mutable->s_flags &= ~NL_OWN_PORT;
387 return sk->s_local.nl_pid;
404 if (!(sk->s_flags & NL_OWN_PORT))
405 release_local_port(sk->s_local.nl_pid);
406 sk->s_flags |= NL_OWN_PORT;
407 sk->s_local.nl_pid = port;
439 return -NLE_BAD_SOCK;
449 err = setsockopt(sk->s_fd, SOL_NETLINK, NETLINK_ADD_MEMBERSHIP,
450 &group,
sizeof(group));
455 NL_DBG(4,
"nl_socket_add_memberships(%p): setsockopt() failed with %d (%s)\n",
456 sk, errno, strerror_r(errno, buf,
sizeof(buf)));
457 return -nl_syserr2nlerr(errno);
460 group = va_arg(ap,
int);
468 int nl_socket_add_membership(
struct nl_sock *sk,
int group)
491 return -NLE_BAD_SOCK;
501 err = setsockopt(sk->s_fd, SOL_NETLINK, NETLINK_DROP_MEMBERSHIP,
502 &group,
sizeof(group));
507 NL_DBG(4,
"nl_socket_drop_memberships(%p): setsockopt() failed with %d (%s)\n",
508 sk, errno, strerror_r(errno, buf,
sizeof(buf)));
509 return -nl_syserr2nlerr(errno);
512 group = va_arg(ap,
int);
520 int nl_socket_drop_membership(
struct nl_sock *sk,
int group)
537 sk->s_local.nl_groups |= groups;
548 uint32_t nl_socket_get_peer_port(
const struct nl_sock *sk)
550 return sk->s_peer.nl_pid;
553 void nl_socket_set_peer_port(
struct nl_sock *sk, uint32_t port)
555 sk->s_peer.nl_pid = port;
558 uint32_t nl_socket_get_peer_groups(
const struct nl_sock *sk)
560 return sk->s_peer.nl_groups;
563 void nl_socket_set_peer_groups(
struct nl_sock *sk, uint32_t groups)
565 sk->s_peer.nl_groups = groups;
623 struct sockaddr_nl local = { 0 };
624 int so_type = -1, so_protocol = -1;
627 return -NLE_BAD_SOCK;
631 addrlen =
sizeof(local);
632 err = getsockname(fd, (
struct sockaddr *) &local,
635 NL_DBG(4,
"nl_socket_set_fd(%p,%d): getsockname() failed with %d (%s)\n",
636 sk, fd, errno, strerror_r(errno, buf,
sizeof(buf)));
637 return -nl_syserr2nlerr(errno);
639 if (addrlen !=
sizeof(local))
641 if (local.nl_family != AF_NETLINK) {
642 NL_DBG(4,
"nl_socket_set_fd(%p,%d): getsockname() returned family %d instead of %d (AF_NETLINK)\n",
643 sk, fd, local.nl_family, AF_NETLINK);
647 addrlen =
sizeof(so_type);
648 err = getsockopt(fd, SOL_SOCKET, SO_TYPE, &so_type, &addrlen);
650 NL_DBG(4,
"nl_socket_set_fd(%p,%d): getsockopt() for SO_TYPE failed with %d (%s)\n",
651 sk, fd, errno, strerror_r(errno, buf,
sizeof(buf)));
652 return -nl_syserr2nlerr(errno);
654 if (addrlen !=
sizeof(so_type))
656 if (so_type != SOCK_RAW) {
657 NL_DBG(4,
"nl_socket_set_fd(%p,%d): getsockopt() returned SO_TYPE %d instead of %d (SOCK_RAW)\n",
658 sk, fd, so_type, SOCK_RAW);
663 addrlen =
sizeof(so_protocol);
664 err = getsockopt(fd, SOL_SOCKET, SO_PROTOCOL, &so_protocol, &addrlen);
666 if (errno == ENOPROTOOPT)
668 NL_DBG(4,
"nl_socket_set_fd(%p,%d): getsockopt() for SO_PROTOCOL failed with %d (%s)\n",
669 sk, fd, errno, strerror_r(errno, buf,
sizeof(buf)));
670 return -nl_syserr2nlerr(errno);
672 if (addrlen !=
sizeof(so_protocol))
674 if (protocol >= 0 && protocol != so_protocol) {
675 NL_DBG(4,
"nl_socket_set_fd(%p,%d): getsockopt() for SO_PROTOCOL returned %d instead of %d\n",
676 sk, fd, so_protocol, protocol);
685 NL_DBG(4,
"nl_socket_set_fd(%p,%d): unknown protocol and unable to detect it via SO_PROTOCOL socket option\n",
689 so_protocol = protocol;
695 sk->s_proto = so_protocol;
709 return -NLE_BAD_SOCK;
711 if (fcntl(sk->s_fd, F_SETFL, O_NONBLOCK) < 0) {
714 NL_DBG(4,
"nl_socket_set_nonblocking(%p): fcntl() failed with %d (%s)\n",
715 sk, errno, strerror_r(errno, buf,
sizeof(buf)));
716 return -nl_syserr2nlerr(errno);
728 sk->s_flags |= NL_MSG_PEEK;
737 sk->s_flags &= ~NL_MSG_PEEK;
747 struct nl_cb *nl_socket_get_cb(
const struct nl_sock *sk)
749 return nl_cb_get(sk->s_cb);
752 void nl_socket_set_cb(
struct nl_sock *sk,
struct nl_cb *cb)
758 sk->s_cb = nl_cb_get(cb);
775 return nl_cb_set(sk->s_cb, type, kind, func, arg);
790 return nl_cb_err(sk->s_cb, kind, func, arg);
825 return -NLE_BAD_SOCK;
827 err = setsockopt(sk->s_fd, SOL_SOCKET, SO_SNDBUF,
828 &txbuf,
sizeof(txbuf));
830 NL_DBG(4,
"nl_socket_set_buffer_size(%p): setsockopt() failed with %d (%s)\n",
831 sk, errno, strerror_r(errno, buf,
sizeof(buf)));
832 return -nl_syserr2nlerr(errno);
835 err = setsockopt(sk->s_fd, SOL_SOCKET, SO_RCVBUF,
836 &rxbuf,
sizeof(rxbuf));
838 NL_DBG(4,
"nl_socket_set_buffer_size(%p): setsockopt() failed with %d (%s)\n",
839 sk, errno, strerror_r(errno, buf,
sizeof(buf)));
840 return -nl_syserr2nlerr(errno);
860 sk->s_bufsize = bufsize;
873 return sk->s_bufsize;
888 return -NLE_BAD_SOCK;
890 err = setsockopt(sk->s_fd, SOL_SOCKET, SO_PASSCRED,
891 &state,
sizeof(state));
895 NL_DBG(4,
"nl_socket_set_passcred(%p): setsockopt() failed with %d (%s)\n",
896 sk, errno, strerror_r(errno, buf,
sizeof(buf)));
897 return -nl_syserr2nlerr(errno);
901 sk->s_flags |= NL_SOCK_PASSCRED;
903 sk->s_flags &= ~NL_SOCK_PASSCRED;
920 return -NLE_BAD_SOCK;
922 err = setsockopt(sk->s_fd, SOL_NETLINK, NETLINK_PKTINFO,
923 &state,
sizeof(state));
927 NL_DBG(4,
"nl_socket_recv_pktinfo(%p): setsockopt() failed with %d (%s)\n",
928 sk, errno, strerror_r(errno, buf,
sizeof(buf)));
929 return -nl_syserr2nlerr(errno);
void nl_socket_enable_auto_ack(struct nl_sock *sk)
Enable automatic request for ACK (default)
int nl_socket_set_passcred(struct nl_sock *sk, int state)
Enable/disable credential passing on netlink socket.
int nl_socket_drop_memberships(struct nl_sock *sk, int group,...)
Leave groups.
Customized handler specified by the user.
int nl_socket_get_fd(const struct nl_sock *sk)
Return the file descriptor of the backing socket.
void nl_socket_disable_auto_ack(struct nl_sock *sk)
Disable automatic request for ACK.
void nl_socket_enable_msg_peek(struct nl_sock *sk)
Enable use of MSG_PEEK when reading from socket.
void nl_socket_set_local_port(struct nl_sock *sk, uint32_t port)
Set local port of socket.
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.
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_sock * nl_socket_alloc(void)
Allocate new netlink socket.
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.
struct nl_sock * nl_socket_alloc_cb(struct nl_cb *cb)
Allocate new socket with custom callbacks.
void nl_socket_disable_seq_check(struct nl_sock *sk)
Disable sequence number checking.
int nl_socket_set_nonblocking(const struct nl_sock *sk)
Set file descriptor of socket to non-blocking state.
int(* nl_recvmsg_err_cb_t)(struct sockaddr_nl *nla, struct nlmsgerr *nlerr, void *arg)
nl_recvmsgs() callback for error message processing customization
void nl_socket_free(struct nl_sock *sk)
Free a netlink socket.
unsigned int nl_socket_use_seq(struct nl_sock *sk)
Use next sequence number.
Debug handlers for debugging.
void nl_socket_disable_msg_peek(struct nl_sock *sk)
Disable use of MSG_PEEK when reading from socket.
int(* nl_recvmsg_msg_cb_t)(struct nl_msg *msg, void *arg)
nl_recvmsgs() callback for message processing customization
Called instead of internal sequence number checking.
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()...
Proceed with wathever would come next.
int nl_socket_set_msg_buf_size(struct nl_sock *sk, size_t bufsize)
Set default message buffer size of netlink socket.
nl_cb_type
Callback types.
struct nl_cb * nl_cb_alloc(enum nl_cb_kind kind)
Allocate a new callback handle.
int nl_socket_set_buffer_size(struct nl_sock *sk, int rxbuf, int txbuf)
Set socket buffer size of netlink socket.
void nl_join_groups(struct nl_sock *sk, int groups)
Join multicast groups (deprecated)
int nl_socket_add_memberships(struct nl_sock *sk, int group,...)
Join groups.
int nl_socket_recv_pktinfo(struct nl_sock *sk, int state)
Enable/disable receival of additional packet information.
size_t nl_socket_get_msg_buf_size(struct nl_sock *sk)
Get default message buffer size of netlink socket.
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.
Verbose default handlers (error messages printed)