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>
39 static 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");
57 static uint32_t used_ports_map[32];
58 static NL_RW_LOCK(port_map_lock);
60 static uint32_t generate_local_port(
void)
63 static uint16_t idx_state = 0;
64 uint32_t pid = getpid() & 0x3FFFFF;
66 nl_write_lock(&port_map_lock);
69 uint32_t t = time(NULL);
73 idx_state = t ^ (t >> 16) ^ 0x3047;
75 idx_state = idx_state + 20011;
79 for (j = 0; j < 32; j++) {
88 i = (((i-1) + 7) % 31) + 1;
90 if (used_ports_map[i] == 0xFFFFFFFF)
93 for (m = 0; m < 32; m++) {
95 if (1UL & (used_ports_map[i] >> n))
98 used_ports_map[i] |= (1UL << n);
104 nl_write_unlock(&port_map_lock);
107 pid = pid + (((uint32_t)n) << 22);
108 return pid ? pid : 1024;
112 nl_write_unlock(&port_map_lock);
116 static void release_local_port(uint32_t port)
124 mask = 1UL << (nr % 32);
127 nl_write_lock(&port_map_lock);
128 BUG_ON((used_ports_map[nr] & mask) != mask);
129 used_ports_map[nr] &= ~mask;
130 nl_write_unlock(&port_map_lock);
134 void _nl_socket_used_ports_release_all(
const uint32_t *used_ports)
138 for (i = 0; i < 32; i++) {
139 if (used_ports[i] != 0) {
140 nl_write_lock(&port_map_lock);
141 for (; i < 32; i++) {
142 BUG_ON((used_ports_map[i] & used_ports[i]) != used_ports[i]);
143 used_ports_map[i] &= ~(used_ports[i]);
145 nl_write_unlock(&port_map_lock);
151 void _nl_socket_used_ports_set(uint32_t *used_ports, uint32_t port)
157 mask = 1UL << (nr % 32);
165 used_ports[nr] |= mask;
174 static struct nl_sock *__alloc_socket(
struct nl_cb *cb)
178 sk = calloc(1,
sizeof(*sk));
183 sk->s_cb = nl_cb_get(cb);
184 sk->s_local.nl_family = AF_NETLINK;
185 sk->s_peer.nl_family = AF_NETLINK;
186 sk->s_seq_expect = sk->s_seq_next = time(NULL);
189 sk->s_flags = NL_OWN_PORT;
209 sk = __alloc_socket(cb);
230 return __alloc_socket(cb);
245 if (!(sk->s_flags & NL_OWN_PORT))
246 release_local_port(sk->s_local.nl_pid);
259 static int noop_seq_check(
struct nl_msg *msg,
void *arg)
293 return sk->s_seq_next++;
310 sk->s_flags |= NL_NO_AUTO_ACK;
320 sk->s_flags &= ~NL_NO_AUTO_ACK;
326 int _nl_socket_is_local_port_unspecified(
struct nl_sock *sk)
328 return (sk->s_local.nl_pid == 0);
331 uint32_t _nl_socket_set_local_port_no_release(
struct nl_sock *sk,
int generate_other)
339 port = generate_local_port();
342 sk->s_local.nl_pid = port;
346 sk->s_flags |= NL_OWN_PORT;
348 sk->s_flags &= ~NL_OWN_PORT;
358 uint32_t nl_socket_get_local_port(
const struct nl_sock *sk)
360 if (sk->s_local.nl_pid == 0) {
361 struct nl_sock *sk_mutable = (
struct nl_sock *) sk;
372 sk_mutable->s_local.nl_pid = generate_local_port();
373 if (sk_mutable->s_local.nl_pid == 0) {
377 sk_mutable->s_local.nl_pid = UINT32_MAX;
378 sk_mutable->s_flags |= NL_OWN_PORT;
380 sk_mutable->s_flags &= ~NL_OWN_PORT;
382 return sk->s_local.nl_pid;
399 if (!(sk->s_flags & NL_OWN_PORT))
400 release_local_port(sk->s_local.nl_pid);
401 sk->s_flags |= NL_OWN_PORT;
402 sk->s_local.nl_pid = port;
434 return -NLE_BAD_SOCK;
444 err = setsockopt(sk->s_fd, SOL_NETLINK, NETLINK_ADD_MEMBERSHIP,
445 &group,
sizeof(group));
448 NL_DBG(4,
"nl_socket_add_memberships(%p): setsockopt() failed with %d (%s)\n",
449 sk, errno, nl_strerror_l(errno));
450 return -nl_syserr2nlerr(errno);
453 group = va_arg(ap,
int);
461 int nl_socket_add_membership(
struct nl_sock *sk,
int group)
484 return -NLE_BAD_SOCK;
494 err = setsockopt(sk->s_fd, SOL_NETLINK, NETLINK_DROP_MEMBERSHIP,
495 &group,
sizeof(group));
498 NL_DBG(4,
"nl_socket_drop_memberships(%p): setsockopt() failed with %d (%s)\n",
499 sk, errno, nl_strerror_l(errno));
500 return -nl_syserr2nlerr(errno);
503 group = va_arg(ap,
int);
511 int nl_socket_drop_membership(
struct nl_sock *sk,
int group)
528 sk->s_local.nl_groups |= groups;
539 uint32_t nl_socket_get_peer_port(
const struct nl_sock *sk)
541 return sk->s_peer.nl_pid;
544 void nl_socket_set_peer_port(
struct nl_sock *sk, uint32_t port)
546 sk->s_peer.nl_pid = port;
549 uint32_t nl_socket_get_peer_groups(
const struct nl_sock *sk)
551 return sk->s_peer.nl_groups;
554 void nl_socket_set_peer_groups(
struct nl_sock *sk, uint32_t groups)
556 sk->s_peer.nl_groups = groups;
613 struct sockaddr_nl local = { 0 };
614 int so_type = -1, so_protocol = -1;
617 return -NLE_BAD_SOCK;
621 addrlen =
sizeof(local);
622 err = getsockname(fd, (
struct sockaddr *) &local,
625 NL_DBG(4,
"nl_socket_set_fd(%p,%d): getsockname() failed with %d (%s)\n",
626 sk, fd, errno, nl_strerror_l(errno));
627 return -nl_syserr2nlerr(errno);
629 if (addrlen !=
sizeof(local))
631 if (local.nl_family != AF_NETLINK) {
632 NL_DBG(4,
"nl_socket_set_fd(%p,%d): getsockname() returned family %d instead of %d (AF_NETLINK)\n",
633 sk, fd, local.nl_family, AF_NETLINK);
637 addrlen =
sizeof(so_type);
638 err = getsockopt(fd, SOL_SOCKET, SO_TYPE, &so_type, &addrlen);
640 NL_DBG(4,
"nl_socket_set_fd(%p,%d): getsockopt() for SO_TYPE failed with %d (%s)\n",
641 sk, fd, errno, nl_strerror_l(errno));
642 return -nl_syserr2nlerr(errno);
644 if (addrlen !=
sizeof(so_type))
646 if (so_type != SOCK_RAW) {
647 NL_DBG(4,
"nl_socket_set_fd(%p,%d): getsockopt() returned SO_TYPE %d instead of %d (SOCK_RAW)\n",
648 sk, fd, so_type, SOCK_RAW);
653 addrlen =
sizeof(so_protocol);
654 err = getsockopt(fd, SOL_SOCKET, SO_PROTOCOL, &so_protocol, &addrlen);
656 if (errno == ENOPROTOOPT)
658 NL_DBG(4,
"nl_socket_set_fd(%p,%d): getsockopt() for SO_PROTOCOL failed with %d (%s)\n",
659 sk, fd, errno, nl_strerror_l(errno));
660 return -nl_syserr2nlerr(errno);
662 if (addrlen !=
sizeof(so_protocol))
664 if (protocol >= 0 && protocol != so_protocol) {
665 NL_DBG(4,
"nl_socket_set_fd(%p,%d): getsockopt() for SO_PROTOCOL returned %d instead of %d\n",
666 sk, fd, so_protocol, protocol);
675 NL_DBG(4,
"nl_socket_set_fd(%p,%d): unknown protocol and unable to detect it via SO_PROTOCOL socket option\n",
679 so_protocol = protocol;
685 sk->s_proto = so_protocol;
699 return -NLE_BAD_SOCK;
701 if (fcntl(sk->s_fd, F_SETFL, O_NONBLOCK) < 0) {
702 NL_DBG(4,
"nl_socket_set_nonblocking(%p): fcntl() failed with %d (%s)\n",
703 sk, errno, nl_strerror_l(errno));
704 return -nl_syserr2nlerr(errno);
718 sk->s_flags |= (NL_MSG_PEEK | NL_MSG_PEEK_EXPLICIT);
729 sk->s_flags |= NL_MSG_PEEK_EXPLICIT;
730 sk->s_flags &= ~NL_MSG_PEEK;
740 struct nl_cb *nl_socket_get_cb(
const struct nl_sock *sk)
742 return nl_cb_get(sk->s_cb);
745 void nl_socket_set_cb(
struct nl_sock *sk,
struct nl_cb *cb)
751 sk->s_cb = nl_cb_get(cb);
768 return nl_cb_set(sk->s_cb, type, kind, func, arg);
783 return nl_cb_err(sk->s_cb, kind, func, arg);
817 return -NLE_BAD_SOCK;
819 err = setsockopt(sk->s_fd, SOL_SOCKET, SO_SNDBUF,
820 &txbuf,
sizeof(txbuf));
822 NL_DBG(4,
"nl_socket_set_buffer_size(%p): setsockopt() failed with %d (%s)\n",
823 sk, errno, nl_strerror_l(errno));
824 return -nl_syserr2nlerr(errno);
827 err = setsockopt(sk->s_fd, SOL_SOCKET, SO_RCVBUF,
828 &rxbuf,
sizeof(rxbuf));
830 NL_DBG(4,
"nl_socket_set_buffer_size(%p): setsockopt() failed with %d (%s)\n",
831 sk, errno, nl_strerror_l(errno));
832 return -nl_syserr2nlerr(errno);
865 sk->s_bufsize = bufsize;
878 return sk->s_bufsize;
893 return -NLE_BAD_SOCK;
895 err = setsockopt(sk->s_fd, SOL_SOCKET, SO_PASSCRED,
896 &state,
sizeof(state));
898 NL_DBG(4,
"nl_socket_set_passcred(%p): setsockopt() failed with %d (%s)\n",
899 sk, errno, nl_strerror_l(errno));
900 return -nl_syserr2nlerr(errno);
904 sk->s_flags |= NL_SOCK_PASSCRED;
906 sk->s_flags &= ~NL_SOCK_PASSCRED;
923 return -NLE_BAD_SOCK;
925 err = setsockopt(sk->s_fd, SOL_NETLINK, NETLINK_PKTINFO,
926 &state,
sizeof(state));
928 NL_DBG(4,
"nl_socket_recv_pktinfo(%p): setsockopt() failed with %d (%s)\n",
929 sk, errno, nl_strerror_l(errno));
930 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.
struct nl_sock * nl_socket_alloc_cb(struct nl_cb *cb)
Allocate new socket with custom callbacks.
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.
int nl_socket_set_msg_buf_size(struct nl_sock *sk, size_t bufsize)
Set default message buffer size of netlink socket.
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.
struct nl_sock * nl_socket_alloc(void)
Allocate new netlink socket.