32 #include "sys/socket.h" 34 #include <netlink-private/netlink.h> 35 #include <netlink-private/socket.h> 36 #include <netlink-private/utils.h> 37 #include <netlink/netlink.h> 38 #include <netlink/utils.h> 39 #include <netlink/handlers.h> 40 #include <netlink/msg.h> 41 #include <netlink/attr.h> 45 static void __init init_default_cb(
void)
49 if ((nlcb = getenv(
"NLCB"))) {
50 if (!strcasecmp(nlcb,
"default"))
52 else if (!strcasecmp(nlcb,
"verbose"))
54 else if (!strcasecmp(nlcb,
"debug"))
57 fprintf(stderr,
"Unknown value for NLCB, valid values: " 58 "{default | verbose | debug}\n");
63 static uint32_t used_ports_map[32];
64 static NL_RW_LOCK(port_map_lock);
66 static uint32_t generate_local_port(
void)
69 static uint16_t idx_state = 0;
70 uint32_t pid = getpid() & 0x3FFFFF;
72 nl_write_lock(&port_map_lock);
75 uint32_t t = time(NULL);
79 idx_state = t ^ (t >> 16) ^ 0x3047;
81 idx_state = idx_state + 20011;
85 for (j = 0; j < 32; j++) {
94 i = (((i-1) + 7) % 31) + 1;
96 if (used_ports_map[i] == 0xFFFFFFFF)
99 for (m = 0; m < 32; m++) {
101 if (1UL & (used_ports_map[i] >> n))
104 used_ports_map[i] |= (1UL << n);
110 nl_write_unlock(&port_map_lock);
113 pid = pid + (((uint32_t)n) << 22);
114 return pid ? pid : 1024;
118 nl_write_unlock(&port_map_lock);
122 static void release_local_port(uint32_t port)
130 mask = 1UL << (nr % 32);
133 nl_write_lock(&port_map_lock);
134 BUG_ON((used_ports_map[nr] & mask) != mask);
135 used_ports_map[nr] &= ~mask;
136 nl_write_unlock(&port_map_lock);
140 void _nl_socket_used_ports_release_all(
const uint32_t *used_ports)
144 for (i = 0; i < 32; i++) {
145 if (used_ports[i] != 0) {
146 nl_write_lock(&port_map_lock);
147 for (; i < 32; i++) {
148 BUG_ON((used_ports_map[i] & used_ports[i]) != used_ports[i]);
149 used_ports_map[i] &= ~(used_ports[i]);
151 nl_write_unlock(&port_map_lock);
157 void _nl_socket_used_ports_set(uint32_t *used_ports, uint32_t port)
163 mask = 1UL << (nr % 32);
171 used_ports[nr] |= mask;
180 static struct nl_sock *__alloc_socket(
struct nl_cb *cb)
184 sk = calloc(1,
sizeof(*sk));
189 sk->s_cb = nl_cb_get(cb);
190 sk->s_local.nl_family = AF_NETLINK;
191 sk->s_peer.nl_family = AF_NETLINK;
192 sk->s_seq_expect = sk->s_seq_next = time(NULL);
195 sk->s_flags = NL_OWN_PORT;
215 sk = __alloc_socket(cb);
236 return __alloc_socket(cb);
251 if (!(sk->s_flags & NL_OWN_PORT))
252 release_local_port(sk->s_local.nl_pid);
265 static int noop_seq_check(
struct nl_msg *msg,
void *arg)
299 return sk->s_seq_next++;
316 sk->s_flags |= NL_NO_AUTO_ACK;
326 sk->s_flags &= ~NL_NO_AUTO_ACK;
332 int _nl_socket_is_local_port_unspecified(
struct nl_sock *sk)
334 return (sk->s_local.nl_pid == 0);
337 uint32_t _nl_socket_set_local_port_no_release(
struct nl_sock *sk,
int generate_other)
345 port = generate_local_port();
348 sk->s_local.nl_pid = port;
352 sk->s_flags |= NL_OWN_PORT;
354 sk->s_flags &= ~NL_OWN_PORT;
364 uint32_t nl_socket_get_local_port(
const struct nl_sock *sk)
366 if (sk->s_local.nl_pid == 0) {
367 struct nl_sock *sk_mutable = (
struct nl_sock *) sk;
378 sk_mutable->s_local.nl_pid = generate_local_port();
379 if (sk_mutable->s_local.nl_pid == 0) {
383 sk_mutable->s_local.nl_pid = UINT32_MAX;
384 sk_mutable->s_flags |= NL_OWN_PORT;
386 sk_mutable->s_flags &= ~NL_OWN_PORT;
388 return sk->s_local.nl_pid;
405 if (!(sk->s_flags & NL_OWN_PORT))
406 release_local_port(sk->s_local.nl_pid);
407 sk->s_flags |= NL_OWN_PORT;
408 sk->s_local.nl_pid = port;
440 return -NLE_BAD_SOCK;
450 err = setsockopt(sk->s_fd, SOL_NETLINK, NETLINK_ADD_MEMBERSHIP,
451 &group,
sizeof(group));
454 NL_DBG(4,
"nl_socket_add_memberships(%p): setsockopt() failed with %d (%s)\n",
455 sk, errno, nl_strerror_l(errno));
456 return -nl_syserr2nlerr(errno);
459 group = va_arg(ap,
int);
467 int nl_socket_add_membership(
struct nl_sock *sk,
int group)
490 return -NLE_BAD_SOCK;
500 err = setsockopt(sk->s_fd, SOL_NETLINK, NETLINK_DROP_MEMBERSHIP,
501 &group,
sizeof(group));
504 NL_DBG(4,
"nl_socket_drop_memberships(%p): setsockopt() failed with %d (%s)\n",
505 sk, errno, nl_strerror_l(errno));
506 return -nl_syserr2nlerr(errno);
509 group = va_arg(ap,
int);
517 int nl_socket_drop_membership(
struct nl_sock *sk,
int group)
534 sk->s_local.nl_groups |= groups;
545 uint32_t nl_socket_get_peer_port(
const struct nl_sock *sk)
547 return sk->s_peer.nl_pid;
550 void nl_socket_set_peer_port(
struct nl_sock *sk, uint32_t port)
552 sk->s_peer.nl_pid = port;
555 uint32_t nl_socket_get_peer_groups(
const struct nl_sock *sk)
557 return sk->s_peer.nl_groups;
560 void nl_socket_set_peer_groups(
struct nl_sock *sk, uint32_t groups)
562 sk->s_peer.nl_groups = groups;
619 struct sockaddr_nl local = { 0 };
620 int so_type = -1, so_protocol = -1;
623 return -NLE_BAD_SOCK;
627 addrlen =
sizeof(local);
628 err = getsockname(fd, (
struct sockaddr *) &local,
631 NL_DBG(4,
"nl_socket_set_fd(%p,%d): getsockname() failed with %d (%s)\n",
632 sk, fd, errno, nl_strerror_l(errno));
633 return -nl_syserr2nlerr(errno);
635 if (addrlen !=
sizeof(local))
637 if (local.nl_family != AF_NETLINK) {
638 NL_DBG(4,
"nl_socket_set_fd(%p,%d): getsockname() returned family %d instead of %d (AF_NETLINK)\n",
639 sk, fd, local.nl_family, AF_NETLINK);
643 addrlen =
sizeof(so_type);
644 err = getsockopt(fd, SOL_SOCKET, SO_TYPE, &so_type, &addrlen);
646 NL_DBG(4,
"nl_socket_set_fd(%p,%d): getsockopt() for SO_TYPE failed with %d (%s)\n",
647 sk, fd, errno, nl_strerror_l(errno));
648 return -nl_syserr2nlerr(errno);
650 if (addrlen !=
sizeof(so_type))
652 if (so_type != SOCK_RAW) {
653 NL_DBG(4,
"nl_socket_set_fd(%p,%d): getsockopt() returned SO_TYPE %d instead of %d (SOCK_RAW)\n",
654 sk, fd, so_type, SOCK_RAW);
659 addrlen =
sizeof(so_protocol);
660 err = getsockopt(fd, SOL_SOCKET, SO_PROTOCOL, &so_protocol, &addrlen);
662 if (errno == ENOPROTOOPT)
664 NL_DBG(4,
"nl_socket_set_fd(%p,%d): getsockopt() for SO_PROTOCOL failed with %d (%s)\n",
665 sk, fd, errno, nl_strerror_l(errno));
666 return -nl_syserr2nlerr(errno);
668 if (addrlen !=
sizeof(so_protocol))
670 if (protocol >= 0 && protocol != so_protocol) {
671 NL_DBG(4,
"nl_socket_set_fd(%p,%d): getsockopt() for SO_PROTOCOL returned %d instead of %d\n",
672 sk, fd, so_protocol, protocol);
681 NL_DBG(4,
"nl_socket_set_fd(%p,%d): unknown protocol and unable to detect it via SO_PROTOCOL socket option\n",
685 so_protocol = protocol;
691 sk->s_proto = so_protocol;
705 return -NLE_BAD_SOCK;
707 if (fcntl(sk->s_fd, F_SETFL, O_NONBLOCK) < 0) {
708 NL_DBG(4,
"nl_socket_set_nonblocking(%p): fcntl() failed with %d (%s)\n",
709 sk, errno, nl_strerror_l(errno));
710 return -nl_syserr2nlerr(errno);
724 sk->s_flags |= (NL_MSG_PEEK | NL_MSG_PEEK_EXPLICIT);
735 sk->s_flags |= NL_MSG_PEEK_EXPLICIT;
736 sk->s_flags &= ~NL_MSG_PEEK;
746 struct nl_cb *nl_socket_get_cb(
const struct nl_sock *sk)
748 return nl_cb_get(sk->s_cb);
751 void nl_socket_set_cb(
struct nl_sock *sk,
struct nl_cb *cb)
757 sk->s_cb = nl_cb_get(cb);
774 return nl_cb_set(sk->s_cb, type, kind, func, arg);
789 return nl_cb_err(sk->s_cb, kind, func, arg);
823 return -NLE_BAD_SOCK;
825 err = setsockopt(sk->s_fd, SOL_SOCKET, SO_SNDBUF,
826 &txbuf,
sizeof(txbuf));
828 NL_DBG(4,
"nl_socket_set_buffer_size(%p): setsockopt() failed with %d (%s)\n",
829 sk, errno, nl_strerror_l(errno));
830 return -nl_syserr2nlerr(errno);
833 err = setsockopt(sk->s_fd, SOL_SOCKET, SO_RCVBUF,
834 &rxbuf,
sizeof(rxbuf));
836 NL_DBG(4,
"nl_socket_set_buffer_size(%p): setsockopt() failed with %d (%s)\n",
837 sk, errno, nl_strerror_l(errno));
838 return -nl_syserr2nlerr(errno);
871 sk->s_bufsize = bufsize;
884 return sk->s_bufsize;
899 return -NLE_BAD_SOCK;
901 err = setsockopt(sk->s_fd, SOL_SOCKET, SO_PASSCRED,
902 &state,
sizeof(state));
904 NL_DBG(4,
"nl_socket_set_passcred(%p): setsockopt() failed with %d (%s)\n",
905 sk, errno, nl_strerror_l(errno));
906 return -nl_syserr2nlerr(errno);
910 sk->s_flags |= NL_SOCK_PASSCRED;
912 sk->s_flags &= ~NL_SOCK_PASSCRED;
929 return -NLE_BAD_SOCK;
931 err = setsockopt(sk->s_fd, SOL_NETLINK, NETLINK_PKTINFO,
932 &state,
sizeof(state));
934 NL_DBG(4,
"nl_socket_recv_pktinfo(%p): setsockopt() failed with %d (%s)\n",
935 sk, errno, nl_strerror_l(errno));
936 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)