33 #include "sys/socket.h" 35 #include <netlink-private/netlink.h> 36 #include <netlink-private/socket.h> 37 #include <netlink-private/utils.h> 38 #include <netlink/netlink.h> 39 #include <netlink/utils.h> 40 #include <netlink/handlers.h> 41 #include <netlink/msg.h> 42 #include <netlink/attr.h> 46 static void __init init_default_cb(
void)
50 if ((nlcb = getenv(
"NLCB"))) {
51 if (!strcasecmp(nlcb,
"default"))
53 else if (!strcasecmp(nlcb,
"verbose"))
55 else if (!strcasecmp(nlcb,
"debug"))
58 fprintf(stderr,
"Unknown value for NLCB, valid values: " 59 "{default | verbose | debug}\n");
64 static uint32_t used_ports_map[32];
65 static NL_RW_LOCK(port_map_lock);
67 static uint32_t generate_local_port(
void)
70 static uint16_t idx_state = 0;
71 uint32_t pid = getpid() & 0x3FFFFF;
73 nl_write_lock(&port_map_lock);
76 uint32_t t = time(NULL);
80 idx_state = t ^ (t >> 16) ^ 0x3047;
82 idx_state = idx_state + 20011;
86 for (j = 0; j < 32; j++) {
95 i = (((i-1) + 7) % 31) + 1;
97 if (used_ports_map[i] == 0xFFFFFFFF)
100 for (m = 0; m < 32; m++) {
102 if (1UL & (used_ports_map[i] >> n))
105 used_ports_map[i] |= (1UL << n);
111 nl_write_unlock(&port_map_lock);
114 pid = pid + (((uint32_t)n) << 22);
115 return pid ? pid : 1024;
119 nl_write_unlock(&port_map_lock);
123 static void release_local_port(uint32_t port)
131 mask = 1UL << (nr % 32);
134 nl_write_lock(&port_map_lock);
135 BUG_ON((used_ports_map[nr] & mask) != mask);
136 used_ports_map[nr] &= ~mask;
137 nl_write_unlock(&port_map_lock);
141 void _nl_socket_used_ports_release_all(
const uint32_t *used_ports)
145 for (i = 0; i < 32; i++) {
146 if (used_ports[i] != 0) {
147 nl_write_lock(&port_map_lock);
148 for (; i < 32; i++) {
149 BUG_ON((used_ports_map[i] & used_ports[i]) != used_ports[i]);
150 used_ports_map[i] &= ~(used_ports[i]);
152 nl_write_unlock(&port_map_lock);
158 void _nl_socket_used_ports_set(uint32_t *used_ports, uint32_t port)
164 mask = 1UL << (nr % 32);
172 used_ports[nr] |= mask;
181 static struct nl_sock *__alloc_socket(
struct nl_cb *cb)
185 sk = calloc(1,
sizeof(*sk));
190 sk->s_cb = nl_cb_get(cb);
191 sk->s_local.nl_family = AF_NETLINK;
192 sk->s_peer.nl_family = AF_NETLINK;
193 sk->s_seq_expect = sk->s_seq_next = time(NULL);
196 sk->s_flags = NL_OWN_PORT;
216 sk = __alloc_socket(cb);
237 return __alloc_socket(cb);
252 if (!(sk->s_flags & NL_OWN_PORT))
253 release_local_port(sk->s_local.nl_pid);
266 static int noop_seq_check(
struct nl_msg *msg,
void *arg)
300 return sk->s_seq_next++;
317 sk->s_flags |= NL_NO_AUTO_ACK;
327 sk->s_flags &= ~NL_NO_AUTO_ACK;
333 int _nl_socket_is_local_port_unspecified(
struct nl_sock *sk)
335 return (sk->s_local.nl_pid == 0);
338 uint32_t _nl_socket_set_local_port_no_release(
struct nl_sock *sk,
int generate_other)
346 port = generate_local_port();
349 sk->s_local.nl_pid = port;
353 sk->s_flags |= NL_OWN_PORT;
355 sk->s_flags &= ~NL_OWN_PORT;
365 uint32_t nl_socket_get_local_port(
const struct nl_sock *sk)
367 if (sk->s_local.nl_pid == 0) {
368 struct nl_sock *sk_mutable = (
struct nl_sock *) sk;
379 sk_mutable->s_local.nl_pid = generate_local_port();
380 if (sk_mutable->s_local.nl_pid == 0) {
384 sk_mutable->s_local.nl_pid = UINT32_MAX;
385 sk_mutable->s_flags |= NL_OWN_PORT;
387 sk_mutable->s_flags &= ~NL_OWN_PORT;
389 return sk->s_local.nl_pid;
406 if (!(sk->s_flags & NL_OWN_PORT))
407 release_local_port(sk->s_local.nl_pid);
408 sk->s_flags |= NL_OWN_PORT;
409 sk->s_local.nl_pid = port;
441 return -NLE_BAD_SOCK;
451 err = setsockopt(sk->s_fd, SOL_NETLINK, NETLINK_ADD_MEMBERSHIP,
452 &group,
sizeof(group));
455 NL_DBG(4,
"nl_socket_add_memberships(%p): setsockopt() failed with %d (%s)\n",
456 sk, errno, nl_strerror_l(errno));
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));
505 NL_DBG(4,
"nl_socket_drop_memberships(%p): setsockopt() failed with %d (%s)\n",
506 sk, errno, nl_strerror_l(errno));
507 return -nl_syserr2nlerr(errno);
510 group = va_arg(ap,
int);
518 int nl_socket_drop_membership(
struct nl_sock *sk,
int group)
535 sk->s_local.nl_groups |= groups;
546 uint32_t nl_socket_get_peer_port(
const struct nl_sock *sk)
548 return sk->s_peer.nl_pid;
551 void nl_socket_set_peer_port(
struct nl_sock *sk, uint32_t port)
553 sk->s_peer.nl_pid = port;
556 uint32_t nl_socket_get_peer_groups(
const struct nl_sock *sk)
558 return sk->s_peer.nl_groups;
561 void nl_socket_set_peer_groups(
struct nl_sock *sk, uint32_t groups)
563 sk->s_peer.nl_groups = groups;
620 struct sockaddr_nl local = { 0 };
621 int so_type = -1, so_protocol = -1;
624 return -NLE_BAD_SOCK;
628 addrlen =
sizeof(local);
629 err = getsockname(fd, (
struct sockaddr *) &local,
632 NL_DBG(4,
"nl_socket_set_fd(%p,%d): getsockname() failed with %d (%s)\n",
633 sk, fd, errno, nl_strerror_l(errno));
634 return -nl_syserr2nlerr(errno);
636 if (addrlen !=
sizeof(local))
638 if (local.nl_family != AF_NETLINK) {
639 NL_DBG(4,
"nl_socket_set_fd(%p,%d): getsockname() returned family %d instead of %d (AF_NETLINK)\n",
640 sk, fd, local.nl_family, AF_NETLINK);
644 addrlen =
sizeof(so_type);
645 err = getsockopt(fd, SOL_SOCKET, SO_TYPE, &so_type, &addrlen);
647 NL_DBG(4,
"nl_socket_set_fd(%p,%d): getsockopt() for SO_TYPE failed with %d (%s)\n",
648 sk, fd, errno, nl_strerror_l(errno));
649 return -nl_syserr2nlerr(errno);
651 if (addrlen !=
sizeof(so_type))
653 if (so_type != SOCK_RAW) {
654 NL_DBG(4,
"nl_socket_set_fd(%p,%d): getsockopt() returned SO_TYPE %d instead of %d (SOCK_RAW)\n",
655 sk, fd, so_type, SOCK_RAW);
660 addrlen =
sizeof(so_protocol);
661 err = getsockopt(fd, SOL_SOCKET, SO_PROTOCOL, &so_protocol, &addrlen);
663 if (errno == ENOPROTOOPT)
665 NL_DBG(4,
"nl_socket_set_fd(%p,%d): getsockopt() for SO_PROTOCOL failed with %d (%s)\n",
666 sk, fd, errno, nl_strerror_l(errno));
667 return -nl_syserr2nlerr(errno);
669 if (addrlen !=
sizeof(so_protocol))
671 if (protocol >= 0 && protocol != so_protocol) {
672 NL_DBG(4,
"nl_socket_set_fd(%p,%d): getsockopt() for SO_PROTOCOL returned %d instead of %d\n",
673 sk, fd, so_protocol, protocol);
682 NL_DBG(4,
"nl_socket_set_fd(%p,%d): unknown protocol and unable to detect it via SO_PROTOCOL socket option\n",
686 so_protocol = protocol;
692 sk->s_proto = so_protocol;
706 return -NLE_BAD_SOCK;
708 if (fcntl(sk->s_fd, F_SETFL, O_NONBLOCK) < 0) {
709 NL_DBG(4,
"nl_socket_set_nonblocking(%p): fcntl() failed with %d (%s)\n",
710 sk, errno, nl_strerror_l(errno));
711 return -nl_syserr2nlerr(errno);
725 sk->s_flags |= (NL_MSG_PEEK | NL_MSG_PEEK_EXPLICIT);
736 sk->s_flags |= NL_MSG_PEEK_EXPLICIT;
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);
824 return -NLE_BAD_SOCK;
826 err = setsockopt(sk->s_fd, SOL_SOCKET, SO_SNDBUF,
827 &txbuf,
sizeof(txbuf));
829 NL_DBG(4,
"nl_socket_set_buffer_size(%p): setsockopt() failed with %d (%s)\n",
830 sk, errno, nl_strerror_l(errno));
831 return -nl_syserr2nlerr(errno);
834 err = setsockopt(sk->s_fd, SOL_SOCKET, SO_RCVBUF,
835 &rxbuf,
sizeof(rxbuf));
837 NL_DBG(4,
"nl_socket_set_buffer_size(%p): setsockopt() failed with %d (%s)\n",
838 sk, errno, nl_strerror_l(errno));
839 return -nl_syserr2nlerr(errno);
872 sk->s_bufsize = bufsize;
885 return sk->s_bufsize;
900 return -NLE_BAD_SOCK;
902 err = setsockopt(sk->s_fd, SOL_SOCKET, SO_PASSCRED,
903 &state,
sizeof(state));
905 NL_DBG(4,
"nl_socket_set_passcred(%p): setsockopt() failed with %d (%s)\n",
906 sk, errno, nl_strerror_l(errno));
907 return -nl_syserr2nlerr(errno);
911 sk->s_flags |= NL_SOCK_PASSCRED;
913 sk->s_flags &= ~NL_SOCK_PASSCRED;
930 return -NLE_BAD_SOCK;
932 err = setsockopt(sk->s_fd, SOL_NETLINK, NETLINK_PKTINFO,
933 &state,
sizeof(state));
935 NL_DBG(4,
"nl_socket_recv_pktinfo(%p): setsockopt() failed with %d (%s)\n",
936 sk, errno, nl_strerror_l(errno));
937 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)