1102 lines
33 KiB
HTML
1102 lines
33 KiB
HTML
|
|
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
|
|
<HTML><HEAD><TITLE>Man page of SOCK_DIAG</TITLE>
|
|
</HEAD><BODY>
|
|
<H1>SOCK_DIAG</H1>
|
|
Section: Linux Programmer's Manual (7)<BR>Updated: 2019-03-06<BR><A HREF="#index">Index</A>
|
|
<A HREF="/cgi-bin/man/man2html">Return to Main Contents</A><HR>
|
|
|
|
<A NAME="lbAB"> </A>
|
|
<H2>NAME</H2>
|
|
|
|
sock_diag - obtaining information about sockets
|
|
<A NAME="lbAC"> </A>
|
|
<H2>SYNOPSIS</H2>
|
|
|
|
<PRE>
|
|
<B>#include <<A HREF="file:///usr/include/sys/socket.h">sys/socket.h</A>></B>
|
|
<B>#include <<A HREF="file:///usr/include/linux/sock_diag.h">linux/sock_diag.h</A>></B>
|
|
<B>#include <<A HREF="file:///usr/include/linux/unix_diag.h">linux/unix_diag.h</A>></B> /* for UNIX domain sockets */
|
|
<B>#include <<A HREF="file:///usr/include/linux/inet_diag.h">linux/inet_diag.h</A>></B> /* for IPv4 and IPv6 sockets */
|
|
|
|
<B>diag_socket = socket(AF_NETLINK, </B><I>socket_type</I><B>, NETLINK_SOCK_DIAG);</B>
|
|
</PRE>
|
|
|
|
<A NAME="lbAD"> </A>
|
|
<H2>DESCRIPTION</H2>
|
|
|
|
The sock_diag netlink subsystem provides a mechanism for obtaining
|
|
information about sockets of various address families from the kernel.
|
|
This subsystem can be used to obtain information about individual
|
|
sockets or request a list of sockets.
|
|
<P>
|
|
|
|
In the request, the caller can specify additional information it would
|
|
like to obtain about the socket, for example, memory information or
|
|
information specific to the address family.
|
|
<P>
|
|
|
|
When requesting a list of sockets, the caller can specify filters that
|
|
would be applied by the kernel to select a subset of sockets to report.
|
|
For now, there is only the ability to filter sockets by state (connected,
|
|
listening, and so on.)
|
|
<P>
|
|
|
|
Note that sock_diag reports only those sockets that have a name;
|
|
that is, either sockets bound explicitly with
|
|
<B><A HREF="/cgi-bin/man/man2html?2+bind">bind</A></B>(2)
|
|
|
|
or sockets that were automatically bound to an address (e.g., by
|
|
<B><A HREF="/cgi-bin/man/man2html?2+connect">connect</A></B>(2)).
|
|
|
|
This is the same set of sockets that is available via
|
|
<I>/proc/net/unix</I>,
|
|
|
|
<I>/proc/net/tcp</I>,
|
|
|
|
<I>/proc/net/udp</I>,
|
|
|
|
and so on.
|
|
|
|
<A NAME="lbAE"> </A>
|
|
<H3>Request</H3>
|
|
|
|
The request starts with a
|
|
<I>struct nlmsghdr</I>
|
|
|
|
header described in
|
|
<B><A HREF="/cgi-bin/man/man2html?7+netlink">netlink</A></B>(7)
|
|
|
|
with
|
|
<I>nlmsg_type</I>
|
|
|
|
field set to
|
|
<B>SOCK_DIAG_BY_FAMILY</B>.
|
|
|
|
It is followed by a header specific to the address family that starts with
|
|
a common part shared by all address families:
|
|
<P>
|
|
|
|
|
|
|
|
struct sock_diag_req {
|
|
<BR> __u8 sdiag_family;
|
|
<BR> __u8 sdiag_protocol;
|
|
};
|
|
|
|
|
|
<P>
|
|
|
|
The fields of this structure are as follows:
|
|
<DL COMPACT>
|
|
<DT id="1"><I>sdiag_family</I>
|
|
|
|
<DD>
|
|
An address family.
|
|
It should be set to the appropriate
|
|
<B>AF_*</B>
|
|
|
|
constant.
|
|
<DT id="2"><I>sdiag_protocol</I>
|
|
|
|
<DD>
|
|
Depends on
|
|
<I>sdiag_family</I>.
|
|
|
|
It should be set to the appropriate
|
|
<B>IPPROTO_*</B>
|
|
|
|
constant for
|
|
<B>AF_INET</B>
|
|
|
|
and
|
|
<B>AF_INET6</B>,
|
|
|
|
and to 0 otherwise.
|
|
</DL>
|
|
<P>
|
|
|
|
If the
|
|
<I>nlmsg_flags</I>
|
|
|
|
field of the
|
|
<I>struct nlmsghdr</I>
|
|
|
|
header has the
|
|
<B>NLM_F_DUMP</B>
|
|
|
|
flag set, it means that a list of sockets is being requested;
|
|
otherwise it is a query about an individual socket.
|
|
|
|
<A NAME="lbAF"> </A>
|
|
<H3>Response</H3>
|
|
|
|
The response starts with a
|
|
<I>struct nlmsghdr</I>
|
|
|
|
header and is followed by an array of objects specific to the address family.
|
|
The array is to be accessed with the standard
|
|
<B>NLMSG_*</B>
|
|
|
|
macros from the
|
|
<B><A HREF="/cgi-bin/man/man2html?3+netlink">netlink</A></B>(3)
|
|
|
|
API.
|
|
<P>
|
|
|
|
Each object is the NLA (netlink attributes) list that is to be accessed
|
|
with the
|
|
<B>RTA_*</B>
|
|
|
|
macros from
|
|
<B><A HREF="/cgi-bin/man/man2html?3+rtnetlink">rtnetlink</A></B>(3)
|
|
|
|
API.
|
|
|
|
<A NAME="lbAG"> </A>
|
|
<H3>UNIX domain sockets</H3>
|
|
|
|
For UNIX domain sockets the request is represented in the following structure:
|
|
<P>
|
|
|
|
|
|
|
|
struct unix_diag_req {
|
|
<BR> __u8 sdiag_family;
|
|
<BR> __u8 sdiag_protocol;
|
|
<BR> __u16 pad;
|
|
<BR> __u32 udiag_states;
|
|
<BR> __u32 udiag_ino;
|
|
<BR> __u32 udiag_show;
|
|
<BR> __u32 udiag_cookie[2];
|
|
};
|
|
|
|
|
|
<P>
|
|
|
|
The fields of this structure are as follows:
|
|
<DL COMPACT>
|
|
<DT id="3"><I>sdiag_family</I>
|
|
|
|
<DD>
|
|
The address family; it should be set to
|
|
<B>AF_UNIX</B>.
|
|
|
|
</DL>
|
|
<P>
|
|
|
|
<I>sdiag_protocol</I>
|
|
|
|
|
|
<DL COMPACT>
|
|
<DT id="4">
|
|
<DD>
|
|
<I>pad</I>
|
|
|
|
These fields should be set to 0.
|
|
<DT id="5"><I>udiag_states</I>
|
|
|
|
<DD>
|
|
This is a bit mask that defines a filter of sockets states.
|
|
Only those sockets whose states are in this mask will be reported.
|
|
Ignored when querying for an individual socket.
|
|
Supported values are:
|
|
</DL>
|
|
<P>
|
|
|
|
<DL COMPACT><DT id="6"><DD>
|
|
1 <<
|
|
<B>TCP_ESTABLISHED</B>
|
|
|
|
<P>
|
|
|
|
1 <<
|
|
<B>TCP_LISTEN</B>
|
|
|
|
</DL>
|
|
|
|
<DL COMPACT>
|
|
<DT id="7"><I>udiag_ino</I>
|
|
|
|
<DD>
|
|
This is an inode number when querying for an individual socket.
|
|
Ignored when querying for a list of sockets.
|
|
<DT id="8"><I>udiag_show</I>
|
|
|
|
<DD>
|
|
This is a set of flags defining what kind of information to report.
|
|
Each requested kind of information is reported back as a netlink
|
|
attribute as described below:
|
|
<DL COMPACT><DT id="9"><DD>
|
|
<DL COMPACT>
|
|
<DT id="10"><B>UDIAG_SHOW_NAME</B>
|
|
|
|
<DD>
|
|
The attribute reported in answer to this request is
|
|
<B>UNIX_DIAG_NAME</B>.
|
|
|
|
The payload associated with this attribute is the pathname to which
|
|
the socket was bound (a sequence of bytes up to
|
|
<B>UNIX_PATH_MAX</B>
|
|
|
|
length).
|
|
<DT id="11"><B>UDIAG_SHOW_VFS</B>
|
|
|
|
<DD>
|
|
The attribute reported in answer to this request is
|
|
<B>UNIX_DIAG_VFS</B>.
|
|
|
|
The payload associated with this attribute is represented in the following
|
|
structure:
|
|
<DT id="12"><DD>
|
|
|
|
|
|
struct unix_diag_vfs {
|
|
<BR> __u32 udiag_vfs_dev;
|
|
<BR> __u32 udiag_vfs_ino;
|
|
};
|
|
|
|
|
|
<DT id="13"><DD>
|
|
The fields of this structure are as follows:
|
|
<DL COMPACT><DT id="14"><DD>
|
|
<DL COMPACT>
|
|
<DT id="15"><I>udiag_vfs_dev</I>
|
|
|
|
<DD>
|
|
The device number of the corresponding on-disk socket inode.
|
|
<DT id="16"><I>udiag_vfs_ino</I>
|
|
|
|
<DD>
|
|
The inode number of the corresponding on-disk socket inode.
|
|
</DL>
|
|
</DL>
|
|
|
|
<DT id="17"><B>UDIAG_SHOW_PEER</B>
|
|
|
|
<DD>
|
|
The attribute reported in answer to this request is
|
|
<B>UNIX_DIAG_PEER</B>.
|
|
|
|
The payload associated with this attribute is a __u32 value
|
|
which is the peer's inode number.
|
|
This attribute is reported for connected sockets only.
|
|
<DT id="18"><B>UDIAG_SHOW_ICONS</B>
|
|
|
|
<DD>
|
|
The attribute reported in answer to this request is
|
|
<B>UNIX_DIAG_ICONS</B>.
|
|
|
|
The payload associated with this attribute is an array of __u32 values
|
|
which are inode numbers of sockets that has passed the
|
|
<B><A HREF="/cgi-bin/man/man2html?2+connect">connect</A></B>(2)
|
|
|
|
call, but hasn't been processed with
|
|
<B><A HREF="/cgi-bin/man/man2html?2+accept">accept</A></B>(2)
|
|
|
|
yet.
|
|
This attribute is reported for listening sockets only.
|
|
<DT id="19"><B>UDIAG_SHOW_RQLEN</B>
|
|
|
|
<DD>
|
|
The attribute reported in answer to this request is
|
|
<B>UNIX_DIAG_RQLEN</B>.
|
|
|
|
The payload associated with this attribute is represented in the following
|
|
structure:
|
|
<DT id="20"><DD>
|
|
|
|
|
|
struct unix_diag_rqlen {
|
|
<BR> __u32 udiag_rqueue;
|
|
<BR> __u32 udiag_wqueue;
|
|
};
|
|
|
|
|
|
<DT id="21"><DD>
|
|
The fields of this structure are as follows:
|
|
<DL COMPACT><DT id="22"><DD>
|
|
<DL COMPACT>
|
|
<DT id="23"><I>udiag_rqueue</I>
|
|
|
|
<DD>
|
|
For listening sockets:
|
|
the number of pending connections.
|
|
The length of the array associated with the
|
|
<B>UNIX_DIAG_ICONS</B>
|
|
|
|
response attribute is equal to this value.
|
|
<DT id="24"><DD>
|
|
For established sockets:
|
|
the amount of data in incoming queue.
|
|
<DT id="25"><I>udiag_wqueue</I>
|
|
|
|
<DD>
|
|
For listening sockets:
|
|
the backlog length which equals to the value passed as the second argument to
|
|
<B><A HREF="/cgi-bin/man/man2html?2+listen">listen</A></B>(2).
|
|
|
|
<DT id="26"><DD>
|
|
For established sockets:
|
|
the amount of memory available for sending.
|
|
</DL>
|
|
</DL>
|
|
|
|
<DT id="27"><B>UDIAG_SHOW_MEMINFO</B>
|
|
|
|
<DD>
|
|
The attribute reported in answer to this request is
|
|
<B>UNIX_DIAG_MEMINFO</B>.
|
|
|
|
The payload associated with this attribute is an array of __u32 values
|
|
described below in the subsection "Socket memory information".
|
|
</DL>
|
|
<P>
|
|
|
|
The following attributes are reported back without any specific request:
|
|
<DL COMPACT>
|
|
<DT id="28"><B>UNIX_DIAG_SHUTDOWN</B>
|
|
|
|
<DD>
|
|
The payload associated with this attribute is __u8 value which represents
|
|
bits of
|
|
<B><A HREF="/cgi-bin/man/man2html?2+shutdown">shutdown</A></B>(2)
|
|
|
|
state.
|
|
</DL>
|
|
</DL>
|
|
|
|
<DT id="29"><I>udiag_cookie</I>
|
|
|
|
<DD>
|
|
This is an array of opaque identifiers that could be used along with
|
|
<I>udiag_ino</I>
|
|
|
|
to specify an individual socket.
|
|
It is ignored when querying for a list
|
|
of sockets, as well as when all its elements are set to -1.
|
|
</DL>
|
|
<P>
|
|
|
|
The response to a query for UNIX domain sockets is represented as an array of
|
|
<P>
|
|
|
|
|
|
|
|
struct unix_diag_msg {
|
|
<BR> __u8 udiag_family;
|
|
<BR> __u8 udiag_type;
|
|
<BR> __u8 udiag_state;
|
|
<BR> __u8 pad;
|
|
<BR> __u32 udiag_ino;
|
|
<BR> __u32 udiag_cookie[2];
|
|
};
|
|
|
|
|
|
<P>
|
|
|
|
followed by netlink attributes.
|
|
<P>
|
|
|
|
The fields of this structure are as follows:
|
|
<DL COMPACT>
|
|
<DT id="30"><I>udiag_family</I>
|
|
|
|
<DD>
|
|
This field has the same meaning as in
|
|
<I>struct unix_diag_req</I>.
|
|
|
|
<DT id="31"><I>udiag_type</I>
|
|
|
|
<DD>
|
|
This is set to one of
|
|
<B>SOCK_PACKET</B>,
|
|
|
|
<B>SOCK_STREAM</B>,
|
|
|
|
or
|
|
<B>SOCK_SEQPACKET</B>.
|
|
|
|
<DT id="32"><I>udiag_state</I>
|
|
|
|
<DD>
|
|
This is set to one of
|
|
<B>TCP_LISTEN</B>
|
|
|
|
or
|
|
<B>TCP_ESTABLISHED</B>.
|
|
|
|
<DT id="33"><I>pad</I>
|
|
|
|
<DD>
|
|
This field is set to 0.
|
|
<DT id="34"><I>udiag_ino</I>
|
|
|
|
<DD>
|
|
This is the socket inode number.
|
|
<DT id="35"><I>udiag_cookie</I>
|
|
|
|
<DD>
|
|
This is an array of opaque identifiers that could be used in subsequent
|
|
queries.
|
|
|
|
</DL>
|
|
<A NAME="lbAH"> </A>
|
|
<H3>IPv4 and IPv6 sockets</H3>
|
|
|
|
For IPv4 and IPv6 sockets,
|
|
the request is represented in the following structure:
|
|
<P>
|
|
|
|
|
|
|
|
struct inet_diag_req_v2 {
|
|
<BR> __u8 sdiag_family;
|
|
<BR> __u8 sdiag_protocol;
|
|
<BR> __u8 idiag_ext;
|
|
<BR> __u8 pad;
|
|
<BR> __u32 idiag_states;
|
|
<BR> struct inet_diag_sockid id;
|
|
};
|
|
|
|
|
|
<P>
|
|
|
|
where
|
|
<I>struct inet_diag_sockid</I>
|
|
|
|
is defined as follows:
|
|
<P>
|
|
|
|
|
|
|
|
struct inet_diag_sockid {
|
|
<BR> __be16 idiag_sport;
|
|
<BR> __be16 idiag_dport;
|
|
<BR> __be32 idiag_src[4];
|
|
<BR> __be32 idiag_dst[4];
|
|
<BR> __u32 idiag_if;
|
|
<BR> __u32 idiag_cookie[2];
|
|
};
|
|
|
|
|
|
<P>
|
|
|
|
The fields of
|
|
<I>struct inet_diag_req_v2</I>
|
|
|
|
are as follows:
|
|
<DL COMPACT>
|
|
<DT id="36"><I>sdiag_family</I>
|
|
|
|
<DD>
|
|
This should be set to either
|
|
<B>AF_INET</B>
|
|
|
|
or
|
|
<B>AF_INET6</B>
|
|
|
|
for IPv4 or IPv6 sockets respectively.
|
|
<DT id="37"><I>sdiag_protocol</I>
|
|
|
|
<DD>
|
|
This should be set to one of
|
|
<B>IPPROTO_TCP</B>,
|
|
|
|
<B>IPPROTO_UDP</B>,
|
|
|
|
or
|
|
<B>IPPROTO_UDPLITE</B>.
|
|
|
|
<DT id="38"><I>idiag_ext</I>
|
|
|
|
<DD>
|
|
This is a set of flags defining what kind of extended information to report.
|
|
Each requested kind of information is reported back as a netlink attribute
|
|
as described below:
|
|
<DL COMPACT><DT id="39"><DD>
|
|
<DL COMPACT>
|
|
<DT id="40"><B>INET_DIAG_TOS</B>
|
|
|
|
<DD>
|
|
The payload associated with this attribute is a __u8 value
|
|
which is the TOS of the socket.
|
|
<DT id="41"><B>INET_DIAG_TCLASS</B>
|
|
|
|
<DD>
|
|
The payload associated with this attribute is a __u8 value
|
|
which is the TClass of the socket.
|
|
IPv6 sockets only.
|
|
For LISTEN and CLOSE sockets, this is followed by
|
|
<B>INET_DIAG_SKV6ONLY</B>
|
|
|
|
attribute with associated __u8 payload value meaning whether the socket
|
|
is IPv6-only or not.
|
|
<DT id="42"><B>INET_DIAG_MEMINFO</B>
|
|
|
|
<DD>
|
|
The payload associated with this attribute is represented in the following
|
|
structure:
|
|
<DT id="43"><DD>
|
|
|
|
|
|
struct inet_diag_meminfo {
|
|
<BR> __u32 idiag_rmem;
|
|
<BR> __u32 idiag_wmem;
|
|
<BR> __u32 idiag_fmem;
|
|
<BR> __u32 idiag_tmem;
|
|
};
|
|
|
|
|
|
<DT id="44"><DD>
|
|
The fields of this structure are as follows:
|
|
<DL COMPACT><DT id="45"><DD>
|
|
<DL COMPACT>
|
|
<DT id="46"><I>idiag_rmem</I>
|
|
|
|
<DD>
|
|
The amount of data in the receive queue.
|
|
<DT id="47"><I>idiag_wmem</I>
|
|
|
|
<DD>
|
|
The amount of data that is queued by TCP but not yet sent.
|
|
<DT id="48"><I>idiag_fmem</I>
|
|
|
|
<DD>
|
|
The amount of memory scheduled for future use (TCP only).
|
|
<DT id="49"><I>idiag_tmem</I>
|
|
|
|
<DD>
|
|
The amount of data in send queue.
|
|
</DL>
|
|
</DL>
|
|
|
|
<DT id="50"><B>INET_DIAG_SKMEMINFO</B>
|
|
|
|
<DD>
|
|
The payload associated with this attribute is an array of __u32 values
|
|
described below in the subsection "Socket memory information".
|
|
<DT id="51"><B>INET_DIAG_INFO</B>
|
|
|
|
<DD>
|
|
The payload associated with this attribute is specific to the address family.
|
|
For TCP sockets, it is an object of type
|
|
<I>struct tcp_info</I>.
|
|
|
|
<DT id="52"><B>INET_DIAG_CONG</B>
|
|
|
|
<DD>
|
|
The payload associated with this attribute is a string that describes the
|
|
congestion control algorithm used.
|
|
For TCP sockets only.
|
|
</DL>
|
|
</DL>
|
|
|
|
<DT id="53"><I>pad</I>
|
|
|
|
<DD>
|
|
This should be set to 0.
|
|
<DT id="54"><I>idiag_states</I>
|
|
|
|
<DD>
|
|
This is a bit mask that defines a filter of socket states.
|
|
Only those sockets whose states are in this mask will be reported.
|
|
Ignored when querying for an individual socket.
|
|
<DT id="55"><I>id</I>
|
|
|
|
<DD>
|
|
This is a socket ID object that is used in dump requests, in queries
|
|
about individual sockets, and is reported back in each response.
|
|
Unlike UNIX domain sockets, IPv4 and IPv6 sockets are identified
|
|
using addresses and ports.
|
|
All values are in network byte order.
|
|
</DL>
|
|
<P>
|
|
|
|
The fields of
|
|
<I>struct inet_diag_sockid</I>
|
|
|
|
are as follows:
|
|
<DL COMPACT>
|
|
<DT id="56"><I>idiag_sport</I>
|
|
|
|
<DD>
|
|
The source port.
|
|
<DT id="57"><I>idiag_dport</I>
|
|
|
|
<DD>
|
|
The destination port.
|
|
<DT id="58"><I>idiag_src</I>
|
|
|
|
<DD>
|
|
The source address.
|
|
<DT id="59"><I>idiag_dst</I>
|
|
|
|
<DD>
|
|
The destination address.
|
|
<DT id="60"><I>idiag_if</I>
|
|
|
|
<DD>
|
|
The interface number the socket is bound to.
|
|
<DT id="61"><I>idiag_cookie</I>
|
|
|
|
<DD>
|
|
This is an array of opaque identifiers that could be used along with
|
|
other fields of this structure to specify an individual socket.
|
|
It is ignored when querying for a list of sockets, as well as
|
|
when all its elements are set to -1.
|
|
</DL>
|
|
<P>
|
|
|
|
The response to a query for IPv4 or IPv6 sockets is represented as an array of
|
|
<P>
|
|
|
|
|
|
|
|
struct inet_diag_msg {
|
|
<BR> __u8 idiag_family;
|
|
<BR> __u8 idiag_state;
|
|
<BR> __u8 idiag_timer;
|
|
<BR> __u8 idiag_retrans;
|
|
<P>
|
|
<BR> struct inet_diag_sockid id;
|
|
<P>
|
|
<BR> __u32 idiag_expires;
|
|
<BR> __u32 idiag_rqueue;
|
|
<BR> __u32 idiag_wqueue;
|
|
<BR> __u32 idiag_uid;
|
|
<BR> __u32 idiag_inode;
|
|
};
|
|
|
|
|
|
<P>
|
|
|
|
followed by netlink attributes.
|
|
<P>
|
|
|
|
The fields of this structure are as follows:
|
|
<DL COMPACT>
|
|
<DT id="62"><I>idiag_family</I>
|
|
|
|
<DD>
|
|
This is the same field as in
|
|
<I>struct inet_diag_req_v2</I>.
|
|
|
|
<DT id="63"><I>idiag_state</I>
|
|
|
|
<DD>
|
|
This denotes socket state as in
|
|
<I>struct inet_diag_req_v2</I>.
|
|
|
|
<DT id="64"><I>idiag_timer</I>
|
|
|
|
<DD>
|
|
For TCP sockets, this field describes the type of timer that is currently
|
|
active for the socket.
|
|
It is set to one of the following constants:
|
|
<DT id="65"><DD>
|
|
|
|
<DL COMPACT><DT id="66"><DD>
|
|
<DL COMPACT>
|
|
<DT id="67"><B>0</B>
|
|
|
|
<DD>
|
|
no timer is active
|
|
<DT id="68"><B>1</B>
|
|
|
|
<DD>
|
|
a retransmit timer
|
|
<DT id="69"><B>2</B>
|
|
|
|
<DD>
|
|
a keep-alive timer
|
|
<DT id="70"><B>3</B>
|
|
|
|
<DD>
|
|
a TIME_WAIT timer
|
|
<DT id="71"><B>4</B>
|
|
|
|
<DD>
|
|
a zero window probe timer
|
|
</DL>
|
|
</DL>
|
|
|
|
|
|
<DT id="72"><DD>
|
|
For non-TCP sockets, this field is set to 0.
|
|
<DT id="73"><I>idiag_retrans</I>
|
|
|
|
<DD>
|
|
For
|
|
<I>idiag_timer</I>
|
|
|
|
values 1, 2, and 4, this field contains the number of retransmits.
|
|
For other
|
|
<I>idiag_timer</I>
|
|
|
|
values, this field is set to 0.
|
|
<DT id="74"><I>idiag_expires</I>
|
|
|
|
<DD>
|
|
For TCP sockets that have an active timer, this field describes its expiration
|
|
time in milliseconds.
|
|
For other sockets, this field is set to 0.
|
|
<DT id="75"><I>idiag_rqueue</I>
|
|
|
|
<DD>
|
|
For listening sockets:
|
|
the number of pending connections.
|
|
<DT id="76"><DD>
|
|
For other sockets:
|
|
the amount of data in the incoming queue.
|
|
<DT id="77"><I>idiag_wqueue</I>
|
|
|
|
<DD>
|
|
For listening sockets:
|
|
the backlog length.
|
|
<DT id="78"><DD>
|
|
For other sockets:
|
|
the amount of memory available for sending.
|
|
<DT id="79"><I>idiag_uid</I>
|
|
|
|
<DD>
|
|
This is the socket owner UID.
|
|
<DT id="80"><I>idiag_inode</I>
|
|
|
|
<DD>
|
|
This is the socket inode number.
|
|
|
|
</DL>
|
|
<A NAME="lbAI"> </A>
|
|
<H3>Socket memory information</H3>
|
|
|
|
The payload associated with
|
|
<B>UNIX_DIAG_MEMINFO</B>
|
|
|
|
and
|
|
<B>INET_DIAG_SKMEMINFO</B>
|
|
|
|
netlink attributes is an array of the following __u32 values:
|
|
<DL COMPACT>
|
|
<DT id="81"><B>SK_MEMINFO_RMEM_ALLOC</B>
|
|
|
|
<DD>
|
|
The amount of data in receive queue.
|
|
<DT id="82"><B>SK_MEMINFO_RCVBUF</B>
|
|
|
|
<DD>
|
|
The receive socket buffer as set by
|
|
<B>SO_RCVBUF</B>.
|
|
|
|
<DT id="83"><B>SK_MEMINFO_WMEM_ALLOC</B>
|
|
|
|
<DD>
|
|
The amount of data in send queue.
|
|
<DT id="84"><B>SK_MEMINFO_SNDBUF</B>
|
|
|
|
<DD>
|
|
The send socket buffer as set by
|
|
<B>SO_SNDBUF</B>.
|
|
|
|
<DT id="85"><B>SK_MEMINFO_FWD_ALLOC</B>
|
|
|
|
<DD>
|
|
The amount of memory scheduled for future use (TCP only).
|
|
<DT id="86"><B>SK_MEMINFO_WMEM_QUEUED</B>
|
|
|
|
<DD>
|
|
The amount of data queued by TCP, but not yet sent.
|
|
<DT id="87"><B>SK_MEMINFO_OPTMEM</B>
|
|
|
|
<DD>
|
|
The amount of memory allocated for the socket's service needs (e.g., socket
|
|
filter).
|
|
<DT id="88"><B>SK_MEMINFO_BACKLOG</B>
|
|
|
|
<DD>
|
|
The amount of packets in the backlog (not yet processed).
|
|
</DL>
|
|
<A NAME="lbAJ"> </A>
|
|
<H2>VERSIONS</H2>
|
|
|
|
<B>NETLINK_INET_DIAG</B>
|
|
|
|
was introduced in Linux 2.6.14 and supported
|
|
<B>AF_INET</B>
|
|
|
|
and
|
|
<B>AF_INET6</B>
|
|
|
|
sockets only.
|
|
In Linux 3.3, it was renamed to
|
|
<B>NETLINK_SOCK_DIAG</B>
|
|
|
|
and extended to support
|
|
<B>AF_UNIX</B>
|
|
|
|
sockets.
|
|
<P>
|
|
|
|
<B>UNIX_DIAG_MEMINFO</B>
|
|
|
|
and
|
|
<B>INET_DIAG_SKMEMINFO</B>
|
|
|
|
were introduced in Linux 3.6.
|
|
<A NAME="lbAK"> </A>
|
|
<H2>CONFORMING TO</H2>
|
|
|
|
The NETLINK_SOCK_DIAG API is Linux-specific.
|
|
<A NAME="lbAL"> </A>
|
|
<H2>EXAMPLE</H2>
|
|
|
|
The following example program prints inode number, peer's inode number,
|
|
and name of all UNIX domain sockets in the current namespace.
|
|
<P>
|
|
|
|
|
|
#include <<A HREF="file:///usr/include/errno.h">errno.h</A>>
|
|
#include <<A HREF="file:///usr/include/stdio.h">stdio.h</A>>
|
|
#include <<A HREF="file:///usr/include/string.h">string.h</A>>
|
|
#include <<A HREF="file:///usr/include/unistd.h">unistd.h</A>>
|
|
#include <<A HREF="file:///usr/include/sys/socket.h">sys/socket.h</A>>
|
|
#include <<A HREF="file:///usr/include/sys/un.h">sys/un.h</A>>
|
|
#include <<A HREF="file:///usr/include/linux/netlink.h">linux/netlink.h</A>>
|
|
#include <<A HREF="file:///usr/include/linux/rtnetlink.h">linux/rtnetlink.h</A>>
|
|
#include <<A HREF="file:///usr/include/linux/sock_diag.h">linux/sock_diag.h</A>>
|
|
#include <<A HREF="file:///usr/include/linux/unix_diag.h">linux/unix_diag.h</A>>
|
|
<P>
|
|
static int
|
|
send_query(int fd)
|
|
{
|
|
<BR> struct sockaddr_nl nladdr = {
|
|
<BR> .nl_family = AF_NETLINK
|
|
<BR> };
|
|
<BR> struct
|
|
<BR> {
|
|
<BR> struct nlmsghdr nlh;
|
|
<BR> struct unix_diag_req udr;
|
|
<BR> } req = {
|
|
<BR> .nlh = {
|
|
<BR> .nlmsg_len = sizeof(req),
|
|
<BR> .nlmsg_type = SOCK_DIAG_BY_FAMILY,
|
|
<BR> .nlmsg_flags = NLM_F_REQUEST | NLM_F_DUMP
|
|
<BR> },
|
|
<BR> .udr = {
|
|
<BR> .sdiag_family = AF_UNIX,
|
|
<BR> .udiag_states = -1,
|
|
<BR> .udiag_show = UDIAG_SHOW_NAME | UDIAG_SHOW_PEER
|
|
<BR> }
|
|
<BR> };
|
|
<BR> struct iovec iov = {
|
|
<BR> .iov_base = &req,
|
|
<BR> .iov_len = sizeof(req)
|
|
<BR> };
|
|
<BR> struct msghdr msg = {
|
|
<BR> .msg_name = (void *) &nladdr,
|
|
<BR> .msg_namelen = sizeof(nladdr),
|
|
<BR> .msg_iov = &iov,
|
|
<BR> .msg_iovlen = 1
|
|
<BR> };
|
|
<P>
|
|
<BR> for (;;) {
|
|
<BR> if (sendmsg(fd, &msg, 0) < 0) {
|
|
<BR> if (errno == EINTR)
|
|
<BR> continue;
|
|
<P>
|
|
<BR> perror("sendmsg");
|
|
<BR> return -1;
|
|
<BR> }
|
|
<P>
|
|
<BR> return 0;
|
|
<BR> }
|
|
}
|
|
<P>
|
|
static int
|
|
print_diag(const struct unix_diag_msg *diag, unsigned int len)
|
|
{
|
|
<BR> if (len < NLMSG_LENGTH(sizeof(*diag))) {
|
|
<BR> fputs("short response\n", stderr);
|
|
<BR> return -1;
|
|
<BR> }
|
|
<BR> if (diag->udiag_family != AF_UNIX) {
|
|
<BR> fprintf(stderr, "unexpected family %u\n", diag->udiag_family);
|
|
<BR> return -1;
|
|
<BR> }
|
|
<P>
|
|
<BR> struct rtattr *attr;
|
|
<BR> unsigned int rta_len = len - NLMSG_LENGTH(sizeof(*diag));
|
|
<BR> unsigned int peer = 0;
|
|
<BR> size_t path_len = 0;
|
|
<BR> char path[sizeof(((struct sockaddr_un *) 0)->sun_path) + 1];
|
|
<P>
|
|
<BR> for (attr = (struct rtattr *) (diag + 1);
|
|
<BR> RTA_OK(attr, rta_len); attr = RTA_NEXT(attr, rta_len)) {
|
|
<BR> switch (attr->rta_type) {
|
|
<BR> case UNIX_DIAG_NAME:
|
|
<BR> if (!path_len) {
|
|
<BR> path_len = RTA_PAYLOAD(attr);
|
|
<BR> if (path_len > sizeof(path) - 1)
|
|
<BR> path_len = sizeof(path) - 1;
|
|
<BR> memcpy(path, RTA_DATA(attr), path_len);
|
|
<BR> path[path_len] = '\0';
|
|
<BR> }
|
|
<BR> break;
|
|
<P>
|
|
<BR> case UNIX_DIAG_PEER:
|
|
<BR> if (RTA_PAYLOAD(attr) >= sizeof(peer))
|
|
<BR> peer = *(unsigned int *) RTA_DATA(attr);
|
|
<BR> break;
|
|
<BR> }
|
|
<BR> }
|
|
<P>
|
|
<BR> printf("inode=%u", diag->udiag_ino);
|
|
<P>
|
|
<BR> if (peer)
|
|
<BR> printf(", peer=%u", peer);
|
|
<P>
|
|
<BR> if (path_len)
|
|
<BR> printf(", name=%s%s", *path ? "" : "@",
|
|
<BR> *path ? path : path + 1);
|
|
<P>
|
|
<BR> putchar('\n');
|
|
<BR> return 0;
|
|
}
|
|
<P>
|
|
static int
|
|
receive_responses(int fd)
|
|
{
|
|
<BR> long buf[8192 / sizeof(long)];
|
|
<BR> struct sockaddr_nl nladdr = {
|
|
<BR> .nl_family = AF_NETLINK
|
|
<BR> };
|
|
<BR> struct iovec iov = {
|
|
<BR> .iov_base = buf,
|
|
<BR> .iov_len = sizeof(buf)
|
|
<BR> };
|
|
<BR> int flags = 0;
|
|
<P>
|
|
<BR> for (;;) {
|
|
<BR> struct msghdr msg = {
|
|
<BR> .msg_name = (void *) &nladdr,
|
|
<BR> .msg_namelen = sizeof(nladdr),
|
|
<BR> .msg_iov = &iov,
|
|
<BR> .msg_iovlen = 1
|
|
<BR> };
|
|
<P>
|
|
<BR> ssize_t ret = recvmsg(fd, &msg, flags);
|
|
<P>
|
|
<BR> if (ret < 0) {
|
|
<BR> if (errno == EINTR)
|
|
<BR> continue;
|
|
<P>
|
|
<BR> perror("recvmsg");
|
|
<BR> return -1;
|
|
<BR> }
|
|
<BR> if (ret == 0)
|
|
<BR> return 0;
|
|
<P>
|
|
<BR> const struct nlmsghdr *h = (struct nlmsghdr *) buf;
|
|
<P>
|
|
<BR> if (!NLMSG_OK(h, ret)) {
|
|
<BR> fputs("!NLMSG_OK\n", stderr);
|
|
<BR> return -1;
|
|
<BR> }
|
|
<P>
|
|
<BR> for (; NLMSG_OK(h, ret); h = NLMSG_NEXT(h, ret)) {
|
|
<BR> if (h->nlmsg_type == NLMSG_DONE)
|
|
<BR> return 0;
|
|
<P>
|
|
<BR> if (h->nlmsg_type == NLMSG_ERROR) {
|
|
<BR> const struct nlmsgerr *err = NLMSG_DATA(h);
|
|
<P>
|
|
<BR> if (h->nlmsg_len < NLMSG_LENGTH(sizeof(*err))) {
|
|
<BR> fputs("NLMSG_ERROR\n", stderr);
|
|
<BR> } else {
|
|
<BR> errno = -err->error;
|
|
<BR> perror("NLMSG_ERROR");
|
|
<BR> }
|
|
<P>
|
|
<BR> return -1;
|
|
<BR> }
|
|
<P>
|
|
<BR> if (h->nlmsg_type != SOCK_DIAG_BY_FAMILY) {
|
|
<BR> fprintf(stderr, "unexpected nlmsg_type %u\n",
|
|
<BR> (unsigned) h->nlmsg_type);
|
|
<BR> return -1;
|
|
<BR> }
|
|
<P>
|
|
<BR> if (print_diag(NLMSG_DATA(h), h->nlmsg_len))
|
|
<BR> return -1;
|
|
<BR> }
|
|
<BR> }
|
|
}
|
|
<P>
|
|
int
|
|
main(void)
|
|
{
|
|
<BR> int fd = socket(AF_NETLINK, SOCK_RAW, NETLINK_SOCK_DIAG);
|
|
<P>
|
|
<BR> if (fd < 0) {
|
|
<BR> perror("socket");
|
|
<BR> return 1;
|
|
<BR> }
|
|
<P>
|
|
<BR> int ret = send_query(fd) || receive_responses(fd);
|
|
<P>
|
|
<BR> close(fd);
|
|
<BR> return ret;
|
|
}
|
|
|
|
<A NAME="lbAM"> </A>
|
|
<H2>SEE ALSO</H2>
|
|
|
|
<B><A HREF="/cgi-bin/man/man2html?3+netlink">netlink</A></B>(3),
|
|
|
|
<B><A HREF="/cgi-bin/man/man2html?3+rtnetlink">rtnetlink</A></B>(3),
|
|
|
|
<B><A HREF="/cgi-bin/man/man2html?7+netlink">netlink</A></B>(7),
|
|
|
|
<B><A HREF="/cgi-bin/man/man2html?7+tcp">tcp</A></B>(7)
|
|
|
|
<A NAME="lbAN"> </A>
|
|
<H2>COLOPHON</H2>
|
|
|
|
This page is part of release 5.05 of the Linux
|
|
<I>man-pages</I>
|
|
|
|
project.
|
|
A description of the project,
|
|
information about reporting bugs,
|
|
and the latest version of this page,
|
|
can be found at
|
|
<A HREF="https://www.kernel.org/doc/man-pages/.">https://www.kernel.org/doc/man-pages/.</A>
|
|
<P>
|
|
|
|
<HR>
|
|
<A NAME="index"> </A><H2>Index</H2>
|
|
<DL>
|
|
<DT id="89"><A HREF="#lbAB">NAME</A><DD>
|
|
<DT id="90"><A HREF="#lbAC">SYNOPSIS</A><DD>
|
|
<DT id="91"><A HREF="#lbAD">DESCRIPTION</A><DD>
|
|
<DL>
|
|
<DT id="92"><A HREF="#lbAE">Request</A><DD>
|
|
<DT id="93"><A HREF="#lbAF">Response</A><DD>
|
|
<DT id="94"><A HREF="#lbAG">UNIX domain sockets</A><DD>
|
|
<DT id="95"><A HREF="#lbAH">IPv4 and IPv6 sockets</A><DD>
|
|
<DT id="96"><A HREF="#lbAI">Socket memory information</A><DD>
|
|
</DL>
|
|
<DT id="97"><A HREF="#lbAJ">VERSIONS</A><DD>
|
|
<DT id="98"><A HREF="#lbAK">CONFORMING TO</A><DD>
|
|
<DT id="99"><A HREF="#lbAL">EXAMPLE</A><DD>
|
|
<DT id="100"><A HREF="#lbAM">SEE ALSO</A><DD>
|
|
<DT id="101"><A HREF="#lbAN">COLOPHON</A><DD>
|
|
</DL>
|
|
<HR>
|
|
This document was created by
|
|
<A HREF="/cgi-bin/man/man2html">man2html</A>,
|
|
using the manual pages.<BR>
|
|
Time: 00:06:09 GMT, March 31, 2021
|
|
</BODY>
|
|
</HTML>
|