Blame man7/sock_diag.7

Packit 7cfc04
.\" Copyright (c) 2016 Pavel Emelyanov <xemul@virtuozzo.com>
Packit 7cfc04
.\" Copyright (c) 2016 Dmitry V. Levin <ldv@altlinux.org>
Packit 7cfc04
.\"
Packit 7cfc04
.\" %%%LICENSE_START(GPLv2+_DOC_FULL)
Packit 7cfc04
.\" This is free documentation; you can redistribute it and/or
Packit 7cfc04
.\" modify it under the terms of the GNU General Public License as
Packit 7cfc04
.\" published by the Free Software Foundation; either version 2 of
Packit 7cfc04
.\" the License, or (at your option) any later version.
Packit 7cfc04
.\"
Packit 7cfc04
.\" The GNU General Public License's references to "object code"
Packit 7cfc04
.\" and "executables" are to be interpreted as the output of any
Packit 7cfc04
.\" document formatting or typesetting system, including
Packit 7cfc04
.\" intermediate and printed output.
Packit 7cfc04
.\"
Packit 7cfc04
.\" This manual is distributed in the hope that it will be useful,
Packit 7cfc04
.\" but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 7cfc04
.\" MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
Packit 7cfc04
.\" GNU General Public License for more details.
Packit 7cfc04
.\"
Packit 7cfc04
.\" You should have received a copy of the GNU General Public
Packit 7cfc04
.\" License along with this manual; if not, see
Packit 7cfc04
.\" <http://www.gnu.org/licenses/>.
Packit 7cfc04
.\" %%%LICENSE_END
Packit 7cfc04
.TH SOCK_DIAG 7 2017-09-15 "Linux" "Linux Programmer's Manual"
Packit 7cfc04
.SH NAME
Packit 7cfc04
sock_diag \- obtaining information about sockets
Packit 7cfc04
.SH SYNOPSIS
Packit 7cfc04
.nf
Packit 7cfc04
.B #include <sys/socket.h>
Packit 7cfc04
.B #include <linux/sock_diag.h>
Packit 7cfc04
.BR "#include <linux/unix_diag.h>" " /* for UNIX domain sockets */"
Packit 7cfc04
.BR "#include <linux/inet_diag.h>" " /* for IPv4 and IPv6 sockets */"
Packit 7cfc04
.PP
Packit 7cfc04
.BI "diag_socket = socket(AF_NETLINK, " socket_type ", NETLINK_SOCK_DIAG);"
Packit 7cfc04
.fi
Packit 7cfc04
.SH DESCRIPTION
Packit 7cfc04
The sock_diag netlink subsystem provides a mechanism for obtaining
Packit 7cfc04
information about sockets of various address families from the kernel.
Packit 7cfc04
This subsystem can be used to obtain information about individual
Packit 7cfc04
sockets or request a list of sockets.
Packit 7cfc04
.PP
Packit 7cfc04
In the request, the caller can specify additional information it would
Packit 7cfc04
like to obtain about the socket, for example, memory information or
Packit 7cfc04
information specific to the address family.
Packit 7cfc04
.PP
Packit 7cfc04
When requesting a list of sockets, the caller can specify filters that
Packit 7cfc04
would be applied by the kernel to select a subset of sockets to report.
Packit 7cfc04
For now, there is only the ability to filter sockets by state (connected,
Packit 7cfc04
listening, and so on.)
Packit 7cfc04
.PP
Packit 7cfc04
Note that sock_diag reports only those sockets that have a name;
Packit 7cfc04
that is, either sockets bound explicitly with
Packit 7cfc04
.BR bind (2)
Packit 7cfc04
or sockets that were automatically bound to an address (e.g., by
Packit 7cfc04
.BR connect (2)).
Packit 7cfc04
This is the same set of sockets that is available via
Packit 7cfc04
.IR /proc/net/unix ,
Packit 7cfc04
.IR /proc/net/tcp ,
Packit 7cfc04
.IR /proc/net/udp ,
Packit 7cfc04
and so on.
Packit 7cfc04
.\"
Packit 7cfc04
.SS Request
Packit 7cfc04
The request starts with a
Packit 7cfc04
.I "struct nlmsghdr"
Packit 7cfc04
header described in
Packit 7cfc04
.BR netlink (7)
Packit 7cfc04
with
Packit 7cfc04
.I nlmsg_type
Packit 7cfc04
field set to
Packit 7cfc04
.BR SOCK_DIAG_BY_FAMILY .
Packit 7cfc04
It is followed by a header specific to the address family that starts with
Packit 7cfc04
a common part shared by all address families:
Packit 7cfc04
.PP
Packit 7cfc04
.in +4n
Packit 7cfc04
.EX
Packit 7cfc04
struct sock_diag_req {
Packit 7cfc04
    __u8 sdiag_family;
Packit 7cfc04
    __u8 sdiag_protocol;
Packit 7cfc04
};
Packit 7cfc04
.EE
Packit 7cfc04
.in
Packit 7cfc04
.PP
Packit 7cfc04
The fields of this structure are as follows:
Packit 7cfc04
.TP
Packit 7cfc04
.I sdiag_family
Packit 7cfc04
An address family.
Packit 7cfc04
It should be set to the appropriate
Packit 7cfc04
.B AF_*
Packit 7cfc04
constant.
Packit 7cfc04
.TP
Packit 7cfc04
.I sdiag_protocol
Packit 7cfc04
Depends on
Packit 7cfc04
.IR sdiag_family .
Packit 7cfc04
It should be set to the appropriate
Packit 7cfc04
.B IPPROTO_*
Packit 7cfc04
constant for
Packit 7cfc04
.B AF_INET
Packit 7cfc04
and
Packit 7cfc04
.BR AF_INET6,
Packit 7cfc04
and to 0 otherwise.
Packit 7cfc04
.PP
Packit 7cfc04
If the
Packit 7cfc04
.I nlmsg_flags
Packit 7cfc04
field of the
Packit 7cfc04
.I "struct nlmsghdr"
Packit 7cfc04
header has the
Packit 7cfc04
.BR NLM_F_DUMP
Packit 7cfc04
flag set, it means that a list of sockets is being requested;
Packit 7cfc04
otherwise it is a query about an individual socket.
Packit 7cfc04
.\"
Packit 7cfc04
.SS Response
Packit 7cfc04
The response starts with a
Packit 7cfc04
.I "struct nlmsghdr"
Packit 7cfc04
header and is followed by an array of objects specific to the address family.
Packit 7cfc04
The array is to be accessed with the standard
Packit 7cfc04
.B NLMSG_*
Packit 7cfc04
macros from the
Packit 7cfc04
.BR netlink (3)
Packit 7cfc04
API.
Packit 7cfc04
.PP
Packit 7cfc04
Each object is the NLA (netlink attributes) list that is to be accessed
Packit 7cfc04
with the
Packit 7cfc04
.B RTA_*
Packit 7cfc04
macros from
Packit 7cfc04
.BR rtnetlink (3)
Packit 7cfc04
API.
Packit 7cfc04
.\"
Packit 7cfc04
.SS UNIX domain sockets
Packit 7cfc04
For UNIX domain sockets the request is represented in the following structure:
Packit 7cfc04
.PP
Packit 7cfc04
.in +4n
Packit 7cfc04
.EX
Packit 7cfc04
struct unix_diag_req {
Packit 7cfc04
    __u8    sdiag_family;
Packit 7cfc04
    __u8    sdiag_protocol;
Packit 7cfc04
    __u16   pad;
Packit 7cfc04
    __u32   udiag_states;
Packit 7cfc04
    __u32   udiag_ino;
Packit 7cfc04
    __u32   udiag_show;
Packit 7cfc04
    __u32   udiag_cookie[2];
Packit 7cfc04
};
Packit 7cfc04
.EE
Packit 7cfc04
.in
Packit 7cfc04
.PP
Packit 7cfc04
The fields of this structure are as follows:
Packit 7cfc04
.TP
Packit 7cfc04
.I sdiag_family
Packit 7cfc04
The address family; it should be set to
Packit 7cfc04
.BR AF_UNIX .
Packit 7cfc04
.PP
Packit 7cfc04
.I sdiag_protocol
Packit 7cfc04
.PD 0
Packit 7cfc04
.TP
Packit 7cfc04
.PD
Packit 7cfc04
.I pad
Packit 7cfc04
These fields should be set to 0.
Packit 7cfc04
.TP
Packit 7cfc04
.I udiag_states
Packit 7cfc04
This is a bit mask that defines a filter of sockets states.
Packit 7cfc04
Only those sockets whose states are in this mask will be reported.
Packit 7cfc04
Ignored when querying for an individual socket.
Packit 7cfc04
Supported values are:
Packit 7cfc04
.PP
Packit 7cfc04
.RS 12
Packit 7cfc04
1 <<
Packit 7cfc04
.B TCP_ESTABLISHED
Packit 7cfc04
.PP
Packit 7cfc04
1 <<
Packit 7cfc04
.B TCP_LISTEN
Packit 7cfc04
.RE
Packit 7cfc04
.TP
Packit 7cfc04
.I udiag_ino
Packit 7cfc04
This is an inode number when querying for an individual socket.
Packit 7cfc04
Ignored when querying for a list of sockets.
Packit 7cfc04
.TP
Packit 7cfc04
.I udiag_show
Packit 7cfc04
This is a set of flags defining what kind of information to report.
Packit 7cfc04
Each requested kind of information is reported back as a netlink
Packit 7cfc04
attribute as described below:
Packit 7cfc04
.RS
Packit 7cfc04
.TP
Packit 7cfc04
.B UDIAG_SHOW_NAME
Packit 7cfc04
The attribute reported in answer to this request is
Packit 7cfc04
.BR UNIX_DIAG_NAME .
Packit 7cfc04
The payload associated with this attribute is the pathname to which
Packit 7cfc04
the socket was bound (a sequence of bytes up to
Packit 7cfc04
.B UNIX_PATH_MAX
Packit 7cfc04
length).
Packit 7cfc04
.TP
Packit 7cfc04
.B UDIAG_SHOW_VFS
Packit 7cfc04
The attribute reported in answer to this request is
Packit 7cfc04
.BR UNIX_DIAG_VFS .
Packit 7cfc04
The payload associated with this attribute is represented in the following
Packit 7cfc04
structure:
Packit 7cfc04
.IP
Packit 7cfc04
.in +4n
Packit 7cfc04
.EX
Packit 7cfc04
struct unix_diag_vfs {
Packit 7cfc04
    __u32 udiag_vfs_dev;
Packit 7cfc04
    __u32 udiag_vfs_ino;
Packit 7cfc04
};
Packit 7cfc04
.EE
Packit 7cfc04
.in
Packit 7cfc04
.IP
Packit 7cfc04
The fields of this structure are as follows:
Packit 7cfc04
.RS
Packit 7cfc04
.TP
Packit 7cfc04
.I udiag_vfs_dev
Packit 7cfc04
The device number of the corresponding on-disk socket inode.
Packit 7cfc04
.TP
Packit 7cfc04
.I udiag_vfs_ino
Packit 7cfc04
The inode number of the corresponding on-disk socket inode.
Packit 7cfc04
.RE
Packit 7cfc04
.TP
Packit 7cfc04
.B UDIAG_SHOW_PEER
Packit 7cfc04
The attribute reported in answer to this request is
Packit 7cfc04
.BR UNIX_DIAG_PEER .
Packit 7cfc04
The payload associated with this attribute is a __u32 value
Packit 7cfc04
which is the peer's inode number.
Packit 7cfc04
This attribute is reported for connected sockets only.
Packit 7cfc04
.TP
Packit 7cfc04
.B UDIAG_SHOW_ICONS
Packit 7cfc04
The attribute reported in answer to this request is
Packit 7cfc04
.BR UNIX_DIAG_ICONS .
Packit 7cfc04
The payload associated with this attribute is an array of __u32 values
Packit 7cfc04
which are inode numbers of sockets that has passed the
Packit 7cfc04
.BR connect (2)
Packit 7cfc04
call, but hasn't been processed with
Packit 7cfc04
.BR accept (2)
Packit 7cfc04
yet.
Packit 7cfc04
This attribute is reported for listening sockets only.
Packit 7cfc04
.TP
Packit 7cfc04
.B UDIAG_SHOW_RQLEN
Packit 7cfc04
The attribute reported in answer to this request is
Packit 7cfc04
.BR UNIX_DIAG_RQLEN .
Packit 7cfc04
The payload associated with this attribute is represented in the following
Packit 7cfc04
structure:
Packit 7cfc04
.IP
Packit 7cfc04
.in +4n
Packit 7cfc04
.EX
Packit 7cfc04
struct unix_diag_rqlen {
Packit 7cfc04
    __u32 udiag_rqueue;
Packit 7cfc04
    __u32 udiag_wqueue;
Packit 7cfc04
};
Packit 7cfc04
.EE
Packit 7cfc04
.in
Packit 7cfc04
.IP
Packit 7cfc04
The fields of this structure are as follows:
Packit 7cfc04
.RS
Packit 7cfc04
.TP
Packit 7cfc04
.I udiag_rqueue
Packit 7cfc04
For listening sockets:
Packit 7cfc04
the number of pending connections.
Packit 7cfc04
The length of the array associated with the
Packit 7cfc04
.B UNIX_DIAG_ICONS
Packit 7cfc04
response attribute is equal to this value.
Packit 7cfc04
.IP
Packit 7cfc04
For established sockets:
Packit 7cfc04
the amount of data in incoming queue.
Packit 7cfc04
.TP
Packit 7cfc04
.I udiag_wqueue
Packit 7cfc04
For listening sockets:
Packit 7cfc04
the backlog length which equals to the value passed as the second argument to
Packit 7cfc04
.BR listen (2).
Packit 7cfc04
.IP
Packit 7cfc04
For established sockets:
Packit 7cfc04
the amount of memory available for sending.
Packit 7cfc04
.RE
Packit 7cfc04
.TP
Packit 7cfc04
.B UDIAG_SHOW_MEMINFO
Packit 7cfc04
The attribute reported in answer to this request is
Packit 7cfc04
.BR UNIX_DIAG_MEMINFO .
Packit 7cfc04
The payload associated with this attribute is an array of __u32 values
Packit 7cfc04
described below in the subsection "Socket memory information".
Packit 7cfc04
.PP
Packit 7cfc04
The following attributes are reported back without any specific request:
Packit 7cfc04
.TP
Packit 7cfc04
.BR UNIX_DIAG_SHUTDOWN
Packit 7cfc04
The payload associated with this attribute is __u8 value which represents
Packit 7cfc04
bits of
Packit 7cfc04
.BR shutdown (2)
Packit 7cfc04
state.
Packit 7cfc04
.RE
Packit 7cfc04
.TP
Packit 7cfc04
.I udiag_cookie
Packit 7cfc04
This is an array of opaque identifiers that could be used along with
Packit 7cfc04
.I udiag_ino
Packit 7cfc04
to specify an individual socket.
Packit 7cfc04
It is ignored when querying for a list
Packit 7cfc04
of sockets, as well as when all its elements are set to \-1.
Packit 7cfc04
.PP
Packit 7cfc04
The response to a query for UNIX domain sockets is represented as an array of
Packit 7cfc04
.PP
Packit 7cfc04
.in +4n
Packit 7cfc04
.EX
Packit 7cfc04
struct unix_diag_msg {
Packit 7cfc04
    __u8    udiag_family;
Packit 7cfc04
    __u8    udiag_type;
Packit 7cfc04
    __u8    udiag_state;
Packit 7cfc04
    __u8    pad;
Packit 7cfc04
    __u32   udiag_ino;
Packit 7cfc04
    __u32   udiag_cookie[2];
Packit 7cfc04
};
Packit 7cfc04
.EE
Packit 7cfc04
.in
Packit 7cfc04
.PP
Packit 7cfc04
followed by netlink attributes.
Packit 7cfc04
.PP
Packit 7cfc04
The fields of this structure are as follows:
Packit 7cfc04
.TP
Packit 7cfc04
.I udiag_family
Packit 7cfc04
This field has the same meaning as in
Packit 7cfc04
.IR "struct unix_diag_req" .
Packit 7cfc04
.TP
Packit 7cfc04
.I udiag_type
Packit 7cfc04
This is set to one of
Packit 7cfc04
.BR SOCK_PACKET ,
Packit 7cfc04
.BR SOCK_STREAM ,
Packit 7cfc04
or
Packit 7cfc04
.BR SOCK_SEQPACKET .
Packit 7cfc04
.TP
Packit 7cfc04
.I udiag_state
Packit 7cfc04
This is set to one of
Packit 7cfc04
.BR TCP_LISTEN
Packit 7cfc04
or
Packit 7cfc04
.BR TCP_ESTABLISHED .
Packit 7cfc04
.TP
Packit 7cfc04
.I pad
Packit 7cfc04
This field is set to 0.
Packit 7cfc04
.TP
Packit 7cfc04
.I udiag_ino
Packit 7cfc04
This is the socket inode number.
Packit 7cfc04
.TP
Packit 7cfc04
.I udiag_cookie
Packit 7cfc04
This is an array of opaque identifiers that could be used in subsequent
Packit 7cfc04
queries.
Packit 7cfc04
.\"
Packit 7cfc04
.SS IPv4 and IPv6 sockets
Packit 7cfc04
For IPv4 and IPv6 sockets,
Packit 7cfc04
the request is represented in the following structure:
Packit 7cfc04
.PP
Packit 7cfc04
.in +4n
Packit 7cfc04
.EX
Packit 7cfc04
struct inet_diag_req_v2 {
Packit 7cfc04
    __u8    sdiag_family;
Packit 7cfc04
    __u8    sdiag_protocol;
Packit 7cfc04
    __u8    idiag_ext;
Packit 7cfc04
    __u8    pad;
Packit 7cfc04
    __u32   idiag_states;
Packit 7cfc04
    struct inet_diag_sockid id;
Packit 7cfc04
};
Packit 7cfc04
.EE
Packit 7cfc04
.in
Packit 7cfc04
.PP
Packit 7cfc04
where
Packit 7cfc04
.I "struct inet_diag_sockid"
Packit 7cfc04
is defined as follows:
Packit 7cfc04
.PP
Packit 7cfc04
.in +4n
Packit 7cfc04
.EX
Packit 7cfc04
struct inet_diag_sockid {
Packit 7cfc04
    __be16  idiag_sport;
Packit 7cfc04
    __be16  idiag_dport;
Packit 7cfc04
    __be32  idiag_src[4];
Packit 7cfc04
    __be32  idiag_dst[4];
Packit 7cfc04
    __u32   idiag_if;
Packit 7cfc04
    __u32   idiag_cookie[2];
Packit 7cfc04
};
Packit 7cfc04
.EE
Packit 7cfc04
.in
Packit 7cfc04
.PP
Packit 7cfc04
The fields of
Packit 7cfc04
.I "struct inet_diag_req_v2"
Packit 7cfc04
are as follows:
Packit 7cfc04
.TP
Packit 7cfc04
.I sdiag_family
Packit 7cfc04
This should be set to either
Packit 7cfc04
.B AF_INET
Packit 7cfc04
or
Packit 7cfc04
.B AF_INET6
Packit 7cfc04
for IPv4 or IPv6 sockets respectively.
Packit 7cfc04
.TP
Packit 7cfc04
.I sdiag_protocol
Packit 7cfc04
This should be set to one of
Packit 7cfc04
.BR IPPROTO_TCP ,
Packit 7cfc04
.BR IPPROTO_UDP ,
Packit 7cfc04
or
Packit 7cfc04
.BR IPPROTO_UDPLITE .
Packit 7cfc04
.TP
Packit 7cfc04
.I idiag_ext
Packit 7cfc04
This is a set of flags defining what kind of extended information to report.
Packit 7cfc04
Each requested kind of information is reported back as a netlink attribute
Packit 7cfc04
as described below:
Packit 7cfc04
.RS
Packit 7cfc04
.TP
Packit 7cfc04
.B INET_DIAG_TOS
Packit 7cfc04
The payload associated with this attribute is a __u8 value
Packit 7cfc04
which is the TOS of the socket.
Packit 7cfc04
.TP
Packit 7cfc04
.B INET_DIAG_TCLASS
Packit 7cfc04
The payload associated with this attribute is a __u8 value
Packit 7cfc04
which is the TClass of the socket.
Packit 7cfc04
IPv6 sockets only.
Packit 7cfc04
For LISTEN and CLOSE sockets, this is followed by
Packit 7cfc04
.B INET_DIAG_SKV6ONLY
Packit 7cfc04
attribute with associated __u8 payload value meaning whether the socket
Packit 7cfc04
is IPv6-only or not.
Packit 7cfc04
.TP
Packit 7cfc04
.B INET_DIAG_MEMINFO
Packit 7cfc04
The payload associated with this attribute is represented in the following
Packit 7cfc04
structure:
Packit 7cfc04
.IP
Packit 7cfc04
.in +4n
Packit 7cfc04
.EX
Packit 7cfc04
struct inet_diag_meminfo {
Packit 7cfc04
    __u32 idiag_rmem;
Packit 7cfc04
    __u32 idiag_wmem;
Packit 7cfc04
    __u32 idiag_fmem;
Packit 7cfc04
    __u32 idiag_tmem;
Packit 7cfc04
};
Packit 7cfc04
.EE
Packit 7cfc04
.in
Packit 7cfc04
.IP
Packit 7cfc04
The fields of this structure are as follows:
Packit 7cfc04
.RS
Packit 7cfc04
.TP 12
Packit 7cfc04
.I idiag_rmem
Packit 7cfc04
The amount of data in the receive queue.
Packit 7cfc04
.TP
Packit 7cfc04
.I idiag_wmem
Packit 7cfc04
The amount of data that is queued by TCP but not yet sent.
Packit 7cfc04
.TP
Packit 7cfc04
.I idiag_fmem
Packit 7cfc04
The amount of memory scheduled for future use (TCP only).
Packit 7cfc04
.TP
Packit 7cfc04
.I idiag_tmem
Packit 7cfc04
The amount of data in send queue.
Packit 7cfc04
.RE
Packit 7cfc04
.TP
Packit 7cfc04
.B INET_DIAG_SKMEMINFO
Packit 7cfc04
The payload associated with this attribute is an array of __u32 values
Packit 7cfc04
described below in the subsection "Socket memory information".
Packit 7cfc04
.TP
Packit 7cfc04
.B INET_DIAG_INFO
Packit 7cfc04
The payload associated with this attribute is specific to the address family.
Packit 7cfc04
For TCP sockets, it is an object of type
Packit 7cfc04
.IR "struct tcp_info" .
Packit 7cfc04
.TP
Packit 7cfc04
.B INET_DIAG_CONG
Packit 7cfc04
The payload associated with this attribute is a string that describes the
Packit 7cfc04
congestion control algorithm used.
Packit 7cfc04
For TCP sockets only.
Packit 7cfc04
.RE
Packit 7cfc04
.TP
Packit 7cfc04
.I pad
Packit 7cfc04
This should be set to 0.
Packit 7cfc04
.TP
Packit 7cfc04
.I idiag_states
Packit 7cfc04
This is a bit mask that defines a filter of socket states.
Packit 7cfc04
Only those sockets whose states are in this mask will be reported.
Packit 7cfc04
Ignored when querying for an individual socket.
Packit 7cfc04
.TP
Packit 7cfc04
.I id
Packit 7cfc04
This is a socket ID object that is used in dump requests, in queries
Packit 7cfc04
about individual sockets, and is reported back in each response.
Packit 7cfc04
Unlike UNIX domain sockets, IPv4 and IPv6 sockets are identified
Packit 7cfc04
using addresses and ports.
Packit 7cfc04
All values are in network byte order.
Packit 7cfc04
.PP
Packit 7cfc04
The fields of
Packit 7cfc04
.I "struct inet_diag_sockid"
Packit 7cfc04
are as follows:
Packit 7cfc04
.TP
Packit 7cfc04
.I idiag_sport
Packit 7cfc04
The source port.
Packit 7cfc04
.TP
Packit 7cfc04
.I idiag_dport
Packit 7cfc04
The destination port.
Packit 7cfc04
.TP
Packit 7cfc04
.I idiag_src
Packit 7cfc04
The source address.
Packit 7cfc04
.TP
Packit 7cfc04
.I idiag_dst
Packit 7cfc04
The destination address.
Packit 7cfc04
.TP
Packit 7cfc04
.I idiag_if
Packit 7cfc04
The interface number the socket is bound to.
Packit 7cfc04
.TP
Packit 7cfc04
.I idiag_cookie
Packit 7cfc04
This is an array of opaque identifiers that could be used along with
Packit 7cfc04
other fields of this structure to specify an individual socket.
Packit 7cfc04
It is ignored when querying for a list of sockets, as well as
Packit 7cfc04
when all its elements are set to \-1.
Packit 7cfc04
.PP
Packit 7cfc04
The response to a query for IPv4 or IPv6 sockets is represented as an array of
Packit 7cfc04
.PP
Packit 7cfc04
.in +4n
Packit 7cfc04
.EX
Packit 7cfc04
struct inet_diag_msg {
Packit 7cfc04
    __u8    idiag_family;
Packit 7cfc04
    __u8    idiag_state;
Packit 7cfc04
    __u8    idiag_timer;
Packit 7cfc04
    __u8    idiag_retrans;
Packit 7cfc04
Packit 7cfc04
    struct inet_diag_sockid id;
Packit 7cfc04
Packit 7cfc04
    __u32   idiag_expires;
Packit 7cfc04
    __u32   idiag_rqueue;
Packit 7cfc04
    __u32   idiag_wqueue;
Packit 7cfc04
    __u32   idiag_uid;
Packit 7cfc04
    __u32   idiag_inode;
Packit 7cfc04
};
Packit 7cfc04
.EE
Packit 7cfc04
.in
Packit 7cfc04
.PP
Packit 7cfc04
followed by netlink attributes.
Packit 7cfc04
.PP
Packit 7cfc04
The fields of this structure are as follows:
Packit 7cfc04
.TP
Packit 7cfc04
.I idiag_family
Packit 7cfc04
This is the same field as in
Packit 7cfc04
.IR "struct inet_diag_req_v2" .
Packit 7cfc04
.TP
Packit 7cfc04
.I idiag_state
Packit 7cfc04
This denotes socket state as in
Packit 7cfc04
.IR "struct inet_diag_req_v2" .
Packit 7cfc04
.TP
Packit 7cfc04
.I idiag_timer
Packit 7cfc04
For TCP sockets, this field describes the type of timer that is currently
Packit 7cfc04
active for the socket.
Packit 7cfc04
It is set to one of the following constants:
Packit 7cfc04
.IP
Packit 7cfc04
.PD 0
Packit 7cfc04
.RS 12
Packit 7cfc04
.TP
Packit 7cfc04
.B 0
Packit 7cfc04
no timer is active
Packit 7cfc04
.TP
Packit 7cfc04
.B 1
Packit 7cfc04
a retransmit timer
Packit 7cfc04
.TP
Packit 7cfc04
.B 2
Packit 7cfc04
a keep-alive timer
Packit 7cfc04
.TP
Packit 7cfc04
.B 3
Packit 7cfc04
a TIME_WAIT timer
Packit 7cfc04
.TP
Packit 7cfc04
.B 4
Packit 7cfc04
a zero window probe timer
Packit 7cfc04
.RE
Packit 7cfc04
.PD
Packit 7cfc04
.IP
Packit 7cfc04
For non-TCP sockets, this field is set to 0.
Packit 7cfc04
.TP
Packit 7cfc04
.I idiag_retrans
Packit 7cfc04
For
Packit 7cfc04
.I idiag_timer
Packit 7cfc04
values 1, 2, and 4, this field contains the number of retransmits.
Packit 7cfc04
For other
Packit 7cfc04
.I idiag_timer
Packit 7cfc04
values, this field is set to 0.
Packit 7cfc04
.TP
Packit 7cfc04
.I idiag_expires
Packit 7cfc04
For TCP sockets that have an active timer, this field describes its expiration
Packit 7cfc04
time in milliseconds.
Packit 7cfc04
For other sockets, this field is set to 0.
Packit 7cfc04
.TP
Packit 7cfc04
.I idiag_rqueue
Packit 7cfc04
For listening sockets:
Packit 7cfc04
the number of pending connections.
Packit 7cfc04
.IP
Packit 7cfc04
For other sockets:
Packit 7cfc04
the amount of data in the incoming queue.
Packit 7cfc04
.TP
Packit 7cfc04
.I idiag_wqueue
Packit 7cfc04
For listening sockets:
Packit 7cfc04
the backlog length.
Packit 7cfc04
.IP
Packit 7cfc04
For other sockets:
Packit 7cfc04
the amount of memory available for sending.
Packit 7cfc04
.TP
Packit 7cfc04
.I idiag_uid
Packit 7cfc04
This is the socket owner UID.
Packit 7cfc04
.TP
Packit 7cfc04
.I idiag_inode
Packit 7cfc04
This is the socket inode number.
Packit 7cfc04
.\"
Packit 7cfc04
.SS Socket memory information
Packit 7cfc04
The payload associated with
Packit 7cfc04
.B UNIX_DIAG_MEMINFO
Packit 7cfc04
and
Packit 7cfc04
.BR INET_DIAG_SKMEMINFO
Packit 7cfc04
netlink attributes is an array of the following __u32 values:
Packit 7cfc04
.TP
Packit 7cfc04
.B SK_MEMINFO_RMEM_ALLOC
Packit 7cfc04
The amount of data in receive queue.
Packit 7cfc04
.TP
Packit 7cfc04
.B SK_MEMINFO_RCVBUF
Packit 7cfc04
The receive socket buffer as set by
Packit 7cfc04
.BR SO_RCVBUF .
Packit 7cfc04
.TP
Packit 7cfc04
.B SK_MEMINFO_WMEM_ALLOC
Packit 7cfc04
The amount of data in send queue.
Packit 7cfc04
.TP
Packit 7cfc04
.B SK_MEMINFO_SNDBUF
Packit 7cfc04
The send socket buffer as set by
Packit 7cfc04
.BR SO_SNDBUF .
Packit 7cfc04
.TP
Packit 7cfc04
.B SK_MEMINFO_FWD_ALLOC
Packit 7cfc04
The amount of memory scheduled for future use (TCP only).
Packit 7cfc04
.TP
Packit 7cfc04
.B SK_MEMINFO_WMEM_QUEUED
Packit 7cfc04
The amount of data queued by TCP, but not yet sent.
Packit 7cfc04
.TP
Packit 7cfc04
.B SK_MEMINFO_OPTMEM
Packit 7cfc04
The amount of memory allocated for the socket's service needs (e.g., socket
Packit 7cfc04
filter).
Packit 7cfc04
.TP
Packit 7cfc04
.B SK_MEMINFO_BACKLOG
Packit 7cfc04
The amount of packets in the backlog (not yet processed).
Packit 7cfc04
.SH VERSIONS
Packit 7cfc04
.B NETLINK_INET_DIAG
Packit 7cfc04
was introduced in Linux 2.6.14 and supported
Packit 7cfc04
.B AF_INET
Packit 7cfc04
and
Packit 7cfc04
.B AF_INET6
Packit 7cfc04
sockets only.
Packit 7cfc04
In Linux 3.3, it was renamed to
Packit 7cfc04
.B NETLINK_SOCK_DIAG
Packit 7cfc04
and extended to support
Packit 7cfc04
.B AF_UNIX
Packit 7cfc04
sockets.
Packit 7cfc04
.PP
Packit 7cfc04
.B UNIX_DIAG_MEMINFO
Packit 7cfc04
and
Packit 7cfc04
.BR INET_DIAG_SKMEMINFO
Packit 7cfc04
were introduced in Linux 3.6.
Packit 7cfc04
.SH CONFORMING TO
Packit 7cfc04
The NETLINK_SOCK_DIAG API is Linux-specific.
Packit 7cfc04
.SH EXAMPLE
Packit 7cfc04
The following example program prints inode number, peer's inode number,
Packit 7cfc04
and name of all UNIX domain sockets in the current namespace.
Packit 7cfc04
.PP
Packit 7cfc04
.EX
Packit 7cfc04
#include <errno.h>
Packit 7cfc04
#include <stdio.h>
Packit 7cfc04
#include <string.h>
Packit 7cfc04
#include <unistd.h>
Packit 7cfc04
#include <sys/socket.h>
Packit 7cfc04
#include <sys/un.h>
Packit 7cfc04
#include <linux/netlink.h>
Packit 7cfc04
#include <linux/rtnetlink.h>
Packit 7cfc04
#include <linux/sock_diag.h>
Packit 7cfc04
#include <linux/unix_diag.h>
Packit 7cfc04
Packit 7cfc04
static int
Packit 7cfc04
send_query(int fd)
Packit 7cfc04
{
Packit 7cfc04
    struct sockaddr_nl nladdr = {
Packit 7cfc04
        .nl_family = AF_NETLINK
Packit 7cfc04
    };
Packit 7cfc04
    struct
Packit 7cfc04
    {
Packit 7cfc04
        struct nlmsghdr nlh;
Packit 7cfc04
        struct unix_diag_req udr;
Packit 7cfc04
    } req = {
Packit 7cfc04
        .nlh = {
Packit 7cfc04
            .nlmsg_len = sizeof(req),
Packit 7cfc04
            .nlmsg_type = SOCK_DIAG_BY_FAMILY,
Packit 7cfc04
            .nlmsg_flags = NLM_F_REQUEST | NLM_F_DUMP
Packit 7cfc04
        },
Packit 7cfc04
        .udr = {
Packit 7cfc04
            .sdiag_family = AF_UNIX,
Packit 7cfc04
            .udiag_states = \-1,
Packit 7cfc04
            .udiag_show = UDIAG_SHOW_NAME | UDIAG_SHOW_PEER
Packit 7cfc04
        }
Packit 7cfc04
    };
Packit 7cfc04
    struct iovec iov = {
Packit 7cfc04
        .iov_base = &req,
Packit 7cfc04
        .iov_len = sizeof(req)
Packit 7cfc04
    };
Packit 7cfc04
    struct msghdr msg = {
Packit 7cfc04
        .msg_name = (void *) &nladdr,
Packit 7cfc04
        .msg_namelen = sizeof(nladdr),
Packit 7cfc04
        .msg_iov = &iov,
Packit 7cfc04
        .msg_iovlen = 1
Packit 7cfc04
    };
Packit 7cfc04
Packit 7cfc04
    for (;;) {
Packit 7cfc04
        if (sendmsg(fd, &msg, 0) < 0) {
Packit 7cfc04
            if (errno == EINTR)
Packit 7cfc04
                continue;
Packit 7cfc04
Packit 7cfc04
            perror("sendmsg");
Packit 7cfc04
            return \-1;
Packit 7cfc04
        }
Packit 7cfc04
Packit 7cfc04
        return 0;
Packit 7cfc04
    }
Packit 7cfc04
}
Packit 7cfc04
Packit 7cfc04
static int
Packit 7cfc04
print_diag(const struct unix_diag_msg *diag, unsigned int len)
Packit 7cfc04
{
Packit 7cfc04
    if (len < NLMSG_LENGTH(sizeof(*diag))) {
Packit 7cfc04
        fputs("short response\\n", stderr);
Packit 7cfc04
        return \-1;
Packit 7cfc04
    }
Packit 7cfc04
    if (diag\->udiag_family != AF_UNIX) {
Packit 7cfc04
        fprintf(stderr, "unexpected family %u\\n", diag\->udiag_family);
Packit 7cfc04
        return \-1;
Packit 7cfc04
    }
Packit 7cfc04
Packit 7cfc04
    struct rtattr *attr;
Packit 7cfc04
    unsigned int rta_len = len \- NLMSG_LENGTH(sizeof(*diag));
Packit 7cfc04
    unsigned int peer = 0;
Packit 7cfc04
    size_t path_len = 0;
Packit 7cfc04
    char path[sizeof(((struct sockaddr_un *) 0)\->sun_path) + 1];
Packit 7cfc04
Packit 7cfc04
    for (attr = (struct rtattr *) (diag + 1);
Packit 7cfc04
             RTA_OK(attr, rta_len); attr = RTA_NEXT(attr, rta_len)) {
Packit 7cfc04
        switch (attr\->rta_type) {
Packit 7cfc04
        case UNIX_DIAG_NAME:
Packit 7cfc04
            if (!path_len) {
Packit 7cfc04
                path_len = RTA_PAYLOAD(attr);
Packit 7cfc04
                if (path_len > sizeof(path) \- 1)
Packit 7cfc04
                    path_len = sizeof(path) \- 1;
Packit 7cfc04
                memcpy(path, RTA_DATA(attr), path_len);
Packit 7cfc04
                path[path_len] = '\\0';
Packit 7cfc04
            }
Packit 7cfc04
            break;
Packit 7cfc04
Packit 7cfc04
        case UNIX_DIAG_PEER:
Packit 7cfc04
            if (RTA_PAYLOAD(attr) >= sizeof(peer))
Packit 7cfc04
                peer = *(unsigned int *) RTA_DATA(attr);
Packit 7cfc04
            break;
Packit 7cfc04
        }
Packit 7cfc04
    }
Packit 7cfc04
Packit 7cfc04
    printf("inode=%u", diag->udiag_ino);
Packit 7cfc04
Packit 7cfc04
    if (peer)
Packit 7cfc04
        printf(", peer=%u", peer);
Packit 7cfc04
Packit 7cfc04
    if (path_len)
Packit 7cfc04
        printf(", name=%s%s", *path ? "" : "@",
Packit 7cfc04
                *path ? path : path + 1);
Packit 7cfc04
Packit 7cfc04
    putchar('\\n');
Packit 7cfc04
    return 0;
Packit 7cfc04
}
Packit 7cfc04
Packit 7cfc04
static int
Packit 7cfc04
receive_responses(int fd)
Packit 7cfc04
{
Packit 7cfc04
    long buf[8192 / sizeof(long)];
Packit 7cfc04
    struct sockaddr_nl nladdr = {
Packit 7cfc04
        .nl_family = AF_NETLINK
Packit 7cfc04
    };
Packit 7cfc04
    struct iovec iov = {
Packit 7cfc04
        .iov_base = buf,
Packit 7cfc04
        .iov_len = sizeof(buf)
Packit 7cfc04
    };
Packit 7cfc04
    int flags = 0;
Packit 7cfc04
Packit 7cfc04
    for (;;) {
Packit 7cfc04
        struct msghdr msg = {
Packit 7cfc04
            .msg_name = (void *) &nladdr,
Packit 7cfc04
            .msg_namelen = sizeof(nladdr),
Packit 7cfc04
            .msg_iov = &iov,
Packit 7cfc04
            .msg_iovlen = 1
Packit 7cfc04
        };
Packit 7cfc04
Packit 7cfc04
        ssize_t ret = recvmsg(fd, &msg, flags);
Packit 7cfc04
Packit 7cfc04
        if (ret < 0) {
Packit 7cfc04
            if (errno == EINTR)
Packit 7cfc04
                continue;
Packit 7cfc04
Packit 7cfc04
            perror("recvmsg");
Packit 7cfc04
            return \-1;
Packit 7cfc04
        }
Packit 7cfc04
        if (ret == 0)
Packit 7cfc04
            return 0;
Packit 7cfc04
Packit 7cfc04
        const struct nlmsghdr *h = (struct nlmsghdr *) buf;
Packit 7cfc04
Packit 7cfc04
        if (!NLMSG_OK(h, ret)) {
Packit 7cfc04
            fputs("!NLMSG_OK\\n", stderr);
Packit 7cfc04
            return \-1;
Packit 7cfc04
        }
Packit 7cfc04
Packit 7cfc04
        for (; NLMSG_OK(h, ret); h = NLMSG_NEXT(h, ret)) {
Packit 7cfc04
            if (h\->nlmsg_type == NLMSG_DONE)
Packit 7cfc04
                return 0;
Packit 7cfc04
Packit 7cfc04
            if (h\->nlmsg_type == NLMSG_ERROR) {
Packit 7cfc04
                const struct nlmsgerr *err = NLMSG_DATA(h);
Packit 7cfc04
Packit 7cfc04
                if (h\->nlmsg_len < NLMSG_LENGTH(sizeof(*err))) {
Packit 7cfc04
                    fputs("NLMSG_ERROR\\n", stderr);
Packit 7cfc04
                } else {
Packit 7cfc04
                    errno = \-err\->error;
Packit 7cfc04
                    perror("NLMSG_ERROR");
Packit 7cfc04
                }
Packit 7cfc04
Packit 7cfc04
                return \-1;
Packit 7cfc04
            }
Packit 7cfc04
Packit 7cfc04
            if (h\->nlmsg_type != SOCK_DIAG_BY_FAMILY) {
Packit 7cfc04
                fprintf(stderr, "unexpected nlmsg_type %u\\n",
Packit 7cfc04
                        (unsigned) h\->nlmsg_type);
Packit 7cfc04
                return \-1;
Packit 7cfc04
            }
Packit 7cfc04
Packit 7cfc04
            if (print_diag(NLMSG_DATA(h), h\->nlmsg_len))
Packit 7cfc04
                return \-1;
Packit 7cfc04
        }
Packit 7cfc04
    }
Packit 7cfc04
}
Packit 7cfc04
Packit 7cfc04
int
Packit 7cfc04
main(void)
Packit 7cfc04
{
Packit 7cfc04
    int fd = socket(AF_NETLINK, SOCK_RAW, NETLINK_SOCK_DIAG);
Packit 7cfc04
Packit 7cfc04
    if (fd < 0) {
Packit 7cfc04
        perror("socket");
Packit 7cfc04
        return 1;
Packit 7cfc04
    }
Packit 7cfc04
Packit 7cfc04
    int ret = send_query(fd) || receive_responses(fd);
Packit 7cfc04
Packit 7cfc04
    close(fd);
Packit 7cfc04
    return ret;
Packit 7cfc04
}
Packit 7cfc04
.EE
Packit 7cfc04
.SH SEE ALSO
Packit 7cfc04
.BR netlink (3),
Packit 7cfc04
.BR rtnetlink (3),
Packit 7cfc04
.BR netlink (7),
Packit 7cfc04
.BR tcp (7)
Packit 7cfc04
.SH COLOPHON
Packit 7cfc04
This page is part of release 4.15 of the Linux
Packit 7cfc04
.I man-pages
Packit 7cfc04
project.
Packit 7cfc04
A description of the project,
Packit 7cfc04
information about reporting bugs,
Packit 7cfc04
and the latest version of this page,
Packit 7cfc04
can be found at
Packit 7cfc04
\%https://www.kernel.org/doc/man\-pages/.