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);
111 return pid + (((uint32_t)n) << 22);
115 nl_write_unlock(&port_map_lock);
118 NL_DBG(1,
"Warning: Ran out of unique local port namespace\n");
122 static void release_local_port(uint32_t port)
127 if (port == UINT32_MAX)
133 mask = 1UL << (nr % 32);
136 nl_write_lock(&port_map_lock);
137 BUG_ON((used_ports_map[nr] & mask) != mask);
138 used_ports_map[nr] &= ~mask;
139 nl_write_unlock(&port_map_lock);
143 void _nl_socket_used_ports_release_all(
const uint32_t *used_ports)
147 for (i = 0; i < 32; i++) {
148 if (used_ports[i] != 0) {
149 nl_write_lock(&port_map_lock);
150 for (; i < 32; i++) {
151 BUG_ON((used_ports_map[i] & used_ports[i]) != used_ports[i]);
152 used_ports_map[i] &= ~(used_ports[i]);
154 nl_write_unlock(&port_map_lock);
160 void _nl_socket_used_ports_set(uint32_t *used_ports, uint32_t port)
166 mask = 1UL << (nr % 32);
174 used_ports[nr] |= mask;
183 static struct nl_sock *__alloc_socket(
struct nl_cb *cb)
187 sk = calloc(1,
sizeof(*sk));
192 sk->s_cb = nl_cb_get(cb);
193 sk->s_local.nl_family = AF_NETLINK;
194 sk->s_peer.nl_family = AF_NETLINK;
195 sk->s_seq_expect = sk->s_seq_next = time(NULL);
198 sk->s_flags = NL_OWN_PORT;
218 sk = __alloc_socket(cb);
239 return __alloc_socket(cb);
254 if (!(sk->s_flags & NL_OWN_PORT))
255 release_local_port(sk->s_local.nl_pid);
268 static int noop_seq_check(
struct nl_msg *msg,
void *arg)
302 return sk->s_seq_next++;
319 sk->s_flags |= NL_NO_AUTO_ACK;
329 sk->s_flags &= ~NL_NO_AUTO_ACK;
335 int _nl_socket_is_local_port_unspecified(
struct nl_sock *sk)
337 return (sk->s_local.nl_pid == 0);
340 uint32_t _nl_socket_generate_local_port_no_release(
struct nl_sock *sk)
347 port = generate_local_port();
348 sk->s_flags &= ~NL_OWN_PORT;
349 sk->s_local.nl_pid = port;
359 uint32_t nl_socket_get_local_port(
const struct nl_sock *sk)
361 if (sk->s_local.nl_pid == 0) {
371 return _nl_socket_generate_local_port_no_release((
struct nl_sock *) sk);
373 return sk->s_local.nl_pid;
390 if (!(sk->s_flags & NL_OWN_PORT))
391 release_local_port(sk->s_local.nl_pid);
392 sk->s_flags |= NL_OWN_PORT;
393 sk->s_local.nl_pid = port;
425 return -NLE_BAD_SOCK;
435 err = setsockopt(sk->s_fd, SOL_NETLINK, NETLINK_ADD_MEMBERSHIP,
436 &group,
sizeof(group));
441 NL_DBG(4,
"nl_socket_add_memberships(%p): setsockopt() failed with %d (%s)\n",
442 sk, errno, strerror_r(errno, buf,
sizeof(buf)));
443 return -nl_syserr2nlerr(errno);
446 group = va_arg(ap,
int);
454 int nl_socket_add_membership(
struct nl_sock *sk,
int group)
477 return -NLE_BAD_SOCK;
487 err = setsockopt(sk->s_fd, SOL_NETLINK, NETLINK_DROP_MEMBERSHIP,
488 &group,
sizeof(group));
493 NL_DBG(4,
"nl_socket_drop_memberships(%p): setsockopt() failed with %d (%s)\n",
494 sk, errno, strerror_r(errno, buf,
sizeof(buf)));
495 return -nl_syserr2nlerr(errno);
498 group = va_arg(ap,
int);
506 int nl_socket_drop_membership(
struct nl_sock *sk,
int group)
523 sk->s_local.nl_groups |= groups;
534 uint32_t nl_socket_get_peer_port(
const struct nl_sock *sk)
536 return sk->s_peer.nl_pid;
539 void nl_socket_set_peer_port(
struct nl_sock *sk, uint32_t port)
541 sk->s_peer.nl_pid = port;
544 uint32_t nl_socket_get_peer_groups(
const struct nl_sock *sk)
546 return sk->s_peer.nl_groups;
549 void nl_socket_set_peer_groups(
struct nl_sock *sk, uint32_t groups)
551 sk->s_peer.nl_groups = groups;
609 struct sockaddr_nl local = { 0 };
610 int so_type = -1, so_protocol = -1;
613 return -NLE_BAD_SOCK;
617 addrlen =
sizeof(local);
618 err = getsockname(fd, (
struct sockaddr *) &local,
621 NL_DBG(4,
"nl_socket_set_fd(%p,%d): getsockname() failed with %d (%s)\n",
622 sk, fd, errno, strerror_r(errno, buf,
sizeof(buf)));
623 return -nl_syserr2nlerr(errno);
625 if (addrlen !=
sizeof(local))
627 if (local.nl_family != AF_NETLINK) {
628 NL_DBG(4,
"nl_socket_set_fd(%p,%d): getsockname() returned family %d instead of %d (AF_NETLINK)\n",
629 sk, fd, local.nl_family, AF_NETLINK);
633 addrlen =
sizeof(so_type);
634 err = getsockopt(fd, SOL_SOCKET, SO_TYPE, &so_type, &addrlen);
636 NL_DBG(4,
"nl_socket_set_fd(%p,%d): getsockopt() for SO_TYPE failed with %d (%s)\n",
637 sk, fd, errno, strerror_r(errno, buf,
sizeof(buf)));
638 return -nl_syserr2nlerr(errno);
640 if (addrlen !=
sizeof(so_type))
642 if (so_type != SOCK_RAW) {
643 NL_DBG(4,
"nl_socket_set_fd(%p,%d): getsockopt() returned SO_TYPE %d instead of %d (SOCK_RAW)\n",
644 sk, fd, so_type, SOCK_RAW);
649 addrlen =
sizeof(so_protocol);
650 err = getsockopt(fd, SOL_SOCKET, SO_PROTOCOL, &so_protocol, &addrlen);
652 if (errno == ENOPROTOOPT)
654 NL_DBG(4,
"nl_socket_set_fd(%p,%d): getsockopt() for SO_PROTOCOL failed with %d (%s)\n",
655 sk, fd, errno, strerror_r(errno, buf,
sizeof(buf)));
656 return -nl_syserr2nlerr(errno);
658 if (addrlen !=
sizeof(so_protocol))
660 if (protocol >= 0 && protocol != so_protocol) {
661 NL_DBG(4,
"nl_socket_set_fd(%p,%d): getsockopt() for SO_PROTOCOL returned %d instead of %d\n",
662 sk, fd, so_protocol, protocol);
671 NL_DBG(4,
"nl_socket_set_fd(%p,%d): unknown protocol and unable to detect it via SO_PROTOCOL socket option\n",
675 so_protocol = protocol;
678 if (sk->s_local.nl_pid != local.nl_pid) {
686 sk->s_proto = so_protocol;
700 return -NLE_BAD_SOCK;
702 if (fcntl(sk->s_fd, F_SETFL, O_NONBLOCK) < 0) {
705 NL_DBG(4,
"nl_socket_set_nonblocking(%p): fcntl() failed with %d (%s)\n",
706 sk, errno, strerror_r(errno, buf,
sizeof(buf)));
707 return -nl_syserr2nlerr(errno);
719 sk->s_flags |= NL_MSG_PEEK;
728 sk->s_flags &= ~NL_MSG_PEEK;
738 struct nl_cb *nl_socket_get_cb(
const struct nl_sock *sk)
740 return nl_cb_get(sk->s_cb);
743 void nl_socket_set_cb(
struct nl_sock *sk,
struct nl_cb *cb)
749 sk->s_cb = nl_cb_get(cb);
766 return nl_cb_set(sk->s_cb, type, kind, func, arg);
781 return nl_cb_err(sk->s_cb, kind, func, arg);
816 return -NLE_BAD_SOCK;
818 err = setsockopt(sk->s_fd, SOL_SOCKET, SO_SNDBUF,
819 &txbuf,
sizeof(txbuf));
821 NL_DBG(4,
"nl_socket_set_buffer_size(%p): setsockopt() failed with %d (%s)\n",
822 sk, errno, strerror_r(errno, buf,
sizeof(buf)));
823 return -nl_syserr2nlerr(errno);
826 err = setsockopt(sk->s_fd, SOL_SOCKET, SO_RCVBUF,
827 &rxbuf,
sizeof(rxbuf));
829 NL_DBG(4,
"nl_socket_set_buffer_size(%p): setsockopt() failed with %d (%s)\n",
830 sk, errno, strerror_r(errno, buf,
sizeof(buf)));
831 return -nl_syserr2nlerr(errno);
851 sk->s_bufsize = bufsize;
864 return sk->s_bufsize;
879 return -NLE_BAD_SOCK;
881 err = setsockopt(sk->s_fd, SOL_SOCKET, SO_PASSCRED,
882 &state,
sizeof(state));
886 NL_DBG(4,
"nl_socket_set_passcred(%p): setsockopt() failed with %d (%s)\n",
887 sk, errno, strerror_r(errno, buf,
sizeof(buf)));
888 return -nl_syserr2nlerr(errno);
892 sk->s_flags |= NL_SOCK_PASSCRED;
894 sk->s_flags &= ~NL_SOCK_PASSCRED;
911 return -NLE_BAD_SOCK;
913 err = setsockopt(sk->s_fd, SOL_NETLINK, NETLINK_PKTINFO,
914 &state,
sizeof(state));
918 NL_DBG(4,
"nl_socket_recv_pktinfo(%p): setsockopt() failed with %d (%s)\n",
919 sk, errno, strerror_r(errno, buf,
sizeof(buf)));
920 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
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_recvmsg_msg_cb_t)(struct nl_msg *msg, void *arg)
nl_recvmsgs() callback for message processing customization
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.
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.
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)